← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~ack/maas-site-manager:tests-site-details-fixture into maas-site-manager:main

 

Alberto Donato has proposed merging ~ack/maas-site-manager:tests-site-details-fixture into maas-site-manager:main.

Commit message:
add test fixtures helpers to reduce duplication



Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~ack/maas-site-manager/+git/site-manager/+merge/442524
-- 
Your team MAAS Committers is requested to review the proposed merge of ~ack/maas-site-manager:tests-site-details-fixture into maas-site-manager:main.
diff --git a/backend/tests/user_api/test_handlers.py b/backend/tests/user_api/test_handlers.py
index 9da88b4..fd43e3b 100644
--- a/backend/tests/user_api/test_handlers.py
+++ b/backend/tests/user_api/test_handlers.py
@@ -2,6 +2,7 @@ from datetime import (
     datetime,
     timedelta,
 )
+from typing import Any
 
 from httpx import AsyncClient
 import pytest
@@ -23,6 +24,17 @@ def duration_format(delta: timedelta, time_format: str) -> str:
             raise ValueError(f"Invalid time format {time_format}")
 
 
+def site_details(**extra_details: Any) -> dict[str, Any]:
+    """Return sample details for creating a site."""
+    details = {
+        "name": "LondonHQ",
+        "url": "https://londoncalling.example.com";,
+        "accepted": True,
+    }
+    details.update(extra_details)
+    return details
+
+
 @pytest.mark.asyncio
 class TestRootHandler:
     async def test_get(self, user_app_client: AsyncClient) -> None:
@@ -44,29 +56,13 @@ class TestSitesHandler:
     async def test_get(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site1 = {
-            "name": "LondonHQ",
-            "city": "London",
-            "country": "gb",
-            "latitude": "51.509865",
-            "longitude": "-0.118092",
-            "note": "the first site",
-            "region": "Blue Fin Bldg",
-            "street": "110 Southwark St",
-            "timezone": "Europe/London",
-            "url": "https://londoncalling.example.com";,
-            "accepted": True,
-        }
-        site2 = site1.copy()
-        site2.update(
-            {
-                "name": "BerlinHQ",
-                "timezone": "Europe/Berlin",
-                "city": "Berlin",
-                "country": "de",
-            }
+        sites = await fixture.create(
+            "site",
+            [
+                site_details(city="London"),
+                site_details(name="BerlinHQ", city="Berlin"),
+            ],
         )
-        sites = await fixture.create("site", [site1, site2])
         for site in sites:
             site["stats"] = None
             del site["created"]
@@ -104,30 +100,12 @@ class TestSitesHandler:
     async def test_get_only_accepted(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site1 = {
-            "name": "LondonHQ",
-            "city": "London",
-            "country": "gb",
-            "latitude": "51.509865",
-            "longitude": "-0.118092",
-            "note": "the first site",
-            "region": "Blue Fin Bldg",
-            "street": "110 Southwark St",
-            "timezone": "Europe/London",
-            "url": "https://londoncalling.example.com";,
-            "accepted": True,
-        }
-        site2 = site1.copy()
-        site2.update(
-            {
-                "name": "BerlinHQ",
-                "timezone": "Europe/Berlin",
-                "city": "Berlin",
-                "country": "de",
-                "accepted": False,
-            }
+        created_site, _ = await fixture.create(
+            "site", [
+                site_details(),
+                site_details(name="BerlinHQ", accepted=False),
+            ],
         )
-        created_site, _ = await fixture.create("site", [site1, site2])
         created_site["stats"] = None
         del created_site["created"]
         del created_site["accepted"]
@@ -144,29 +122,13 @@ class TestSitesHandler:
     async def test_get_filter_timezone(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site1 = {
-            "name": "LondonHQ",
-            "city": "London",
-            "country": "gb",
-            "latitude": "51.509865",
-            "longitude": "-0.118092",
-            "note": "the first site",
-            "region": "Blue Fin Bldg",
-            "street": "110 Southwark St",
-            "timezone": "Europe/London",
-            "url": "https://londoncalling.example.com";,
-            "accepted": True,
-        }
-        site2 = site1.copy()
-        site2.update(
-            {
-                "name": "BerlinHQ",
-                "timezone": "Europe/Berlin",
-                "city": "Berlin",
-                "country": "de",
-            }
+        [created_site, _] = await fixture.create(
+            "site",
+            [
+                site_details(timezone="Europe/London"),
+                site_details(name="BerlinHQ", timezone="Europe/Berlin"),
+            ],
         )
-        [created_site, _] = await fixture.create("site", [site1, site2])
         created_site["stats"] = None
         del created_site["created"]
         del created_site["accepted"]
@@ -184,24 +146,7 @@ class TestSitesHandler:
     async def test_get_with_stats(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        [site] = await fixture.create(
-            "site",
-            [
-                {
-                    "name": "LondonHQ",
-                    "city": "London",
-                    "country": "gb",
-                    "latitude": "51.509865",
-                    "longitude": "-0.118092",
-                    "note": "the first site",
-                    "region": "Blue Fin Bldg",
-                    "street": "110 Southwark St",
-                    "timezone": "Europe/London",
-                    "url": "https://londoncalling.example.com";,
-                    "accepted": True,
-                }
-            ],
-        )
+        [site] = await fixture.create("site", [site_details()])
         [site_data] = await fixture.create(
             "site_data",
             [
@@ -237,30 +182,12 @@ class TestPendingSitesHandler:
     async def test_get(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site1 = {
-            "name": "LondonHQ",
-            "city": "London",
-            "country": "gb",
-            "latitude": "51.509865",
-            "longitude": "-0.118092",
-            "note": "the first site",
-            "region": "Blue Fin Bldg",
-            "street": "110 Southwark St",
-            "timezone": "Europe/London",
-            "url": "https://londoncalling.example.com";,
-            "accepted": True,
-        }
-        site2 = site1.copy()
-        site2.update(
-            {
-                "name": "BerlinHQ",
-                "timezone": "Europe/Berlin",
-                "city": "Berlin",
-                "country": "de",
-                "accepted": False,
-            }
+        _, pending_site = await fixture.create(
+            "site", [
+                site_details(),
+                site_details(name="BerlinHQ", accepted=False),
+            ]
         )
-        _, pending_site = await fixture.create("site", [site1, site2])
 
         response = await authenticated_user_app_client.get("/requests")
         assert response.status_code == 200
@@ -281,12 +208,9 @@ class TestPendingSitesHandler:
     async def test_post_accept(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site = {
-            "name": "LondonHQ",
-            "url": "https://londoncalling.example.com";,
-            "accepted": False,
-        }
-        [pending_site] = await fixture.create("site", [site])
+        [pending_site] = await fixture.create(
+            "site", [site_details(accepted=False)]
+        )
 
         response = await authenticated_user_app_client.post(
             "/requests",
@@ -299,12 +223,9 @@ class TestPendingSitesHandler:
     async def test_post_reject(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site = {
-            "name": "LondonHQ",
-            "url": "https://londoncalling.example.com";,
-            "accepted": False,
-        }
-        [pending_site] = await fixture.create("site", [site])
+        [pending_site] = await fixture.create(
+            "site", [site_details(accepted=False)]
+        )
 
         response = await authenticated_user_app_client.post(
             "/requests",
@@ -316,12 +237,7 @@ class TestPendingSitesHandler:
     async def test_post_invalid_ids(
         self, authenticated_user_app_client: AuthAsyncClient, fixture: Fixture
     ) -> None:
-        site = {
-            "name": "LondonHQ",
-            "url": "https://londoncalling.example.com";,
-            "accepted": True,
-        }
-        [site] = await fixture.create("site", [site])
+        [site] = await fixture.create("site", [site_details()])
         # unknown IDs and IDs for non-pending sites are invalid
         ids = [site["id"], 10000]
         response = await authenticated_user_app_client.post(

Follow ups