forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApolloSchemaTests.swift
More file actions
124 lines (95 loc) · 4.61 KB
/
ApolloSchemaTests.swift
File metadata and controls
124 lines (95 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// ApolloSchemaTests.swift
// ApolloCodegenTests
//
// Created by Ellen Shapiro on 10/7/19.
// Copyright © 2019 Apollo GraphQL. All rights reserved.
//
import XCTest
@testable import ApolloCodegenLib
class ApolloSchemaTests: XCTestCase {
private lazy var endpointURL = URL(string: "http://localhost:8080/graphql")!
func testCreatingOptionsWithDefaultParameters() throws {
let sourceRoot = CodegenTestHelper.sourceRootURL()
let options = ApolloSchemaOptions(endpointURL: self.endpointURL,
outputFolderURL: sourceRoot)
let expectedOutputURL = sourceRoot.appendingPathComponent("schema.json")
XCTAssertEqual(options.endpointURL, self.endpointURL)
XCTAssertEqual(options.outputURL, expectedOutputURL)
XCTAssertNil(options.apiKey)
XCTAssertNil(options.header)
XCTAssertEqual(options.arguments, [
"client:download-schema",
"--endpoint=http://localhost:8080/graphql",
"'\(expectedOutputURL.path)'"
])
}
func testCreatingOptionsWithAllParameters() throws {
let sourceRoot = CodegenTestHelper.sourceRootURL()
let apiKey = "Fake_API_Key"
let header = "Authorization: Bearer tokenGoesHere"
let options = ApolloSchemaOptions(schemaFileName: "different_name",
schemaFileType: .schemaDefinitionLanguage,
apiKey: apiKey,
endpointURL: self.endpointURL,
header: header,
outputFolderURL: sourceRoot)
XCTAssertEqual(options.apiKey, apiKey)
XCTAssertEqual(options.endpointURL, self.endpointURL)
XCTAssertEqual(options.header, header)
let expectedOutputURL = sourceRoot.appendingPathComponent("different_name.graphql")
XCTAssertEqual(options.outputURL, expectedOutputURL)
XCTAssertEqual(options.arguments, [
"client:download-schema",
"--endpoint=http://localhost:8080/graphql",
"--key=\(apiKey)",
"'\(expectedOutputURL.path)'",
"--header='\(header)'"
])
}
func testDownloadingSchemaAsJSON() throws {
let testOutputFolderURL = CodegenTestHelper.outputFolderURL()
let options = ApolloSchemaOptions(endpointURL: self.endpointURL,
outputFolderURL: testOutputFolderURL)
// Delete anything existing at the output URL
try FileManager.default.apollo_deleteFile(at: options.outputURL)
XCTAssertFalse(FileManager.default.apollo_fileExists(at: options.outputURL))
let cliFolderURL = CodegenTestHelper.cliFolderURL()
_ = try ApolloSchemaDownloader.run(with: cliFolderURL,
options: options)
// Does the file now exist?
XCTAssertTrue(FileManager.default.apollo_fileExists(at: options.outputURL))
// Is it non-empty?
let data = try Data(contentsOf: options.outputURL)
XCTAssertFalse(data.isEmpty)
// Is it JSON?
let json = try XCTUnwrap(JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable:Any])
// Is it schema json?
_ = try XCTUnwrap(json["__schema"])
// OK delete it now
try FileManager.default.apollo_deleteFile(at: options.outputURL)
XCTAssertFalse(FileManager.default.apollo_fileExists(at: options.outputURL))
}
func testDownloadingSchemaInSchemaDefinitionLanguage() throws {
let testOutputFolderURL = CodegenTestHelper.outputFolderURL()
let options = ApolloSchemaOptions(schemaFileType: .schemaDefinitionLanguage,
endpointURL: self.endpointURL,
outputFolderURL: testOutputFolderURL)
// Delete anything existing at the output URL
try FileManager.default.apollo_deleteFile(at: options.outputURL)
XCTAssertFalse(FileManager.default.apollo_fileExists(at: options.outputURL))
let cliFolderURL = CodegenTestHelper.cliFolderURL()
print(try ApolloSchemaDownloader.run(with: cliFolderURL,
options: options))
// Does the file now exist?
XCTAssertTrue(FileManager.default.apollo_fileExists(at: options.outputURL))
// Is it non-empty?
let data = try Data(contentsOf: options.outputURL)
XCTAssertFalse(data.isEmpty)
// It should not be JSON
XCTAssertNil(try? JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable:Any])
// OK delete it now
try FileManager.default.apollo_deleteFile(at: options.outputURL)
XCTAssertFalse(FileManager.default.apollo_fileExists(at: options.outputURL))
}
}