Skip to content

Commit

Permalink
Add unit tests #1524
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jan 21, 2025
1 parent b642451 commit 9b6867b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ v34.9.4 (unreleased)
multiple projects at once using labels and searching by project name.
https://github.com/aboutcode-org/scancode.io/issues/1524

- Add the ability to "select across" in Projects list when using the "select all"
checkbox on paginated list.
https://github.com/aboutcode-org/scancode.io/issues/1524

v34.9.3 (2024-12-31)
--------------------

Expand Down
65 changes: 64 additions & 1 deletion scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@

from django.apps import apps
from django.core.exceptions import SuspiciousFileOperation
from django.http.response import Http404
from django.test import TestCase
from django.test import override_settings
from django.urls import reverse
from django.urls.exceptions import NoReverseMatch

import requests

from scanpipe.forms import BaseProjectActionForm
from scanpipe.models import CodebaseRelation
from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredDependency
Expand All @@ -48,9 +50,11 @@
from scanpipe.tests import dependency_data2
from scanpipe.tests import make_dependency
from scanpipe.tests import make_package
from scanpipe.tests import make_project
from scanpipe.tests import make_resource_file
from scanpipe.tests import package_data1
from scanpipe.tests import package_data2
from scanpipe.views import ProjectActionView
from scanpipe.views import ProjectCodebaseView
from scanpipe.views import ProjectDetailView

Expand Down Expand Up @@ -147,7 +151,7 @@ def test_scanpipe_views_project_list_state_of_filters_in_search_form(self):
def test_scanpipe_views_project_list_filters_exclude_page(self, mock_paginate_by):
url = reverse("project_list")
# Create another project to enable pagination
Project.objects.create(name="project2")
make_project()
mock_paginate_by.return_value = 1

data = {"page": "2"}
Expand All @@ -162,6 +166,34 @@ def test_scanpipe_views_project_list_filters_exclude_page(self, mock_paginate_by
expected = '<a href="?sort=" class="dropdown-item is-active">Newest</a>'
self.assertContains(response, expected)

def test_scanpipe_views_project_list_modal_forms_include_url_query(self):
url = reverse("project_list")
response = self.client.get(url)

expected = '<input type="hidden" name="url_query" value="">'
self.assertContains(response, expected, html=True)

url_query = "name=search_value"
response = self.client.get(url + "?" + url_query)
expected = f'<input type="hidden" name="url_query" value="{url_query}">'
self.assertContains(response, expected, html=True)

@mock.patch("scanpipe.views.ProjectListView.get_paginate_by")
def test_scanpipe_views_project_list_modal_forms_include_show_on_all_checked(
self, mock_paginate_by
):
url = reverse("project_list")
# Create another project to enable pagination
make_project()
mock_paginate_by.return_value = 1
response = self.client.get(url)
expected = '<div class="show-on-all-checked">'
self.assertContains(response, expected)

mock_paginate_by.return_value = 2
response = self.client.get(url)
self.assertNotContains(response, expected)

def test_scanpipe_views_project_actions_view(self):
url = reverse("project_action")
response = self.client.get(url)
Expand Down Expand Up @@ -198,6 +230,37 @@ def test_scanpipe_views_project_action_report_view(self):
response = self.client.post(url, data=data, follow=True)
self.assertEqual("report.xlsx", response.filename)

def test_scanpipe_views_project_action_view_get_project_queryset(self):
queryset = ProjectActionView.get_project_queryset(
selected_project_ids=[self.project1.uuid],
action_form=None,
)
self.assertQuerySetEqual(queryset, [self.project1])

# No project selection, no select_across
form_data = {"select_across": 0}
action_form = BaseProjectActionForm(data=form_data)
action_form.full_clean()
with self.assertRaises(Http404):
ProjectActionView.get_project_queryset(
selected_project_ids=None,
action_form=action_form,
)

# select_across, no active filters
form_data = {"select_across": 1}
action_form = BaseProjectActionForm(data=form_data)
action_form.full_clean()
self.assertQuerySetEqual(queryset, [self.project1])

# select_across, active filters
make_project()
self.assertEqual(2, Project.objects.count())
form_data = {"select_across": 1, "url_query": f"name={self.project1.name}"}
action_form = BaseProjectActionForm(data=form_data)
action_form.full_clean()
self.assertQuerySetEqual(queryset, [self.project1])

def test_scanpipe_views_project_details_is_archived(self):
url = self.project1.get_absolute_url()
expected1 = "WARNING: This project is archived and read-only."
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ def get_action_form(self, action):

return action_form

def perform_action(self, action, project, action_kwargs=None):
def perform_action(self, project, action, action_kwargs=None):
if not action_kwargs:
action_kwargs = {}

Expand Down

0 comments on commit 9b6867b

Please sign in to comment.