Skip to content

Commit

Permalink
Adding lt/lte/gt/gte filters (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Bauduin <geoffrey.bauduin@corp.ovh.com>
  • Loading branch information
geoffreybauduin authored Nov 13, 2017
1 parent 8ef410b commit 7617ad3
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 13 deletions.
76 changes: 76 additions & 0 deletions filterapply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package yaorm_test

import (
"testing"

"github.com/geoffreybauduin/yaorm"
"github.com/geoffreybauduin/yaorm/testdata"
"github.com/geoffreybauduin/yaorm/yaormfilter"
"github.com/stretchr/testify/assert"
)

func TestFilterApplier_ApplyLt(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider("test")
assert.Nil(t, err)
category := &testdata.Category{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.Category{Name: "category2"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Lt(category2.ID)))
assert.Nil(t, err)
assert.Len(t, models, 1)
assert.Equal(t, models[0].(*testdata.Category).ID, category.ID)
}

func TestFilterApplier_ApplyLte(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider("test")
assert.Nil(t, err)
category := &testdata.Category{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.Category{Name: "category2"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Lte(category2.ID)))
assert.Nil(t, err)
assert.Len(t, models, 2)
}

func TestFilterApplier_ApplyGt(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider("test")
assert.Nil(t, err)
category := &testdata.Category{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.Category{Name: "category2"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Gt(category.ID)))
assert.Nil(t, err)
assert.Len(t, models, 1)
assert.Equal(t, models[0].(*testdata.Category).ID, category2.ID)
}

func TestFilterApplier_ApplyGte(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider("test")
assert.Nil(t, err)
category := &testdata.Category{Name: "category"}
saveModel(t, dbp, category)
category2 := &testdata.Category{Name: "category2"}
saveModel(t, dbp, category2)

models, err := yaorm.GenericSelectAll(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Gte(category.ID)))
assert.Nil(t, err)
assert.Len(t, models, 2)
}
36 changes: 32 additions & 4 deletions yaormfilter/datefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ func NewDateFilter() ValueFilter {
return &DateFilter{}
}

