forked from prisma/prisma1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request prisma#4522 from prisma/ErrorOnInvalidWhereUnique
Invalid WhereUnique Bug
- Loading branch information
Showing
3 changed files
with
92 additions
and
4 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
77 changes: 77 additions & 0 deletions
77
server/servers/api/src/test/scala/com/prisma/api/filters/nonEmbedded/WhereUniqueSpec.scala
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,77 @@ | ||
package com.prisma.api.filters.nonEmbedded | ||
|
||
import com.prisma.IgnoreMongo | ||
import com.prisma.api.ApiSpecBase | ||
import com.prisma.shared.models.Project | ||
import com.prisma.shared.schema_dsl.SchemaDsl | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class WhereUniqueSpec extends FlatSpec with Matchers with ApiSpecBase { | ||
|
||
val project: Project = SchemaDsl.fromStringV11() { """ | ||
|type User { | ||
| id: ID! @id | ||
| unique: Int! @unique | ||
| email: String! @unique | ||
|}""" } | ||
|
||
database.setup(project) | ||
|
||
server | ||
.query( | ||
s"""mutation{createUser( | ||
| data: { | ||
| unique:2, | ||
| email: "[email protected]" | ||
|}) | ||
|{id} | ||
|} | ||
""", | ||
project | ||
) | ||
|
||
// We cannot express that we expect exactly one field in the WhereUniqueInput in GraphQL, we therefore will error at runtime | ||
// and hint at the correct way to use the api | ||
|
||
"Providing 0 unique fields" should "error" in { | ||
server.queryThatMustFail( | ||
s"""query{user(where: {}){unique}}""", | ||
project, | ||
errorCode = 3040, | ||
errorContains = """You provided an invalid argument for the where selector on User. Please provide exactly one unique field and value.""" | ||
) | ||
} | ||
|
||
"Providing 1 unique field" should "work" in { | ||
server.query(s"""query{user(where: {email: "[email protected]"}){unique}}""", project).toString should be("""{"data":{"user":{"unique":2}}}""") | ||
} | ||
|
||
"Providing more than 1 unique field" should "error" in { | ||
server.queryThatMustFail( | ||
s"""query{user(where: {id:"wrong", email: "[email protected]"}){unique}}""", | ||
project, | ||
errorCode = 3045, | ||
errorContains = | ||
"""You provided more than one field for the unique selector on User. If you want that behavior you can use the many query and combine fields with AND / OR.""" | ||
) | ||
} | ||
|
||
"Using two unique fields with the many query" should "work with an implicit AND" in { | ||
server.query(s"""query{users(where: {unique:2, email: "[email protected]"}){unique}}""", project).toString should be("""{"data":{"users":[{"unique":2}]}}""") | ||
} | ||
|
||
"Using two unique fields with the many query" should "work with an explicit AND" in { | ||
server.query(s"""query{users(where: {AND: [{unique:2}, {email: "[email protected]"}]}){unique}}""", project).toString should be( | ||
"""{"data":{"users":[{"unique":2}]}}""") | ||
} | ||
|
||
"Using two unique fields with the many query" should "work with an explicit OR" taggedAs (IgnoreMongo) in { | ||
server.query(s"""query{users(where: {OR: [{unique:2}, {email: "does not exist"}]}){unique}}""", project).toString should be( | ||
"""{"data":{"users":[{"unique":2}]}}""") | ||
} | ||
|
||
"Using two unique fields with the many query" should "work with an explicit OR 2" taggedAs (IgnoreMongo) in { | ||
server.query(s"""query{users(where: {OR: [{unique:24235}, {email: "[email protected]"}]}){unique}}""", project).toString should be( | ||
"""{"data":{"users":[{"unique":2}]}}""") | ||
} | ||
} |