-
Notifications
You must be signed in to change notification settings - Fork 17
API v1 initial tests. #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Tests for the commitfest application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| from django.test import Client, TestCase, override_settings | ||
|
|
||
| import json | ||
| from datetime import date, timedelta | ||
|
|
||
| from pgcommitfest.commitfest.models import CommitFest | ||
|
|
||
|
|
||
| @override_settings(AUTO_CREATE_COMMITFESTS=False) | ||
| class NeedsCIEndpointTestCase(TestCase): | ||
| """Test the /api/v1/commitfests/needs_ci endpoint.""" | ||
|
|
||
| @classmethod | ||
| def setUpTestData(cls): | ||
| today = date.today() | ||
|
|
||
| # Create test commitfests with various statuses | ||
| cls.open_cf = CommitFest.objects.create( | ||
| name="2025-01", | ||
| status=CommitFest.STATUS_OPEN, | ||
| startdate=today - timedelta(days=30), | ||
| enddate=today + timedelta(days=30), | ||
| draft=False, | ||
| ) | ||
|
|
||
| cls.in_progress_cf = CommitFest.objects.create( | ||
| name="2024-11", | ||
| status=CommitFest.STATUS_INPROGRESS, | ||
| startdate=today - timedelta(days=60), | ||
| enddate=today + timedelta(days=0), | ||
| draft=False, | ||
| ) | ||
|
|
||
| # Previous CF that ended 3 days ago (should be included - within 7 day window) | ||
| cls.recent_previous_cf = CommitFest.objects.create( | ||
| name="2024-09", | ||
| status=CommitFest.STATUS_CLOSED, | ||
| startdate=today - timedelta(days=90), | ||
| enddate=today - timedelta(days=3), | ||
| draft=False, | ||
| ) | ||
|
|
||
| # Old previous CF that ended 10 days ago (should be excluded - outside 7 day window) | ||
| cls.old_previous_cf = CommitFest.objects.create( | ||
| name="2024-07", | ||
| status=CommitFest.STATUS_CLOSED, | ||
| startdate=today - timedelta(days=120), | ||
| enddate=today - timedelta(days=10), | ||
| draft=False, | ||
| ) | ||
|
|
||
| # Draft commitfest | ||
| cls.draft_cf = CommitFest.objects.create( | ||
| name="2025-03-draft", | ||
| status=CommitFest.STATUS_OPEN, | ||
| startdate=today + timedelta(days=60), | ||
| enddate=today + timedelta(days=120), | ||
| draft=True, | ||
| ) | ||
|
|
||
| def setUp(self): | ||
| self.client = Client() | ||
|
|
||
| def test_endpoint_returns_200(self): | ||
| """Test that the endpoint returns HTTP 200 OK.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| self.assertEqual(response.status_code, 200) | ||
|
|
||
| def test_response_is_valid_json(self): | ||
| """Test that the response is valid JSON.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| try: | ||
| data = json.loads(response.content) | ||
| except json.JSONDecodeError: | ||
| self.fail("Response is not valid JSON") | ||
|
|
||
| self.assertIn("commitfests", data) | ||
| self.assertIsInstance(data["commitfests"], dict) | ||
|
|
||
| def test_response_content_type(self): | ||
| """Test that the response has correct Content-Type header.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| self.assertEqual(response["Content-Type"], "application/json") | ||
|
|
||
| def test_cors_header_present(self): | ||
| """Test that CORS header is present for API access.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| self.assertEqual(response["Access-Control-Allow-Origin"], "*") | ||
|
|
||
| def test_includes_open_commitfest(self): | ||
| """Test that open commitfests are included in response.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
| commitfests = data["commitfests"] | ||
|
|
||
| # Should include the open commitfest | ||
| self.assertIn("open", commitfests) | ||
| self.assertEqual(commitfests["open"]["name"], self.open_cf.name) | ||
|
|
||
| def test_includes_in_progress_commitfest(self): | ||
| """Test that in-progress commitfests are included in response.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
| commitfests = data["commitfests"] | ||
|
|
||
| # Should include the in-progress commitfest | ||
| self.assertEqual(commitfests["in_progress"]["name"], self.in_progress_cf.name) | ||
|
|
||
| def test_includes_recent_previous_commitfest(self): | ||
| """Test that recently ended commitfests are included (within 7 days).""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
| commitfests = data["commitfests"] | ||
|
|
||
| # Should include recent previous commitfest (ended 3 days ago) | ||
| self.assertIsNotNone(commitfests["previous"]) | ||
|
|
||
| def test_excludes_old_previous_commitfest(self): | ||
| """Test that old commitfests are excluded (older than 7 days).""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
| commitfests = data["commitfests"] | ||
|
|
||
| # Should not include old previous commitfest (ended 10 days ago) | ||
| self.assertNotEqual( | ||
| commitfests["previous"]["name"], | ||
| self.old_previous_cf.name, | ||
| "Old previous commitfest should be excluded", | ||
| ) | ||
|
|
||
| def test_excludes_next_open_and_final(self): | ||
| """Test that next_open and final are excluded from response.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
| commitfests = data["commitfests"] | ||
|
|
||
| # These keys should not be present in the response | ||
| self.assertNotIn("next_open", commitfests) | ||
| self.assertNotIn("final", commitfests) | ||
|
|
||
| def test_response_structure(self): | ||
| """Test that response has expected structure.""" | ||
| response = self.client.get("/api/v1/commitfests/needs_ci") | ||
| data = json.loads(response.content) | ||
|
|
||
| # Top-level structure | ||
| self.assertIn("commitfests", data) | ||
| self.assertIsInstance(data["commitfests"], dict) | ||
|
|
||
| # Check that commitfest objects have expected fields | ||
| commitfests = data["commitfests"] | ||
| for key, cf_data in commitfests.items(): | ||
| self.assertIsInstance(cf_data, dict) | ||
| # Basic fields that should be present | ||
| self.assertIn("id", cf_data) | ||
| self.assertIn("name", cf_data) | ||
| self.assertIn("status", cf_data) | ||
| self.assertIn("startdate", cf_data) | ||
| self.assertIn("enddate", cf_data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.