-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error if creating 1-1 from wrong side
- Loading branch information
1 parent
d80e00b
commit 2c0991b
Showing
10 changed files
with
464 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
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
90 changes: 90 additions & 0 deletions
90
tests/integration/collection/create/one_to_one/save_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,90 @@ | ||
// Copyright 2022 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 create | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCollectionCreateSaveErrorsGivenRelationSetOnWrongSide(t *testing.T) { | ||
doc, err := client.NewDocFromJSON( | ||
[]byte( | ||
`{ | ||
"name": "Painted House", | ||
"author_id": "bae-fd541c25-229e-5280-b44b-e5c2af3e374d" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
test := testUtils.TestCase{ | ||
CollectionCalls: map[string][]func(client.Collection) error{ | ||
"book": []func(c client.Collection) error{ | ||
func(c client.Collection) error { | ||
return c.Save(context.Background(), doc) | ||
}, | ||
}, | ||
}, | ||
ExpectedError: "The given field does not exist", | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestCollectionCreateSaveCreatesDoc(t *testing.T) { | ||
doc, err := client.NewDocFromJSON( | ||
[]byte( | ||
`{ | ||
"name": "John", | ||
"published_id": "bae-fd541c25-229e-5280-b44b-e5c2af3e374d" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
test := testUtils.TestCase{ | ||
CollectionCalls: map[string][]func(client.Collection) error{ | ||
"author": []func(c client.Collection) error{ | ||
func(c client.Collection) error { | ||
err := c.Save(context.Background(), doc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d, err := c.Get(context.Background(), doc.Key()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name, err := d.Get("name") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
assert.Equal(t, "John", name) | ||
|
||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} |
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,36 @@ | ||
// Copyright 2022 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 create | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
) | ||
|
||
var schema = ` | ||
type book { | ||
name: String | ||
rating: Float | ||
author: author | ||
} | ||
type author { | ||
name: String | ||
age: Int | ||
verified: Boolean | ||
published: book @primary | ||
} | ||
` | ||
|
||
func executeTestCase(t *testing.T, test testUtils.TestCase) { | ||
testUtils.ExecuteQueryTestCase(t, schema, test) | ||
} |
109 changes: 109 additions & 0 deletions
109
tests/integration/collection/update/one_to_one/save_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,109 @@ | ||
// Copyright 2022 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 update | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUpdateOneToOneSaveErrorsGivenWrongSideOfRelation(t *testing.T) { | ||
doc, err := client.NewDocFromJSON( | ||
[]byte( | ||
`{ | ||
"name": "Painted House" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
err = doc.SetWithJSON( | ||
[]byte( | ||
`{ | ||
"author_id": "ValueDoesntMatter" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
test := testUtils.TestCase{ | ||
Docs: map[string][]string{ | ||
"book": { | ||
`{ | ||
"name": "Painted House" | ||
}`, | ||
}, | ||
}, | ||
CollectionCalls: map[string][]func(client.Collection) error{ | ||
"book": []func(c client.Collection) error{ | ||
func(c client.Collection) error { | ||
return c.Save(context.Background(), doc) | ||
}, | ||
}, | ||
}, | ||
ExpectedError: "The given field does not exist", | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
// Note: This test should probably not pass, as it contains a | ||
// reference to a document that doesnt exist. | ||
func TestUpdateOneToOneSavesGivenNewRelationValue(t *testing.T) { | ||
doc, err := client.NewDocFromJSON( | ||
[]byte( | ||
`{ | ||
"name": "John Grisham" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
err = doc.SetWithJSON( | ||
[]byte( | ||
`{ | ||
"published_id": "bae-fd541c25-229e-5280-b44b-e5c2af3e374d" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
test := testUtils.TestCase{ | ||
Docs: map[string][]string{ | ||
"author": { | ||
`{ | ||
"name": "John Grisham" | ||
}`, | ||
}, | ||
}, | ||
CollectionCalls: map[string][]func(client.Collection) error{ | ||
"author": []func(c client.Collection) error{ | ||
func(c client.Collection) error { | ||
return c.Save(context.Background(), doc) | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} |
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,36 @@ | ||
// Copyright 2022 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 update | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
) | ||
|
||
var schema = ` | ||
type book { | ||
name: String | ||
rating: Float | ||
author: author | ||
} | ||
type author { | ||
name: String | ||
age: Int | ||
verified: Boolean | ||
published: book @primary | ||
} | ||
` | ||
|
||
func executeTestCase(t *testing.T, test testUtils.TestCase) { | ||
testUtils.ExecuteQueryTestCase(t, schema, test) | ||
} |
Oops, something went wrong.