Skip to content

Commit

Permalink
tests: Add tests for linking series feature
Browse files Browse the repository at this point in the history
Closes getpatchwork#506

Signed-off-by: andrepapoti <[email protected]>
  • Loading branch information
andrepapoti committed Mar 18, 2024
1 parent beaab2c commit c2baf31
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
21 changes: 21 additions & 0 deletions patchwork/tests/api/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,24 @@ def test_create_update_delete(self):

resp = self.client.delete(self.api_url(series.id))
self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)

def test_series_linking(self):
series_a = self._create_series()
series_b = create_series(
project=series_a.project, submitter=series_a.submitter
)

self.client.authenticate(user=series_a.submitter.user)
url = reverse(
'api-series-link',
kwargs={'pk': series_a.id, 'related_series_id': series_b.id},
)
resp = self.client.options(url)
resp = self.client.patch(url)

related_series = resp.data.get('related_series')
self.assertEqual(len(related_series), 1)
self.assertEqual(
related_series[0]['web_url'],
f'http://example.com/project/myproject/list/?series={series_b.id}',
)
48 changes: 48 additions & 0 deletions patchwork/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import unittest

from django.test import TestCase
from rest_framework.request import HttpRequest

from patchwork import models
from patchwork import parser
from patchwork.tests import utils
from patchwork.views.utils import patch_to_mbox
from patchwork.api.series import SeriesSerializer


TEST_SERIES_DIR = os.path.join(os.path.dirname(__file__), 'series')
Expand Down Expand Up @@ -804,3 +806,49 @@ def test_custom_name(self):
self.assertEqual(series.name, series_name)

mbox.close()


class SeriesSerializerTestCase(TestCase):
def test_related_series_validation_equal_project_id(self):
series_a = utils.create_series()
series_b = utils.create_series(project=series_a.project)

mock_request = HttpRequest()
mock_request.version = '1.4'
mock_request.META['SERVER_NAME'] = 'example.com'
mock_request.META['SERVER_PORT'] = '8000'

related_series_ids_a = list(
series_a.related_series.all().values_list('id', flat=True)
)
serializer = SeriesSerializer(
series_a,
context={'request': mock_request},
data={'related_series': related_series_ids_a + [series_b.pk]},
partial=True,
)
serializer.is_valid()
serializer.save()

related_series_urls = serializer.data['related_series']
self.assertEqual(len(related_series_urls), 1)
self.assertEqual(
f'series={series_b.id}' in related_series_urls[0]['web_url'], True
)

def test_related_series_validation_different_project_id(self):
series_a = utils.create_series()
series_b = utils.create_series()

mock_request = HttpRequest()
mock_request.version = '1.4'
mock_request.META['SERVER_NAME'] = 'example.com'
mock_request.META['SERVER_PORT'] = '8000'

serializer = SeriesSerializer(
series_a,
context={'request': mock_request},
data={'related_series': [series_b.pk]},
)
is_valid = serializer.is_valid()
self.assertFalse(is_valid)

0 comments on commit c2baf31

Please sign in to comment.