def test_pagination_sectors(self): """ The pagination endpoint should return all sectors that are used by canteens, even when the data is filtered by another sector """ school = SectorFactory.create(name="School") enterprise = SectorFactory.create(name="Enterprise") administration = SectorFactory.create(name="Administration") # unused sectors shouldn't show up as an option SectorFactory.create(name="Unused") CanteenFactory.create(publication_status="published", sectors=[school, enterprise], name="Shiso") CanteenFactory.create(publication_status="published", sectors=[school], name="Wasabi") CanteenFactory.create(publication_status="published", sectors=[school], name="Mochi") CanteenFactory.create(publication_status="published", sectors=[administration], name="Umami") url = f"{reverse('published_canteens')}" response = self.client.get(url) body = response.json() self.assertEqual(len(body.get("sectors")), 3) self.assertIn(school.id, body.get("sectors")) self.assertIn(enterprise.id, body.get("sectors")) self.assertIn(administration.id, body.get("sectors")) url = f"{reverse('published_canteens')}?sectors={enterprise.id}" response = self.client.get(url) body = response.json() self.assertEqual(len(body.get("sectors")), 3) self.assertIn(school.id, body.get("sectors")) self.assertIn(enterprise.id, body.get("sectors")) self.assertIn(administration.id, body.get("sectors"))
def test_sectors_filter(self): school = SectorFactory.create(name="School") enterprise = SectorFactory.create(name="Enterprise") social = SectorFactory.create(name="Social") CanteenFactory.create(publication_status="published", sectors=[school], name="Shiso") CanteenFactory.create(publication_status="published", sectors=[enterprise], name="Wasabi") CanteenFactory.create(publication_status="published", sectors=[social], name="Mochi") CanteenFactory.create(publication_status="published", sectors=[school, social], name="Umami") url = f"{reverse('published_canteens')}?sectors={school.id}" response = self.client.get(url) results = response.json().get("results", []) self.assertEqual(len(results), 2) result_names = list(map(lambda x: x.get("name"), results)) self.assertIn("Shiso", result_names) self.assertIn("Umami", result_names) url = f"{reverse('published_canteens')}?sectors={enterprise.id}" response = self.client.get(url) results = response.json().get("results", []) self.assertEqual(len(results), 1) self.assertEqual(results[0].get("name"), "Wasabi") url = f"{reverse('published_canteens')}?sectors={enterprise.id}§ors={social.id}" response = self.client.get(url) results = response.json().get("results", []) self.assertEqual(len(results), 3) result_names = list(map(lambda x: x.get("name"), results)) self.assertIn("Wasabi", result_names) self.assertIn("Mochi", result_names) self.assertIn("Umami", result_names)
def test_invalid_sectors_raise_error(self, _): """ If file specifies invalid sector, error is raised for that line """ SectorFactory.create(name="Social et Médico-social (ESMS)") with open("./api/tests/files/diagnostics_sectors.csv") as diag_file: response = self.client.post(reverse("import_diagnostics"), {"file": diag_file}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(Canteen.objects.count(), 0) body = response.json() self.assertEqual(body["errors"][0]["status"], 400) self.assertEqual( body["errors"][0]["message"], "Le secteur spécifié ne fait pas partie des options acceptées", )
def test_diversification_badge_earned(self): """ Test that the right canteens are identifies in diversification badge qs """ primaire = SectorFactory(name="Scolaire primaire", category="education") secondaire = SectorFactory(name="Scolaire secondaire", category="education") # --- canteens which don't earn diversification badge: high_canteen = CanteenFactory.create() high_canteen.sectors.remove(primaire) high_canteen.sectors.remove(secondaire) DiagnosticFactory.create( canteen=high_canteen, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.HIGH.value) low_canteen = CanteenFactory.create() low_canteen.sectors.add(primaire) DiagnosticFactory.create( canteen=low_canteen, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW.value) # --- canteens which earn diversification badge: daily_vege = CanteenFactory.create() daily_vege.sectors.remove(primaire) daily_vege.sectors.remove(secondaire) DiagnosticFactory.create( canteen=daily_vege, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY.value) scolaire_mid_vege = CanteenFactory.create() scolaire_mid_vege.sectors.add(secondaire) DiagnosticFactory.create( canteen=scolaire_mid_vege, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.MID.value) badges = badges_for_queryset(Diagnostic.objects.all()) diversification_badge_qs = badges["diversification"] self.assertEqual(diversification_badge_qs.count(), 2) self.assertTrue( diversification_badge_qs.filter(canteen=daily_vege).exists()) self.assertTrue( diversification_badge_qs.filter( canteen=scolaire_mid_vege).exists())
def test_canteen_stats_by_sectors(self): year = 2020 school = SectorFactory.create(name="School") enterprise = SectorFactory.create(name="Enterprise") social = SectorFactory.create(name="Social") CanteenFactory.create(sectors=[school]) CanteenFactory.create(sectors=[enterprise]) CanteenFactory.create(sectors=[enterprise, social]) CanteenFactory.create(sectors=[social]) response = self.client.get(reverse("canteen_statistics"), { "sectors": [school.id, enterprise.id], "year": year }) self.assertEqual(response.status_code, status.HTTP_200_OK) body = response.json() self.assertEqual(body["canteenCount"], 3)
def test_valid_sectors_parsed(self, _): """ File can specify 0+ sectors to add to the canteen """ SectorFactory.create(name="Social et Médico-social (ESMS)") SectorFactory.create(name="Crèche") SectorFactory.create(name="Scolaire") with open("./api/tests/files/diagnostics_sectors.csv") as diag_file: response = self.client.post(reverse("import_diagnostics"), {"file": diag_file}) self.assertEqual(response.status_code, status.HTTP_200_OK) canteen = Canteen.objects.get(siret="21340172201787") self.assertEqual(canteen.sectors.count(), 3)
def test_get_sectors(self): """ The API should return all sectors """ SectorFactory.create() SectorFactory.create() SectorFactory.create() response = self.client.get(reverse("sectors_list")) self.assertEqual(response.status_code, status.HTTP_200_OK) body = response.json() self.assertEqual(len(body), 3)
def test_canteen_statistics(self): """ This public endpoint returns some summary statistics for a region and a location """ # TODO: more nuance when choosing canteens to get stats for? # How do we know that the canteen diagnostic is done? # Could check for total value ht # create 5 canteens (3 in region of interest), 1 unpublished region = "01" year = 2020 school = SectorFactory.create(name="School") enterprise = SectorFactory.create(name="Enterprise") social = SectorFactory.create(name="Social") published = CanteenFactory.create( region=region, publication_status=Canteen.PublicationStatus.PUBLISHED.value, sectors=[school, enterprise], daily_meal_count=50, ) unpublished = CanteenFactory.create( region=region, publication_status=Canteen.PublicationStatus.DRAFT.value, sectors=[school], daily_meal_count=50, ) other_region = CanteenFactory.create(region="03", sectors=[social], daily_meal_count=50) # relevant diagnostics DiagnosticFactory.create( canteen=published, year=year, value_total_ht=100, value_bio_ht=20, value_sustainable_ht=30, has_waste_diagnostic=False, waste_actions=[], vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY, plastic_tableware_substituted=False, communicates_on_food_quality=False, ) DiagnosticFactory.create( canteen=unpublished, year=year, value_total_ht=1000, value_bio_ht=400, value_sustainable_ht=500, has_waste_diagnostic=True, waste_actions=["action1", "action2"], vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW, cooking_plastic_substituted=True, serving_plastic_substituted=True, plastic_bottles_substituted=True, plastic_tableware_substituted=True, communicates_on_food_quality=True, ) # irrelevant diagnostics DiagnosticFactory.create( canteen=published, year=2019, value_total_ht=100, value_bio_ht=100, value_sustainable_ht=0, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY, ) DiagnosticFactory.create( canteen=other_region, year=year, value_total_ht=100, value_bio_ht=100, value_sustainable_ht=0, cooking_plastic_substituted=True, serving_plastic_substituted=True, plastic_bottles_substituted=True, plastic_tableware_substituted=True, communicates_on_food_quality=True, ) response = self.client.get(reverse("canteen_statistics"), { "region": region, "year": year }) self.assertEqual(response.status_code, status.HTTP_200_OK) body = response.json() self.assertEqual(body["canteenCount"], 2) self.assertEqual(body["publishedCanteenCount"], 1) self.assertEqual(body["bioPercent"], 30) self.assertEqual(body["sustainablePercent"], 40) self.assertEqual(body["approPercent"], 100) self.assertEqual(body["wastePercent"], 50) self.assertEqual(body["diversificationPercent"], 50) self.assertEqual(body["plasticPercent"], 50) self.assertEqual(body["infoPercent"], 50) expected_sectors = {} expected_sectors[str(school.id)] = 2 expected_sectors[str(enterprise.id)] = 1 expected_sectors[str(social.id)] = 0 self.assertEqual(body["sectors"], expected_sectors) # can also call without location info response = self.client.get(reverse("canteen_statistics"), {"year": 2020}) self.assertEqual(response.status_code, status.HTTP_200_OK)