Skip to content

Commit 51e52a0

Browse files
authored
fix(mock): normalize database catalog key; drop broken example (#189)
* fix(mock): normalize database catalog key to strip /catalog suffix Write and read paths keyed on different forms of the name — writes used patch.Name (with /catalog suffix) while reads used the bare databaseName argument — so any write-then-read round-trip through the mock failed. Normalize both sides to use the bare name. This unblocks the first catalog acceptance test (BYT-9296 prerequisite). * docs: remove stale bytebase_database_catalog data source from example The data source referenced here was never registered in provider.go, so the example failed terraform validate. Drop the broken block; a catalog example will be reintroduced alongside object_schema_json support.
1 parent eaebfdf commit 51e52a0

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

examples/database/main.tf

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,3 @@ data "bytebase_database_list" "all" {
2727
output "all_databases" {
2828
value = data.bytebase_database_list.all
2929
}
30-
31-
data "bytebase_database_catalog" "employee" {
32-
database = "instances/test-sample-instance/databases/employee"
33-
}
34-
35-
output "employee_catalog" {
36-
value = data.bytebase_database_catalog.employee
37-
}

provider/internal/mock_client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ func (c *mockClient) GetDatabaseCatalog(_ context.Context, databaseName string)
463463
func (c *mockClient) UpdateDatabaseCatalog(_ context.Context, patch *v1pb.DatabaseCatalog) (*v1pb.DatabaseCatalog, error) {
464464
mu.Lock()
465465
defer mu.Unlock()
466-
c.databaseCatalogMap[patch.Name] = patch
466+
// The catalog proto's Name carries the "/catalog" suffix. Key by the
467+
// bare database name so GetDatabaseCatalog(databaseName) round-trips.
468+
key := strings.TrimSuffix(patch.Name, DatabaseCatalogNameSuffix)
469+
c.databaseCatalogMap[key] = patch
467470
return patch, nil
468471
}
469472

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
v1pb "buf.build/gen/go/bytebase/bytebase/protocolbuffers/go/v1"
8+
)
9+
10+
// Verify that a catalog written via UpdateDatabaseCatalog is readable via
11+
// GetDatabaseCatalog using the database name the real client would pass in.
12+
// The real client calls GetDatabaseCatalog(ctx, databaseName) where
13+
// databaseName does NOT include the "/catalog" suffix; the suffix is added
14+
// inside the catalog proto's Name field by convertToV1DatabaseCatalog.
15+
func TestMockDatabaseCatalog_WriteThenReadRoundTrip(t *testing.T) {
16+
c := &mockClient{
17+
databaseCatalogMap: map[string]*v1pb.DatabaseCatalog{},
18+
}
19+
databaseName := "instances/test-inst/databases/test-db"
20+
patch := &v1pb.DatabaseCatalog{
21+
Name: databaseName + DatabaseCatalogNameSuffix,
22+
}
23+
if _, err := c.UpdateDatabaseCatalog(context.Background(), patch); err != nil {
24+
t.Fatalf("update: %v", err)
25+
}
26+
got, err := c.GetDatabaseCatalog(context.Background(), databaseName)
27+
if err != nil {
28+
t.Fatalf("get: %v", err)
29+
}
30+
if got.Name != patch.Name {
31+
t.Errorf("catalog name mismatch: got %q want %q", got.Name, patch.Name)
32+
}
33+
}

0 commit comments

Comments
 (0)