Commit a6fd8d74 authored by Dominique Merle's avatar Dominique Merle
Browse files

Add basic unit-testing and fix some bugs.

We're getting ready for tagging a release…
parent fcee644b
......@@ -4,6 +4,8 @@
## Usage
Download the source files. (or just `limaju.py`)
cd limaju
### Via STDIN
......@@ -37,11 +39,15 @@ Mentions are ordered. Topmost wins. Case matters.
REJECT
## Input Vote Data
## Judgments Data Example
### As CSV
#### votes.csv
#### judgments.csv
Quotes (`"`) are optional,
if you don't put commas or quotes in your candidates or mentions. _(best not)_
```csv
"Tyrant","Capitalist","Ecologist","Communist","Preacher","Random"
POOR,REJECT,VERY GOOD,VERY GOOD,PASSABLE,EXCELLENT
......@@ -91,8 +97,10 @@ PASSABLE,PASSABLE,REJECT,GOOD,PASSABLE,VERY GOOD
## Tests
This software is unit-tested with `unittest`.
It's basic, but it works.
python limaju/test_limaju.py -vvv
......@@ -98,8 +98,8 @@ def sort_two_candidates(judgments_of, tally_of, mentions, ca, cb):
positions = get_positions(mentions)
if mdca == mdcb:
cotoca = copy.copy(toca)
cotocb = copy.copy(tocb)
cotoca = copy.deepcopy(toca)
cotocb = copy.deepcopy(tocb)
while not is_tally_empty(cotoca) and not is_tally_empty(cotocb):
nemdca = get_median(cotoca, mentions)
......@@ -109,7 +109,8 @@ def sort_two_candidates(judgments_of, tally_of, mentions, ca, cb):
decrement_mention(cotocb, nemdcb)
else:
return positions[nemdca] - positions[nemdcb]
log("EXACT EQUALITY FOUND FOR CANDIDATES")
log("%s == %s" % (ca, cb))
return 0
else:
return positions[mdca] - positions[mdcb]
......@@ -162,6 +163,7 @@ def main(args_parser, args): # move to bottom, no need for a func
int(args.skip_cols)
)
log("\nDELIBERATION")
for i, candidate in enumerate(deliberation):
log("%02d.\t%18s\t%s" % (
i+1,
......@@ -181,8 +183,8 @@ def load_judgments_from_string(judgments_string):
return judgments_data
def load_mentions_from_string(ms):
return [m.strip() for m in ms.strip().split() if m and m.strip()]
def load_mentions_from_string(ms, sep="\n"):
return [m.strip() for m in ms.strip().split(sep) if m and m.strip()]
def deliberate(judgments_data,
......
import unittest
from limaju import deliberate
class TestLimaju(unittest.TestCase):
test_mentions_array = [
u'EXCELLENT',
u'VERY GOOD',
u'GOOD',
u'SOMEWHAT GOOD',
u'PASSABLE',
u'POOR',
u'REJECT',
]
test_mentions = """
EXCELLENT
VERY GOOD
GOOD
SOMEWHAT GOOD
PASSABLE
POOR
REJECT
"""
def test_run_with_array(self):
deliberation, tally = deliberate([
['A', 'B', 'C'],
['POOR', 'GOOD', 'REJECT'],
['EXCELLENT', 'GOOD', 'EXCELLENT'],
], self.test_mentions_array)
self.assertEqual(deliberation, ['B', 'A', 'C'])
def test_dumb_deliberation_with_one_candidate(self):
deliberation, tally = deliberate(u"""
A
PASSABLE
EXCELLENT
""", self.test_mentions)
self.assertEqual(deliberation, ['A'])
def test_deliberation_with_one_judge(self):
deliberation, tally = deliberate(u"""
A, B, C, D
REJECT, PASSABLE, POOR, GOOD
""", self.test_mentions)
self.assertEqual(deliberation, ['D', 'B', 'C', 'A'])
def test_deliberation_with_two_judges(self):
deliberation, tally = deliberate(u"""
A, B, C
POOR, GOOD, REJECT
EXCELLENT, GOOD, EXCELLENT
""", self.test_mentions)
self.assertEqual(deliberation, ['B', 'A', 'C'])
def test_deliberation_with_same_median_mention(self):
deliberation, tally = deliberate(u"""
A, B, C, D
POOR, GOOD, REJECT, PASSABLE
EXCELLENT, EXCELLENT, EXCELLENT, EXCELLENT
EXCELLENT, EXCELLENT, EXCELLENT, EXCELLENT
""", self.test_mentions)
self.assertEqual(deliberation, ['B', 'D', 'A', 'C'])
# def test_raise(self):
# with self.assertRaises(TypeError):
# s.split(2)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment