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-
91import XCTest
102import ApolloTestSupport
113import ApolloCodegenTestSupport
124@testable import ApolloCodegenLib
135
146class 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