add test for search tasks
@@ -8,7 +8,7 @@ from django.utils import timezone
from readthedocs.projects.models import Project
from readthedocs.builds.models import Version
-from readthedocs.search.models import SearchQuery
+from readthedocs.search.models import SearchQuery, PageView
from readthedocs.search import tasks
@@ -124,3 +124,74 @@ class TestSearchTasks:
assert SearchQuery.objects.all().count() == 1
tasks.delete_old_search_queries_from_db()
assert SearchQuery.objects.all().count() == 0
+
+ def test_increase_page_view_count(self, project):
+ today = timezone.now()
+ tomorrow = timezone.now() + timezone.timedelta(days=1)
+ yesterday = timezone.now() - timezone.timedelta(days=1)
+
+ assert (
+ PageView.objects.all().count() == 0
+ ), 'There\'s no PageView object created yet.'
+
+ # testing for yesterday
+ with mock.patch('readthedocs.search.tasks.timezone') as mocked_timezone:
+ mocked_timezone.return_value = yesterday
+
+ tasks.increase_page_view_count(
+ project_slug=project.slug,
+ version_slug=project.versions.all().first().slug,
+ path='index',
+ )
+
+ assert (
+ PageView.objects.all().count() == 1,
+ ), 'PageView object for path \'index\' is created'
+ assert (
+ PageView.objects.all().first().view_count == 1
+ ), '\'index\' has 1 view'
+
+ tasks.increase_page_view_count(
+ project_slug=project.slug,
+ version_slug=project.versions.all().first().slug,
+ path='index',
+ )
+
+ assert (
+ PageView.objects.all().count() == 1,
+ ), 'PageView object for path \'index\' is already created'
+ assert (
+ PageView.objects.all().first().view_count == 2
+ ), '\'index\' has 2 views now'
+
+ # testing for today
+ with mock.patch('readthedocs.search.tasks.timezone') as mocked_timezone:
+ mocked_timezone.return_value = today
+ tasks.increase_page_view_count(
+ project_slug=project.slug,
+ version_slug=project.versions.all().first().slug,
+ path='index',
+ )
+
+ assert (
+ PageView.objects.all().count() == 2,
+ ), 'PageView object for path \'index\' is created for two days (yesterday and today)'
+ assert (
+ PageView.objects.all().order_by('-created').first().view_count == 1
+ ), '\'index\' has 1 view today'
+
+ # testing for tomorrow
+ with mock.patch('readthedocs.search.tasks.timezone') as mocked_timezone:
+ mocked_timezone.return_value = tomorrow
+ tasks.increase_page_view_count(
+ project_slug=project.slug,
+ version_slug=project.versions.all().first().slug,
+ path='index',
+ )
+
+ assert (
+ PageView.objects.all().count() == 3,
+ ), 'PageView object for path \'index\' is created for three days (yesterday, today & tomorrow)'
+ assert (
+ PageView.objects.all().order_by('-created').first().view_count == 1
+ ), '\'index\' has 1 view tomorrow'
