Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Dec 1, 2024
1 parent 763a229 commit 3d32526
Show file tree
Hide file tree
Showing 17 changed files with 481 additions and 46 deletions.
40 changes: 40 additions & 0 deletions gq/any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gq_test

import (
"encoding/json"
"testing"

"github.com/thegogod/rum/gq"
)

func TestAny(t *testing.T) {
t.Run("should resolve", func(t *testing.T) {
schema := gq.Any{}
res := schema.Do(&gq.DoParams{
Value: "testing",
})

if res.Error != nil {
t.Fatal(res.Error)
}

v, ok := res.Data.(string)

if !ok {
t.Fatal(res.Data)
}

if v != "testing" {
t.Fatalf("expected `%s`, received `%s`", "testing", v)
}
})

t.Run("should json", func(t *testing.T) {
schema := gq.Any{}
b, _ := json.Marshal(schema)

if string(b) != `"any"` {
t.FailNow()
}
})
}
4 changes: 1 addition & 3 deletions gq/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ func (self Bool) Resolve(params *ResolveParams) Result {
switch value := params.Value.(type) {
case bool:
return Result{Data: value}
case *bool:
return Result{Data: value}
}

return Result{Error: NewError("", "must be a boolean")}
}

func (self Bool) MarshalJSON() ([]byte, error) {
return json.Marshal("bool")
return json.Marshal(self.Key())
}
51 changes: 51 additions & 0 deletions gq/bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gq_test

import (
"encoding/json"
"testing"

"github.com/thegogod/rum/gq"
)

func TestBool(t *testing.T) {
t.Run("should resolve", func(t *testing.T) {
schema := gq.Bool{}
res := schema.Do(&gq.DoParams{
Value: true,
})

if res.Error != nil {
t.Fatal(res.Error)
}

v, ok := res.Data.(bool)

if !ok {
t.Fatal(res.Data)
}

if v != true {
t.Fatalf("expected `%v`, received `%v`", true, v)
}
})

t.Run("should not resolve", func(t *testing.T) {
schema := gq.Bool{}
res := schema.Do(&gq.DoParams{
Value: "test",
})

if res.Error == nil {
t.FailNow()
}
})

t.Run("should json", func(t *testing.T) {
schema := gq.Bool{}
b, _ := json.Marshal(schema)

if string(b) != `"bool"` {
t.FailNow()
}
})
}
4 changes: 1 addition & 3 deletions gq/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ func (self Date) Resolve(params *ResolveParams) Result {
switch value := params.Value.(type) {
case time.Time:
return Result{Data: value}
case *time.Time:
return Result{Data: value}
}

return Result{Error: NewError("", "must be a Date")}
}

func (self Date) MarshalJSON() ([]byte, error) {
return json.Marshal("Date")
return json.Marshal(self.Key())
}
48 changes: 48 additions & 0 deletions gq/date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gq_test

import (
"encoding/json"
"testing"
"time"

"github.com/thegogod/rum/gq"
)

func TestDate(t *testing.T) {
t.Run("should resolve", func(t *testing.T) {
schema := gq.Date{}
res := schema.Do(&gq.DoParams{
Value: time.Now(),
})

if res.Error != nil {
t.Fatal(res.Error)
}

_, ok := res.Data.(time.Time)

if !ok {
t.Fatal(res.Data)
}
})

t.Run("should not resolve", func(t *testing.T) {
schema := gq.Date{}
res := schema.Do(&gq.DoParams{
Value: "test",
})

if res.Error == nil {
t.FailNow()
}
})

t.Run("should json", func(t *testing.T) {
schema := gq.Date{}
b, _ := json.Marshal(schema)

if string(b) != `"Date"` {
t.FailNow()
}
})
}
6 changes: 1 addition & 5 deletions gq/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ func (self Float) Resolve(params *ResolveParams) Result {
switch value := params.Value.(type) {
case float32:
return Result{Data: value}
case *float32:
return Result{Data: value}
case float64:
return Result{Data: value}
case *float64:
return Result{Data: value}
}

return Result{Error: NewError("", "must be a float")}
}

func (self Float) MarshalJSON() ([]byte, error) {
return json.Marshal("float")
return json.Marshal(self.Key())
}
72 changes: 72 additions & 0 deletions gq/float_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gq_test

import (
"encoding/json"
"testing"

"github.com/thegogod/rum/gq"
)

func TestFloat(t *testing.T) {
t.Run("should resolve float32", func(t *testing.T) {
schema := gq.Float{}
res := schema.Do(&gq.DoParams{
Value: float32(1.22),
})

if res.Error != nil {
t.Fatal(res.Error)
}

v, ok := res.Data.(float32)

if !ok {
t.Fatal(res.Data)
}

if v != 1.22 {
t.Fatalf("expected `%v`, received `%v`", 1.22, v)
}
})

t.Run("should resolve float64", func(t *testing.T) {
schema := gq.Float{}
res := schema.Do(&gq.DoParams{
Value: 1.22,
})

if res.Error != nil {
t.Fatal(res.Error)
}

v, ok := res.Data.(float64)

if !ok {
t.Fatal(res.Data)
}

if v != 1.22 {
t.Fatalf("expected `%v`, received `%v`", 1.22, v)
}
})

t.Run("should not resolve", func(t *testing.T) {
schema := gq.Float{}
res := schema.Do(&gq.DoParams{
Value: "test",
})

if res.Error == nil {
t.FailNow()
}
})

t.Run("should json", func(t *testing.T) {
schema := gq.Float{}
b, _ := json.Marshal(schema)

if string(b) != `"float"` {
t.FailNow()
}
})
}
12 changes: 6 additions & 6 deletions gq/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gq

import (
"encoding/json"
"reflect"
)

type Int struct{}
Expand All @@ -18,16 +19,15 @@ func (self Int) Do(params *DoParams) Result {
}

func (self Int) Resolve(params *ResolveParams) Result {
switch value := params.Value.(type) {
case int:
return Result{Data: value}
case *int:
return Result{Data: value}
value := reflect.ValueOf(params.Value)

if value.IsValid() && value.CanInt() {
return Result{Data: value.Interface()}
}

return Result{Error: NewError("", "must be an integer")}
}

func (self Int) MarshalJSON() ([]byte, error) {
return json.Marshal("int")
return json.Marshal(self.Key())
}
Loading

0 comments on commit 3d32526

Please sign in to comment.