-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Relevant issue(s) Resolves #1542 ## Description This PR add the `_not` operator as an option for filters in graphql queries.
- Loading branch information
Showing
6 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package connor | ||
|
||
// not is an operator which performs object equality test | ||
// and returns the inverse of the result. | ||
func not(condition, data any) (bool, error) { | ||
m, err := eq(condition, data) | ||
if err != nil { | ||
return false, err | ||
} | ||
return !m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package connor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNot_WithNotAndNotNot_NoError(t *testing.T) { | ||
const testString = "Source is the glue of web3" | ||
|
||
// not equal | ||
result, err := not(testString, testString) | ||
require.NoError(t, err) | ||
require.False(t, result) | ||
|
||
// not not equal | ||
result, err = not("Source is the glue", testString) | ||
require.NoError(t, err) | ||
require.True(t, result) | ||
} | ||
|
||
func TestNot_WithEmptyCondition_ReturnError(t *testing.T) { | ||
const testString = "Source is the glue of web3" | ||
|
||
_, err := not(map[FilterKey]any{&operator{"_some"}: "test"}, testString) | ||
require.ErrorIs(t, err, ErrUnknownOperator) | ||
} | ||
|
||
type operator struct { | ||
// The filter operation string that this `operator`` represents. | ||
// | ||
// E.g. "_eq", or "_and". | ||
Operation string | ||
} | ||
|
||
func (k *operator) GetProp(data any) any { | ||
return data | ||
} | ||
|
||
func (k *operator) GetOperatorOrDefault(defaultOp string) string { | ||
return k.Operation | ||
} | ||
|
||
func (k *operator) Equal(other FilterKey) bool { | ||
if otherKey, isOk := other.(*operator); isOk && *k == *otherKey { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
tests/integration/query/simple/with_filter/with_not_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package simple | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/tests/integration" | ||
) | ||
|
||
func TestQuerySimple_WithNotEqualToXFilter_NoError(t *testing.T) { | ||
test := testUtils.RequestTestCase{ | ||
Description: "Simple query with logical compound filter (not)", | ||
Request: `query { | ||
Users(filter: {_not: {Age: {_eq: 55}}}) { | ||
Name | ||
Age | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
`{ | ||
"Name": "John", | ||
"Age": 21 | ||
}`, | ||
`{ | ||
"Name": "Bob", | ||
"Age": 32 | ||
}`, | ||
`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`, | ||
`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`, | ||
}, | ||
}, | ||
Results: []map[string]any{ | ||
{ | ||
"Name": "Bob", | ||
"Age": uint64(32), | ||
}, | ||
{ | ||
"Name": "Alice", | ||
"Age": uint64(19), | ||
}, | ||
{ | ||
"Name": "John", | ||
"Age": uint64(21), | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestQuerySimple_WithNotEqualToXorYFilter_NoError(t *testing.T) { | ||
test := testUtils.RequestTestCase{ | ||
Description: "Simple query with logical compound filter (not)", | ||
Request: `query { | ||
Users(filter: {_not: {_or: [{Age: {_eq: 55}}, {Name: {_eq: "Alice"}}]}}) { | ||
Name | ||
Age | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
`{ | ||
"Name": "John", | ||
"Age": 21 | ||
}`, | ||
`{ | ||
"Name": "Bob", | ||
"Age": 32 | ||
}`, | ||
`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`, | ||
`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`, | ||
}, | ||
}, | ||
Results: []map[string]any{ | ||
{ | ||
"Name": "Bob", | ||
"Age": uint64(32), | ||
}, | ||
{ | ||
"Name": "John", | ||
"Age": uint64(21), | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestQuerySimple_WithEmptyNotFilter_ReturnError(t *testing.T) { | ||
test := testUtils.RequestTestCase{ | ||
Description: "Simple query with empty logical compound filter (not) returns empty result set", | ||
Request: `query { | ||
Users(filter: {_not: {}}) { | ||
Name | ||
Age | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
`{ | ||
"Name": "John", | ||
"Age": 21 | ||
}`, | ||
`{ | ||
"Name": "Bob", | ||
"Age": 32 | ||
}`, | ||
`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`, | ||
`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`, | ||
}, | ||
}, | ||
Results: []map[string]any{}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestQuerySimple_WithNotEqualToXAndNotYFilter_NoError(t *testing.T) { | ||
test := testUtils.RequestTestCase{ | ||
Description: "Simple query with logical compound filter (not)", | ||
Request: `query { | ||
Users(filter: {_not: {Age: {_eq: 55}, _not: {Name: {_eq: "Carlo"}}}}) { | ||
Name | ||
Age | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
`{ | ||
"Name": "John", | ||
"Age": 21 | ||
}`, | ||
`{ | ||
"Name": "Bob", | ||
"Age": 32 | ||
}`, | ||
`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`, | ||
`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`, | ||
`{ | ||
"Name": "Frank", | ||
"Age": 55 | ||
}`, | ||
}, | ||
}, | ||
Results: []map[string]any{ | ||
{ | ||
"Name": "Bob", | ||
"Age": uint64(32), | ||
}, | ||
{ | ||
"Name": "Alice", | ||
"Age": uint64(19), | ||
}, | ||
{ | ||
"Name": "John", | ||
"Age": uint64(21), | ||
}, | ||
{ | ||
"Name": "Carlo", | ||
"Age": uint64(55), | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} |