Skip to content

Commit

Permalink
Merge pull request #202 from ciudadanointeligente/questions_loading
Browse files Browse the repository at this point in the history
Questions loading
  • Loading branch information
lfalvarez committed Feb 1, 2013
2 parents 65a2c29 + f7587ef commit f531fcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
7 changes: 7 additions & 0 deletions elections/management/commands/elections_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def getCandidate(self, line):
class QuestionsParser(object):
def __init__(self, election):
self.election = election
self.election.backgroundcategory_set.all().delete()

def createQuestions(self, lines):
for line in lines:
Expand All @@ -88,6 +89,12 @@ def createQuestions(self, lines):
if line[0]=="answer":
answer = line[1].decode('utf-8').strip()
self.answer = Answer.objects.create(caption=answer, question=self.question)
if line[0]=="background history category":
background_category_name = line[1].decode('utf-8').strip()
self.background_category = BackgroundCategory.objects.create(name=background_category_name, election=self.election)
if line[0]=="background history":
background_history_name = line[1].decode('utf-8').strip()
self.background_history = Background.objects.create(name=background_history_name, category=self.background_category)


class Command(BaseCommand):
Expand Down
44 changes: 40 additions & 4 deletions elections/tests/election_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ def setUp(self):
["question","la pregunta2"],
["answer","respuesta 3"],
["answer","respuesta 4"],
["answer","respuesta 5"],
["background history category","Background category 1"],
["background history","background history record 1"],
["background history","background history record 2"],
["background history category","Background category 2"],
["background history","background history record 3"],
["background history","background history record 4"],
]
self.user = User.objects.create_user(username='ciudadanointeligente',
password='fci',
email='[email protected]')
self.election = Election.objects.create(owner=self.user, name="Elección para molestar a Marcel")
[category.delete() for category in self.election.category_set.all()]
self.election = Election.objects.create(owner=self.user, name="Elección para molestar a la Fiera")
self.election.category_set.all().delete()

def test_create_categories(self):
parser = QuestionsParser(self.election)
Expand Down Expand Up @@ -59,10 +66,39 @@ def test_create_answer(self):
self.assertEquals(first_category_questions[0].answer_set.count(), 2)
self.assertEquals(first_category_questions[0].answer_set.all()[0].caption, u"respuesta 1")
self.assertEquals(first_category_questions[0].answer_set.all()[1].caption, u"respuesta 2")

self.assertEquals(second_category_questions[0].answer_set.count(), 2)
self.assertEquals(second_category_questions[0].answer_set.count(), 3)
self.assertEquals(second_category_questions[0].answer_set.all()[0].caption, u"respuesta 3")
self.assertEquals(second_category_questions[0].answer_set.all()[1].caption, u"respuesta 4")
self.assertEquals(second_category_questions[0].answer_set.all()[2].caption, u"respuesta 5")


def test_create_background_category(self):
parser = QuestionsParser(self.election)
parser.createQuestions(self.lines)
election_after_questions_created = Election.objects.get(pk=self.election.pk)
self.assertEquals(election_after_questions_created.backgroundcategory_set.count(), 2)
self.assertEquals(election_after_questions_created.backgroundcategory_set.all()[0].name, u"Background category 1")
self.assertEquals(election_after_questions_created.backgroundcategory_set.all()[1].name, u"Background category 2")


def test_create_background(self):
parser = QuestionsParser(self.election)
parser.createQuestions(self.lines)
election_after_questions_created = Election.objects.get(pk=self.election.pk)

first_background_category = election_after_questions_created.backgroundcategory_set.all()[0]
second_background_category = election_after_questions_created.backgroundcategory_set.all()[1]


self.assertEquals(first_background_category.background_set.count(), 2)
self.assertEquals(first_background_category.background_set.all()[0].name, u"background history record 1")
self.assertEquals(first_background_category.background_set.all()[1].name, u"background history record 2")
self.assertEquals(second_background_category.background_set.count(), 2)
self.assertEquals(second_background_category.background_set.all()[0].name, u"background history record 3")
self.assertEquals(second_background_category.background_set.all()[1].name, u"background history record 4")






Expand Down

0 comments on commit f531fcb

Please sign in to comment.