|
| 1 | +import uuid |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.asyncio |
| 7 | +async def test_get_root_catalog(catalogs_app_client, load_test_data): |
| 8 | + """Test getting the root catalog.""" |
| 9 | + resp = await catalogs_app_client.get("/catalogs") |
| 10 | + assert resp.status_code == 200 |
| 11 | + |
| 12 | + catalog = resp.json() |
| 13 | + assert catalog["type"] == "Catalog" |
| 14 | + assert catalog["id"] == "root" |
| 15 | + assert catalog["stac_version"] == "1.0.0" |
| 16 | + assert "links" in catalog |
| 17 | + |
| 18 | + # Check for required links |
| 19 | + links = catalog["links"] |
| 20 | + link_rels = [link["rel"] for link in links] |
| 21 | + assert "self" in link_rels |
| 22 | + assert "root" in link_rels |
| 23 | + assert "parent" in link_rels |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_create_catalog(catalogs_app_client, load_test_data): |
| 28 | + """Test creating a new catalog.""" |
| 29 | + test_catalog = load_test_data("test_catalog.json") |
| 30 | + test_catalog["id"] = f"test-catalog-{uuid.uuid4()}" |
| 31 | + |
| 32 | + resp = await catalogs_app_client.post("/catalogs", json=test_catalog) |
| 33 | + assert resp.status_code == 201 |
| 34 | + |
| 35 | + created_catalog = resp.json() |
| 36 | + assert created_catalog["id"] == test_catalog["id"] |
| 37 | + assert created_catalog["type"] == "Catalog" |
| 38 | + assert created_catalog["title"] == test_catalog["title"] |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.asyncio |
| 42 | +async def test_get_catalog(catalogs_app_client, load_test_data): |
| 43 | + """Test getting a specific catalog.""" |
| 44 | + # First create a catalog |
| 45 | + test_catalog = load_test_data("test_catalog.json") |
| 46 | + test_catalog["id"] = f"test-catalog-{uuid.uuid4()}" |
| 47 | + |
| 48 | + create_resp = await catalogs_app_client.post("/catalogs", json=test_catalog) |
| 49 | + assert create_resp.status_code == 201 |
| 50 | + |
| 51 | + # Now get it back |
| 52 | + resp = await catalogs_app_client.get(f"/catalogs/{test_catalog['id']}") |
| 53 | + assert resp.status_code == 200 |
| 54 | + |
| 55 | + catalog = resp.json() |
| 56 | + assert catalog["id"] == test_catalog["id"] |
| 57 | + assert catalog["title"] == test_catalog["title"] |
| 58 | + assert catalog["description"] == test_catalog["description"] |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_get_nonexistent_catalog(catalogs_app_client): |
| 63 | + """Test getting a catalog that doesn't exist.""" |
| 64 | + resp = await catalogs_app_client.get("/catalogs/nonexistent-catalog") |
| 65 | + assert resp.status_code == 404 |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_get_catalog_collections(catalogs_app_client, load_test_data, ctx): |
| 70 | + """Test getting collections linked from a catalog.""" |
| 71 | + # First create a catalog with a link to the test collection |
| 72 | + test_catalog = load_test_data("test_catalog.json") |
| 73 | + test_catalog["id"] = f"test-catalog-{uuid.uuid4()}" |
| 74 | + |
| 75 | + # Update the catalog links to point to the actual test collection |
| 76 | + for link in test_catalog["links"]: |
| 77 | + if link["rel"] == "child": |
| 78 | + link["href"] = f"http://test-server/collections/{ctx.collection['id']}" |
| 79 | + |
| 80 | + create_resp = await catalogs_app_client.post("/catalogs", json=test_catalog) |
| 81 | + assert create_resp.status_code == 201 |
| 82 | + |
| 83 | + # Now get collections from the catalog |
| 84 | + resp = await catalogs_app_client.get(f"/catalogs/{test_catalog['id']}/collections") |
| 85 | + assert resp.status_code == 200 |
| 86 | + |
| 87 | + collections_response = resp.json() |
| 88 | + assert "collections" in collections_response |
| 89 | + assert "links" in collections_response |
| 90 | + |
| 91 | + # Should contain the test collection |
| 92 | + collection_ids = [col["id"] for col in collections_response["collections"]] |
| 93 | + assert ctx.collection["id"] in collection_ids |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_get_catalog_collections_nonexistent_catalog(catalogs_app_client): |
| 98 | + """Test getting collections from a catalog that doesn't exist.""" |
| 99 | + resp = await catalogs_app_client.get("/catalogs/nonexistent-catalog/collections") |
| 100 | + assert resp.status_code == 404 |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +async def test_root_catalog_with_multiple_catalogs(catalogs_app_client, load_test_data): |
| 105 | + """Test that root catalog includes links to multiple catalogs.""" |
| 106 | + # Create multiple catalogs |
| 107 | + catalog_ids = [] |
| 108 | + for i in range(3): |
| 109 | + test_catalog = load_test_data("test_catalog.json") |
| 110 | + test_catalog["id"] = f"test-catalog-{uuid.uuid4()}-{i}" |
| 111 | + test_catalog["title"] = f"Test Catalog {i}" |
| 112 | + |
| 113 | + resp = await catalogs_app_client.post("/catalogs", json=test_catalog) |
| 114 | + assert resp.status_code == 201 |
| 115 | + catalog_ids.append(test_catalog["id"]) |
| 116 | + |
| 117 | + # Get root catalog |
| 118 | + resp = await catalogs_app_client.get("/catalogs") |
| 119 | + assert resp.status_code == 200 |
| 120 | + |
| 121 | + catalog = resp.json() |
| 122 | + child_links = [link for link in catalog["links"] if link["rel"] == "child"] |
| 123 | + |
| 124 | + # Should have child links for all created catalogs |
| 125 | + child_hrefs = [link["href"] for link in child_links] |
| 126 | + for catalog_id in catalog_ids: |
| 127 | + assert any(catalog_id in href for href in child_hrefs) |
0 commit comments