Skip to content

Commit

Permalink
Making tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Nov 10, 2023
1 parent 38d4beb commit d15ccb3
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 4 deletions.
17 changes: 15 additions & 2 deletions metrics/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from django.urls import reverse
from django.contrib.auth.models import Permission
from datetime import datetime, timedelta
from metrics.templatetags.metricstags import categorize, perc
from metrics.templatetags.metricstags import categorize, perc, bool_yesno
from django.utils.translation import gettext_lazy as _


class AreaModelTests(TestCase):
Expand Down Expand Up @@ -592,4 +593,16 @@ def test_perc_for_more_than_100(self):

def test_perc_for_text(self):
result = perc("invalid", 100)
self.assertEqual(result, "-")
self.assertEqual(result, "-")

def test_bool_yesno_returns_yes_if_true(self):
result = bool_yesno(True)
self.assertEqual(result, _("Yes"))

def test_bool_yesno_returns_no_if_false(self):
result = bool_yesno(False)
self.assertEqual(result, _("No"))

def test_bool_yesno_returns_value_if_not_boolean(self):
result = bool_yesno("Test")
self.assertEqual(result, _("Test"))
54 changes: 53 additions & 1 deletion report/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,58 @@ def clean_end_date(self):
else:
return initial_date

def add_metrics_related_depending_on_values(self):
metrics_related = self.cleaned_data.get("metrics_related")
main_funding = Project.objects.get(text="Wikimedia Community Fund")
metrics_main_funding = Metric.objects.filter(project=main_funding)

int_fields_names = [
["wikipedia_created", "wikipedia_edited"],
["commons_created", "commons_edited"],
["wikidata_created", "wikidata_edited"],
["wikiversity_created", "wikiversity_edited"],
["wikibooks_created", "wikibooks_edited"],
["wikisource_created", "wikisource_edited"],
["wikinews_created", "wikinews_edited"],
["wikiquote_created", "wikiquote_edited"],
["wiktionary_created", "wiktionary_edited"],
["wikivoyage_created", "wikivoyage_edited"],
["wikispecies_created", "wikispecies_edited"],
["metawiki_created", "metawiki_edited"],
["mediawiki_created", "mediawiki_edited"],
["participants"],
["resources"],
["feedbacks"],
["number_of_people_reached_through_social_media"],
]

for field_set in int_fields_names:
if any(self.cleaned_data.get(field_name) > 0 for field_name in field_set):
query = Q()
for field_name in field_set:
if hasattr(Metric, f"{field_name}"):
query |= Q(**{f"{field_name}__gt": 0})
else:
query |= Q(**{f"number_of_{field_name}__gt": 0})
if len(query):
metrics_related = metrics_related.union(metrics_main_funding.filter(query))

obj_fields_names = {
"editors": ["number_of_editors", "number_of_editors_retained", "number_of_new_editors"],
"organizers": ["number_of_organizers", "number_of_organizers_retained"],
"partners_activated": ["number_of_partnerships"],
}

for field_set, field_names in obj_fields_names.items():
if self.cleaned_data.get(field_set):
query = Q()
for field_name in field_names:
query |= Q(**{f"{field_name}__gt": 0})
if len(query):
metrics_related = metrics_related.union(metrics_main_funding.filter(query))

return metrics_related

def save(self, commit=True, user=None, *args, **kwargs):
report = super(NewReportForm, self).save(commit=False)
if commit:
Expand All @@ -122,7 +174,7 @@ def save(self, commit=True, user=None, *args, **kwargs):
report.area_activated.set(self.cleaned_data['area_activated'])
report.directions_related.set(self.cleaned_data['directions_related'])
report.learning_questions_related.set(self.cleaned_data['learning_questions_related'])
report.metrics_related.set(self.cleaned_data['metrics_related'])
report.metrics_related.set(self.add_metrics_related_depending_on_values())
report.end_date = report.initial_date
return report

Expand Down
114 changes: 113 additions & 1 deletion report/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ def test_add_report_view_post(self):
metric = Metric.objects.create(text="Metric", activity=activity_associated, number_of_editors=10)

