Skip to content

Commit 75b7278

Browse files
committed
Add schema test back to ApolloCodegenTests using static JSON schema
1 parent 062225c commit 75b7278

4 files changed

Lines changed: 57 additions & 73 deletions

File tree

Apollo.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
E61DD76526D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E61DD76426D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift */; };
253253
E6630B8C26F0639B002D9E41 /* MockNetworkSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6D79AB926EC05290094434A /* MockNetworkSession.swift */; };
254254
E6630B8E26F071F9002D9E41 /* SchemaRegistryApolloSchemaDownloaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6630B8D26F071F9002D9E41 /* SchemaRegistryApolloSchemaDownloaderTests.swift */; };
255+
E6C4267B26F16CB400904AD2 /* introspection_response.json in Resources */ = {isa = PBXBuildFile; fileRef = E6C4267A26F16CB400904AD2 /* introspection_response.json */; };
255256
E6D79AB826E9D59C0094434A /* URLDownloaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6D79AB626E97D0D0094434A /* URLDownloaderTests.swift */; };
256257
E86D8E05214B32FD0028EFE1 /* JSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E86D8E03214B32DA0028EFE1 /* JSONTests.swift */; };
257258
F16D083C21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */; };
@@ -818,6 +819,7 @@
818819
E616B6D026C3335600DB049E /* ExecutionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExecutionTests.swift; sourceTree = "<group>"; };
819820
E61DD76426D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLiteDotSwiftDatabaseBehaviorTests.swift; sourceTree = "<group>"; };
820821
E6630B8D26F071F9002D9E41 /* SchemaRegistryApolloSchemaDownloaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchemaRegistryApolloSchemaDownloaderTests.swift; sourceTree = "<group>"; };
822+
E6C4267A26F16CB400904AD2 /* introspection_response.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = introspection_response.json; sourceTree = "<group>"; };
821823
E6D79AB626E97D0D0094434A /* URLDownloaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLDownloaderTests.swift; sourceTree = "<group>"; };
822824
E6D79AB926EC05290094434A /* MockNetworkSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNetworkSession.swift; sourceTree = "<group>"; };
823825
E86D8E03214B32DA0028EFE1 /* JSONTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONTests.swift; sourceTree = "<group>"; };
@@ -1238,6 +1240,7 @@
12381240
children = (
12391241
9F1A96AE258F367100A06EEB /* Frontend */,
12401242
9B8110A623A1994000688AC4 /* SourcePackages */,
1243+
E6BE04ED26F11B3500CF858D /* Resources */,
12411244
9BAEEC16234C275600808306 /* ApolloSchemaTests.swift */,
12421245
9BAEEC18234C297800808306 /* ApolloCodegenTests.swift */,
12431246
9B518C88235F8AD4004C426D /* CLIDownloaderTests.swift */,
@@ -1764,6 +1767,14 @@
17641767
path = DefaultImplementation;
17651768
sourceTree = "<group>";
17661769
};
1770+
E6BE04ED26F11B3500CF858D /* Resources */ = {
1771+
isa = PBXGroup;
1772+
children = (
1773+
E6C4267A26F16CB400904AD2 /* introspection_response.json */,
1774+
);
1775+
path = Resources;
1776+
sourceTree = "<group>";
1777+
};
17671778
/* End PBXGroup section */
17681779

17691780
/* Begin PBXHeadersBuildPhase section */
@@ -2309,6 +2320,7 @@
23092320
isa = PBXResourcesBuildPhase;
23102321
buildActionMask = 2147483647;
23112322
files = (
2323+
E6C4267B26F16CB400904AD2 /* introspection_response.json in Resources */,
23122324
);
23132325
runOnlyForDeploymentPostprocessing = 0;
23142326
};

