Skip to content

Commit

Permalink
fix test cases and refactor queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
susilnem committed Dec 17, 2024
1 parent ca1c3d3 commit 32c4d62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
29 changes: 20 additions & 9 deletions per/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,26 +946,37 @@ def stats(self, request):
)

learning_by_sector = (
SectorTag.objects.filter(validated_sectors__in=queryset, title__isnull=False)
SectorTag.objects.filter(validated_sectors__in=queryset)
.annotate(count=Count("validated_sectors", distinct=True))
.values("title", "count")
)

sources_overtime = {
sources_overtime = (
Appeal.objects.filter(opslearning__in=queryset)
.annotate(type=F("atype"), date=F("start_date"), count=Count("opslearning", distinct=True))
.annotate(
type=F("atype"),
date=F("start_date"),
count=Count("appealdocument", distinct=True),
)
.values("type", "date", "count")
}
)

learning_by_region = (
Region.objects.filter(appeal__in=queryset.values("appeal_code__id"))
.annotate(region_name=F("label"), count=Count("appeal__opslearning", distinct=True))
Region.objects.filter(appeal__opslearning__in=queryset)
.annotate(
region_name=F("label"),
count=Count("appeal__opslearning", distinct=True),
)
.values("region_name", "count")
)

learning_by_country = (
Country.objects.filter(appeal__in=queryset.values("appeal_code__id"))
.annotate(country_id=F("id"), country_name=F("name"), count=Count("appeal__opslearning", distinct=True))
Country.objects.filter(appeal__opslearning__in=queryset)
.annotate(
country_id=F("id"),
country_name=F("name"),
count=Count("appeal__opslearning", distinct=True),
)
.values("country_id", "country_name", "count")
)

Expand All @@ -979,7 +990,7 @@ def stats(self, request):
"sources_overtime": sources_overtime,
"learning_by_country": learning_by_country,
}
return response.Response(data)
return response.Response(OpsLearningStatSerializer(data).data)


class PerDocumentUploadViewSet(viewsets.ModelViewSet):
Expand Down
2 changes: 1 addition & 1 deletion per/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ class LearningBySectorSerializer(serializers.Serializer):

class LearningSourcesOvertimeSerializer(serializers.Serializer):
type = serializers.IntegerField()
date = serializers.DateField()
date = serializers.DateTimeField()
count = serializers.IntegerField()


Expand Down
20 changes: 7 additions & 13 deletions per/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def setUp(self):
self.ops_learning1 = OpsLearningFactory.create(is_validated=True, appeal_code=self.appeal1)
self.ops_learning1.sector_validated.set([self.sector1])

self.ops_learning2 = OpsLearningFactory.create(is_validated=False, appeal_code=self.appeal2)
self.ops_learning2 = OpsLearningFactory.create(is_validated=True, appeal_code=self.appeal2)
self.ops_learning2.sector_validated.set([self.sector2])

self.ops_learning3 = OpsLearningFactory.create(is_validated=False, appeal_code=self.appeal2)
Expand All @@ -279,28 +279,22 @@ def test_ops_learning_stats(self):
self.assertIn(key, response.data)

# Updated counts based on validated entries
self.assertEqual(response.data["operations_included"], 1)
self.assertEqual(response.data["sources_used"], 1)
self.assertEqual(response.data["learning_extracts"], 1)
self.assertEqual(response.data["sectors_covered"], 1)
self.assertEqual(response.data["operations_included"], 2)
self.assertEqual(response.data["sources_used"], 2)
self.assertEqual(response.data["learning_extracts"], 2)
self.assertEqual(response.data["sectors_covered"], 2)

# Validate learning by region
region_data = response.data["learning_by_region"]
self.assertEqual(len(region_data), 1)
self.assertEqual(region_data[0]["region_name"], "Region A")
self.assertEqual(region_data[0]["count"], 1)
self.assertEqual(region_data[0]["count"], 2)

# Validate learning by sector
sector_data = response.data["learning_by_sector"]
self.assertEqual(len(sector_data), 2)
self.assertEqual(sector_data[0]["title"], "Sector 1")
self.assertEqual(sector_data[0]["count"], 1)

# Validate learning by country
country_data = response.data["learning_by_country"]
self.assertEqual(len(country_data), 1)
self.assertEqual(country_data[0]["country_name"], "Country A")
self.assertEqual(country_data[0]["count"], 1)

sources_overtime = response.data["sources_overtime"]
self.assertEqual(len(sources_overtime), 1)
self.assertEqual(len(sources_overtime), 2)

0 comments on commit 32c4d62

Please sign in to comment.