|
| 1 | +"""Integration tests for prefix management operations.""" |
| 2 | +import pytest |
| 3 | +from terminusdb_client.client import Client |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def client(): |
| 8 | + """Create a test client connected to TerminusDB.""" |
| 9 | + client = Client("http://127.0.0.1:6363") |
| 10 | + client.connect(user="admin", key="root", team="admin") |
| 11 | + return client |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def test_db(client): |
| 16 | + """Create and cleanup a test database.""" |
| 17 | + import time |
| 18 | + db_name = f"test_prefix_{int(time.time() * 1000)}" |
| 19 | + |
| 20 | + client.create_database(db_name, label="Test Prefix DB", description="Database for testing prefix operations") |
| 21 | + client.connect(db=db_name) |
| 22 | + |
| 23 | + yield db_name |
| 24 | + |
| 25 | + # Cleanup |
| 26 | + try: |
| 27 | + client.delete_database(db_name) |
| 28 | + except Exception: |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +class TestPrefixManagement: |
| 33 | + """Test suite for prefix management operations.""" |
| 34 | + |
| 35 | + def test_add_prefix_success(self, client, test_db): |
| 36 | + """Test adding a new prefix successfully.""" |
| 37 | + result = client.add_prefix("ex", "http://example.org/") |
| 38 | + assert result["api:status"] == "api:success" |
| 39 | + assert result["api:prefix_name"] == "ex" |
| 40 | + assert result["api:prefix_uri"] == "http://example.org/" |
| 41 | + |
| 42 | + def test_add_prefix_duplicate(self, client, test_db): |
| 43 | + """Test that adding duplicate prefix fails.""" |
| 44 | + client.add_prefix("ex", "http://example.org/") |
| 45 | + |
| 46 | + with pytest.raises(Exception) as exc_info: |
| 47 | + client.add_prefix("ex", "http://example.com/") |
| 48 | + |
| 49 | + # Should get 400 error with PrefixAlreadyExists |
| 50 | + assert exc_info.value.response.status_code == 400 |
| 51 | + |
| 52 | + def test_add_prefix_invalid_iri(self, client, test_db): |
| 53 | + """Test that invalid IRI is rejected.""" |
| 54 | + with pytest.raises(Exception) as exc_info: |
| 55 | + client.add_prefix("ex", "not-a-valid-uri") |
| 56 | + |
| 57 | + assert exc_info.value.response.status_code == 400 |
| 58 | + |
| 59 | + def test_add_prefix_reserved(self, client, test_db): |
| 60 | + """Test that reserved prefix names are rejected.""" |
| 61 | + with pytest.raises(Exception) as exc_info: |
| 62 | + client.add_prefix("@custom", "http://example.org/") |
| 63 | + |
| 64 | + assert exc_info.value.response.status_code == 400 |
| 65 | + |
| 66 | + def test_get_prefix_success(self, client, test_db): |
| 67 | + """Test retrieving an existing prefix.""" |
| 68 | + client.add_prefix("ex", "http://example.org/") |
| 69 | + uri = client.get_prefix("ex") |
| 70 | + assert uri == "http://example.org/" |
| 71 | + |
| 72 | + def test_get_prefix_not_found(self, client, test_db): |
| 73 | + """Test that getting non-existent prefix fails.""" |
| 74 | + with pytest.raises(Exception) as exc_info: |
| 75 | + client.get_prefix("nonexistent") |
| 76 | + |
| 77 | + assert exc_info.value.response.status_code == 404 |
| 78 | + |
| 79 | + def test_update_prefix_success(self, client, test_db): |
| 80 | + """Test updating an existing prefix.""" |
| 81 | + client.add_prefix("ex", "http://example.org/") |
| 82 | + result = client.update_prefix("ex", "http://example.com/") |
| 83 | + |
| 84 | + assert result["api:status"] == "api:success" |
| 85 | + assert result["api:prefix_uri"] == "http://example.com/" |
| 86 | + |
| 87 | + # Verify the update |
| 88 | + uri = client.get_prefix("ex") |
| 89 | + assert uri == "http://example.com/" |
| 90 | + |
| 91 | + def test_update_prefix_not_found(self, client, test_db): |
| 92 | + """Test that updating non-existent prefix fails.""" |
| 93 | + with pytest.raises(Exception) as exc_info: |
| 94 | + client.update_prefix("nonexistent", "http://example.org/") |
| 95 | + |
| 96 | + assert exc_info.value.response.status_code == 404 |
| 97 | + |
| 98 | + def test_upsert_prefix_create(self, client, test_db): |
| 99 | + """Test upsert creates prefix if it doesn't exist.""" |
| 100 | + result = client.upsert_prefix("ex", "http://example.org/") |
| 101 | + assert result["api:status"] == "api:success" |
| 102 | + assert result["api:prefix_uri"] == "http://example.org/" |
| 103 | + |
| 104 | + def test_upsert_prefix_update(self, client, test_db): |
| 105 | + """Test upsert updates prefix if it already exists.""" |
| 106 | + client.add_prefix("ex", "http://example.org/") |
| 107 | + result = client.upsert_prefix("ex", "http://example.com/") |
| 108 | + |
| 109 | + assert result["api:status"] == "api:success" |
| 110 | + assert result["api:prefix_uri"] == "http://example.com/" |
| 111 | + |
| 112 | + def test_delete_prefix_success(self, client, test_db): |
| 113 | + """Test deleting an existing prefix.""" |
| 114 | + client.add_prefix("ex", "http://example.org/") |
| 115 | + result = client.delete_prefix("ex") |
| 116 | + assert result["api:status"] == "api:success" |
| 117 | + |
| 118 | + # Verify deletion |
| 119 | + with pytest.raises(Exception) as exc_info: |
| 120 | + client.get_prefix("ex") |
| 121 | + assert exc_info.value.response.status_code == 404 |
| 122 | + |
| 123 | + def test_delete_prefix_not_found(self, client, test_db): |
| 124 | + """Test that deleting non-existent prefix fails.""" |
| 125 | + with pytest.raises(Exception) as exc_info: |
| 126 | + client.delete_prefix("nonexistent") |
| 127 | + |
| 128 | + assert exc_info.value.response.status_code == 404 |
| 129 | + |
| 130 | + def test_delete_prefix_reserved(self, client, test_db): |
| 131 | + """Test that deleting reserved prefix fails.""" |
| 132 | + with pytest.raises(Exception) as exc_info: |
| 133 | + client.delete_prefix("@base") |
| 134 | + |
| 135 | + assert exc_info.value.response.status_code == 400 |
0 commit comments