Skip to content

Commit

Permalink
chore: add some diff tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Oct 12, 2024
1 parent a3a13f6 commit 6d84006
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
52 changes: 49 additions & 3 deletions pkg/config/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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")
})
}

0 comments on commit 6d84006

Please sign in to comment.