Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ederporto committed Feb 2, 2024
1 parent 9505cef commit bf1b617
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 229 deletions.
42 changes: 1 addition & 41 deletions metrics/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from report.models import Report, Editor
from users.models import User, UserProfile, TeamArea
from strategy.models import StrategicAxis
from .views import get_metrics_and_aggregate_per_project, get_aggregated_metrics_data, get_aggregated_metrics_data_done
from .views import get_metrics_and_aggregate_per_project
from django.urls import reverse
from django.contrib.auth.models import Permission
from datetime import datetime, timedelta
Expand Down Expand Up @@ -304,46 +304,6 @@ def setUp(self):
self.editor_2 = Editor.objects.create(username="Editor 2")
self.editor_3 = Editor.objects.create(username="Editor 3")

def test_get_aggregated_metrics_data_with_data(self):
self.metric_1.wikipedia_created = 1
self.metric_2.number_of_editors = 4
self.metric_3.number_of_editors = 2
self.metric_1.save()
self.metric_2.save()
self.metric_3.save()

aggregated_metrics = get_aggregated_metrics_data()
self.assertEqual(aggregated_metrics["editors"], self.metric_2.number_of_editors + self.metric_3.number_of_editors)
self.assertEqual(aggregated_metrics["wikipedia_created"], self.metric_1.wikipedia_created)
self.assertEqual(aggregated_metrics["wikipedia_edited"], 0)

def test_get_aggregated_metrics_data_without_data(self):
aggregated_metrics = get_aggregated_metrics_data()
self.assertEqual(aggregated_metrics["editors"], 0)
self.assertEqual(aggregated_metrics["wikipedia_created"], 0)
self.assertEqual(aggregated_metrics["wikipedia_edited"], 0)

def test_get_aggregated_metrics_data_done_with_data(self):
self.report_1.wikipedia_created = 1
self.report_1.editors.add(self.editor_1)
self.report_1.editors.add(self.editor_2)
self.report_2.editors.add(self.editor_3)
self.report_3.editors.add(self.editor_3)
self.report_1.save()
self.report_2.save()
self.report_3.save()

aggregated_metrics_done = get_aggregated_metrics_data_done()
self.assertEqual(aggregated_metrics_done["editors"], self.report_1.editors.count() + self.report_2.editors.count() + self.report_3.editors.count())
self.assertEqual(aggregated_metrics_done["wikipedia_created"], self.report_1.wikipedia_created)
self.assertEqual(aggregated_metrics_done["wikipedia_edited"], 0)

def test_get_aggregated_metrics_data_done_without_data(self):
aggregated_metrics_done = get_aggregated_metrics_data_done()
self.assertEqual(aggregated_metrics_done["editors"], 0)
self.assertEqual(aggregated_metrics_done["wikipedia_created"], 0)
self.assertEqual(aggregated_metrics_done["wikipedia_edited"], 0)

def test_get_metrics_and_aggregate_per_project_with_data_and_metric_unclear_when_id_ge_1(self):
project = Project.objects.create(text="Project")
self.metric_3.project.add(project)
Expand Down
170 changes: 0 additions & 170 deletions metrics/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,176 +146,6 @@ def get_done_for_report(reports):
}


def get_aggregated_metrics_data(project=None):
total_sum = {}

q_filter = Q(project__isnull=True)
if project:
q_filter = Q(project=project)

wikipedia_created = Metric.objects.filter(q_filter).aggregate(Sum('wikipedia_created'))['wikipedia_created__sum'] or 0
commons_created = Metric.objects.filter(q_filter).aggregate(Sum('commons_created'))['commons_created__sum'] or 0
wikidata_created = Metric.objects.filter(q_filter).aggregate(Sum('wikidata_created'))['wikidata_created__sum'] or 0
wikiversity_created = Metric.objects.filter(q_filter).aggregate(Sum('wikiversity_created'))['wikiversity_created__sum'] or 0
wikibooks_created = Metric.objects.filter(q_filter).aggregate(Sum('wikibooks_created'))['wikibooks_created__sum'] or 0
wikisource_created = Metric.objects.filter(q_filter).aggregate(Sum('wikisource_created'))['wikisource_created__sum'] or 0
wikinews_created = Metric.objects.filter(q_filter).aggregate(Sum('wikinews_created'))['wikinews_created__sum'] or 0
wikiquote_created = Metric.objects.filter(q_filter).aggregate(Sum('wikiquote_created'))['wikiquote_created__sum'] or 0
wiktionary_created = Metric.objects.filter(q_filter).aggregate(Sum('wiktionary_created'))['wiktionary_created__sum'] or 0
wikivoyage_created = Metric.objects.filter(q_filter).aggregate(Sum('wikivoyage_created'))['wikivoyage_created__sum'] or 0
wikispecies_created = Metric.objects.filter(q_filter).aggregate(Sum('wikispecies_created'))['wikispecies_created__sum'] or 0
metawiki_created = Metric.objects.filter(q_filter).aggregate(Sum('metawiki_created'))['metawiki_created__sum'] or 0
mediawiki_created = Metric.objects.filter(q_filter).aggregate(Sum('mediawiki_created'))['mediawiki_created__sum'] or 0

