def test_get_active_offers_with_criteria_should_return_offer_with_highest_base_score_first_bust_keep_the_partition( self, app): # Given offerer = create_offerer() venue = create_venue(offerer, postal_code='34000', departement_code='34') offer1 = create_offer_with_thing_product( venue, thing_type=ThingType.CINEMA_ABO, thumb_count=1) stock1 = create_stock_from_offer(offer1, price=0) offer1.criteria = [create_criterion(name='negative', score_delta=1)] offer2 = create_offer_with_thing_product( venue, thing_type=ThingType.CINEMA_ABO, thumb_count=1) stock2 = create_stock_from_offer(offer2, price=0) offer2.criteria = [create_criterion(name='positive', score_delta=2)] offer3 = create_offer_with_thing_product( venue, thing_type=ThingType.JEUX_VIDEO, thumb_count=1) stock3 = create_stock_from_offer(offer3, price=0) offer3.criteria = [] PcObject.save(stock1, stock2, stock3) # When offers = get_active_offers(departement_codes=['00'], offer_id=None, order_by=order_by_with_criteria) # Then assert offers == [offer2, offer3, offer1]
def test_order_by_base_score(self, app): # Given offerer = create_offerer() venue = create_venue(offerer, postal_code='34000', departement_code='34') criterion_negative = create_criterion(name='negative', score_delta=-1) criterion_positive = create_criterion(name='positive', score_delta=1) offer1 = create_offer_with_thing_product(venue) offer2 = create_offer_with_thing_product(venue) offer1.criteria = [criterion_negative] offer2.criteria = [criterion_negative, criterion_positive] PcObject.save(offer1, offer2) # When offers = Offer.query \ .order_by(Offer.baseScore.desc()) \ .all() # Then assert offers == [offer2, offer1]
def test_offer_base_score_with_multiple_criteria(self): # Given offerer = create_offerer() venue = create_venue(offerer) offer = create_offer_with_event_product(venue) offer.criteria = [ create_criterion(name='negative', score_delta=-1), create_criterion(name='positive', score_delta=2) ] PcObject.save(offer) # Then assert offer.baseScore == 1
def test_serialize_with_criteria(self): criterion1 = create_criterion(self.subcategory.id) criterion2 = create_criterion(self.subcategory.id) self.assertEqual( { 'id': self.subcategory.id, 'category_id': self.category.id, 'title': 'Safe Work Environment', 'help_text': 'Some help text', 'active': True, 'deactivated_at': None, 'criteria': [ criterion1.serialize(), criterion2.serialize(), ] }, self.subcategory.serialize(with_criteria=True))
def test_put_criterion_deactivate(self, mock_auth): criterion = create_criterion(self.subcategory.id) data = { 'active': False, } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() criterion = Criterion.query.first() self.assertFalse(criterion.active) self.assertTrue(isinstance(criterion.deactivated_at, datetime.datetime)) # Criterion cannot be reactivated deactivated_at = criterion.deactivated_at data = { 'active': True, } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) criterion = Criterion.query.first() self.assertFalse(criterion.active) self.assertEqual(criterion.deactivated_at, deactivated_at)
def setUp(self): self.state = create_state() self.category = create_category() self.subcategory = create_subcategory(self.category.id) self.criterion = create_criterion(self.subcategory.id) self.score = Score( criterion_id=self.criterion.id, state=self.state.code, meets_criterion=True, ).save()
def test_put_criterion(self, mock_auth): subcategory_id = self.subcategory.id criterion = create_criterion(subcategory_id) data = { 'title': 'A New Title', 'help_text': 'Some new help text', } response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 200) mock_auth.assert_called_once() criterion = Criterion.query.first() self.assertEqual(criterion.subcategory_id, subcategory_id) self.assertEqual(criterion.title, 'A New Title') self.assertEqual( criterion.recommendation_text, "The state's definition of domestic violence should include a framework of economic " 'abuse', ) self.assertEqual(criterion.help_text, 'Some new help text') self.assertFalse(criterion.adverse) self.assertTrue(criterion.active) self.assertIsNone(criterion.deactivated_at) json_response = json.loads(response.data) self.assertEqual( json_response, { 'id': criterion.id, 'subcategory_id': subcategory_id, 'title': 'A New Title', 'recommendation_text': "The state's definition of domestic violence should include a framework of " 'economic abuse', 'help_text': 'Some new help text', 'adverse': False, 'active': True, 'deactivated_at': None, })
def test_put_criterion_cannot_change_subcategory(self, mock_auth): subcategory_id = self.subcategory.id criterion = create_criterion(subcategory_id) data = {'subcategory_id': subcategory_id + 1} response = self.client.put( '/criteria/%i' % criterion.id, json=data, headers=auth_headers(), ) self.assertEqual(response.status_code, 400) mock_auth.assert_called_once() json_response = json.loads(response.data) self.assertEqual(json_response['description'], strings.cannot_change_subcategory)
def setUp(self): self.client = app.test_client() self.state = State( code='NY', name='New York', innovative_idea='Innovative idea', honorable_mention='Honorable mention', ).save() other_state = create_state(code='AZ') category1 = create_category() category2 = create_category() subcategory = create_subcategory(category1.id) criterion1 = create_criterion(subcategory.id) criterion2 = create_criterion(subcategory.id) self.link1 = create_link(subcategory.id, self.state.code) self.link2 = create_link(subcategory.id, self.state.code) self.state_grade1 = create_state_grade(self.state.code) self.state_grade2 = create_state_grade(self.state.code) # state_grade1 is more recent than state_grade2 self.state_grade1.created_at = datetime.utcnow() self.state_grade2.created_at = datetime.utcnow() - timedelta(5) self.state_category_grade1 = create_state_category_grade( self.state.code, category1.id) self.state_category_grade2 = create_state_category_grade( self.state.code, category1.id) self.state_category_grade3 = create_state_category_grade( self.state.code, category2.id) # state_category_grade1 is more recent than state_category_grade2 self.state_category_grade1.created_at = datetime.utcnow() self.state_category_grade2.created_at = datetime.utcnow() - timedelta( 5) self.score1 = Score( criterion_id=criterion1.id, state=self.state.code, meets_criterion=True, ) self.score2 = Score( criterion_id=criterion2.id, state=self.state.code, meets_criterion=False, ) self.score3 = Score( criterion_id=criterion2.id, state=self.state.code, meets_criterion=True, ) # score2 is more recent than score3 self.score3.created_at = datetime.utcnow() - timedelta(5) self.score4 = Score( criterion_id=criterion2.id, state=other_state.code, meets_criterion=True, ) Score.save_all([self.score1, self.score2, self.score3, self.score4]) self.maxDiff = None
def setUp(self): self.client = app.test_client() self.category = create_category() self.subcategory = create_subcategory(self.category.id) self.criterion = create_criterion(self.subcategory.id) self.state = create_state()