Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions loading #202

Merged
merged 3 commits into from
Feb 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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