wikipedia_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikipedia_edited'))['wikipedia_edited__sum'] or 0
commons_edited = Metric.objects.filter(q_filter).aggregate(Sum('commons_edited'))['commons_edited__sum'] or 0
wikidata_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikidata_edited'))['wikidata_edited__sum'] or 0
wikiversity_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikiversity_edited'))['wikiversity_edited__sum'] or 0
wikibooks_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikibooks_edited'))['wikibooks_edited__sum'] or 0
wikisource_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikisource_edited'))['wikisource_edited__sum'] or 0
wikinews_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikinews_edited'))['wikinews_edited__sum'] or 0
wikiquote_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikiquote_edited'))['wikiquote_edited__sum'] or 0
wiktionary_edited = Metric.objects.filter(q_filter).aggregate(Sum('wiktionary_edited'))['wiktionary_edited__sum'] or 0
wikivoyage_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikivoyage_edited'))['wikivoyage_edited__sum'] or 0
wikispecies_edited = Metric.objects.filter(q_filter).aggregate(Sum('wikispecies_edited'))['wikispecies_edited__sum'] or 0
metawiki_edited = Metric.objects.filter(q_filter).aggregate(Sum('metawiki_edited'))['metawiki_edited__sum'] or 0
mediawiki_edited = Metric.objects.filter(q_filter).aggregate(Sum('mediawiki_edited'))['mediawiki_edited__sum'] or 0

number_of_participants = Metric.objects.filter(q_filter).aggregate(Sum('number_of_participants'))['number_of_participants__sum'] or 0
number_of_resources = Metric.objects.filter(q_filter).aggregate(Sum('number_of_resources'))['number_of_resources__sum'] or 0
number_of_feedbacks = Metric.objects.filter(q_filter).aggregate(Sum('number_of_feedbacks'))['number_of_feedbacks__sum'] or 0
number_of_editors = Metric.objects.filter(q_filter).aggregate(Sum('number_of_editors'))['number_of_editors__sum'] or 0
number_of_organizers = Metric.objects.filter(q_filter).aggregate(Sum('number_of_organizers'))['number_of_organizers__sum'] or 0
number_of_partnerships_activated = Metric.objects.filter(q_filter).aggregate(Sum('number_of_partnerships_activated'))['number_of_partnerships_activated__sum'] or 0

number_of_retained_editors = Metric.objects.filter(q_filter).filter(other_type="retained").aggregate(Sum('number_of_editors'))['number_of_editors__sum'] or 0
number_of_retained_organizers = Metric.objects.filter(q_filter).filter(other_type="retained").aggregate(Sum('number_of_organizers'))['number_of_organizers__sum'] or 0
number_of_retained_partnerships = Metric.objects.filter(q_filter).filter(other_type="retained").aggregate(Sum('number_of_partnerships_activated'))['number_of_partnerships_activated__sum'] or 0

total_sum["wikipedia_created"] = wikipedia_created
total_sum["commons_created"] = commons_created
total_sum["wikidata_created"] = wikidata_created
total_sum["wikiversity_created"] = wikiversity_created
total_sum["wikibooks_created"] = wikibooks_created
total_sum["wikisource_created"] = wikisource_created
total_sum["wikinews_created"] = wikinews_created
total_sum["wikiquote_created"] = wikiquote_created
total_sum["wiktionary_created"] = wiktionary_created
total_sum["wikivoyage_created"] = wikivoyage_created
total_sum["wikispecies_created"] = wikispecies_created
total_sum["metawiki_created"] = metawiki_created
total_sum["mediawiki_created"] = mediawiki_created

