def test_conditional_invalid(self): """ Test that it errors on completely wrong conditional. """ tip1_mock = get_tip() tip1_mock['rules'] = new_rule('true') tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] with self.assertRaises(TypeError): tips_generator(self.get_client_data(), tips_pool)
def get_income_tips(): # This is a POST because the user data gets sent in the body. # This data is too large and inappropriate for a GET, also because of privacy reasons with open(PERSOONLIJK_INKOMENS_TIPS_FILE) as fp: persoonlijk_inkomens_tips = json.load(fp) tips_data = tips_generator(request.get_json(), persoonlijk_inkomens_tips) return tips_data
def test_audience(self): tip1_mock = get_tip() tip1_mock["audience"] = ["zakelijk"] tips_pool = [tip1_mock] tips = tips_generator(self.get_client_data(), tips_pool, audience=["zakelijk"]) self.assertEqual(len(tips), 1) tips = tips_generator(self.get_client_data(), tips_pool, audience=["somethingelse"]) self.assertEqual(len(tips), 0) # should not take audience into account when its not passed in tips = tips_generator(self.get_client_data(), tips_pool) self.assertEqual(len(tips), 1)
def get_tips(): # This is a POST because the user data gets sent in the body. # This data is too large and inappropriate for a GET, also because of privacy reasons audience = request.args.get("audience", None) if audience: audience = audience.split(",") request_data = get_tips_request_data(request_data=request.get_json()) tips_data = tips_generator(request_data, audience=audience) return tips_data
def test_start_end(start_date: str, end_date: str, expected: bool): tip1_mock["dateActiveStart"] = start_date tip1_mock["dateActiveEnd"] = end_date tips_pool = [tip1_mock] tips = tips_generator(self.get_client_data(), tips_pool) if expected: self.assertEqual(len(tips), 1) else: self.assertEqual(len(tips), 0)
def test_is_personalized_no_optin(self): tip1_mock = get_tip() tip1_mock["rules"] = [new_rule("True")] tip1_mock["isPersonalized"] = True tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] tips = tips_generator(self.get_client_data(), tips_pool) self.assertEqual(len(tips), 1) self.assertEqual(tips[0]["id"], tip2_mock["id"])
def test_conditional_exception(self): """Test that invalid conditional is (silently) ignored. Probably not the best idea...""" tip1_mock = get_tip() tip1_mock["rules"] = [new_rule("@")] tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] tips = tips_generator(self.get_client_data(), tips_pool) # make sure the other is in there self.assertEqual(len(tips), 1) self.assertEqual(tips[0]["id"], tip2_mock["id"])
def test_data_based_tip_with_list(self): tip1_mock = get_tip() tip1_mock['rules'] = [new_rule("$.focus[@._id is '0-0' and @.processtappen.aanvraag._id is 0]")] tip1_mock['isPersonalized'] = True tips_pool = [tip1_mock] client_data = self.get_client_data(optin=True) result = tips_generator(client_data, tips_pool) tips = result['items'] self.assertEqual(len(tips), 1) self.assertEqual(tips[0]['id'], tip1_mock['id']) self.assertEqual(tips[0]['isPersonalized'], True)
def test_is_personalized_optin(self): tip1_mock = get_tip() tip1_mock["rules"] = [new_rule("True")] tip1_mock["isPersonalized"] = True tip2_mock = get_tip() tip2_mock["rules"] = [new_rule("True")] # do not add isPersonalized to tip 2. It should default to False tips_pool = [tip1_mock, tip2_mock] tips = tips_generator(self.get_client_data(True), tips_pool) self.assertEqual(len(tips), 1) self.assertEqual(tips[0]["isPersonalized"], True)
def test_allow_listed_fields(self): tip1_mock = get_tip() tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] result = tips_generator(self.get_client_data(), tips_pool) tips = result['items'] # only these fields are allowed allow_list = sorted(['datePublished', 'description', 'id', 'link', 'title', 'priority', 'imgUrl', 'isPersonalized']) for tip in tips: fields = sorted(tip.keys()) self.assertEqual(allow_list, fields)
def test_active(self): """Add one active and one inactive tip.""" tip1_mock = get_tip() tip1_mock["active"] = False tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] tips = tips_generator(self.get_client_data(), tips_pool) self.assertEqual(len(tips), 1) # Test if the correct ones are accepted ids = [tip["id"] for tip in tips] self.assertEqual(ids, [tip2_mock["id"]])
def test_conditional(self): """Test one passing conditional, one failing and one without (the default)""" tip1_mock = get_tip() tip1_mock["rules"] = [new_rule("false")] tip2_mock = get_tip() tip2_mock["rules"] = [new_rule("true")] tip3_mock = get_tip() tips_pool = [tip1_mock, tip2_mock, tip3_mock] tips = tips_generator(self.get_client_data(), tips_pool) self.assertEqual(len(tips), 2) # Test if the correct ones are accepted ids = [tip["id"] for tip in tips] self.assertEqual(ids, [tip2_mock["id"], tip3_mock["id"]])
def test_is_personalized(self): tip1_mock = get_tip() tip1_mock['rules'] = [new_rule("True")] tip1_mock['isPersonalized'] = True tip2_mock = get_tip() tip2_mock['rules'] = [new_rule("True")] # do not add isPersonalized to tip 2. It should default to False tips_pool = [tip1_mock, tip2_mock] result = tips_generator(self.get_client_data(), tips_pool) tips = result['items'] self.assertEqual(len(tips), 3) self.assertEqual(tips[0]['isPersonalized'], True) self.assertEqual(tips[1]['isPersonalized'], False)
def test_data_based_tip(self): """ Test whether a tip works correctly when based on user data. """ tip1_mock = get_tip() tip1_mock["rule"] = [new_rule("$.ERFPACHT.isKnown is true")] tip1_mock["isPersonalized"] = True tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] client_data = self.get_client_data(optin=True) tips = tips_generator(client_data, tips_pool) # make sure the other is in there self.assertEqual(len(tips), 1)
def test_active(self): """ Add one active and one inactive tip. """ tip1_mock = get_tip() tip1_mock['active'] = False tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] result = tips_generator(self.get_client_data(), tips_pool) tips = result['items'] self.assertEqual(len(tips), 2) # Test if the correct ones are accepted ids = [tip['id'] for tip in tips] self.assertEqual(ids, [tip2_mock['id'], 'belasting-5'])
def test_data_based_tip_with_list(self): tip1_mock = get_tip() tip1_mock["rules"] = [ new_rule( "$.WPI_AANVRAGEN[@.id is '810805911' and @.steps.*[@.id is 'aanvraag']]" ) ] tip1_mock["isPersonalized"] = True tips_pool = [tip1_mock] client_data = self.get_client_data(optin=True) tips = tips_generator(client_data, tips_pool) self.assertEqual(len(tips), 1) self.assertEqual(tips[0]["id"], tip1_mock["id"]) self.assertEqual(tips[0]["isPersonalized"], True)
def test_conditional(self): """ Test one passing conditional, one failing and one without (the default) """ tip1_mock = get_tip() tip1_mock['rules'] = [new_rule("false")] tip2_mock = get_tip() tip2_mock['rules'] = [new_rule("true")] tip3_mock = get_tip() tips_pool = [tip1_mock, tip2_mock, tip3_mock] result = tips_generator(self.get_client_data(), tips_pool) tips = result['items'] self.assertEqual(len(tips), 3) # Test if the correct ones are accepted ids = [tip['id'] for tip in tips] self.assertEqual(ids, [tip2_mock['id'], tip3_mock['id'], 'belasting-5'])
def test_always_visible(self): tip1_mock = get_tip() tip1_mock["rule"] = [new_rule("$.ERFPACHT.isKnown is true")] tip1_mock["isPersonalized"] = True tip2_mock = get_tip() tip2_mock["isPersonalized"] = False tip2_mock["alwaysVisible"] = True tips_pool = [tip1_mock, tip2_mock] client_data = self.get_client_data(optin=True) tips = tips_generator(client_data, tips_pool) # make sure the other is in there self.assertEqual(len(tips), 2)
def test_data_based_tip(self): """ Test whether a tip works correctly when based on user data. """ tip1_mock = get_tip() tip1_mock['rule'] = [new_rule('$.erfpacht is true')] tip1_mock['isPersonalized'] = True tip2_mock = get_tip() tips_pool = [tip1_mock, tip2_mock] client_data = self.get_client_data(optin=True) result = tips_generator(client_data, tips_pool) tips = result['items'] # make sure the other is in there self.assertEqual(len(tips), 1)
def test_allow_listed_fields(self): tip1_mock = get_tip() tip2_mock = get_tip() tip1_mock["isPersonalized"] = True tip2_mock["isPersonalized"] = True tips_pool = [tip1_mock, tip2_mock] tips = tips_generator(get_tips_request_data(get_fixture(optin=True)), tips_pool) # only these fields are allowed extended_fields_list = sorted(FRONT_END_TIP_KEYS) fields = sorted(tips[0].keys()) self.assertEqual(extended_fields_list, fields) fields = sorted(tips[1].keys()) self.assertEqual(extended_fields_list, fields)
def test_data_based_tip_path(self): tip1_mock = get_tip() tip1_mock["rules"] = [new_rule("$.ERFPACHT.isKnown is true")] tip1_mock["isPersonalized"] = True tip2_mock = get_tip() # 18 or older tip2_mock["rules"] = [{"type": "ref", "ref_id": "2"}] tip2_mock["isPersonalized"] = True tips_pool = [tip1_mock, tip2_mock] client_data = self.get_client_data(optin=True) tips = tips_generator(client_data, tips_pool) # make sure the other is in there self.assertEqual(len(tips), 2) self.assertEqual(tips[0]["id"], tip1_mock["id"]) self.assertEqual(tips[0]["isPersonalized"], True) self.assertEqual(tips[1]["id"], tip2_mock["id"]) self.assertEqual(tips[0]["isPersonalized"], True)
def test_generator(self): tip0 = get_tip(10) tip1 = get_tip(20) tip2 = get_tip(30) tip3 = get_tip(40) tip4 = get_tip(50) tip4["rules"] = [new_rule("False")] # add them out of order to test the ordering tips_pool = [tip1, tip0, tip2, tip3, tip4] tips = tips_generator(self.get_client_data(), tips_pool) self.assertEqual(len(tips), 4) # check order self.assertEqual(tips[3]["id"], tip0["id"]) self.assertEqual(tips[2]["id"], tip1["id"]) self.assertEqual(tips[1]["id"], tip2["id"]) self.assertEqual(tips[0]["id"], tip3["id"])
def test_data_based_tip_path(self): tip1_mock = get_tip() tip1_mock['rules'] = [new_rule("$.erfpacht is true")] tip1_mock['isPersonalized'] = True tip2_mock = get_tip() # 18 or older tip2_mock['rules'] = [{"type": "ref", "ref_id": "2"}] tip2_mock['isPersonalized'] = True tips_pool = [tip1_mock, tip2_mock] client_data = self.get_client_data(optin=True) result = tips_generator(client_data, tips_pool) tips = result['items'] # make sure the other is in there self.assertEqual(len(tips), 2) self.assertEqual(tips[0]['id'], tip1_mock['id']) self.assertEqual(tips[0]['isPersonalized'], True) self.assertEqual(tips[1]['id'], tip2_mock['id']) self.assertEqual(tips[0]['isPersonalized'], True)
def test_generator(self): tip0 = get_tip(10) tip1 = get_tip(20) tip2 = get_tip(30) tip3 = get_tip(40) tip4 = get_tip(50) tip4['rules'] = [new_rule("False")] # add them out of order to test the ordering tips_pool = [tip1, tip0, tip2, tip3, tip4] result = tips_generator(self.get_client_data(), tips_pool) tips = result['items'] self.assertEqual(len(tips), 5) # check order self.assertEqual(tips[3]['id'], tip0['id']) self.assertEqual(tips[2]['id'], tip1['id']) self.assertEqual(tips[1]['id'], tip2['id']) self.assertEqual(tips[0]['id'], tip3['id']) # check enrichment self.assertEqual(tips[4]['imgUrl'], 'api/tips/static/tip_images/belastingen.jpg')
def get_tips(): # This is a POST because the user data gets sent in the body. # This data is too large and inappropriate for a GET, also because of privacy reasons tips_data = tips_generator(request.get_json()) return tips_data