Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15 from alexpearce/init-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
Albert Puig committed Apr 13, 2015
2 parents 48d7dfa + dfc832c commit dc4816b
Show file tree
Hide file tree
Showing 13 changed files with 1,101 additions and 141 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: python
python:
- 2.6
- 2.7
install:
# Install Condorcet dependencies
- pip install -r requirements.txt
# Install test dependencies
- pip install flake8 unittest2 flask-testing mock blinker
# Run flake8, a Python code linter
before_script:
flake8 Condorcet
# Run the test suite
script:
- unit2 discover
58 changes: 44 additions & 14 deletions Condorcet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@
import random
import string
import time
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'..'))
# Add this directory and the one above to PYTHONPATH
this_files_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(this_files_dir)
sys.path.append(os.path.join(this_files_dir, '..'))


app = Flask(__name__)
app.config.from_object('Condorcet.config')

# convert times in times-tuple
for time_label in 'START_ELECTION', 'CLOSE_ELECTION', 'VIEW_RESULTS':
app.config[time_label] = time.strptime(app.config[time_label],app.config['DATE_FORMAT'])
app.config[time_label] = time.strptime(
app.config[time_label], app.config['DATE_FORMAT']
)

import manageDB
from verifyAuthors import isAuthor
import elections

alphabet = string.lowercase
name2letter = dict([(key, val) for key, val in zip(app.config['OPTIONS'], alphabet)])
letter2name = dict([(key, val) for key, val in zip(alphabet, app.config['OPTIONS'])])
name2letter = dict([
(key, val) for key, val in zip(app.config['OPTIONS'], alphabet)
])
letter2name = dict([
(key, val) for key, val in zip(alphabet, app.config['OPTIONS'])
])


def getStrOrder(choice_made):
Expand All @@ -57,11 +65,11 @@ def set_user():
session['user'] = {
'username': get_environ('ADFS_LOGIN'),
'fullname': ' '.join([
get_environ('ADFS_FIRSTNAME'), get_environ('ADFS_LASTNAME')
get_environ('ADFS_FIRSTNAME'), get_environ('ADFS_LASTNAME')
])
}
session['user']['author'] = isAuthor(session['user']['fullname'])


def author_required(f):
@wraps(f)
Expand All @@ -71,27 +79,46 @@ def decorated_function(*args, **kwargs):
return f(*args, **kwargs)
return decorated_function


def during_elections(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if time.localtime() < app.config['START_ELECTION']:
# TODO factor out long string to view
start = time.strftime(
'%d %B %Y at %H.%M',
app.config['START_ELECTION']
)
message = 'The election will be begin on ' + start
return render_template('notCorrectDate.html',
title = 'Too early to vote',
message='You are a bit too early to vote, we appreciate your enthusiasm but the election will be opening only on '+time.strftime('%d %B %Y at %H.%M',app.config['START_ELECTION']))
title='Too early to vote',
message=message)
if time.localtime() > app.config['CLOSE_ELECTION']:
# TODO factor out long string to view
close = time.strftime(
'%d %B %Y at %H.%M',
app.config['CLOSE_ELECTION']
)
message = 'The closing date of the election was the ' + close
return render_template('notCorrectDate.html',
title = 'Too late to vote',
message='I am sorry but the closing date of the election was the '+time.strftime('%d %B %Y at %H.%M',app.config['START_ELECTION']))
title='Too late to vote',
message=message)
return f(*args, **kwargs)
return decorated_function


def publish_results(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if time.localtime() < app.config['VIEW_RESULTS']:
results = time.strftime(
'%d %B %Y at %H.%M',
app.config['START_ELECTION']
)
message = 'The results will be availabe on ' + results
return render_template('notCorrectDate.html',
title = 'Too early to see the results',
message='I am sorry but the results are not available yet, visit again this page after the '+time.strftime('%d %B %Y at %H.%M',app.config['START_ELECTION']))
title='Too early to see the results',
message=message)
return f(*args, **kwargs)
return decorated_function

Expand All @@ -103,7 +130,8 @@ def root():
fullname = session['user']['fullname']
if manageDB.isInDB(fullname):
return render_template('alreadyVoted.html')
try: session['candidates']
try:
session['candidates']
except KeyError:
choices_copy = app.config['OPTIONS'][:]
random.shuffle(choices_copy)
Expand Down Expand Up @@ -144,6 +172,8 @@ def confirmVote():
@during_elections
@author_required
def savePoll():
if not session.get('vote'):
return redirect(url_for('root'))
fullname = session['user']['fullname']
if manageDB.isInDB(fullname):
return render_template('alreadyVoted.html')
Expand Down
30 changes: 23 additions & 7 deletions Condorcet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
AUTHORS_LIST is the name of the xml file with author lis
"""
import os
import sys
this_files_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(this_files_dir, '..'))

# Poll title
TITLE = 'Order the quarks in the order you prefer them'
Expand All @@ -15,8 +18,9 @@

# Dates

# Format to parse the date, see details at https://docs.python.org/2/library/time.html#time.strftime
DATE_FORMAT = '%d/%m/%Y %H.%M' # '%d/%m/%Y'
# Format to parse the date, see details at
# https://docs.python.org/2/library/time.html#time.strftime
DATE_FORMAT = '%d/%m/%Y %H.%M'
# Startig time of the election
START_ELECTION = '28/03/2008 12.23'
# Closing time of the election
Expand All @@ -25,11 +29,13 @@
VIEW_RESULTS = '28/03/2014 12.24'


# xml file with author list, can be downloaded form: https://lhcbglance.web.cern.ch/lhcbglance/membership/authorlist.php
# write only the file name and place the file in the same folder as this config file
# xml file with author list, can be downloaded form:
# https://lhcbglance.web.cern.ch/lhcbglance/membership/authorlist.php
# write only the file name and place the file in the same folder as this config
# file
AUTHORS_LIST = 'LHCb_Authorship_flat_28-Mar-2015.xml'

####################################################################################################
###############################################################################

# Configuration below is for the application itself, does not affect voting

Expand All @@ -48,7 +54,17 @@
SECRET_KEY = 'cH\xc5\xd9\xd2\xc4,^\x8c\x9f3S\x94Y\xe5\xc7!\x06>A'

# SQLite3 database paths
SQLALCHEMY_DATABASE_URI = r'sqlite:///databases/votes.db'
# Name of the folder to contain the databases
DB_DIR = os.path.join(this_files_dir, 'databases')
# Name of the database file to hold votes
VOTES_DB = 'votes.db'
# Name the database file to hold list of voters who have voted
VOTERS_DB = 'voters.db'
SQLALCHEMY_DATABASE_URI = r'sqlite:///{0}/{1}'.format(DB_DIR, VOTES_DB)
SQLALCHEMY_BINDS = {
'voters': r'sqlite:///databases/voters.db',
'voters': r'sqlite:///{0}/{1}'.format(DB_DIR, VOTERS_DB),
}

# Build the full path to the authors list, should be in the same dir as this
# config file
AUTHORS_LIST = os.path.join(this_files_dir, AUTHORS_LIST)
Loading

0 comments on commit dc4816b

Please sign in to comment.