From 140154b43324d6b60720ceddd3b64d08b44a1877 Mon Sep 17 00:00:00 2001 From: Matt Wang Date: Thu, 16 Mar 2023 14:26:12 +0800 Subject: [PATCH] test(sponsor): add test cases for sponsor apis --- src/sponsors/tests.py | 0 .../{api/serializers.py => tests/__init__.py} | 0 src/sponsors/tests/test_api.py | 72 +++++++++++++++++++ 3 files changed, 72 insertions(+) delete mode 100644 src/sponsors/tests.py rename src/sponsors/{api/serializers.py => tests/__init__.py} (100%) create mode 100644 src/sponsors/tests/test_api.py diff --git a/src/sponsors/tests.py b/src/sponsors/tests.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/sponsors/api/serializers.py b/src/sponsors/tests/__init__.py similarity index 100% rename from src/sponsors/api/serializers.py rename to src/sponsors/tests/__init__.py diff --git a/src/sponsors/tests/test_api.py b/src/sponsors/tests/test_api.py new file mode 100644 index 000000000..169188988 --- /dev/null +++ b/src/sponsors/tests/test_api.py @@ -0,0 +1,72 @@ +import pytest + +from sponsors.models import OpenRole, Sponsor + + +@pytest.fixture(autouse=True, scope='function') +def test_data(): + # sponsors + # platinum + sponsor_1 = Sponsor.objects.create(name='1', level=1) + # gold + sponsor_2 = Sponsor.objects.create(name='2', level=2, order=2) + sponsor_3 = Sponsor.objects.create(name='3', level=2, order=None) # noqa + sponsor_4 = Sponsor.objects.create(name='4', level=2, order=1) # noqa + + # roles + OpenRole.objects.create(sponsor=sponsor_1, name='11', description='...') + OpenRole.objects.create(sponsor=sponsor_2, name='21', description='...') + OpenRole.objects.create(sponsor=sponsor_2, name='22', description='...') + + +@pytest.mark.django_db +class TestSponsorAPIView: + def test_should_get_sponsor_data(self, api_client): + # arrange: test_data fixture + # action + resp = api_client.get('/api/sponsors/') + + # assert + assert resp.status_code == 200 + + data = {level_data['level_name']: level_data['sponsors'] for level_data in resp.data['data']} + assert list(data.keys()) == ['platinum', 'gold'] + + # assert: should have correct sponsor count + platinum_sponsors = data['platinum'] + assert len(platinum_sponsors) == 1 + + gold_sponsors = data['gold'] + assert len(gold_sponsors) == 3 + + # assert: gold sponsors should be in correct sequence + # small-order > large-order > null-order + gold_sponsor_names = [s['name_en_us'] for s in gold_sponsors] + assert gold_sponsor_names == ['4', '2', '3'] + + def test_should_get_role_data(self, api_client): + # arrange: test_data fixture + # action + resp = api_client.get('/api/sponsors/jobs/') + + # assert + assert resp.status_code == 200 + + data = resp.json()['data'] + assert len(data) == 2 + + # assert: sponsor 1 + assert data[0]['sponsor_name'] == '1' + + jobs_1 = data[0]['jobs'] + assert len(jobs_1) == 1 + assert jobs_1[0]['job_name_en_us'] == '11' + + # assert: sponsor 2 + assert data[1]['sponsor_name'] == '2' + + jobs_2 = data[1]['jobs'] + assert len(jobs_2) == 2 + + job_names = [j['job_name_en_us'] for j in jobs_2] + assert set(job_names) == {'21', '22'}