Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mul14 committed Oct 17, 2024
1 parent 94b93ed commit 2c4d2b0
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 28 deletions.
41 changes: 23 additions & 18 deletions pkg/db/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ func (q *Query) OrIlike(column string, value string) *Query {

func (q *Query) Is(column string, value any) *Query {

if !isValueWhitelist(value) {
panic("isValueWhitelist: only \"true\", \"false\", \"null\", or \"unknown\" are allowed")
if getWhitelistIsValue(value) == "" {
panic("getWhitelistIsValue: only \"true\", \"false\", \"nil\", \"null\", or \"unknown\" are allowed")

Check warning on line 409 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L409

Added line #L409 was not covered by tests
}

if q.IsList == nil {
Expand All @@ -415,16 +415,16 @@ func (q *Query) Is(column string, value any) *Query {

*q.IsList = append(
*q.IsList,
fmt.Sprintf("%s=is.%s", column, getStringValue(value)),
fmt.Sprintf("%s=is.%s", column, getWhitelistIsValue(value)),
)

return q
}

func (q *Query) NotIs(column string, value any) *Query {

if !isValueWhitelist(value) {
panic("isValueWhitelist: only \"true\", \"false\", \"null\", or \"unknown\" are allowed")
if getWhitelistIsValue(value) == "" {
panic("getWhitelistIsValue: only \"true\", \"false\", \"nil\", \"null\", or \"unknown\" are allowed")

Check warning on line 427 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L427

Added line #L427 was not covered by tests
}

if q.IsList == nil {
Expand All @@ -433,7 +433,7 @@ func (q *Query) NotIs(column string, value any) *Query {

*q.IsList = append(
*q.IsList,
fmt.Sprintf("%s=not.is.%s", column, getStringValue(value)),
fmt.Sprintf("%s=not.is.%s", column, getWhitelistIsValue(value)),
)

return q
Expand Down Expand Up @@ -473,17 +473,22 @@ func SliceToStringSlice(slice interface{}) []string {
return stringSlice
}

func isValueWhitelist(value any) bool {
switch getStringValue(value) {
case "true":
return true
case "false":
return true
case "null":
return true
case "unknown":
return true
default:
return false
func getWhitelistIsValue(value any) string {
if getStringValue(value) == "true" {
return "true"
}

if getStringValue(value) == "false" {
return "false"
}

if value == nil || getStringValue(value) == "null" {
return "null"
}

if getStringValue(value) == "unknown" {
return "unknown"
}

return ""

Check warning on line 493 in pkg/db/where.go

View check run for this annotation

Codecov / codecov/patch

pkg/db/where.go#L493

Added line #L493 was not covered by tests
}
84 changes: 74 additions & 10 deletions pkg/db/where_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,85 @@ func TestOrIlike(t *testing.T) {
}

func TestIs(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).Is("is_featured", "true")
t.Run("is true", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).Is("is_featured", true)

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}
if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=is.true", q.GetUrl(), "the url should match")
})

t.Run("is false", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).Is("is_featured", false)

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=is.false", q.GetUrl(), "the url should match")
})

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=is.true", q.GetUrl(), "the url should match")
t.Run("is null", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).Is("is_featured", nil)

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=is.null", q.GetUrl(), "the url should match")
})

t.Run("is unknown", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).Is("is_featured", "unknown")

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=is.unknown", q.GetUrl(), "the url should match")
})
}

func TestNotIs(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).NotIs("is_featured", "true")
t.Run("not is true", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).NotIs("is_featured", true)

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}
if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=not.is.true", q.GetUrl(), "the url should match")
})

t.Run("not is false", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).NotIs("is_featured", "false")

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=not.is.false", q.GetUrl(), "the url should match")
})

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=not.is.true", q.GetUrl(), "the url should match")
t.Run("not is null", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).NotIs("is_featured", nil)

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=not.is.null", q.GetUrl(), "the url should match")
})

t.Run("not is unknown", func(t *testing.T) {
q := NewQuery(&mockRaidenContext).Model(articleMockModel).NotIs("is_featured", "unknown")

if q.IsList == nil {
t.Error("Expected where clause not to be nil")
}

assert.Equalf(t, "/rest/v1/articles?select=*&is_featured=not.is.unknown", q.GetUrl(), "the url should match")
})
}

0 comments on commit 2c4d2b0

Please sign in to comment.