class UserTest(TestCase): def setUp(self): self.backend = PersistantBackend(slug="AnotherMockBackend") self.backend.save() self.reporter = Reporter(alias="ReporterName") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1001") self.pconnection.save() self.reporter.connections.add(self.pconnection) Registration(governorate = 3, district = 4, phone = self.pconnection).save() def test_set_user_geolocation(self): user = User() user.set_user_geolocation_if_registered(self.pconnection) self.assertEquals(user.governorate , "3") self.assertEquals(user.district, "4") def test_dont_set_geolocation_when_not_present(self): user = User() user.set_user_geolocation_if_registered(None) self.assertEquals(user.governorate , None) self.assertEquals(user.district, None) def set_value_on_user(self): user = User() self.assertEquals(user.age, None) self.assertEquals(user.gender, None) user.set_value("age", 12) self.assertEquals(user.age, 12) self.assertEquals(user.gender, None) user.set_value("gender", "f") self.assertEquals(user.gender, "f")
def setUp(self): backend = PersistantBackend(slug="MockBackend") backend.save() reporter = Reporter(alias="ReporterName") reporter.save() self.connection = PersistantConnection( backend=backend, reporter=reporter, identity="1000", governorate=2, district=4 ) self.connection.save()
def test_junk_trigger_message(self): backend = PersistantBackend(slug="MockBackend1") backend.save() reporter = Reporter(alias="ReporterName1") reporter.save() pconnection = PersistantConnection(backend=backend, reporter=reporter, identity="1001") pconnection.save() session = UserSession.open(pconnection) self.assertEquals(session.respond(TestMessage(text='trigger junk')), TRIGGER_INCORRECT_MESSAGE )
def setUp(self): self.backend = PersistantBackend(slug="AnotherMockBackend") self.backend.save() self.reporter = Reporter(alias="ReporterName") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1001") self.pconnection.save() self.reporter.connections.add(self.pconnection) Registration(governorate = 3, district = 4, phone = self.pconnection).save()
def create_reporters(): from apps.contacts.models import Contact, CommunicationChannel, ChannelConnection from apps.reporters.models import Reporter, PersistantBackend, PersistantConnection # add reporter_id, allowing null values for now cursor = connection.cursor() cursor.execute("ALTER TABLE `contacts_contact` ADD COLUMN `reporter_id` INT(11) DEFAULT NULL AFTER `node_ptr_id`;") cursor.execute("ALTER TABLE `contacts_contact` ADD CONSTRAINT `reporter_id_refs_id_32f59ca5` FOREIGN KEY (`reporter_id`) REFERENCES `reporters_reporter` (`id`);") cursor.execute("ALTER TABLE `contacts_contact` ADD CONSTRAINT UNIQUE `reporter_id_unique` (`reporter_id`);") all_contacts = Contact.objects.all() for contact in all_contacts: print "processing contact %s" % contact.get_signature() # NEW reporter models rep = Reporter() rep.alias = contact.unique_id rep.first_name = contact.given_name rep.last_name = contact.family_name rep.language = contact._locale rep.save() contact.reporter = rep contact.save() # check conns = contact.channel_connections.all() conns_count = conns.count() if conns_count==0: print "%s has no connections" % (contact.get_signature()) elif conns_count>1: print "%s has more than one connections" % (contact.get_signature()) # create various connection objects for conn in conns: new_conn = PersistantConnection() new_conn.identity = conn.user_identifier new_conn.backend = PersistantBackend.objects.get(slug=conn.communication_channel.backend_slug) new_conn.reporter = rep new_conn.save() conn.delete() # check unassociated_contacts = Contact.objects.filter(reporter=None) if len(unassociated_contacts)>0: print "PROBLEMS WITH GENERATING REPORTERS!" print "UNASSOCIATED: " for con in unassociated_contacts: print " contact id: %s\n" % con.id # check contact_count = all_contacts.count() reporter_count = Reporter.objects.all().count() if contact_count != reporter_count: raise Exception("Mismatch between contact and reporter count! %s %s" \ % (contact_count, reporter_count))
def setUp(self): self.questionnaire = Questionnaire(trigger = "trigger", max_retries=3) self.questionnaire.save() Question.objects.all().delete() self.backend = PersistantBackend(slug="MockBackend") self.backend.save() self.reporter = Reporter(alias="ReporterName") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1000", governorate = 2, district = 4) self.pconnection.save() self.pconnection1 = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="10001", governorate = 7, district = 8) self.pconnection1.save() self.reporter.connections.add(self.pconnection) self.question1 = Question(text = "question1", questionnaire=self.questionnaire) self.question1.is_first = True self.question1.save() self.question2 = Question(text = "question2", questionnaire=self.questionnaire) self.question2.save() self.question3 = Question(text = "question3", questionnaire=self.questionnaire) self.question3.save() self.question1.next_question = self.question2 self.question2.next_question = self.question3 self.question1.save() self.question2.save() self.question3.save() self.setup_choices(self.question1) self.setup_choices(self.question2) self.setup_choices(self.question3) DemographicParser(questionnaire=self.questionnaire, name='age', regex='[0-9]+', order=1, type='i').save() DemographicParser(questionnaire=self.questionnaire, name='gender', regex='m|f|male|female', order=2, type='c').save() r = Registration(phone = self.pconnection) r.save() r1 = Registration(phone = self.pconnection) r1.save()
def setUp(self): Question.objects.all().delete() self.backend = PersistantBackend(slug="MockBackend1") self.backend.save() self.reporter = Reporter(alias="ReporterName1") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1000") self.pconnection.save() self.reporter.connections.add(self.pconnection) self.user = User(connection=self.pconnection, age=12, gender='m', governorate=1, district=1) self.user.save()
class QuestionTest(TestCase): def setUp(self): self.questionnaire = Questionnaire(trigger="trig") self.questionnaire.save() Question.objects.all().delete() self.backend = PersistantBackend(slug="MockBackend1") self.backend.save() self.reporter = Reporter(alias="ReporterName1") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1000") self.pconnection.save() self.reporter.connections.add(self.pconnection) self.user = User(connection=self.pconnection, age=12, gender='m', governorate=1, district=1) self.user.save() def test_save(self): initial_no_of_questions = len(Question.objects.all()) question1 = Question(text = 'question 1', num_answers_expected = 3, questionnaire=self.questionnaire) question2 = Question(text = 'question 2', num_answers_expected = 3, questionnaire=self.questionnaire) question1.next_question = question2 question1.save() question2.save() def test_last_question(self): question1 = Question(text = 'question 1', questionnaire=self.questionnaire) question1.save() next_question = question1.next_question self.assertEquals(next_question, None) def setup_question_and_choices(self): question = Question(text = 'question 1',num_answers_expected = 1, questionnaire=self.questionnaire) question.save() choice1 = Choice(code= 'a',question=question, text="apple") choice2 = Choice(code= 'b',question=question, text="bannana") choice3 = Choice(code= 'c',question=question, text="carrot") choice1.save() choice2.save() choice3.save() return question def test_matching_choices(self): question = self.setup_question_and_choices() self.assertEquals(len(question.matching_choices('jdenjn')), 0) self.assertEquals(len(question.matching_choices('a')), 1) self.assertEquals(len(question.matching_choices(None)), 0) def test_humanize_options(self): question = self.setup_question_and_choices() self.assertEquals(question.humanize_options(), "a. apple b. bannana c. carrot") def test_humanize_questions(self): question = self.setup_question_and_choices() self.assertEquals(str(question), "question 1: a. apple b. bannana c. carrot") def test_questions_with_helper_text(self): question = Question(text = 'question 1',num_answers_expected = 1, helper_text="(Prioritize)", questionnaire=self.questionnaire) question.save() choice1 = Choice(code= 'a',question=question, text="apple") choice2 = Choice(code= 'b',question=question, text="bannana") choice3 = Choice(code= 'c',question=question, text="carrot") choice1.save() choice2.save() choice3.save() self.assertEquals(str(question), "question 1: (Prioritize) a. apple b. bannana c. carrot") def test_get_categories(self): self.setup_question_choices_and_categories() categories = self.question.get_categories() self.assertEquals(len(categories), 2) self.assertEquals(categories[0].name, self.fruits.name) self.assertEquals(categories[1].name, self.vegetables.name) def setup_question_choices_and_categories(self): self.question = Question(text = 'question 1',num_answers_expected = 1, helper_text="(Prioritize)", questionnaire=self.questionnaire) self.question.save() self.blue = Color(file_name="blue.png", code="#blue") self.red = Color(file_name="red.png", code="#red") self.blue.save() self.red.save() self.fruits = Category(name="fruits", color=self.red) self.fruits.save() self.vegetables = Category(name="vegetables",color=self.blue) self.vegetables.save() self.carrot = Choice(code= 'b',question=self.question, text="carrot", category=self.vegetables) self.apple = Choice(code= 'a',question=self.question, text="apple", category=self.fruits) self.banana = Choice(code= 'c',question=self.question, text="banana", category=self.fruits) self.ginger = Choice(code= 'd',question=self.question, text="ginger", category=self.vegetables) self.apple.save() self.carrot.save() self.banana.save() self.ginger.save() def test_get_response_break_up(self): self.setup_question_choices_and_categories() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.banana).save() UserResponse(user = self.user, question = self.question, choice = self.banana).save() UserResponse(user = self.user, question = self.question, choice = self.ginger).save() response_break_up = self.question.response_break_up() self.assertEquals(response_break_up["by_choice"].count(), 4) #apple apple = filter( lambda r : r["choice"] == self.apple.id, response_break_up["by_choice"])[0] self.assertEquals(apple["votes"], 3)
class UserSessionTest(TestCase): def setUp(self): self.questionnaire = Questionnaire(trigger = "trigger", max_retries=3) self.questionnaire.save() Question.objects.all().delete() self.backend = PersistantBackend(slug="MockBackend") self.backend.save() self.reporter = Reporter(alias="ReporterName") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1000", governorate = 2, district = 4) self.pconnection.save() self.pconnection1 = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="10001", governorate = 7, district = 8) self.pconnection1.save() self.reporter.connections.add(self.pconnection) self.question1 = Question(text = "question1", questionnaire=self.questionnaire) self.question1.is_first = True self.question1.save() self.question2 = Question(text = "question2", questionnaire=self.questionnaire) self.question2.save() self.question3 = Question(text = "question3", questionnaire=self.questionnaire) self.question3.save() self.question1.next_question = self.question2 self.question2.next_question = self.question3 self.question1.save() self.question2.save() self.question3.save() self.setup_choices(self.question1) self.setup_choices(self.question2) self.setup_choices(self.question3) DemographicParser(questionnaire=self.questionnaire, name='age', regex='[0-9]+', order=1, type='i').save() DemographicParser(questionnaire=self.questionnaire, name='gender', regex='m|f|male|female', order=2, type='c').save() r = Registration(phone = self.pconnection) r.save() r1 = Registration(phone = self.pconnection) r1.save() def setup_choices(self,question): choice1 = Choice(code= 'a',question=question, text="a") choice2 = Choice(code= 'b',question=question, text="a") choice3 = Choice(code= 'c',question=question, text="a") choice1.save() choice2.save() choice3.save() def test_open_new_session(self): session = UserSession.open(self.pconnection) user = session.user user.save() self.assertEquals(session.question, None) def test_respond_with_first_question_on_new_session_for_any_message(self): session = UserSession.open(self.pconnection) user = session.user user.save() self.assertEquals(session.respond(TestMessage(text="trigger m 16")), str(self.question1)) def test_demographic_information_is_mandatory_to_start_the_poll(self): session = UserSession.open(self.pconnection) user = session.user user.save() self.assertEquals(session.respond(TestMessage(text="trigger")), TRIGGER_INCORRECT_MESSAGE) def test_correct_response_to_question_sends_next_question(self): session = UserSession.open(self.pconnection) user = session.user user.save() self.assertEquals(session.question, None) response1 = session.respond(TestMessage(text="trigger m 16")) self.assertEquals(session.question, self.question1) response2 = session.respond(TestMessage(text="a")) self.assertEquals(response2, str(self.question2)) self.assertEquals(session.question, self.question2) def test_wrong_response_to_question_sends_error(self): session = UserSession.open(self.pconnection) self.assertEquals(session.question, None) response1 = session.respond(TestMessage(text="trigger f 16")) self.assertEquals(session.question, self.question1) response2 = session.respond(TestMessage(text="django")) self.assertEquals(response2, "error_parsing_response") self.assertEquals(session.question, self.question1) def test_retrieve_ongoing_session_at_question2(self): session = UserSession.open(self.pconnection) user = session.user user.save() session.user = user session.question = self.question2 session.save() session = UserSession.open(self.pconnection) self.assertEquals(session.respond(TestMessage(text="b")), str(self.question3)) self.assertEquals(session.question, self.question3) def test_close_ongoing_session_at_trigger(self): session = UserSession.open(self.pconnection) user = session.user user.save() session.user = user session.question = self.question2 self.assertEquals(session.respond(TestMessage(text="c")), str(self.question3)) self.assertEquals(session.question, self.question3) self.assertEquals(session.respond(TestMessage(text="trigger 13 m")), str(self.question1)) self.assertEquals(session.question, self.question1) def test_close_session_on_last_answer(self): session = UserSession.open(self.pconnection) user = session.user user.save() session.user = user session.question = self.question3 self.assertEquals(session.respond(TestMessage(text="c")), "thanks") self.assertEquals(session.user.active, False) def test_user_interaction_is_saved_when_successful(self): initial_number_of_responses = len(UserResponse.objects.all()) initial_number_of_users = len(User.objects.all()) session = UserSession.open(self.pconnection1) session.respond(TestMessage(text='trigger 14 f')) self.assertEquals(len(User.objects.all()), initial_number_of_users + 1) session.respond(TestMessage(text='a')) self.assertEquals(len(UserResponse.objects.all()), initial_number_of_responses + 1) def test_end_session_on_reaching_max_num_allowed_retries(self): session = UserSession.open(self.pconnection1) user = session.user user.save() session.user = user session.question = self.question1 session.respond(TestMessage(text='t')) session.respond(TestMessage(text='t')) session.respond(TestMessage(text='t')) self.assertEquals(session.user.active, False) def test_user_demographics_saved_when_present(self): session = UserSession.open(self.pconnection1) session.respond(TestMessage(text='trigger 13 f')) latest_user = User.objects.all().order_by('-id')[0] self.assertEquals(latest_user.age, 13) self.assertEquals(latest_user.gender, 'f') def test_user_location_from_registration(self): session = UserSession.open(self.pconnection) session.respond(TestMessage(text='trigger 14 f')) latest_user = User.objects.all().order_by('-id')[0] self.assertEquals(latest_user.governorate, 2) self.assertEquals(latest_user.district, 4) def test_junk_trigger_message(self): backend = PersistantBackend(slug="MockBackend1") backend.save() reporter = Reporter(alias="ReporterName1") reporter.save() pconnection = PersistantConnection(backend=backend, reporter=reporter, identity="1001") pconnection.save() session = UserSession.open(pconnection) self.assertEquals(session.respond(TestMessage(text='trigger junk')), TRIGGER_INCORRECT_MESSAGE ) def test_junk_message(self): session = UserSession.open(self.pconnection) self.assertEquals(session.respond(TestMessage(text='junk')), TRIGGER_INCORRECT_MESSAGE ) def test_recreate_user_if_demographic_data_changes(self): session = UserSession.open(self.pconnection) session.respond(TestMessage(text="trigger m 16")) session.respond(TestMessage(text="trigger m 12")) self.assertEquals(len(User.objects.all()), 2) def test_reset_session_on_sending_trigger(self): session = UserSession.open(self.pconnection) session.respond(TestMessage(text="trigger m 16")) self.assertEquals(session.question, self.question1) session.respond(TestMessage(text="trigger f 12")) self.assertEquals(session.question, self.question1) session.respond(TestMessage(text="a")) self.assertEquals(session.question, self.question2) def test_less_than_required_choices_reminds_user(self): self.question1.num_answers_expected = 2 self.question1.save() session = UserSession.open(self.pconnection) session.respond(TestMessage(text="trigger m 16")) self.assertEquals(session.question.num_answers_expected, 2) error = session.respond(TestMessage(text="a")) self.assertEquals(error, "err_less_than_expected_choices") error = session.respond(TestMessage(text="a a")) self.assertEquals(error, "err_less_than_expected_choices") def test_more_than_required_choices_reminds_user(self): session = UserSession.open(self.pconnection) session.respond(TestMessage(text="trigger m 16")) self.assertEquals(session.question.num_answers_expected, 1) error = session.respond(TestMessage(text="a b")) self.assertEquals(error, "err_more_than_expected_choices")
class QuestionTest(TestCase): def setUp(self): Question.objects.all().delete() self.backend = PersistantBackend(slug="MockBackend1") self.backend.save() self.reporter = Reporter(alias="ReporterName1") self.reporter.save() self.pconnection = PersistantConnection(backend=self.backend, reporter=self.reporter, identity="1000") self.pconnection.save() self.reporter.connections.add(self.pconnection) self.user = User(connection=self.pconnection, age=12, gender='m', governorate=1, district=1) self.user.save() def test_save(self): initial_no_of_questions = len(Question.objects.all()) question1 = Question(text = 'question 1', max_choices = 3) question2 = Question(text = 'question 2', max_choices = 3) question1.next_question = question2 question1.save() question2.save() def test_last_question(self): question1 = Question(text = 'question 1') question1.save() next_question = question1.next_question self.assertEquals(next_question, None) def test_first(self): question1 = Question(text = 'question 1') question2 = Question(text = 'question 2') question3 = Question(text = 'question 3') question2.is_first = True question1.save() question2.save() question3.save() first_question = Question.first() self.assertEquals(first_question, question2) def setup_question_and_choices(self): question = Question(text = 'question 1',max_choices = 1) question.save() choice1 = Choice(code= 'a',question=question, text="apple") choice2 = Choice(code= 'b',question=question, text="bannana") choice3 = Choice(code= 'c',question=question, text="carrot") choice1.save() choice2.save() choice3.save() return question def test_matching_choices(self): question = self.setup_question_and_choices() self.assertEquals(len(question.matching_choices('jdenjn')), 0) self.assertEquals(len(question.matching_choices('a')), 1) self.assertEquals(len(question.matching_choices(None)), 0) def test_humanize_options(self): question = self.setup_question_and_choices() self.assertEquals(question.humanize_options(), "a. apple b. bannana c. carrot") def test_humanize_questions(self): question = self.setup_question_and_choices() self.assertEquals(str(question), "question 1: a. apple b. bannana c. carrot") def test_questions_with_helper_text(self): question = Question(text = 'question 1',max_choices = 1, helper_text="(Prioritize)") question.save() choice1 = Choice(code= 'a',question=question, text="apple") choice2 = Choice(code= 'b',question=question, text="bannana") choice3 = Choice(code= 'c',question=question, text="carrot") choice1.save() choice2.save() choice3.save() self.assertEquals(str(question), "question 1: (Prioritize) a. apple b. bannana c. carrot") def test_get_categories(self): self.setup_question_choices_and_categories() categories = self.question.get_categories() self.assertEquals(len(categories), 2) self.assertEquals(categories[0]['category__name'], self.fruits.name) self.assertEquals(categories[1]['category__name'], self.vegetables.name) def setup_question_choices_and_categories(self): self.question = Question(text = 'question 1',max_choices = 1, helper_text="(Prioritize)") self.question.save() self.blue = Color(file_name="blue.png", code="#blue") self.red = Color(file_name="red.png", code="#red") self.blue.save() self.red.save() self.fruits = Category(name="fruits", color=self.red) self.fruits.save() self.vegetables = Category(name="vegetables",color=self.blue) self.vegetables.save() self.carrot = Choice(code= 'b',question=self.question, text="carrot", category=self.vegetables) self.apple = Choice(code= 'a',question=self.question, text="apple", category=self.fruits) self.banana = Choice(code= 'c',question=self.question, text="banana", category=self.fruits) self.ginger = Choice(code= 'd',question=self.question, text="ginger", category=self.vegetables) self.apple.save() self.carrot.save() self.banana.save() self.ginger.save() def test_get_response_break_up(self): self.setup_question_choices_and_categories() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.apple).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.carrot).save() UserResponse(user = self.user, question = self.question, choice = self.banana).save() UserResponse(user = self.user, question = self.question, choice = self.banana).save() UserResponse(user = self.user, question = self.question, choice = self.ginger).save() response_break_up = self.question.response_break_up() self.assertEquals(len(response_break_up), 2) self.assertEquals(response_break_up[0].percentage, 50.0) self.assertEquals(response_break_up[1].percentage, 50.0) self.assertEquals(response_break_up[0].color, self.fruits.color.code) self.assertEquals(response_break_up[1].color, self.vegetables.color.code) self.assertEquals(response_break_up[0].text, self.fruits.name) self.assertEquals(response_break_up[1].text, self.vegetables.name) def test_get_response_break_up_for_no_responses(self): self.setup_question_choices_and_categories() response_break_up = self.question.response_break_up() self.assertEquals(len(response_break_up), 1) self.assertEquals(response_break_up[0].text, "No responses yet") self.assertEquals(response_break_up[0].percentage, 0.0) self.assertEquals(response_break_up[0].color, "#FAAFBE")