def test_information_holder_with_categories(self): information_holder = InformationHolder() herbs_category = Category.objects.create(name="Herbs") self.marihuana_topic.category = herbs_category self.marihuana_topic.save() others_category = Category.objects.create(name="Others") self.religion_topic.category = others_category self.religion_topic.save() information_holder.add_category(herbs_category) information_holder.add_category(others_category) self.assertEquals(information_holder.categories, [herbs_category, others_category]) self.assertEquals(information_holder.topics, [self.marihuana_topic, self.religion_topic])
def get_information_holder(self, data={}): holder = InformationHolder(adapter=self.adapter_class) for category in self.object.categories.all(): holder.add_category(category) for candidate in self.object.candidates.all(): holder.add_person(candidate) if data: taken_positions = self.determine_taken_positions(data) for taken_position in taken_positions: holder.add_position(taken_position) return holder
def get_information_holder(self, data={}): holder = InformationHolder(adapter=self.adapter_class) categories = cache.get(str(self.object.id) + '_categories') if categories is None: categories = self.object.categories.all() cache.set( str(self.object.id) + '_categories', categories, 60 * settings.SOUL_MATE_INFO_ABOUT_CANDIDATES_MINUTES) for category in categories: holder.add_category(category) candidates = cache.get(str(self.object.id) + '_candidates') if candidates is None: candidates = self.object.candidates.exclude( taken_positions__isnull=True) cache.set( str(self.object.id) + '_candidates', candidates, 60 * settings.SOUL_MATE_INFO_ABOUT_CANDIDATES_MINUTES) for candidate in candidates: holder.add_person(candidate) if data: taken_positions = self.determine_taken_positions(data) for taken_position in taken_positions: holder.add_position(taken_position) return holder
def get_information_holder(self, data={}): holder = InformationHolder(adapter=self.adapter_class) categories = cache.get(str(self.object.id) + '_categories') if categories is None: categories = self.object.categories.all() cache.set(str(self.object.id) + '_categories', categories, 60 * config.SOUL_MATE_INFO_ABOUT_CANDIDATES_MINUTES) for category in categories: holder.add_category(category) candidates = cache.get(str(self.object.id) + '_candidates') if candidates is None: candidates = self.object.candidates.exclude(taken_positions__isnull=True) cache.set(str(self.object.id) + '_candidates', candidates, 60 * config.SOUL_MATE_INFO_ABOUT_CANDIDATES_MINUTES) for candidate in candidates: holder.add_person(candidate) if data: taken_positions = self.determine_taken_positions(data) for taken_position in taken_positions: holder.add_position(taken_position) return holder
def test_compare_categories_with_information_holder(self): information_holder = InformationHolder() herbs_category = Category.objects.create(name="Herbs") self.marihuana_topic.category = herbs_category self.marihuana_topic.save() self.chamomile_topic.category = herbs_category self.chamomile_topic.save() others_category = Category.objects.create(name="Others") self.religion_topic.category = others_category self.religion_topic.save() self.gay_marriage_topic.category = others_category self.gay_marriage_topic.save() marihuana_position = TakenPosition( topic=self.marihuana_topic, position=self.marihuana_no, ) religion_position = TakenPosition( topic=self.religion_topic, position=self.religion_no, ) chamomile_position = TakenPosition( topic=self.chamomile_topic, position=self.chamomile_no, ) gay_marriage_position = TakenPosition( topic=self.gay_marriage_topic, position=self.gay_marriage_yes, ) information_holder.add_position(marihuana_position) information_holder.add_position(religion_position) information_holder.add_position(chamomile_position) information_holder.add_position(gay_marriage_position) information_holder.add_person(self.person1) information_holder.add_person(self.person2) information_holder.add_person(self.person3) information_holder.add_category(herbs_category) information_holder.add_category(others_category) comparer = Comparer() result = comparer.compare(information_holder) # # topic\person | person1 | person2 | person3 | my positions #==================================================================== # marihuana | y | n | n | n # chamomille | y | n | n | n # religion | n | y | n | n # gay marriage | y | y | y | y #==================================================================== # Afinity % | 50% | 75% | 100% | N/A # self.maxDiff = None expected_result = [{"person": self.person3, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": True, 'my_position': self.marihuana_no, 'their_position': self.marihuana_no }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": True, 'my_position': self.chamomile_no, 'their_position': self.chamomile_no }, } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": True, 'my_position': self.religion_no, 'their_position': self.religion_no }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 1.0 }, {"person": self.person2, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": True, 'my_position': self.marihuana_no, 'their_position': self.marihuana_no }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": True, 'my_position': self.chamomile_no, 'their_position': self.chamomile_no } } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": False, 'my_position': self.religion_no, 'their_position': self.religion_yes }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 0.75 }, {"person": self.person1, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": False, 'my_position': self.marihuana_no, 'their_position': self.marihuana_yes }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": False, 'my_position': self.chamomile_no, 'their_position': self.chamomile_yes } } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": True, 'my_position': self.religion_no, 'their_position': self.religion_no }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 0.5 }] self.assertEquals(result, expected_result) # # topic\person | person1 | person2 | person3 | my positions #==================================================================== # marihuana | y | n | n | n # chamomille | y | n | n | n # religion | - | y | n | n # gay marriage | y | y | y | y #==================================================================== # Afinity % | 25% | 75% | 100% | N/A # self.person1_religion_no.delete() result = comparer.compare(information_holder) self.assertEquals(result[2]['percentage'], 0.25)
def test_split_positions_in_categories(self): information_holder = InformationHolder() herbs_category = Category.objects.create(name="Herbs") self.marihuana_topic.category = herbs_category self.marihuana_topic.save() self.chamomile_topic.category = herbs_category self.chamomile_topic.save() others_category = Category.objects.create(name="Others") self.religion_topic.category = others_category self.religion_topic.save() self.gay_marriage_topic.category = others_category self.gay_marriage_topic.save() marihuana_position = TakenPosition( topic=self.marihuana_topic, position=self.marihuana_yes, ) religion_position = TakenPosition( topic=self.religion_topic, position=self.religion_yes, ) chamomile_position = TakenPosition( topic=self.chamomile_topic, position=self.chamomile_no, ) gay_marriage_position = TakenPosition( topic=self.gay_marriage_topic, position=self.gay_marriage_yes, ) information_holder.add_position(marihuana_position) information_holder.add_position(religion_position) information_holder.add_position(chamomile_position) information_holder.add_position(gay_marriage_position) positions_by_herbs = information_holder.positions_by(herbs_category) self.assertEquals(positions_by_herbs[self.marihuana_topic.id], marihuana_position) self.assertEquals(positions_by_herbs[self.chamomile_topic.id], chamomile_position) positions_by_others = information_holder.positions_by(others_category) self.assertEquals(positions_by_others[self.religion_topic.id], religion_position) self.assertEquals(positions_by_others[self.gay_marriage_topic.id], gay_marriage_position)
def test_information_holder(self): '''InformationHolder''' information_holder = InformationHolder() marihuana_position = TakenPosition( topic=self.marihuana_topic, position=self.marihuana_yes, ) religion_position = TakenPosition( topic=self.religion_topic, position=self.religion_yes, ) information_holder.add_position(marihuana_position) information_holder.add_position(religion_position) self.assertEquals(information_holder.positions[self.marihuana_topic.id], marihuana_position) self.assertEquals(information_holder.positions[self.religion_topic.id], religion_position) information_holder.add_person(self.person1) self.assertEquals(information_holder.persons, [self.person1]) information_holder.add_person(self.person2) self.assertEquals(information_holder.persons, [self.person1, self.person2]) information_holder.add_person(self.person3) self.assertEquals(information_holder.persons, [self.person1, self.person2, self.person3]) information_holder.add_topic(self.marihuana_topic) self.assertEquals(information_holder.topics, [self.marihuana_topic])
def test_compare_categories_with_information_holder(self): information_holder = InformationHolder() herbs_category = Category.objects.create(name="Herbs") self.marihuana_topic.category = herbs_category self.marihuana_topic.save() self.chamomile_topic.category = herbs_category self.chamomile_topic.save() others_category = Category.objects.create(name="Others") self.religion_topic.category = others_category self.religion_topic.save() self.gay_marriage_topic.category = others_category self.gay_marriage_topic.save() marihuana_position = TakenPosition( topic=self.marihuana_topic, position=self.marihuana_no, ) religion_position = TakenPosition( topic=self.religion_topic, position=self.religion_no, ) chamomile_position = TakenPosition( topic=self.chamomile_topic, position=self.chamomile_no, ) gay_marriage_position = TakenPosition( topic=self.gay_marriage_topic, position=self.gay_marriage_yes, ) information_holder.add_position(marihuana_position) information_holder.add_position(religion_position) information_holder.add_position(chamomile_position) information_holder.add_position(gay_marriage_position) information_holder.add_person(self.person1) information_holder.add_person(self.person2) information_holder.add_person(self.person3) information_holder.add_category(herbs_category) information_holder.add_category(others_category) comparer = Comparer() result = comparer.compare(information_holder) # # topic\person | person1 | person2 | person3 | my positions #==================================================================== # marihuana | y | n | n | n # chamomille | y | n | n | n # religion | n | y | n | n # gay marriage | y | y | y | y #==================================================================== # Afinity % | 50% | 75% | 100% | N/A # self.maxDiff = None expected_result = [{ "person": self.person3, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": True, 'my_position': self.marihuana_no, 'their_position': self.marihuana_no }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": True, 'my_position': self.chamomile_no, 'their_position': self.chamomile_no }, } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": True, 'my_position': self.religion_no, 'their_position': self.religion_no }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 1.0 }, { "person": self.person2, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": True, 'my_position': self.marihuana_no, 'their_position': self.marihuana_no }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": True, 'my_position': self.chamomile_no, 'their_position': self.chamomile_no } } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": False, 'my_position': self.religion_no, 'their_position': self.religion_yes }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 0.75 }, { "person": self.person1, "explanation": { herbs_category.slug: { "category": herbs_category, "per_topic": { self.marihuana_topic.id: { "topic": self.marihuana_topic, "match": False, 'my_position': self.marihuana_no, 'their_position': self.marihuana_yes }, self.chamomile_topic.id: { "topic": self.chamomile_topic, "match": False, 'my_position': self.chamomile_no, 'their_position': self.chamomile_yes } } }, others_category.slug: { "category": others_category, "per_topic": { self.religion_topic.id: { "topic": self.religion_topic, "match": True, 'my_position': self.religion_no, 'their_position': self.religion_no }, self.gay_marriage_topic.id: { "topic": self.gay_marriage_topic, "match": True, 'my_position': self.gay_marriage_yes, 'their_position': self.gay_marriage_yes } } } }, "percentage": 0.5 }] self.assertEquals(result, expected_result) # # topic\person | person1 | person2 | person3 | my positions #==================================================================== # marihuana | y | n | n | n # chamomille | y | n | n | n # religion | - | y | n | n # gay marriage | y | y | y | y #==================================================================== # Afinity % | 25% | 75% | 100% | N/A # self.person1_religion_no.delete() result = comparer.compare(information_holder) self.assertEquals(result[2]['percentage'], 0.25)
def test_information_holder(self): '''InformationHolder''' information_holder = InformationHolder() marihuana_position = TakenPosition( topic=self.marihuana_topic, position=self.marihuana_yes, ) religion_position = TakenPosition( topic=self.religion_topic, position=self.religion_yes, ) information_holder.add_position(marihuana_position) information_holder.add_position(religion_position) self.assertEquals( information_holder.positions[self.marihuana_topic.id], marihuana_position) self.assertEquals(information_holder.positions[self.religion_topic.id], religion_position) information_holder.add_person(self.person1) self.assertEquals(information_holder.persons, [self.person1]) information_holder.add_person(self.person2) self.assertEquals(information_holder.persons, [self.person1, self.person2]) information_holder.add_person(self.person3) self.assertEquals(information_holder.persons, [self.person1, self.person2, self.person3]) information_holder.add_topic(self.marihuana_topic) self.assertEquals(information_holder.topics, [self.marihuana_topic])