Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/webapp/polls/tests/poll_result_file_tests.py
blob: 36e0c872a6fe397e52c4487097bf39f36e3ed238 (plain)
1
2
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# pylint: disable=C0111,C0103,C0321,R0904
import json
import tempfile
import os
import hashlib

from django.test import TestCase
from django.conf import settings
from mock import Mock, patch

from polls.models import PollResultFile, Poll
from utils.test import MongoTestCase, create_results_dir


def json_construc(data):
    return json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))


def make_temp_file(data):
    json_str = json_construc(data)
    file_ = tempfile.NamedTemporaryFile(suffix='.poll_result',
                                        delete=False)
    file_.write(json_str)
    file_.close()
    return file_.name


class PollResultFileTest(MongoTestCase):

    def setUp(self):
        create_results_dir()

        data = {}
        data['result'] = {}
        data['result']['0'] = {}
        data['result']['0']['polled'] = {}
        self.data = data

        poll = Poll({'name': 'poll'})
        poll_id = str(poll.save())
        self.poll = Poll.get(poll_id)

    def test_it_should_respond_to_pollster_username(self):
        data = self.data
        pollster_username = "encuestador1"
        data['pollster_username'] = pollster_username
        file_path = make_temp_file(data)
        result = PollResultFile(file_path)
        self.assertEqual(pollster_username, result.get_pollster_username())

    def test_it_should_respond_to_polled_count(self):
        data = {}
        data['result'] = {}

        for polled_count in range(1, 3):
            data['result'][str(polled_count - 1)] = {}
            file_path = make_temp_file(data)
            result = PollResultFile(file_path)
            self.assertEqual(polled_count, result.get_polled_count())

    def test_it_should_respond_to_poll_result_filename(self):
        file_path = make_temp_file({})
        poll_result = PollResultFile(file_path)

        filename = os.path.basename(file_path)
        self.assertEqual(filename, poll_result.get_file_name())

    def test_it_should_respond_to_upload_timestamp(self):
        time_string = "31/12/2000 23:59hs"

        data = self.data
        data['upload_timestamp'] = time_string
        file_path = make_temp_file(data)
        result = PollResultFile(file_path)
        self.assertEqual(time_string, result.get_upload_timestamp())

    def test_it_should_return_None_when_there_is_no_timestamp(self):
        data = self.data
        file_path = make_temp_file(data)
        result = PollResultFile(file_path)
        self.assertIsNone(result.get_upload_timestamp())

    def test_it_should_set_the_upload_timestamp(self):
        data = self.data
        file_path = make_temp_file(data)
        result = PollResultFile(file_path)
        expected_time_string = "31/12/1999 23:59hs"

        result.set_upload_timestamp(expected_time_string)

        time_string = result.get_upload_timestamp()
        self.assertEqual(expected_time_string, time_string)

    def test_the_upload_timestamp_should_be_persistent(self):
        expected_time_string = "31/12/1999 23:59hs"
        data = self.data
        file_path = make_temp_file(data)

        result = PollResultFile(file_path)
        result.set_upload_timestamp(expected_time_string)
        del(result)

        new_result_same_file = PollResultFile(file_path)
        time_string = new_result_same_file.get_upload_timestamp()
        self.assertEqual(expected_time_string, time_string)

    def test_it_should_be_available_for_his_related_poll_when_it_saves(self):
        poll = self.poll
        poll_id = poll.id.__str__()
        self.assertEqual(0, len(poll.get_result_files()))

        data = self.data
        data['poll_id'] = poll_id

        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)
        result_file.save()

        self.assertEqual(1, len(poll.get_result_files()))

    def test_it_should_saves_with_a_chosen_name(self):
        poll = self.poll
        poll_id = poll.id.__str__()
        self.assertEqual(0, len(poll.get_result_files()))

        data = self.data
        data['poll_id'] = poll_id

        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)
        result_file.name = chosen_name = 'super_name.poll_result'
        result_file.save()

        result_name = poll.get_result_files()[0].get_file_name()
        self.assertEqual(chosen_name, result_name)

    def test_it_should_respond_with_data_structure(self):
        data = self.data
        file_path = make_temp_file(data)
        poll_result_file = PollResultFile(file_path)

        expected_data = data
        self.assertEqual(expected_data, poll_result_file.get_data())

    def test_it_should_respond_with_absolute_url_for_poll_result_file(self):
        poll = self.poll
        poll_id = poll.id.__str__()
        data = self.data
        data['poll_id'] = poll_id

        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)

        expected_url = os.path.join(
            settings.RESULT_BCK_URL, poll_id, result_file.name)
        self.assertEqual(expected_url, result_file.get_absolute_url())

    def test_it_should_respond_with_hash(self):
        data = {}
        expected_hash = hashlib.md5(str(data)).hexdigest()

        data["upload_timestamp"] = "timestamp"
        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)

        self.assertEqual(expected_hash, result_file.hash)


