Skip to content

Commit

Permalink
fix: Filter with date and document with nil date value (sourcenetwork…
Browse files Browse the repository at this point in the history
…#2946)

## Relevant issue(s)

Resolves sourcenetwork#2945 

## Description

This PR ensures that no error occurs when filtering for a date while
having documents with `nil` date value.
  • Loading branch information
fredcarle authored Aug 22, 2024
1 parent 53e55c4 commit 64a2ada
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/connor/ge.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func ge(condition, data any) (bool, error) {
return false, err
}
return dt.After(c) || dt.Equal(c), nil
case nil:
return false, nil
default:
return false, client.NewErrUnhandledType("data", d)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/connor/gt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func gt(condition, data any) (bool, error) {
return false, err
}
return dt.After(c), nil
case nil:
return false, nil
default:
return false, client.NewErrUnhandledType("data", d)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/connor/le.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func le(condition, data any) (bool, error) {
return false, err
}
return dt.Before(c) || dt.Equal(c), nil
case nil:
return false, nil
default:
return false, client.NewErrUnhandledType("data", d)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/connor/lt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func lt(condition, data any) (bool, error) {
return false, err
}
return dt.Before(c), nil
case nil:
return false, nil
default:
return false, client.NewErrUnhandledType("data", d)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestQuerySimpleWithDateTimeEqualsFilterBlock(t *testing.T) {

func TestQuerySimpleWithDateTimeEqualsNilFilterBlock(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter(age)",
Description: "Simple query with basic filter(CreatedAt)",
Actions: []any{
testUtils.CreateDoc{
Doc: `{
Expand Down Expand Up @@ -105,3 +105,51 @@ func TestQuerySimpleWithDateTimeEqualsNilFilterBlock(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeEqualsAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_eq: "2016-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": testUtils.MustParseTime("2016-07-23T03:46:56-05:00"),
},
},
},
},
},
}

executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,56 @@ func TestQuerySimpleWithDateTimeGEFilterBlockWithNilValue(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeGEAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_ge: "2016-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": testUtils.MustParseTime("2016-07-23T03:46:56-05:00"),
},
{
"Name": "John",
"Age": int64(21),
"CreatedAt": testUtils.MustParseTime("2017-07-23T03:46:56-05:00"),
},
},
},
},
},
}

executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,51 @@ func TestQuerySimpleWithDateTimeGTFilterBlockWithNilValue(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeGTAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_gt: "2016-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "John",
"Age": int64(21),
"CreatedAt": testUtils.MustParseTime("2017-07-23T03:46:56-05:00"),
},
},
},
},
},
}

executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,56 @@ func TestQuerySimpleWithDateTimeLEFilterBlockWithNullValue(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeLEAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_le: "2017-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": testUtils.MustParseTime("2016-07-23T03:46:56-05:00"),
},
{
"Name": "John",
"Age": int64(21),
"CreatedAt": testUtils.MustParseTime("2017-07-23T03:46:56-05:00"),
},
},
},
},
},
}

executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,52 @@ func TestQuerySimpleWithDateTimeLTFilterBlockWithNullValue(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeLTAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_lt: "2017-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{

{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": testUtils.MustParseTime("2016-07-23T03:46:56-05:00"),
},
},
},
},
},
}

executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,56 @@ func TestQuerySimpleWithDateTimeNotEqualsNilFilterBlock(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithNilDateTimeNotEqualAndNonNilFilterBlock_ShouldSucceed(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with basic filter with nil value and non-nil filter",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(21),
"CreatedAt": "2017-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Bob",
"Age": int64(32),
"CreatedAt": "2016-07-23T03:46:56-05:00",
},
},
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "Fred",
"Age": 44,
},
},
testUtils.Request{
Request: `query {
Users(filter: {CreatedAt: {_ne: "2016-07-23T03:46:56-05:00"}}) {
Name
Age
CreatedAt
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "John",
"Age": int64(21),
"CreatedAt": testUtils.MustParseTime("2017-07-23T03:46:56-05:00"),
},
{
"Name": "Fred",
"Age": int64(44),
"CreatedAt": nil,
},
},
},
},
},
}

executeTestCase(t, test)
}

0 comments on commit 64a2ada

Please sign in to comment.