total_sum["wikipedia_edited"] = wikipedia_edited
total_sum["commons_edited"] = commons_edited
total_sum["wikidata_edited"] = wikidata_edited
total_sum["wikiversity_edited"] = wikiversity_edited
total_sum["wikibooks_edited"] = wikibooks_edited
total_sum["wikisource_edited"] = wikisource_edited
total_sum["wikinews_edited"] = wikinews_edited
total_sum["wikiquote_edited"] = wikiquote_edited
total_sum["wiktionary_edited"] = wiktionary_edited
total_sum["wikivoyage_edited"] = wikivoyage_edited
total_sum["wikispecies_edited"] = wikispecies_edited
total_sum["metawiki_edited"] = metawiki_edited
total_sum["mediawiki_edited"] = mediawiki_edited

total_sum["participants"] = number_of_participants
total_sum["resources"] = number_of_resources
total_sum["feedbacks"] = number_of_feedbacks
total_sum["editors"] = number_of_editors
total_sum["organizers"] = number_of_organizers
total_sum["partnerships"] = number_of_partnerships_activated
total_sum["retained_editors"] = number_of_retained_editors
total_sum["retained_organizers"] = number_of_retained_organizers
total_sum["retained_partnerships"] = number_of_retained_partnerships

return total_sum


def get_aggregated_metrics_data_done():
total_sum = {}
wikipedia_created = Report.objects.aggregate(Sum('wikipedia_created'))['wikipedia_created__sum']
commons_created = Report.objects.aggregate(Sum('commons_created'))['commons_created__sum']
wikidata_created = Report.objects.aggregate(Sum('wikidata_created'))['wikidata_created__sum']
wikiversity_created = Report.objects.aggregate(Sum('wikiversity_created'))['wikiversity_created__sum']
wikibooks_created = Report.objects.aggregate(Sum('wikibooks_created'))['wikibooks_created__sum']
wikisource_created = Report.objects.aggregate(Sum('wikisource_created'))['wikisource_created__sum']
wikinews_created = Report.objects.aggregate(Sum('wikinews_created'))['wikinews_created__sum']
wikiquote_created = Report.objects.aggregate(Sum('wikiquote_created'))['wikiquote_created__sum']
wiktionary_created = Report.objects.aggregate(Sum('wiktionary_created'))['wiktionary_created__sum']
wikivoyage_created = Report.objects.aggregate(Sum('wikivoyage_created'))['wikivoyage_created__sum']
wikispecies_created = Report.objects.aggregate(Sum('wikispecies_created'))['wikispecies_created__sum']
metawiki_created = Report.objects.aggregate(Sum('metawiki_created'))['metawiki_created__sum']
mediawiki_created = Report.objects.aggregate(Sum('mediawiki_created'))['mediawiki_created__sum']

wikipedia_edited = Report.objects.aggregate(Sum('wikipedia_edited'))['wikipedia_edited__sum']
commons_edited = Report.objects.aggregate(Sum('commons_edited'))['commons_edited__sum']
wikidata_edited = Report.objects.aggregate(Sum('wikidata_edited'))['wikidata_edited__sum']
wikiversity_edited = Report.objects.aggregate(Sum('wikiversity_edited'))['wikiversity_edited__sum']
wikibooks_edited = Report.objects.aggregate(Sum('wikibooks_edited'))['wikibooks_edited__sum']
wikisource_edited = Report.objects.aggregate(Sum('wikisource_edited'))['wikisource_edited__sum']
wikinews_edited = Report.objects.aggregate(Sum('wikinews_edited'))['wikinews_edited__sum']
wikiquote_edited = Report.objects.aggregate(Sum('wikiquote_edited'))['wikiquote_edited__sum']
wiktionary_edited = Report.objects.aggregate(Sum('wiktionary_edited'))['wiktionary_edited__sum']
wikivoyage_edited = Report.objects.aggregate(Sum('wikivoyage_edited'))['wikivoyage_edited__sum']
wikispecies_edited = Report.objects.aggregate(Sum('wikispecies_edited'))['wikispecies_edited__sum']
metawiki_edited = Report.objects.aggregate(Sum('metawiki_edited'))['metawiki_edited__sum']
mediawiki_edited = Report.objects.aggregate(Sum('mediawiki_edited'))['mediawiki_edited__sum']

