test_limaju.py 2.93 KB
Newer Older
1
import unittest
2
from limaju import deliberate, plot_merit_profile, load_mentions_from_string
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71


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'])

72
    def test_plotting_deliberation_with_merit_profiles(self):
73
74
75
76
77
78
79
80
        mentions = load_mentions_from_string(self.test_mentions)
        judgments = ''
        with open("examples/judgments_01.csv") as sample:
            judgments = "".join(sample.readlines())
        deliberation, tally = deliberate(judgments, self.test_mentions)

        plot_merit_profile(tally, deliberation, mentions, filename="test_plot.png")

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    def test_plotting_deliberation_02_with_merit_profiles(self):
        # mentions = load_mentions_from_string(self.test_mentions)
        mentions = (
            u"excellent⋅e",
            u"très bien",
            u"bien",
            u"assez bien",
            u"passable",
            u"insuffisant⋅e",
            u"à rejeter",
        )
        judgments = ''
        with open("examples/judgments_02.csv") as sample:
            judgments = "".join(sample.readlines())
        deliberation, tally = deliberate(judgments, mentions, skip_cols=1)

        filename = "merit_profiles_02.png"
        plot_merit_profile(tally, deliberation, mentions, filename=filename)
99

100
101
102
103
104
105
106
    # def test_raise(self):
    #     with self.assertRaises(TypeError):
    #         s.split(2)


if __name__ == '__main__':
    unittest.main()