Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCode Raguet <ignacio.code@gmail.com>2013-09-04 15:05:52 (GMT)
committer Code Raguet <ignacio.code@gmail.com>2013-09-04 15:05:52 (GMT)
commit1f1f8be5a65dfd19eb38a04a2561f1569a7b6854 (patch)
tree8919c62e240a07cc987083a96eb94411c4fedece
parent7f0fb1b6424473e5e861e7fc9810b56012f2a8cb (diff)
Change PollResultFile so that it can recive a file path
-rw-r--r--webapp/polls/models.py21
-rw-r--r--webapp/polls/tests/poll_result_file_tests.py95
-rw-r--r--webapp/polls/tests/poll_tests.py2
3 files changed, 54 insertions, 64 deletions
diff --git a/webapp/polls/models.py b/webapp/polls/models.py
index 90d20c8..a461041 100644
--- a/webapp/polls/models.py
+++ b/webapp/polls/models.py
@@ -462,8 +462,7 @@ class Poll(Document, AbstracErrorObject):
result_files = []
for file_path in glob.glob(results_path + '/*'):
- with open(file_path) as f:
- result_files.append(result_file(f))
+ result_files.append(result_file(file_path))
return result_files
def add_result_files(self, path_and_name):
@@ -1510,11 +1509,13 @@ class PollResult(AbstractObject):
class PollResultFile(object):
- def __init__(self, file_):
- self.file = file_
- self.file.seek(0)
- self.json_str = self.file.read()
- self.data = json.loads(self.json_str, 'utf-8')
+ def __init__(self, file_path):
+ self.file_path = file_path
+ self.name = os.path.basename(self.file_path)
+ with open(self.file_path) as f:
+ f.seek(0)
+ self.json_str = f.read()
+ self.data = json.loads(self.json_str, 'utf-8')
def get_numero_escuela(self):
return self.data['result']['0']['polled']['NUM_ESC']
@@ -1527,15 +1528,15 @@ class PollResultFile(object):
return polled_count
def get_file_name(self):
- return os.path.basename(self.file.name)
+ return self.name
def get_upload_timestamp(self):
time_string = self.data['upload_timestamp']
return time_string
def save(self):
- name = self.name
- file_path = self.file.name
+ name = self.get_file_name()
+ file_path = self.file_path
poll_id = self.data['poll_id']
poll = Poll.get(poll_id)
poll.add_result_files([(file_path, name)])
diff --git a/webapp/polls/tests/poll_result_file_tests.py b/webapp/polls/tests/poll_result_file_tests.py
index 3a27ac1..fd0885d 100644
--- a/webapp/polls/tests/poll_result_file_tests.py
+++ b/webapp/polls/tests/poll_result_file_tests.py
@@ -1,5 +1,4 @@
import json
-import StringIO
import tempfile
import os
@@ -27,28 +26,21 @@ class PollResultFileTest(MongoTestCase):
data['result']['0']['polled']['NUM_ESC'] = numero_escuela
json_str = self.json_construc(data)
- try:
- file_ = StringIO.StringIO()
- file_.write(json_str)
- result = PollResultFile(file_)
- finally:
- file_.close()
+ 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
- json_str = self.json_construc(data)
-
- try:
- file_ = StringIO.StringIO()
- file_.write(json_str)
- result = PollResultFile(file_)
- finally:
- file_.close()
-
+ 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):
@@ -56,44 +48,25 @@ class PollResultFileTest(MongoTestCase):
data['result'] = {}
for polled_count in range(1, 3):
-
data['result'][str(polled_count - 1)] = {}
- json_str = self.json_construc(data)
-
- try:
- file_ = StringIO.StringIO()
- file_.write(json_str)
- result = PollResultFile(file_)
- finally:
- file_.close()
-
+ 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):
- uploaded_file = tempfile.NamedTemporaryFile(suffix='.poll_result')
- uploaded_filename = os.path.basename(uploaded_file.name)
- json_str = self.json_construc({})
- uploaded_file.write(json_str)
- try:
- poll_result = PollResultFile(uploaded_file)
- finally:
- uploaded_file.close()
+ file_path = self.make_temp_file({})
+ poll_result = PollResultFile(file_path)
- self.assertEqual(uploaded_filename, poll_result.get_file_name())
+ 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
- json_str = self.json_construc(data)
- try:
- file_ = StringIO.StringIO()
- file_.write(json_str)
- result = PollResultFile(file_)
- finally:
- file_.close()
-
+ file_path = self.make_temp_file(data)
+ result = PollResultFile(file_path)
self.assertEqual(time_string, result.get_upload_timestamp())
def test_it_should_be_available_for_his_related_poll_when_it_saves(self):
@@ -104,18 +77,34 @@ class PollResultFileTest(MongoTestCase):
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 = self.json_construc(data)
file_ = tempfile.NamedTemporaryFile(suffix='.poll_result',
delete=False)
- expected_name = 'result.poll_result'
- try:
- file_.write(json_str)
- result_file = PollResultFile(file_)
- result_file.name = expected_name
- result_file.save()
- finally:
- file_.close()
+ 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()
- self.assertEqual(1, len(poll.get_result_files()))
result_name = poll.get_result_files()[0].get_file_name()
- self.assertEqual(expected_name, result_name)
+ self.assertEqual(chosen_name, result_name)
diff --git a/webapp/polls/tests/poll_tests.py b/webapp/polls/tests/poll_tests.py
index 4f58787..b663d33 100644
--- a/webapp/polls/tests/poll_tests.py
+++ b/webapp/polls/tests/poll_tests.py
@@ -443,7 +443,7 @@ class AddResultFilesTest(MongoTestCase):
poll = self.poll
poll.add_result_files([(uploaded_file_path, uploaded_filename)])
- stub = lambda f: os.path.basename(f.name)
+ stub = lambda path: os.path.basename(path)
results = poll.get_result_files(as_instance_of=stub)
result_filename = results[0]
self.assertEqual(uploaded_filename, result_filename)