number_of_participants = Report.objects.aggregate(Sum('participants'))['participants__sum']
# number_of_resources = Report.objects.aggregate(Sum('resources'))['resources__sum']
number_of_feedbacks = Report.objects.aggregate(Sum('feedbacks'))['feedbacks__sum']
number_of_editors = Report.objects.annotate(num_editors=Count('editors')).aggregate(Sum('num_editors'))['num_editors__sum']
number_of_organizers = Report.objects.annotate(num_organizers=Count('organizers')).aggregate(Sum('num_organizers'))['num_organizers__sum']
number_of_partnerships_activated = Report.objects.annotate(num_partnerships_activated=Count('partners_activated')).aggregate(Sum('num_partnerships_activated'))['num_partnerships_activated__sum']

editors_in_more_than_one_activity = Editor.objects.annotate(report_count=Count('editors')).filter(report_count__gt=1)
organizers_in_more_than_one_activity = Organizer.objects.annotate(report_count=Count('organizers')).filter(report_count__gt=1)
partners_in_more_than_one_activity = Partner.objects.annotate(report_count=Count('partners')).filter(report_count__gt=1)

total_sum["wikipedia_created"] = wikipedia_created
total_sum["commons_created"] = commons_created
total_sum["wikidata_created"] = wikidata_created
total_sum["wikiversity_created"] = wikiversity_created
total_sum["wikibooks_created"] = wikibooks_created
total_sum["wikisource_created"] = wikisource_created
total_sum["wikinews_created"] = wikinews_created
total_sum["wikiquote_created"] = wikiquote_created
total_sum["wiktionary_created"] = wiktionary_created
total_sum["wikivoyage_created"] = wikivoyage_created
total_sum["wikispecies_created"] = wikispecies_created
total_sum["metawiki_created"] = metawiki_created
total_sum["mediawiki_created"] = mediawiki_created

total_sum["wikipedia_edited"] = wikipedia_edited
total_sum["commons_edited"] = commons_edited
total_sum["wikidata_edited"] = wikidata_edited
total_sum["wikiversity_edited"] = wikiversity_edited
total_sum["wikibooks_edited"] = wikibooks_edited
total_sum["wikisource_edited"] = wikisource_edited
total_sum["wikinews_edited"] = wikinews_edited
total_sum["wikiquote_edited"] = wikiquote_edited
total_sum["wiktionary_edited"] = wiktionary_edited
total_sum["wikivoyage_edited"] = wikivoyage_edited
total_sum["wikispecies_edited"] = wikispecies_edited
total_sum["metawiki_edited"] = metawiki_edited
total_sum["mediawiki_edited"] = mediawiki_edited

total_sum["participants"] = number_of_participants
# total_sum["resources"] = number_of_resources
total_sum["feedbacks"] = number_of_feedbacks
total_sum["editors"] = number_of_editors
total_sum["organizers"] = number_of_organizers
total_sum["partnerships"] = number_of_partnerships_activated

total_sum["retained_editors"] = editors_in_more_than_one_activity.count()
total_sum["retained_organizers"] = organizers_in_more_than_one_activity.count()
total_sum["retained_partnerships"] = partners_in_more_than_one_activity.count()

return total_sum


def update_metrics_relations(request):
main_funding = Project.objects.get(text="Wikimedia Community Fund")
editors_metrics = Metric.objects.filter(project=main_funding).filter(Q(number_of_editors__gt=0) | Q(number_of_editors_retained__gt=0) | Q(number_of_new_editors__gt=0))
Expand Down
1 change: 0 additions & 1 deletion report/templates/report/operations.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% load i18n %}
{% load reporttags %}
<div class="w3-container">
{{ operation_metrics.management_form }}
{% for form in operation_metrics.forms %}
Expand Down
16 changes: 0 additions & 16 deletions report/templatetags/reporttags.py

This file was deleted.

11 changes: 11 additions & 0 deletions report/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,17 @@ def test_export_technologies_used_without_report_id_returns_technologies_from_al
self.assertTrue(result[result.isin(expected_df)].equals(expected_df))


def test_export_with_no_reports_redirects_to_list_of_reports(self):
self.client.login(username=self.username, password=self.password)
self.report_1.delete()
self.report_2.delete()

url = reverse("report:export_report", kwargs={"report_id": 1})
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response,f"{reverse('report:list_reports')}")


class OtherViewsTest(TestCase):
def setUp(self):
self.username = "testuser"
Expand Down
2 changes: 1 addition & 1 deletion report/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def add_excel_file(report_id=None):
@login_required
@permission_required("report.view_report")
def export_report(request, report_id=None):
if report_id or Report.objects.count():
if Report.objects.count():
buffer = BytesIO()
zip_file = zipfile.ZipFile(buffer, mode="w")
sub_directory = "csv/"
Expand Down

0 comments on commit bf1b617

Please sign in to comment.