forked from GraphQLSwift/DataLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLoaderAbuseTests.swift
More file actions
106 lines (84 loc) · 3.45 KB
/
DataLoaderAbuseTests.swift
File metadata and controls
106 lines (84 loc) · 3.45 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
@testable import DataLoader
import NIOPosix
import XCTest
/// Provides descriptive error messages for API abuse
class DataLoaderAbuseTests: XCTestCase {
func testFuntionWithNoValues() throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
let identityLoader = DataLoader<Int, Int>(
options: DataLoaderOptions(batchingEnabled: false)
) { _ in
eventLoopGroup.next().makeSucceededFuture([])
}
let value = try identityLoader.load(key: 1, on: eventLoopGroup)
XCTAssertThrowsError(
try value.wait(),
"Did not return value for key: 1"
)
}
func testBatchFuntionMustPromiseAnArrayOfCorrectLength() throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
let identityLoader = DataLoader<Int, Int>() { _ in
eventLoopGroup.next().makeSucceededFuture([])
}
let value = try identityLoader.load(key: 1, on: eventLoopGroup)
XCTAssertThrowsError(
try value.wait(),
"The function did not return an array of the same length as the array of keys. \nKeys count: 1\nValues count: 0"
)
}
func testBatchFuntionWithSomeValues() throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
let identityLoader = DataLoader<Int, Int>() { keys in
var results = [DataLoaderFutureValue<Int>]()
for key in keys {
if key == 1 {
results.append(DataLoaderFutureValue.success(key))
} else {
results.append(
DataLoaderFutureValue.failure(DataLoaderError.typeError("Test error"))
)
}
}
return eventLoopGroup.next().makeSucceededFuture(results)
}
let value1 = try identityLoader.load(key: 1, on: eventLoopGroup)
let value2 = try identityLoader.load(key: 2, on: eventLoopGroup)
XCTAssertThrowsError(try value2.wait())
XCTAssertTrue(try value1.wait() == 1)
}
func testFuntionWithSomeValues() throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
let identityLoader = DataLoader<Int, Int>(
options: DataLoaderOptions(batchingEnabled: false)
) { keys in
var results = [DataLoaderFutureValue<Int>]()
for key in keys {
if key == 1 {
results.append(DataLoaderFutureValue.success(key))
} else {
results.append(
DataLoaderFutureValue.failure(DataLoaderError.typeError("Test error"))
)
}
}
return eventLoopGroup.next().makeSucceededFuture(results)
}
let value1 = try identityLoader.load(key: 1, on: eventLoopGroup)
let value2 = try identityLoader.load(key: 2, on: eventLoopGroup)
XCTAssertThrowsError(try value2.wait())
XCTAssertTrue(try value1.wait() == 1)
}
}