From 80e4d269fd4a6b49c8412a660594acb1cf45bd30 Mon Sep 17 00:00:00 2001 From: AndrewSisley Date: Mon, 11 Jul 2022 10:50:37 -0400 Subject: [PATCH] test: Add tests for default properties (#611) * Rename variable It is not true when a doc has been read, but when the whole span is complete * Add email field to simple test schema Need two string fields for an upcoming test case * Add tests for default properties --- db/fetcher/fetcher.go | 8 +-- .../i412-no-change-tests-updated.md | 3 + .../query/inline_array/simple_test.go | 22 +++++++ .../query/one_to_many/with_cid_dockey_test.go | 2 +- tests/integration/query/simple/simple_test.go | 58 +++++++++++++++++++ tests/integration/query/simple/utils.go | 1 + .../query/simple/with_cid_dockey_test.go | 8 +-- .../query/simple/with_version_test.go | 8 +-- 8 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 docs/data_format_changes/i412-no-change-tests-updated.md diff --git a/db/fetcher/fetcher.go b/db/fetcher/fetcher.go index 822a1cc8d8..784258e84e 100644 --- a/db/fetcher/fetcher.go +++ b/db/fetcher/fetcher.go @@ -201,10 +201,10 @@ func (df *DocumentFetcher) ProcessKV(kv *core.KeyValue) error { // nextKey gets the next kv. It sets both kv and kvEnd internally. // It returns true if the current doc is completed -func (df *DocumentFetcher) nextKey(ctx context.Context) (docDone bool, err error) { +func (df *DocumentFetcher) nextKey(ctx context.Context) (spanDone bool, err error) { // get the next kv from nextKV() for { - docDone, df.kv, err = df.nextKV() + spanDone, df.kv, err = df.nextKV() // handle any internal errors if err != nil { return false, err @@ -213,10 +213,10 @@ func (df *DocumentFetcher) nextKey(ctx context.Context) (docDone bool, err error if df.kv != nil && df.kv.Key.InstanceType != core.ValueKey { // We can only ready value values, if we escape the collection's value keys // then we must be done and can stop reading - docDone = true + spanDone = true } - df.kvEnd = docDone + df.kvEnd = spanDone if df.kvEnd { hasNextSpan, err := df.startNextSpan(ctx) if err != nil { diff --git a/docs/data_format_changes/i412-no-change-tests-updated.md b/docs/data_format_changes/i412-no-change-tests-updated.md new file mode 100644 index 0000000000..4146a4eeb7 --- /dev/null +++ b/docs/data_format_changes/i412-no-change-tests-updated.md @@ -0,0 +1,3 @@ +# Add tests for default properties + +This is is not a breaking change, but the test schema was updated which resulted in a few false-failures in the change-detector tool. Not relevent to defra users. diff --git a/tests/integration/query/inline_array/simple_test.go b/tests/integration/query/inline_array/simple_test.go index 69482603c9..a18fb2d38a 100644 --- a/tests/integration/query/inline_array/simple_test.go +++ b/tests/integration/query/inline_array/simple_test.go @@ -93,6 +93,28 @@ func TestQueryInlineArrayWithBooleans(t *testing.T) { func TestQueryInlineArrayWithIntegers(t *testing.T) { tests := []testUtils.QueryTestCase{ + { + Description: "Simple inline array with no filter, default integer array", + Query: `query { + users { + Name + FavouriteIntegers + } + }`, + Docs: map[int][]string{ + 0: { + `{ + "Name": "John" + }`, + }, + }, + Results: []map[string]interface{}{ + { + "Name": "John", + "FavouriteIntegers": nil, + }, + }, + }, { Description: "Simple inline array with no filter, nil integer array", Query: `query { diff --git a/tests/integration/query/one_to_many/with_cid_dockey_test.go b/tests/integration/query/one_to_many/with_cid_dockey_test.go index 93c3be776c..44866da51c 100644 --- a/tests/integration/query/one_to_many/with_cid_dockey_test.go +++ b/tests/integration/query/one_to_many/with_cid_dockey_test.go @@ -25,7 +25,7 @@ func TestQueryOneToManyWithUnknownCidAndDocKey(t *testing.T) { Description: "One-to-many relation query from one side with unknown cid and dockey", Query: `query { book ( - cid: "bafybeihtn2xjbjjqxeqp2uhwhvk3tmjfkaf2qtfqh5w5q3ews7ax2dc75a", + cid: "bafybeicgwjdyqyuntdop5ytpsfrqg5a4t2r25pfv6prfppl5ta5k5altca", dockey: "bae-fd541c25-229e-5280-b44b-e5c2af3e374d" ) { name diff --git a/tests/integration/query/simple/simple_test.go b/tests/integration/query/simple/simple_test.go index 904379be6d..ed2fc2a88d 100644 --- a/tests/integration/query/simple/simple_test.go +++ b/tests/integration/query/simple/simple_test.go @@ -121,3 +121,61 @@ func TestQuerySimpleWithUndefinedField(t *testing.T) { executeTestCase(t, test) } + +func TestQuerySimpleWithSomeDefaultValues(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Simple query with some default-value fields", + Query: `query { + users { + Name + Email + Age + HeightM + Verified + } + }`, + Docs: map[int][]string{ + 0: { + `{ + "Name": "John" + }`, + }, + }, + Results: []map[string]interface{}{ + { + "Name": "John", + "Email": nil, + "Age": nil, + "HeightM": nil, + "Verified": nil, + }, + }, + } + + executeTestCase(t, test) +} + +// This test documents undesirable behaviour and should be altered +// with https://github.com/sourcenetwork/defradb/issues/610. +// A document with nil fields should be returned. +func TestQuerySimpleWithDefaultValue(t *testing.T) { + test := testUtils.QueryTestCase{ + Description: "Simple query with default-value fields", + Query: `query { + users { + Name + Age + HeightM + Verified + } + }`, + Docs: map[int][]string{ + 0: { + `{ }`, + }, + }, + Results: []map[string]interface{}{}, + } + + executeTestCase(t, test) +} diff --git a/tests/integration/query/simple/utils.go b/tests/integration/query/simple/utils.go index 455a4977bb..5b508a1bb3 100644 --- a/tests/integration/query/simple/utils.go +++ b/tests/integration/query/simple/utils.go @@ -21,6 +21,7 @@ type dataMap = map[string]interface{} var userCollectionGQLSchema = (` type users { Name: String + Email: String Age: Int HeightM: Float Verified: Boolean diff --git a/tests/integration/query/simple/with_cid_dockey_test.go b/tests/integration/query/simple/with_cid_dockey_test.go index 251786583f..ae8626100b 100644 --- a/tests/integration/query/simple/with_cid_dockey_test.go +++ b/tests/integration/query/simple/with_cid_dockey_test.go @@ -73,7 +73,7 @@ func TestQuerySimpleWithCidAndDocKey(t *testing.T) { Description: "Simple query with cid and dockey", Query: `query { users ( - cid: "bafybeihtn2xjbjjqxeqp2uhwhvk3tmjfkaf2qtfqh5w5q3ews7ax2dc75a", + cid: "bafybeigriq4rcvsugsqiohxvtov2kvcmtqtldesobtx7vsjl556dhpliau", dockey: "bae-52b9170d-b77a-5887-b877-cbdbb99b009f" ) { Name @@ -102,7 +102,7 @@ func TestQuerySimpleWithUpdateAndFirstCidAndDocKey(t *testing.T) { Description: "Simple query with (first) cid and dockey", Query: `query { users ( - cid: "bafybeihtn2xjbjjqxeqp2uhwhvk3tmjfkaf2qtfqh5w5q3ews7ax2dc75a", + cid: "bafybeigriq4rcvsugsqiohxvtov2kvcmtqtldesobtx7vsjl556dhpliau", dockey: "bae-52b9170d-b77a-5887-b877-cbdbb99b009f" ) { Name @@ -143,7 +143,7 @@ func TestQuerySimpleWithUpdateAndLastCidAndDocKey(t *testing.T) { Description: "Simple query with (last) cid and dockey", Query: `query { users ( - cid: "bafybeiexy3vpswo6hzn6hw2ibw6rcsd6py5dxzcwxhr7rs7sd5jmzimo7q", + cid: "bafybeiehnzljzuijwsf474kwa2wdzptbiaxtszfgz2q4o3xnczfgwj22ei", dockey: "bae-52b9170d-b77a-5887-b877-cbdbb99b009f" ) { Name @@ -184,7 +184,7 @@ func TestQuerySimpleWithUpdateAndMiddleCidAndDocKey(t *testing.T) { Description: "Simple query with (middle) cid and dockey", Query: `query { users ( - cid: "bafybeif65sq3ajrocd5lieudu6lr6k4kufiiv3dbzn4qgtbal23hbnufwm", + cid: "bafybeiceb4p4bq7z3wrrr7q3jzsg44liuxvkov5xucxqwawx4hj6hx7tiy", dockey: "bae-52b9170d-b77a-5887-b877-cbdbb99b009f" ) { Name diff --git a/tests/integration/query/simple/with_version_test.go b/tests/integration/query/simple/with_version_test.go index 376d5fe614..7de0064b26 100644 --- a/tests/integration/query/simple/with_version_test.go +++ b/tests/integration/query/simple/with_version_test.go @@ -45,14 +45,14 @@ func TestQuerySimpleWithEmbeddedLatestCommit(t *testing.T) { "Age": uint64(21), "_version": []map[string]interface{}{ { - "cid": "bafybeihtn2xjbjjqxeqp2uhwhvk3tmjfkaf2qtfqh5w5q3ews7ax2dc75a", + "cid": "bafybeigriq4rcvsugsqiohxvtov2kvcmtqtldesobtx7vsjl556dhpliau", "links": []map[string]interface{}{ { "cid": "bafybeidst2mzxhdoh4ayjdjoh4vibo7vwnuoxk3xgyk5mzmep55jklni2a", "name": "Age", }, { - "cid": "bafybeidkse2jiqekdebh6zdq4zvyx4gzyrupujbtb6gd7qqdb4hj3pyaeq", + "cid": "bafybeicgwjdyqyuntdop5ytpsfrqg5a4t2r25pfv6prfppl5ta5k5altca", "name": "Name", }, }, @@ -97,14 +97,14 @@ func TestQuerySimpleWithMultipleAliasedEmbeddedLatestCommit(t *testing.T) { "Age": uint64(21), "_version": []map[string]interface{}{ { - "cid": "bafybeihtn2xjbjjqxeqp2uhwhvk3tmjfkaf2qtfqh5w5q3ews7ax2dc75a", + "cid": "bafybeigriq4rcvsugsqiohxvtov2kvcmtqtldesobtx7vsjl556dhpliau", "L1": []map[string]interface{}{ { "cid": "bafybeidst2mzxhdoh4ayjdjoh4vibo7vwnuoxk3xgyk5mzmep55jklni2a", "name": "Age", }, { - "cid": "bafybeidkse2jiqekdebh6zdq4zvyx4gzyrupujbtb6gd7qqdb4hj3pyaeq", + "cid": "bafybeicgwjdyqyuntdop5ytpsfrqg5a4t2r25pfv6prfppl5ta5k5altca", "name": "Name", }, },