diff options
Diffstat (limited to 'webapp/polls/tests/poll_result_file_tests.py')
-rw-r--r-- | webapp/polls/tests/poll_result_file_tests.py | 126 |
1 files changed, 115 insertions, 11 deletions
diff --git a/webapp/polls/tests/poll_result_file_tests.py b/webapp/polls/tests/poll_result_file_tests.py index 996a8b9..36e0c87 100644 --- a/webapp/polls/tests/poll_result_file_tests.py +++ b/webapp/polls/tests/poll_result_file_tests.py @@ -1,13 +1,15 @@ -# pylint: disable=C0111,C0103 +# 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 +from utils.test import MongoTestCase, create_results_dir def json_construc(data): @@ -26,12 +28,18 @@ def make_temp_file(data): 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" @@ -97,9 +105,8 @@ class PollResultFileTest(MongoTestCase): 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) + poll = self.poll + poll_id = poll.id.__str__() self.assertEqual(0, len(poll.get_result_files())) data = self.data @@ -112,9 +119,8 @@ class PollResultFileTest(MongoTestCase): 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) + poll = self.poll + poll_id = poll.id.__str__() self.assertEqual(0, len(poll.get_result_files())) data = self.data @@ -137,9 +143,8 @@ class PollResultFileTest(MongoTestCase): 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) + poll = self.poll + poll_id = poll.id.__str__() data = self.data data['poll_id'] = poll_id @@ -150,6 +155,55 @@ class PollResultFileTest(MongoTestCase): 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): @@ -195,3 +249,53 @@ class RemovePollResultFileTest(TestCase): 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)) |