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: 2e11d8f6e1364b11f4d52a4a6141957ac532a79b (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
import json
import tempfile
import os

from polls.models import PollResultFile, Poll
from utils.test import MongoTestCase
from django.conf import settings


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


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_numero_escuela(self):
        data = self.data
        numero_escuela = '1'
        data['result']['0']['polled']['NUM_ESC'] = numero_escuela
        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)
        self.assertEqual(numero_escuela, result.get_numero_escuela())

    def test_it_should_respond_to_pollster_username(self):
        data = self.data
        pollster_username = "encuestador1"
        data['pollster_username'] = pollster_username
        file_path = self.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 = self.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 = self.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 = self.make_temp_file(data)
        result = PollResultFile(file_path)
        self.assertEqual(time_string, result.get_upload_timestamp())

    def test_it_should_set_the_upload_timestamp(self):
        data = self.data
        file_path = self.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 = self.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 = self.make_temp_file(data)
        result_file = PollResultFile(file_path)
        result_file.save()

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

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

    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 = self.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 = self.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 = self.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())