data = {
"wikipedia_created": 0, "wikipedia_edited": 0,
"commons_created": 0, "commons_edited": 0,
"wikidata_created": 0, "wikidata_edited": 0,
"wikiversity_created": 0, "wikiversity_edited": 0,
"wikibooks_created": 0, "wikibooks_edited": 0,
"wikisource_created": 0, "wikisource_edited": 0,
"wikinews_created": 0, "wikinews_edited": 0,
"wikiquote_created": 0, "wikiquote_edited": 0,
"wiktionary_created": 0, "wiktionary_edited": 0,
"wikivoyage_created": 0, "wikivoyage_edited": 0,
"wikispecies_created": 0, "wikispecies_edited": 0,
"metawiki_created": 0, "metawiki_edited": 0,
"mediawiki_created": 0, "mediawiki_edited": 0,
"participants": 0, "resources": 0, "feedbacks": 0,
"number_of_people_reached_through_social_media": 0,
"description": "Report",
"initial_date": datetime.now().date().strftime("%Y-%m-%d"),
"directions_related": [direction.id],
Expand All @@ -83,6 +98,58 @@ def test_add_report_view_post(self):
report = Report.objects.get(id=1)
self.assertEqual(report.description, "Report")

def test_add_report_view_post_associates_metrics_based_on_values(self):
self.client.login(username=self.username, password=self.password)
url = reverse("report:add_report")

project = Project.objects.create(text="Wikimedia Community Fund")
strategic_axis = StrategicAxis.objects.create(text="Strategic Axis")
direction = Direction.objects.create(text="Direction", strategic_axis=strategic_axis)
learning_area = LearningArea.objects.create(text="Learning area")
slq = StrategicLearningQuestion.objects.create(text="SLQ", learning_area=learning_area)
activity_associated = Activity.objects.create(text="Activity")
area_reponsible = TeamArea.objects.create(text="Area")
metric = Metric.objects.create(text="Metric", activity=activity_associated, number_of_editors=10)
metric_2 = Metric.objects.create(text="Metric 2", activity=activity_associated, wikipedia_edited=2)
metric_2.project.add(project)
metric_2.save()
metric_3 = Metric.objects.create(text="Metric 3", activity=activity_associated, number_of_editors=23)
metric_3.project.add(project)
metric_3.save()
Editor.objects.create(username="Editor")

data = {
"wikipedia_created": 0, "wikipedia_edited": 5,
"commons_created": 0, "commons_edited": 0,
"wikidata_created": 0, "wikidata_edited": 0,
"wikiversity_created": 0, "wikiversity_edited": 0,
"wikibooks_created": 0, "wikibooks_edited": 0,
"wikisource_created": 0, "wikisource_edited": 0,
"wikinews_created": 0, "wikinews_edited": 0,
"wikiquote_created": 0, "wikiquote_edited": 0,
"wiktionary_created": 0, "wiktionary_edited": 0,
"wikivoyage_created": 0, "wikivoyage_edited": 0,
"wikispecies_created": 0, "wikispecies_edited": 0,
"metawiki_created": 0, "metawiki_edited": 0,
"mediawiki_created": 0, "mediawiki_edited": 0,
"participants": 10, "resources": 0, "feedbacks": 0,
"number_of_people_reached_through_social_media": 0,
"description": "Report",
"initial_date": datetime.now().date().strftime("%Y-%m-%d"),
"directions_related": [direction.id],
"editors_string": "Editor",
"learning": "Learnings!"*51,
"learning_questions_related": [slq.id],
"activity_associated": activity_associated.id,
"area_responsible": area_reponsible.id,
"links": "Links",
"metrics_related": [metric.id]
}
self.client.post(url, data=data)
report = Report.objects.get(id=1)
self.assertIn(metric_2, report.metrics_related.all())
self.assertIn(metric_3, report.metrics_related.all())

def test_add_report_view_post_fails_with_invalid_parameters(self):
self.client.login(username=self.username, password=self.password)
url = reverse("report:add_report")
Expand Down Expand Up @@ -1407,6 +1474,17 @@ def test_clean_organizers_multiple_organizers(self):
self.assertEqual(len(cleaned_data[0].institution.all()), 0)
self.assertEqual(len(cleaned_data[1].institution.all()), 0)

def test_clean_organizers_with_already_created_organizer(self):
Organizer.objects.create(name="Organizer 1")
form_data = {"organizers_string": "Organizer 1;\r\nOrganizer 2;"}
form = NewReportForm(data=form_data, user=self.user)
cleaned_data = form.clean_organizers()
self.assertEqual(len(cleaned_data), 2)
self.assertEqual(cleaned_data[0].name, "Organizer 1")
self.assertEqual(cleaned_data[1].name, "Organizer 2")
self.assertEqual(len(cleaned_data[0].institution.all()), 0)
self.assertEqual(len(cleaned_data[1].institution.all()), 0)

def test_clean_organizers_single_institution(self):
form_data = {"organizers_string": "Organizer 1;Institution 1;"}
form = NewReportForm(data=form_data, user=self.user)
Expand Down Expand Up @@ -1501,4 +1579,38 @@ def test_learning_areas_as_choices(self):

expected_result = [["Learning area 1", [[1, "SLQ 1"], [2, "SLQ 2"]]],["Learning area 2", [[3, "SLQ 3"]]]]
result = learning_areas_as_choices()
self.assertEqual(expected_result, result)
self.assertEqual(expected_result, result)


class ParterFormTest(TestCase):
def test_valid_partner_form(self):
data = {
"name": "Partner",
"website": "https://example.com"
}

form = PartnerForm(data=data)
self.assertTrue(form.is_valid())

def test_name_is_required(self):
data = {"website": "https://example.com"}
form = PartnerForm(data=data)
self.assertFalse(form.is_valid())
self.assertIn("name", form.errors)

def test_name_is_required_and_not_empty(self):
data = {
"name": "",
"website": "https://example.com"
}
form = PartnerForm(data=data)
self.assertFalse(form.is_valid())
self.assertIn("name", form.errors)
def test_website_is_a_url(self):
data = {
"name": "Partner",
"website": "Example"
}
form = PartnerForm(data=data)
self.assertFalse(form.is_valid())
self.assertIn("website", form.errors)

0 comments on commit d15ccb3

Please sign in to comment.