Sources/ApolloCodegenLib/ApolloSchemaDownloader.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ public struct ApolloSchemaDownloader {
255255
try URLDownloader().downloadSynchronously(with: urlRequest,
256256
to: jsonOutputURL,
257257
timeout: configuration.downloadTimeout)
258-
259258

260259
try convertFromIntrospectionJSONToSDLFile(jsonFileURL: jsonOutputURL, configuration: configuration)
261260

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,46 @@
1-
//
2-
// ApolloSchemaTests.swift
3-
// ApolloCodegenTests
4-
//
5-
// Created by Ellen Shapiro on 10/7/19.
6-
// Copyright © 2019 Apollo GraphQL. All rights reserved.
7-
//
8-
91
import XCTest
102
import ApolloTestSupport
113
import ApolloCodegenTestSupport
124
@testable import ApolloCodegenLib
135

146
class ApolloSchemaTests: XCTestCase {
15-
16-
override func tearDownWithError() throws {
17-
try FileManager.default.apollo.deleteFile(at: self.defaultOutputURL)
18-
try FileManager.default.apollo.deleteFile(at: self.intermediateOutputURL)
19-
try super.tearDownWithError()
20-
}
21-
227
private var defaultOutputURL: URL {
238
return CodegenTestHelper.schemaFolderURL()
249
.appendingPathComponent("schema.graphqls")
2510
}
26-
11+
2712
private var intermediateOutputURL: URL {
2813
return CodegenTestHelper.schemaFolderURL().appendingPathComponent("registry_response.json")
2914
}
15+
16+
override func tearDownWithError() throws {
17+
try FileManager.default.apollo.deleteFile(at: self.defaultOutputURL)
18+
try FileManager.default.apollo.deleteFile(at: self.intermediateOutputURL)
19+
20+
try super.tearDownWithError()
21+
}
3022

31-
func testCreatingSchemaOptions_forIntrospectionDownload_usingDefaultParameters() throws {
32-
let options = ApolloSchemaDownloadConfiguration(using: .introspection(endpointURL: TestURL.mockPort8080.url),
33-
outputFolderURL: CodegenTestHelper.schemaFolderURL())
23+
func testCreatingSchemaDownloadConfiguration_forIntrospectionDownload_usingDefaultParameters() throws {
24+
let configuration = ApolloSchemaDownloadConfiguration(using: .introspection(endpointURL: TestURL.mockPort8080.url),
25+
outputFolderURL: CodegenTestHelper.schemaFolderURL())
3426

35-
XCTAssertEqual(options.downloadMethod, .introspection(endpointURL: TestURL.mockPort8080.url))
36-
XCTAssertEqual(options.outputURL, self.defaultOutputURL)
37-
XCTAssertTrue(options.headers.isEmpty)
27+
XCTAssertEqual(configuration.downloadMethod, .introspection(endpointURL: TestURL.mockPort8080.url))
28+
XCTAssertEqual(configuration.outputURL, self.defaultOutputURL)
29+
XCTAssertTrue(configuration.headers.isEmpty)
3830
}
3931

40-
func testCreatingSchemaOptions_forRegistryDownload_usingDefaultParameters() throws {
41-
let settings = ApolloSchemaDownloadConfiguration.DownloadMethod.RegistrySettings(apiKey: "Fake_API_Key", graphID: "Fake_Graph_ID")
42-
let options = ApolloSchemaDownloadConfiguration(using: .registry(settings),
43-
outputFolderURL: CodegenTestHelper.schemaFolderURL())
32+
func testCreatingSchemaDownloadConfiguration_forRegistryDownload_usingDefaultParameters() throws {
33+
let settings = ApolloSchemaDownloadConfiguration.DownloadMethod.RegistrySettings(apiKey: "Fake_API_Key",
34+
graphID: "Fake_Graph_ID")
35+
let configuration = ApolloSchemaDownloadConfiguration(using: .registry(settings),
36+
outputFolderURL: CodegenTestHelper.schemaFolderURL())
4437

45-
XCTAssertEqual(options.downloadMethod, .registry(settings))
46-
XCTAssertEqual(options.outputURL, self.defaultOutputURL)
47-
XCTAssertTrue(options.headers.isEmpty)
38+
XCTAssertEqual(configuration.downloadMethod, .registry(settings))
39+
XCTAssertEqual(configuration.outputURL, self.defaultOutputURL)
40+
XCTAssertTrue(configuration.headers.isEmpty)
4841
}
4942

50-
func testCreatingSchemaOptions_forRegistryDownload_usingAllParameters() throws {
43+
func testCreatingSchemaDownloadConfiguration_forRegistryDownload_usingAllParameters() throws {
5144
let sourceRoot = CodegenTestHelper.sourceRootURL()
5245
let settings = ApolloSchemaDownloadConfiguration.DownloadMethod.RegistrySettings(apiKey: "Fake_API_Key",
5346
graphID: "Fake_Graph_ID",
@@ -58,59 +51,38 @@ class ApolloSchemaTests: XCTestCase {
5851
]
5952

6053
let schemaFileName = "different_name"
61-
let options = ApolloSchemaDownloadConfiguration(using: .registry(settings),
62-
headers: headers,
63-
outputFolderURL: sourceRoot,
64-
schemaFilename: schemaFileName)
54+
let configuration = ApolloSchemaDownloadConfiguration(using: .registry(settings),
55+
headers: headers,
56+
outputFolderURL: sourceRoot,
57+
schemaFilename: schemaFileName)
6558

66-
XCTAssertEqual(options.downloadMethod, .registry(settings))
67-
XCTAssertEqual(options.headers, headers)
59+
XCTAssertEqual(configuration.downloadMethod, .registry(settings))
60+
XCTAssertEqual(configuration.headers, headers)
6861

6962
let expectedOutputURL = sourceRoot.appendingPathComponent("\(schemaFileName).graphqls")
70-
XCTAssertEqual(options.outputURL, expectedOutputURL)
71-
}
72-
73-
func testDownloading_usingIntrospection_shouldOutputSchema() throws {
74-
XCTAssertFalse(FileManager.default.apollo.fileExists(at: self.defaultOutputURL))
75-
76-
let configuration = ApolloSchemaDownloadConfiguration(using: .introspection(endpointURL: TestURL.mockPort8080.url),
77-
timeout: 60,
78-
outputFolderURL: CodegenTestHelper.schemaFolderURL())
79-
try ApolloSchemaDownloader.fetch(with: configuration)
80-
81-
// Has the file been downloaded?
82-
XCTAssertTrue(FileManager.default.apollo.fileExists(at: self.defaultOutputURL))
83-
84-
// Can it be turned into the expected schema?
85-
let frontend = try ApolloCodegenFrontend()
86-
let stringOutput = try String(contentsOf: self.defaultOutputURL, encoding: .utf8)
87-
let schema = try frontend.loadSchemaFromIntrospectionResult(stringOutput)
88-
let episodeType = try schema.getType(named: "Episode")
89-
90-
XCTAssertEqual(episodeType?.name, "Episode")
63+
XCTAssertEqual(configuration.outputURL, expectedOutputURL)
9164
}
92-
93-
func testDownloading_fromSchemaRegistry_shouldOutputSchema() throws {
94-
XCTAssertFalse(FileManager.default.apollo.fileExists(at: self.defaultOutputURL))
9565

96-
guard let apiKey = ProcessInfo.processInfo.environment["REGISTRY_API_KEY"] else {
97-
throw XCTSkip("No API key could be fetched from the environment to test downloading from the schema registry")
66+
func testFormatConversion_givenIntrospectionJSON_shouldOutputValidSDL() throws {
67+
let bundle = Bundle(for: type(of: self))
68+
guard let url = bundle.url(forResource: "introspection_response", withExtension: "json") else {
69+
throw XCTFailure("Missing resource file!", file: #file, line: #line)
9870
}
99-
100-
let settings = ApolloSchemaDownloadConfiguration.DownloadMethod.RegistrySettings(apiKey: apiKey, graphID: "Apollo-Fullstack-8zo5jl")
101-
let configuration = ApolloSchemaDownloadConfiguration(using: .registry(settings),
102-
timeout: 60,
71+
let configuration = ApolloSchemaDownloadConfiguration(using: .introspection(endpointURL: TestURL.mockPort8080.url),
10372
outputFolderURL: CodegenTestHelper.schemaFolderURL())
10473

105-
try ApolloSchemaDownloader.fetch(with: configuration)
106-
XCTAssertTrue(FileManager.default.apollo.fileExists(at: self.defaultOutputURL))
74+
try ApolloSchemaDownloader.convertFromIntrospectionJSONToSDLFile(jsonFileURL: url, configuration: configuration)
75+
XCTAssertTrue(FileManager.default.apollo.fileExists(at: configuration.outputURL))
10776

108-
// Can it be turned into the expected schema?
10977
let frontend = try ApolloCodegenFrontend()
110-
let source = try frontend.makeSource(from: self.defaultOutputURL)
78+
let source = try frontend.makeSource(from: configuration.outputURL)
11179
let schema = try frontend.loadSchemaFromSDL(source)
112-
let rocketType = try schema.getType(named: "Rocket")
113-
XCTAssertEqual(rocketType?.name, "Rocket")
80+
81+
let authorType = try schema.getType(named: "Author")
82+
XCTAssertEqual(authorType?.name, "Author")
83+
84+
let postType = try schema.getType(named: "Post")
85+
XCTAssertEqual(postType?.name, "Post")
11486
}
11587
}
11688

0 commit comments

Comments
 (0)