// Equals applies an equal filter on Date
func (f *DateFilter) Equals(v interface{}) ValueFilter {
func (f *DateFilter) getValue(v interface{}) interface{} {
underlyingValue := tools.GetNonPtrValue(v)
// make sure we have an int64
// make sure we have a time.Time
if underlyingValue.Kind() != reflect.Struct || underlyingValue.Type() != timeType {
panic("Value in DateFilter is not a time.Time object")
}
f.equals(underlyingValue.Interface())
return underlyingValue.Interface()
}

// Equals applies an equal filter on Date
func (f *DateFilter) Equals(v interface{}) ValueFilter {
f.equals(f.getValue(v))
return f
}

Expand All @@ -48,3 +52,27 @@ func (f *DateFilter) Nil(v bool) ValueFilter {
func (f *DateFilter) In(values ...interface{}) ValueFilter {
return f
}

// Lt adds a < filter
func (f *DateFilter) Lt(v interface{}) ValueFilter {
f.lt(f.getValue(v))
return f
}

// Lte adds a <= filter
func (f *DateFilter) Lte(v interface{}) ValueFilter {
f.lte(f.getValue(v))
return f
}

// Gt adds a > filter
func (f *DateFilter) Gt(v interface{}) ValueFilter {
f.gt(f.getValue(v))
return f
}

// Gte adds a > filter
func (f *DateFilter) Gte(v interface{}) ValueFilter {
f.gte(f.getValue(v))
return f
}
41 changes: 32 additions & 9 deletions yaormfilter/int64filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ func NewInt64Filter() ValueFilter {
return &Int64Filter{}
}

// Equals adds an equal filter
func (f *Int64Filter) Equals(v interface{}) ValueFilter {
func (f *Int64Filter) getValue(v interface{}) interface{} {
underlyingValue := tools.GetNonPtrValue(v)
// make sure we have an int64
if underlyingValue.Kind() != reflect.Int64 {
panic("Value in Int64Filter is not an int64")
}
f.equals(underlyingValue.Interface())
return underlyingValue.Interface()
}

// Equals adds an equal filter
func (f *Int64Filter) Equals(v interface{}) ValueFilter {
f.equals(f.getValue(v))
return f
}

Expand All @@ -42,13 +46,32 @@ func (f *Int64Filter) Nil(v bool) ValueFilter {
func (f *Int64Filter) In(values ...interface{}) ValueFilter {
interfaceValues := []interface{}{}
for _, v := range values {
underlyingValue := tools.GetNonPtrValue(v)
// make sure we have an int64
if underlyingValue.Kind() != reflect.Int64 {
panic("Value in Int64Filter is not an int64")
}
interfaceValues = append(interfaceValues, underlyingValue.Interface())
interfaceValues = append(interfaceValues, f.getValue(v))
}
f.in(interfaceValues)
return f
}

// Lt adds a < filter
func (f *Int64Filter) Lt(v interface{}) ValueFilter {
f.lt(f.getValue(v))
return f
}

// Lte adds a <= filter
func (f *Int64Filter) Lte(v interface{}) ValueFilter {
f.lte(f.getValue(v))
return f
}

// Gt adds a > filter
func (f *Int64Filter) Gt(v interface{}) ValueFilter {
f.gt(f.getValue(v))
return f
}

// Gte adds a > filter
func (f *Int64Filter) Gte(v interface{}) ValueFilter {
f.gte(f.getValue(v))
return f
}
20 changes: 20 additions & 0 deletions yaormfilter/nilfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,23 @@ func (f *NilFilter) Nil(v bool) ValueFilter {
func (f *NilFilter) In(values ...interface{}) ValueFilter {
return f
}

// Lt adds a < filter
func (f *NilFilter) Lt(v interface{}) ValueFilter {
return f
}

// Lte adds a <= filter
func (f *NilFilter) Lte(v interface{}) ValueFilter {
return f
}

// Gt adds a > filter
func (f *NilFilter) Gt(v interface{}) ValueFilter {
return f
}

// Gte adds a > filter
func (f *NilFilter) Gte(v interface{}) ValueFilter {
return f
}
20 changes: 20 additions & 0 deletions yaormfilter/stringfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ func (f *StringFilter) In(values ...interface{}) ValueFilter {
f.in(interfaceValues)
return f
}

// Lt adds a < filter
func (f *StringFilter) Lt(v interface{}) ValueFilter {
return f
}

// Lte adds a <= filter
func (f *StringFilter) Lte(v interface{}) ValueFilter {
return f
}

// Gt adds a > filter
func (f *StringFilter) Gt(v interface{}) ValueFilter {
return f
}

// Gte adds a > filter
func (f *StringFilter) Gte(v interface{}) ValueFilter {
return f
}
56 changes: 56 additions & 0 deletions yaormfilter/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,59 @@ func Like(v interface{}) ValueFilter {
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in Like filter", underlyingValue.Kind(), v))
}

// Lt returns the correct filter according the value sent
func Lt(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.Int64:
return NewInt64Filter().Lt(v)
case reflect.Struct:
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().Lt(v)
}
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in Lt filter", underlyingValue.Kind(), v))
}

// Lte returns the correct filter according the value sent
func Lte(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.Int64:
return NewInt64Filter().Lte(v)
case reflect.Struct:
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().Lte(v)
}
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in Lte filter", underlyingValue.Kind(), v))
}

// Gt returns the correct filter according the value sent
func Gt(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.Int64:
return NewInt64Filter().Gt(v)
case reflect.Struct:
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().Gt(v)
}
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in Gt filter", underlyingValue.Kind(), v))
}

// Gte returns the correct filter according the value sent
func Gte(v interface{}) ValueFilter {
underlyingValue := tools.GetNonPtrValue(v)
switch underlyingValue.Kind() {
case reflect.Int64:
return NewInt64Filter().Gte(v)
case reflect.Struct:
if _, ok := underlyingValue.Interface().(time.Time); ok {
return NewDateFilter().Gte(v)
}
}
panic(fmt.Errorf("Unknown type: %+v for value %+v in Gte filter", underlyingValue.Kind(), v))
}
20 changes: 20 additions & 0 deletions yaormfilter/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ func TestIn(t *testing.T) {
func TestLike(t *testing.T) {
assert.IsType(t, &yaormfilter.StringFilter{}, yaormfilter.Like("abcdef%"))
}

func TestLt(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Lt(int64(12)))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Lt(time.Now()))
}

func TestLte(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Lte(int64(12)))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Lte(time.Now()))
}

func TestGt(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Gt(int64(12)))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Gt(time.Now()))
}

func TestGte(t *testing.T) {
assert.IsType(t, &yaormfilter.Int64Filter{}, yaormfilter.Gte(int64(12)))
assert.IsType(t, &yaormfilter.DateFilter{}, yaormfilter.Gte(time.Now()))
}
Loading

0 comments on commit 7617ad3

Please sign in to comment.