def test_pests_problem_type(self): """ If the user says their problem are weeds (adventices), the three suggestions must address weeds. """ ble = SimulatorCulture.objects.filter(display_text='Blé dur').first() mais = SimulatorCulture.objects.filter(display_text='Maïs').first() chanvre = SimulatorCulture.objects.filter( display_text='Chanvre').first() answers = { "problem": "DESHERBAGE", "rotation": [ble.external_id, chanvre.external_id, mais.external_id] } engine = Engine(answers, [], []) practices = engine.calculate_results() response_items = engine.get_suggestions(practices) # We ensure there are three suggestions, and all three address weeds self.assertEqual(len(response_items), 3) for response_item in response_items: suggestion = response_item.practice self.assertIn(Problem['DESHERBAGE'].value, suggestion.problems_addressed)
def test_mutliple_groups(self): """ https://github.com/betagouv/peps/issues/16 We need to ensure no practices belonging to the same practice group are selected. """ rumex = Weed.objects.filter(display_text='Rumex').first() chardon = Weed.objects.filter( display_text='Chardon des champs').first() answers = { "problem": "GLYPHOSATE", "glyphosate": "VIVACES,COUVERTS", "weeds": "{0},{1}".format(str(chardon.external_id), str(rumex.external_id)), "tillage": "TRAVAIL_PROFOND", } engine = Engine(answers, [], []) results = engine.calculate_results() suggestions = engine.get_suggestions(results) suggested_groups = [] for practice in map(lambda x: x.practice, suggestions): practice_groups = list(practice.practice_groups.all()) for group in practice_groups: self.assertNotIn(group, suggested_groups) suggested_groups.append(group)
def test_blacklist_practices(self): """ It is possible to blacklist individual practices, this test ensures that blacklisted practices end up with a score of zero. """ ble = SimulatorCulture.objects.filter(display_text='Blé dur').first() mais = SimulatorCulture.objects.filter(display_text='Maïs').first() chanvre = SimulatorCulture.objects.filter( display_text='Chanvre').first() # We make a call to get suggestions answers = { "problem": "DESHERBAGE", "rotation": [ble.external_id, chanvre.external_id, mais.external_id] } engine = Engine(answers, [], []) practices = engine.calculate_results() response_items = engine.get_suggestions(practices) # We get the first suggestion - we will blacklist it later and ensure # it is no longer proposed. blacklisted_suggestion_id = str(response_items[0].practice.id) engine = Engine(answers, [blacklisted_suggestion_id], []) practices = engine.calculate_results() response_items = engine.get_suggestions(practices) # Now let's verify that the suggestions no longer include the # blacklisted practice suggested_ids = list(map(lambda x: str(x.practice.id), response_items)) self.assertNotIn(blacklisted_suggestion_id, suggested_ids) # The blacklisted practice should have a score of zero blacklisted_response_item = next( filter(lambda x: str(x.practice.id) == blacklisted_suggestion_id, practices)) self.assertEqual(blacklisted_response_item.weight, 0.0)
def test_blacklist_types(self): """ It is possible to blacklist entire practice types. This test ensures that practices belonging to blacklisted practice types have a score of zero. """ ble = SimulatorCulture.objects.filter(display_text='Blé dur').first() mais = SimulatorCulture.objects.filter(display_text='Maïs').first() chanvre = SimulatorCulture.objects.filter( display_text='Chanvre').first() # We make a call to get suggestions answers = { "problem": "DESHERBAGE", "rotation": [ble.external_id, chanvre.external_id, mais.external_id] } engine = Engine(answers, [], []) practices = engine.calculate_results() response_items = engine.get_suggestions(practices) # We get the first suggestion's practice type. We will blacklist it # later and ensure no practices of the same type are proposed, and that # they are all set to zero. blacklisted_practice_type = str( list(response_items[0].practice.types.all())[0].id) engine = Engine(answers, [], [blacklisted_practice_type]) practices = engine.calculate_results() response_items = engine.get_suggestions(practices) # Now let's verify that all the practices belonging to that type # have a score of zero. for practice_item in practices: practice_types_ids = list( map(lambda x: str(x.id), practice_item.practice.types.all())) if blacklisted_practice_type in practice_types_ids: self.assertEqual(practice_item.weight, 0.0)
def test_suggestion_rankings(self): """ We check that the weight of the proposed suggestions is correct """ answers = { "problem": "MALADIES_FONGIQUES", "rotation": [], "department": "01" } engine = Engine(answers, [], []) practices = engine.calculate_results() suggestions = engine.get_suggestions(practices) # There should be two practices with weight 1.5 self.assertEqual(len(suggestions), 3) weights = list(map(lambda x: x.weight, suggestions)) self.assertEqual(len(list(filter(lambda x: x == 1.5, weights))), 2)
def get_results(answers, practice_blacklist, type_blacklist): engine = Engine(answers, practice_blacklist, type_blacklist) practices = engine.calculate_results() suggestions = engine.get_suggestions(practices) return (practices, suggestions)