diff options
author | Code Raguet <ignacio.code@gmail.com> | 2013-08-28 15:17:34 (GMT) |
---|---|---|
committer | Code Raguet <ignacio.code@gmail.com> | 2013-08-29 16:50:47 (GMT) |
commit | aeacab569c1affbfc472b3a43ace4e7029d9a502 (patch) | |
tree | 60d5de45d87a4f3df62b5c7603401615800ddfe8 /webapp/polls/tests/poll_tests.py | |
parent | d8efde6456f2d49049d310543a01363e2543455f (diff) |
Refator: get_result_files is a list of PollResultFiles
Diffstat (limited to 'webapp/polls/tests/poll_tests.py')
-rw-r--r-- | webapp/polls/tests/poll_tests.py | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/webapp/polls/tests/poll_tests.py b/webapp/polls/tests/poll_tests.py index 20043d3..5c173bc 100644 --- a/webapp/polls/tests/poll_tests.py +++ b/webapp/polls/tests/poll_tests.py @@ -1,12 +1,14 @@ # -*- encoding: utf-8 -*- import json import warnings - +import tempfile from bson import ObjectId +import os from polls.models import Poll, Field, Structure from polls.forms import PollForm from pollster.models import Pollster +from django.conf import settings from utils.test import MongoTestCase @@ -356,6 +358,16 @@ class PollTests(MongoTestCase): self.assertEqual(expected, template) + def test_results_path_shoud_be_what_is_in_settings_plus_poll_id(self): + poll_without_id = Poll({}) + with self.assertRaises(Exception): + poll_without_id.results_path + + poll_id = ObjectId() + poll_with_id = Poll({'id': poll_id}) + expected = settings.RESULT_BCK_ROOT + self.assertIn(expected, poll_with_id.results_path) + class PollFormTests(MongoTestCase): @@ -379,3 +391,58 @@ class PollFormTests(MongoTestCase): form = PollForm() self.assertFalse(form.is_valid()) + + +class PollResultFilesTest(MongoTestCase): + + def setUp(self): + name = 'nombre de encuesta' + data = {'name': name} + poll = Poll(data=data) + poll_id = poll.save() + self.poll = Poll.get(poll_id) + + def test_it_should_return_empty_list_with_no_files(self): + poll = self.poll + self.assertListEqual([], poll.get_result_files()) + + def test_it_should_return_one_result_file(self): + poll = self.poll + + tmp_path = tempfile.mkstemp(suffix='.poll_result')[1] + with open(tmp_path, 'w') as tmp: + tmp.write( + json.dumps( + '', sort_keys=True, indent=4, separators=(',', ': ') + ) + ) + + uploaded_file_path = tmp_path + poll.add_result_files([uploaded_file_path]) + results_files = poll.get_result_files() + self.assertEqual(1, len(results_files)) + self.assertTrue(hasattr(results_files[0], 'get_numero_escuela')) + + +class AddResultFilesTest(MongoTestCase): + + def setUp(self): + name = 'nombre de encuesta' + data = {'name': name} + poll = Poll(data=data) + poll_id = poll.save() + self.poll = Poll.get(poll_id) + + def test_add_a_result_file(self): + temp_dir = '/tmp' + uploaded_file = tempfile.NamedTemporaryFile(dir=temp_dir, + suffix='.poll_result') + uploaded_file_path = uploaded_file.name + uploaded_filename = os.path.basename(uploaded_file.name) + poll = self.poll + + poll.add_result_files([uploaded_file_path]) + stub = lambda f: os.path.basename(f.name) + results = poll.get_result_files(as_instance_of=stub) + result_filename = results[0] + self.assertEqual(uploaded_filename, result_filename) |