From 6d84006b3d1a3eb329b6fc88bc041280d639ea30 Mon Sep 17 00:00:00 2001 From: avallete Date: Sat, 12 Oct 2024 12:52:38 +0200 Subject: [PATCH] chore: add some diff tests --- pkg/config/api.go | 1 + pkg/config/api_test.go | 52 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/pkg/config/api.go b/pkg/config/api.go index 2d676fade..3711fd792 100644 --- a/pkg/config/api.go +++ b/pkg/config/api.go @@ -67,6 +67,7 @@ func (a *RemoteApi) fromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJW result.Enabled = false return result } + result.Enabled = true // Update Schemas if present in remoteConfig schemas := strings.Split(remoteConfig.DbSchema, ",") result.Schemas = make([]string, len(schemas)) diff --git a/pkg/config/api_test.go b/pkg/config/api_test.go index 2ea6bd634..ca5de36c8 100644 --- a/pkg/config/api_test.go +++ b/pkg/config/api_test.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -10,6 +11,7 @@ import ( func TestApiToUpdatePostgrestConfigBody(t *testing.T) { t.Run("converts all fields correctly", func(t *testing.T) { api := &RemoteApi{ + Enabled: true, Schemas: []string{"public", "private"}, ExtraSearchPath: []string{"extensions", "public"}, MaxRows: 1000, @@ -27,15 +29,15 @@ func TestApiToUpdatePostgrestConfigBody(t *testing.T) { body := api.ToUpdatePostgrestConfigBody() - assert.Nil(t, body.DbSchema) - assert.Nil(t, body.DbExtraSearchPath) - assert.Nil(t, body.MaxRows) + // remote api will be false by default, leading to an empty schema on api side + assert.Equal(t, "", *body.DbSchema) }) } func TestApiDiffWithRemote(t *testing.T) { t.Run("detects differences", func(t *testing.T) { api := &RemoteApi{ + Enabled: true, Schemas: []string{"public", "private"}, ExtraSearchPath: []string{"extensions", "public"}, MaxRows: 1000, @@ -59,6 +61,7 @@ func TestApiDiffWithRemote(t *testing.T) { t.Run("handles no differences", func(t *testing.T) { api := &RemoteApi{ + Enabled: true, Schemas: []string{"public"}, ExtraSearchPath: []string{"public"}, MaxRows: 500, @@ -76,6 +79,7 @@ func TestApiDiffWithRemote(t *testing.T) { }) t.Run("handles multiple schemas and search paths with spaces", func(t *testing.T) { api := &RemoteApi{ + Enabled: true, Schemas: []string{"public", "private"}, ExtraSearchPath: []string{"extensions", "public"}, MaxRows: 500, @@ -91,4 +95,46 @@ func TestApiDiffWithRemote(t *testing.T) { assert.Empty(t, diff) }) + t.Run("handles api disabled on remote side", func(t *testing.T) { + api := &RemoteApi{ + Enabled: true, + Schemas: []string{"public", "private"}, + ExtraSearchPath: []string{"extensions", "public"}, + MaxRows: 500, + } + + remoteConfig := v1API.PostgrestConfigWithJWTSecretResponse{ + DbSchema: "", + DbExtraSearchPath: "", + MaxRows: 0, + } + + diff := api.DiffWithRemote(remoteConfig) + d := string(diff) + fmt.Println(d) + + assert.Contains(t, string(diff), "-enabled = false") + assert.Contains(t, string(diff), "+enabled = true") + }) + t.Run("handles api disabled on local side", func(t *testing.T) { + api := &RemoteApi{ + Enabled: false, + Schemas: []string{"public"}, + ExtraSearchPath: []string{"public"}, + MaxRows: 500, + } + + remoteConfig := v1API.PostgrestConfigWithJWTSecretResponse{ + DbSchema: "public", + DbExtraSearchPath: "public", + MaxRows: 500, + } + + diff := api.DiffWithRemote(remoteConfig) + d := string(diff) + fmt.Println(d) + + assert.Contains(t, string(diff), "-enabled = true") + assert.Contains(t, string(diff), "+enabled = false") + }) }