class ExistenceTest(MongoTestCase):

    def setUp(self):
        poll = Poll({'name': 'poll'})
        poll_id = str(poll.save())
        self.poll = Poll.get(poll_id)

    def test_exists_when_other_poll_result_file_has_same_name(self):
        poll = self.poll
        poll_id = poll.id.__str__()
        data = {}
        data['poll_id'] = poll_id

        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)
        self.assertFalse(result_file.exists())

        result_file.save()
        self.assertTrue(result_file.exists())

    def test_it_should_respond_True_when_content_exists_already(self):
        poll = self.poll
        poll_id = poll.id.__str__()
        self.assertEqual(0, len(poll.get_result_files()))

        data = {}
        data['poll_id'] = poll_id

        file_path = make_temp_file(data)
        original_result_file = PollResultFile(file_path)
        original_result_file.save()
        self.assertEqual(1, len(poll.get_result_files()))

        file_path = make_temp_file(data)
        duplicated_result_file = PollResultFile(file_path)

        self.assertTrue(duplicated_result_file.exists())


class RueeTest(MongoTestCase):

    def test_it_should_get_all_ruee_as_unique_items(self):
        data = {}
        data['result'] = {}
        data['result']['0'] = {}
        data['result']['0']['polled'] = {}
        ruee1 = '1'
        data['result']['0']['polled']['RUEE'] = ruee1
        ruee2 = '2'
        data['result']['1'] = {}
        data['result']['1']['polled'] = {}
        data['result']['1']['polled']['RUEE'] = ruee2
        data['result']['2'] = {}
        data['result']['2']['polled'] = {}
        data['result']['2']['polled']['RUEE'] = ruee2

        json_str = json_construc(data)
        file_ = tempfile.NamedTemporaryFile(suffix='.poll_result',
                                            delete=False)
        file_.write(json_str)
        file_.close()
        file_path = file_.name

        result = PollResultFile(file_path)
        expected = set([ruee1, ruee2])
        self.assertEqual(expected, result.get_ruees())


class RemovePollResultFileTest(TestCase):

    def test_it_should_respond_to_delete(self):
        data = {}
        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)
        self.assertTrue(hasattr(result_file, 'delete'))

    def test_it_should_remove_the_file(self):
        data = {}
        file_path = make_temp_file(data)
        result_file = PollResultFile(file_path)
        result_file.delete()
        saved_file_path = result_file.file_path
        self.assertFalse(os.path.exists(saved_file_path))


class AuthoredByTest(TestCase):

    def test_it_should_be_True_if_user_is_in_result_file(self):
        username = "Is my result"
        user = Mock()
        user.username = username
        file_path = "a_path"
        with patch('__builtin__.open'), patch('polls.models.json'):
            prf = PollResultFile(file_path)
        prf.get_pollster_username = Mock(return_value=username)
        self.assertTrue(prf.is_authored_by(user))

    def test_it_should_be_False_if_user_is_not_in_result_file(self):
        username = "Is not my result"
        user = Mock()
        user.username = username
        file_path = "a_path"
        with patch('__builtin__.open'), patch('polls.models.json'):
            prf = PollResultFile(file_path)
        prf.get_pollster_username = Mock(return_value="Other pollster")
        self.assertFalse(prf.is_authored_by(user))


class PollAssignationTest(TestCase):

    def setUp(self):
        self.poll_id = "poll id"
        self.user = Mock(pollster=Mock())
        file_path = "a_path"
        with patch('__builtin__.open'), patch('polls.models.json'):
            self.prf = PollResultFile(file_path)
            self.prf.get_data = Mock(return_value={"poll_id": self.poll_id})

    def test_it_return_True_if_user_is_assigned_to_poll_in_result(self):
        user = self.user
        prf = self.prf
        mock_poll_id = Mock(id=self.poll_id)
        with patch('polls.models.Poll') as PollMock:
            PollMock.assigned_to_pollster.return_value = [mock_poll_id]
            self.assertTrue(prf.poll_is_assigned_to(user))

    def test_it_return_False_if_user_is_not_assigned_to_poll_in_result(self):
        user = self.user
        prf = self.prf
        mock_poll_id = Mock(id="poll id 2")
        with patch('polls.models.Poll') as PollMock:
            PollMock.assigned_to_pollster.return_value = [mock_poll_id]
            self.assertFalse(prf.poll_is_assigned_to(user))