-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): selection set with optional association
- Loading branch information
Showing
11 changed files
with
425 additions
and
6 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
189 changes: 189 additions & 0 deletions
189
...ore/AWSPluginsCoreTests/Model/GraphQLRequest/GraphQLRequestOptionalAssociationTests.swift
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,189 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import Amplify | ||
@testable import AmplifyTestCommon | ||
@testable import AWSPluginsCore | ||
|
||
class GraphQLRequestOptionalAssociationTests: XCTestCase { | ||
override func setUp() { | ||
ModelRegistry.register(modelType: User.self) | ||
ModelRegistry.register(modelType: UserFollowing.self) | ||
ModelRegistry.register(modelType: UserFollowers.self) | ||
} | ||
|
||
override func tearDown() { | ||
ModelRegistry.reset() | ||
} | ||
|
||
func testCreateUserGraphQLRequest() { | ||
let user = User(name: "username") | ||
let documentStringValue = """ | ||
mutation CreateUser($input: CreateUserInput!) { | ||
createUser(input: $input) { | ||
id | ||
name | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<User>.create(user) | ||
XCTAssertEqual(documentStringValue, request.document) | ||
|
||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
guard let input = variables["input"] as? [String: Any] else { | ||
XCTFail("The document variables property doesn't contain a valid input") | ||
return | ||
} | ||
XCTAssertEqual(input["id"] as? String, user.id) | ||
XCTAssertEqual(input["name"] as? String, user.name) | ||
} | ||
|
||
func testCreateUserFollowingGraphQLRequest() { | ||
let user1 = User(name: "user1") | ||
let user2 = User(name: "user2") | ||
let userFollowing = UserFollowing(user: user1, followingUser: user2) | ||
let documentStringValue = """ | ||
mutation CreateUserFollowing($input: CreateUserFollowingInput!) { | ||
createUserFollowing(input: $input) { | ||
id | ||
followingUser { | ||
id | ||
name | ||
__typename | ||
} | ||
user { | ||
id | ||
name | ||
__typename | ||
} | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<User>.create(userFollowing) | ||
XCTAssertEqual(documentStringValue, request.document) | ||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
guard let input = variables["input"] as? [String: Any] else { | ||
XCTFail("The document variables property doesn't contain a valid input") | ||
return | ||
} | ||
XCTAssertEqual(input["id"] as? String, userFollowing.id) | ||
XCTAssertEqual(input["userFollowingUserId"] as? String, user1.id) | ||
XCTAssertEqual(input["userFollowingFollowingUserId"] as? String, user2.id) | ||
} | ||
|
||
func testQueryUserFollowingGraphQLRequest() { | ||
let documentStringValue = """ | ||
query GetUserFollowing($id: ID!) { | ||
getUserFollowing(id: $id) { | ||
id | ||
followingUser { | ||
id | ||
name | ||
__typename | ||
} | ||
user { | ||
id | ||
name | ||
__typename | ||
} | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<UserFollowing>.get(UserFollowing.self, byId: "id") | ||
XCTAssertEqual(documentStringValue, request.document) | ||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
XCTAssertEqual(variables["id"] as? String, "id") | ||
} | ||
|
||
func testQueryUserGraphQLRequest() { | ||
let documentStringValue = """ | ||
query GetUser($id: ID!) { | ||
getUser(id: $id) { | ||
id | ||
name | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<UserFollowing>.get(User.self, byId: "id") | ||
XCTAssertEqual(documentStringValue, request.document) | ||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
XCTAssertEqual(variables["id"] as? String, "id") | ||
} | ||
|
||
func testListUserFollowingGraphQLRequest() { | ||
let documentStringValue = """ | ||
query ListUserFollowings($limit: Int) { | ||
listUserFollowings(limit: $limit) { | ||
items { | ||
id | ||
followingUser { | ||
id | ||
name | ||
__typename | ||
} | ||
user { | ||
id | ||
name | ||
__typename | ||
} | ||
__typename | ||
} | ||
nextToken | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<UserFollowing>.list(UserFollowing.self) | ||
XCTAssertEqual(documentStringValue, request.document) | ||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
XCTAssertEqual(variables["limit"] as? Int, 1_000) | ||
} | ||
|
||
func testSubscribeToUserFollowingGraphQLRequest() { | ||
let documentStringValue = """ | ||
subscription OnCreateUserFollowing { | ||
onCreateUserFollowing { | ||
id | ||
followingUser { | ||
id | ||
name | ||
__typename | ||
} | ||
user { | ||
id | ||
name | ||
__typename | ||
} | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<UserFollowing>.subscription(of: UserFollowing.self, type: .onCreate) | ||
XCTAssertEqual(documentStringValue, request.document) | ||
XCTAssertNil(request.variables) | ||
} | ||
} |
Oops, something went wrong.