Skip to content

Commit 43786c1

Browse files
committed
test case odds and ends
1 parent 91b18ee commit 43786c1

File tree

10 files changed

+272
-21
lines changed

10 files changed

+272
-21
lines changed

Sources/OpenAPIKit/Content.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ extension OpenAPI.Content {
228228
case "encoding":
229229
self = .encoding
230230
default:
231-
self = .extended(stringValue)
231+
self = .extendedKey(for: stringValue)
232232
}
233233
}
234234

Sources/OpenAPIKit/Document.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,6 @@ extension OpenAPI.Document.Info.License {
118118
}
119119
}
120120

121-
extension OpenAPI {
122-
public struct ExternalDoc: Codable, Equatable {
123-
public let description: String?
124-
public let url: URL
125-
126-
public init(description: String? = nil,
127-
url: URL) {
128-
self.description = description
129-
self.url = url
130-
}
131-
}
132-
}
133-
134121
// MARK: - Codable
135122

136123
extension OpenAPI.Document {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// ExternalDoc.swift
3+
//
4+
//
5+
// Created by Mathew Polzin on 11/2/19.
6+
//
7+
8+
import Foundation
9+
10+
extension OpenAPI {
11+
public struct ExternalDoc: Codable, Equatable {
12+
public let description: String?
13+
public let url: URL
14+
15+
public init(description: String? = nil,
16+
url: URL) {
17+
self.description = description
18+
self.url = url
19+
}
20+
}
21+
}

Sources/OpenAPIKit/Node Conformances/NodeProtocols.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension RawOpenAPINodeType where Self: RawRepresentable, RawValue: OpenAPINode
5252
/// necessary so that one type can conditionally provide a
5353
/// schema and then (under different conditions) provide a
5454
/// different schema. The "different" conditions have to do
55-
/// with Optionality, hence the name of this protocol.
55+
/// with Optionality (a wrapping type), hence the name of this protocol.
5656
public protocol WrappedRawOpenAPIType {
5757
static func wrappedOpenAPINode() throws -> JSONSchema
5858
}
@@ -62,7 +62,7 @@ public protocol WrappedRawOpenAPIType {
6262
/// necessary so that one type can conditionally provide a
6363
/// schema and then (under different conditions) provide a
6464
/// different schema. The "different" conditions have to do
65-
/// with Optionality, hence the name of this protocol.
65+
/// with Optionality (a wrapping type), hence the name of this protocol.
6666
public protocol DoubleWrappedRawOpenAPIType {
6767
// NOTE: This is definitely a rabbit hole... hopefully I
6868
// will realize I've been missing something obvious

Sources/OpenAPIKit/Path Item/Parameter.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ extension OpenAPI.PathItem {
5555
self.deprecated = deprecated
5656
}
5757

58+
public init(name: String,
59+
parameterLocation: Location,
60+
schemaReference: JSONReference<OpenAPI.Components, JSONSchema>,
61+
description: String? = nil,
62+
deprecated: Bool = false) {
63+
self.name = name
64+
self.parameterLocation = parameterLocation
65+
self.schemaOrContent = .init(Schema(schemaReference: schemaReference, style: .default(for: parameterLocation)))
66+
self.description = description
67+
self.deprecated = deprecated
68+
}
69+
5870
public init(name: String,
5971
parameterLocation: Location,
6072
content: OpenAPI.Content.Map,

Tests/OpenAPIKitTests/ContentTests.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ import AnyCodable
1212

1313
final class ContentTests: XCTestCase {
1414
func test_init() {
15-
let _ = OpenAPI.Content(schema: .init(.external("hello.json#/world")))
15+
let t1 = OpenAPI.Content(schema: .init(.external("hello.json#/world")))
16+
XCTAssertNotNil(t1.schema.a)
17+
XCTAssertNil(t1.schema.b)
1618

17-
let _ = OpenAPI.Content(schema: .init(.string))
19+
let t2 = OpenAPI.Content(schema: .init(.string))
20+
XCTAssertNotNil(t2.schema.b)
21+
XCTAssertNil(t2.schema.a)
22+
23+
let t3 = OpenAPI.Content(schemaReference: .external("hello.json#/world"))
24+
XCTAssertNotNil(t3.schema.a)
25+
XCTAssertNil(t3.schema.b)
1826

1927
let withExample = OpenAPI.Content(schema: .init(.string),
2028
example: "hello",
@@ -33,6 +41,16 @@ final class ContentTests: XCTestCase {
3341
XCTAssertNotNil(withExamples.examples)
3442
XCTAssertEqual(withExamples.example?.value as? String, "pick me")
3543

44+
let t4 = OpenAPI.Content(schemaReference: .external("hello.json#/world"),
45+
examples: nil)
46+
XCTAssertNotNil(t4.schema.a)
47+
XCTAssertNil(t4.schema.b)
48+
49+
let t5 = OpenAPI.Content(schema: .string,
50+
examples: nil)
51+
XCTAssertNotNil(t5.schema.b)
52+
XCTAssertNil(t5.schema.a)
53+
3654
let _ = OpenAPI.Content(schema: .init(.string),
3755
example: nil,
3856
encoding: [

Tests/OpenAPIKitTests/EaseOfUseTests.swift

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// EaseOfUseTests.swift
2+
// DeclarativeEaseOfUseTests.swift
33
//
44
//
55
// Created by Mathew Polzin on 10/27/19.
@@ -9,7 +9,7 @@ import Foundation
99
import OpenAPIKit
1010
import XCTest
1111

12-
final class EaseOfUseTests: XCTestCase {
12+
final class DeclarativeEaseOfUseTests: XCTestCase {
1313
func test_wholeBoi() {
1414
let _ = OpenAPI.Document(
1515
openAPIVersion: .v3_0_0,
@@ -140,4 +140,65 @@ final class EaseOfUseTests: XCTestCase {
140140
)
141141
)
142142
}
143+
144+
func test_JSONSchema() {
145+
/*
146+
147+
{
148+
"data": [
149+
{
150+
"id": "1234",
151+
"type": "test_thing",
152+
"attributes": {
153+
"name": "Thing",
154+
"age": 10,
155+
"created_at": "2019-11-03T05:24:55Z"
156+
},
157+
"relationships": {
158+
"other": {
159+
"data": null
160+
}
161+
}
162+
}
163+
]
164+
}
165+
166+
*/
167+
let _ = JSONSchema.object(
168+
properties: [
169+
"data": .array(
170+
items: .object(
171+
properties: [
172+
"id": .string,
173+
"type": .string(allowedValues: ["test_thing"]),
174+
175+
"attributes": .object(
176+
properties: [
177+
"name": .string,
178+
"age": .integer,
179+
"created_at": .string(format: .dateTime)
180+
]
181+
),
182+
183+
"relationships": .object(
184+
properties: [
185+
"other": .object(
186+
properties: [
187+
"data": .object(
188+
nullable: true,
189+
properties: [
190+
"type": .string,
191+
"id": .string
192+
]
193+
)
194+
]
195+
)
196+
]
197+
)
198+
]
199+
)
200+
)
201+
]
202+
)
203+
}
143204
}

Tests/OpenAPIKitTests/ExternalDocTests.swift

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,72 @@ import XCTest
99
import OpenAPIKit
1010

1111
final class ExternalDocTests: XCTestCase {
12-
// TODO: write tests
12+
func test_init() {
13+
let t1 = OpenAPI.ExternalDoc(url: URL(string: "http://google.com")!)
14+
XCTAssertNil(t1.description)
15+
16+
let t2 = OpenAPI.ExternalDoc(description: "hello world",
17+
url: URL(string: "http://google.com")!)
18+
XCTAssertEqual(t2.description, "hello world")
19+
}
20+
}
21+
22+
// MARK: - Codable
23+
@available(OSX 10.13, *)
24+
extension ExternalDocTests {
25+
func test_descriptionAndUrl_encode() {
26+
let externalDoc = OpenAPI.ExternalDoc(description: "hello world",
27+
url: URL(string: "http://google.com")!)
28+
29+
let encodedExternalDoc = try! testStringFromEncoding(of: externalDoc)
30+
31+
XCTAssertEqual(encodedExternalDoc,
32+
"""
33+
{
34+
"description" : "hello world",
35+
"url" : "http:\\/\\/google.com"
36+
}
37+
"""
38+
)
39+
}
40+
41+
func test_descriptionAndUrl_decode() {
42+
let externalDocsData =
43+
"""
44+
{
45+
"description" : "hello world",
46+
"url" : "http:\\/\\/google.com"
47+
}
48+
""".data(using: .utf8)!
49+
let externalDocs = try! testDecoder.decode(OpenAPI.ExternalDoc.self, from: externalDocsData)
50+
51+
XCTAssertEqual(externalDocs, OpenAPI.ExternalDoc(description: "hello world",
52+
url: URL(string: "http://google.com")!))
53+
}
54+
55+
func test_onlyUrl_encode() {
56+
let externalDoc = OpenAPI.ExternalDoc(url: URL(string: "http://google.com")!)
57+
58+
let encodedExternalDoc = try! testStringFromEncoding(of: externalDoc)
59+
60+
XCTAssertEqual(encodedExternalDoc,
61+
"""
62+
{
63+
"url" : "http:\\/\\/google.com"
64+
}
65+
"""
66+
)
67+
}
68+
69+
func test_onlyUrl_decode() {
70+
let externalDocsData =
71+
"""
72+
{
73+
"url" : "http:\\/\\/google.com"
74+
}
75+
""".data(using: .utf8)!
76+
let externalDocs = try! testDecoder.decode(OpenAPI.ExternalDoc.self, from: externalDocsData)
77+
78+
XCTAssertEqual(externalDocs, OpenAPI.ExternalDoc(url: URL(string: "http://google.com")!))
79+
}
1380
}

Tests/OpenAPIKitTests/ResponseTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ final class ResponseTests: XCTestCase {
2626
XCTAssertEqual(r2.headers?["hello"]?.a, header)
2727
XCTAssertEqual(r2.content, [.json: content])
2828
}
29+
30+
func test_responseMap() {
31+
let responseMap: OpenAPI.Response.Map = [
32+
200: .response(.init(description: "hello world", content: [:])),
33+
404: .response(reference: .external("hello.json#/world"))
34+
]
35+
36+
XCTAssertNotNil(responseMap[200]?.a)
37+
XCTAssertNil(responseMap[200]?.b)
38+
XCTAssertNotNil(responseMap[404]?.b)
39+
XCTAssertNil(responseMap[404]?.a)
40+
}
2941
}
3042

3143
// MARK: Response Status Code

0 commit comments

Comments
 (0)