Skip to content

Commit

Permalink
get rid of Is in tests (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
josuebrunel committed Mar 13, 2023
1 parent bb1309f commit 867c3e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 60 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/josuebrunel/clausify

go 1.18

require github.com/matryer/is v1.4.0
107 changes: 49 additions & 58 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,159 +1,151 @@
package clausify

import (
"github.com/matryer/is"
"log"
"net/url"
"reflect"
"strings"
"testing"
)

func assert(t *testing.T, x, y any) {
if !reflect.DeepEqual(x, y) {
t.Fatalf("AssertionError: %v != %v", x, y)
}
}

func getURLQuery(uri string) map[string][]string {
u, _ := url.Parse(uri)
return u.Query()
}

func TestInvalidOperator(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?username__xy=josh")
_, err := Clausify(q)
is.Equal(err.Error(), "Invalid operator")
assert(t, err.Error(), "Invalid operator")
}

func TestEqual(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?username=josh")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.Equal(c.Conditions, "username = ?")
is.Equal(len(c.Variables), 1)
assert(t, c.Conditions, "username = ?")
assert(t, len(c.Variables), 1)
q = getURLQuery("https://httpbin.org/?username=josh&age=30")
c, _ = Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.Equal(strings.Contains(c.Conditions, "username = ?"), true)
is.Equal(strings.Contains(c.Conditions, "age = ?"), true)
is.Equal(len(c.Variables), 2)
assert(t, strings.Contains(c.Conditions, "username = ?"), true)
assert(t, strings.Contains(c.Conditions, "age = ?"), true)
assert(t, len(c.Variables), 2)
}

func TestNotEqual(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?username__neq=josh")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.Equal(c.Conditions, "username != ?")
is.Equal(len(c.Variables), 1)
assert(t, c.Conditions, "username != ?")
assert(t, len(c.Variables), 1)
q = getURLQuery("https://httpbin.org/?username__neq=josh&age__neq=30")
c, _ = Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.Equal(strings.Contains(c.Conditions, "username != ?"), true)
is.Equal(strings.Contains(c.Conditions, "age != ?"), true)
is.Equal(len(c.Variables), 2)
assert(t, strings.Contains(c.Conditions, "username != ?"), true)
assert(t, strings.Contains(c.Conditions, "age != ?"), true)
assert(t, len(c.Variables), 2)
}

func TestGreaterThan(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__gt=15&name=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price > ?"))
is.True(strings.Contains(c.Conditions, "name = ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price > ?"))
assert(t, true, strings.Contains(c.Conditions, "name = ?"))
assert(t, len(c.Variables), 2)
}

func TestGreaterThanEqual(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__gte=15&name=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price >= ?"))
is.True(strings.Contains(c.Conditions, "name = ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price >= ?"))
assert(t, true, strings.Contains(c.Conditions, "name = ?"))
assert(t, len(c.Variables), 2)
}

func TestLessThan(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__lt=15&name=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price < ?"))
is.True(strings.Contains(c.Conditions, "name = ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price < ?"))
assert(t, true, strings.Contains(c.Conditions, "name = ?"))
assert(t, len(c.Variables), 2)
}

func TestLessThanEqual(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__lte=15&name=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price <= ?"))
is.True(strings.Contains(c.Conditions, "name = ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price <= ?"))
assert(t, true, strings.Contains(c.Conditions, "name = ?"))
assert(t, len(c.Variables), 2)
}

func TestLike(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__lte=15&name__like=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price <= ?"))
is.True(strings.Contains(c.Conditions, "name LIKE ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price <= ?"))
assert(t, true, strings.Contains(c.Conditions, "name LIKE ?"))
assert(t, len(c.Variables), 2)
}

func TestILike(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__lte=15&name__ilike=book&category=fruits")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price <= ?"))
is.True(strings.Contains(c.Conditions, "name ILIKE ?"))
is.Equal(len(c.Variables), 3)
assert(t, true, strings.Contains(c.Conditions, "price <= ?"))
assert(t, true, strings.Contains(c.Conditions, "name ILIKE ?"))
assert(t, len(c.Variables), 3)
}

func TestNotLike(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?price__lte=15&name__nlike=book")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "price <= ?"))
is.True(strings.Contains(c.Conditions, "name NOT LIKE ?"))
is.Equal(len(c.Variables), 2)
assert(t, true, strings.Contains(c.Conditions, "price <= ?"))
assert(t, true, strings.Contains(c.Conditions, "name NOT LIKE ?"))
assert(t, len(c.Variables), 2)
}

func TestIn(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?id__in=2,4,6")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "id IN ?"))
is.Equal(len(c.Variables), 1)
assert(t, true, strings.Contains(c.Conditions, "id IN ?"))
assert(t, len(c.Variables), 1)
}

func TestNotIn(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?id__nin=2,4,6")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "id NOT IN ?"))
is.Equal(len(c.Variables), 1)
assert(t, true, strings.Contains(c.Conditions, "id NOT IN ?"))
assert(t, len(c.Variables), 1)
}

func TestBetween(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?category=fruits&price__between=10,20")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, " BETWEEN ? AND ?"))
is.Equal(len(c.Variables), 3)
assert(t, true, strings.Contains(c.Conditions, " BETWEEN ? AND ?"))
assert(t, len(c.Variables), 3)
}

func TestNotBetween(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?category=fruits&price__nbetween=10,20")
c, _ := Clausify(q)
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, " NOT BETWEEN ? AND ?"))
is.Equal(len(c.Variables), 3)
assert(t, true, strings.Contains(c.Conditions, " NOT BETWEEN ? AND ?"))
assert(t, len(c.Variables), 3)
}

type MyClausifier struct {
Expand All @@ -172,10 +164,9 @@ func (m MyClausifier) Clausify(k string, vv []string) (Condition, error) {
}

func TestCustomOperator(t *testing.T) {
is := is.New(t)
q := getURLQuery("https://httpbin.org/?id-<>=1")
c, _ := With(q, MyClausifier{Separator: "-"})
log.Printf("%+v, %+v\n", c.Conditions, c.Variables)
is.True(strings.Contains(c.Conditions, "id <> ?"))
is.Equal(len(c.Variables), 1)
assert(t, true, strings.Contains(c.Conditions, "id <> ?"))
assert(t, len(c.Variables), 1)
}

0 comments on commit 867c3e1

Please sign in to comment.