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
|
import json
import tempfile
import os
from django.test import TestCase
from django.conf import settings
from polls.models import PollResultFile, Poll
from utils.test import MongoTestCase
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):
data = {}
data['result'] = {}
data['result']['0'] = {}
data['result']['0']['polled'] = {}
self.data = data
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 = Poll({'name': 'poll'})
poll_id = str(poll.save())
poll = Poll.get(poll_id)
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 = Poll({'name': 'poll'})
poll_id = str(poll.save())
poll = Poll.get(poll_id)
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 = Poll({'name': 'poll'})
poll_id = str(poll.save())
poll = Poll.get(poll_id)
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())
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))
|