-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONResponseBouncerTests.swift
104 lines (80 loc) · 3.14 KB
/
JSONResponseBouncerTests.swift
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
//
// JSONResponseBouncerTests.swift
//
// Created by Vladyslav Ieliashevskyi on 09.02.2021.
//
import XCTest
@testable import JSONResponseBouncer
//MARK: Test Model
/*
For example we would like to test model, called Player
Imagine it contain all information provided by server after successful login attempt
It will contain following fields:
name is a string
level is an integer
type is a boolean
*/
class TestPlayerResourcesModelKeys: NSObject {
static let name = "name"
static let level = "level"
static let type = "type"
}
class TestPlayerModel: Verifiable {
static let _description = "TestPlayerModel";
static let _keys: Array<ValidationTuple> = [
ValidationTuple(keyName:TestPlayerResourcesModelKeys.name, valueType:.JSONString),
ValidationTuple(keyName:TestPlayerResourcesModelKeys.level, valueType:.JSONNumber),
ValidationTuple(keyName:TestPlayerResourcesModelKeys.type, valueType:.JSONBoolean)
]
func description() -> String {
return TestPlayerModel._description
}
func keys() -> Array<ValidationTuple> {
return TestPlayerModel._keys
}
func keysOnly() -> Array<String> {
var returnKeys: Array<String> = Array<String>();
for tuple:ValidationTuple in TestPlayerModel._keys {
returnKeys.append(tuple.keyName)
}
return returnKeys
}
}
//MARK: Test Model that is not Verifiable
class InvalidTestModel {}
//MARK: JSONResponseBouncer Tests
class JSONResponseBouncerTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func test_ValidJSONResponse_stateValid() {
let dictionary: Dictionary<String, Any> = [ "name" : "vladyslav", "level" : 1, "type" : true ]
let state: JSONResponseState = JSONResponseBouncer.validate(dictionary:dictionary, model:TestPlayerModel())
XCTAssert(state == JSONResponseState.Valid)
}
func test_NotDefinedKeyInJSONResponse_stateQuestionable() {
let dictionary: Dictionary<String, Any> = [ "name_" : "vladyslav", "level" : 1, "type" : true ]
let state: JSONResponseState = JSONResponseBouncer.validate(dictionary: dictionary, model:TestPlayerModel())
XCTAssert(state == .Questionable)
}
func test_InvalidValueInJSONResponse_stateMalformed() {
let dictionary: Dictionary<String, Any> = [ "name" : [], "level" : 1, "type" : true ]
let state: JSONResponseState = JSONResponseBouncer.validate(dictionary: dictionary, model:TestPlayerModel())
XCTAssert(state == .Malformed)
}
func test_EmptyDictionarySpecified_stateMalformed() {
let dictionary: Dictionary<String, Any> = [:]
let state: JSONResponseState = JSONResponseBouncer.validate(dictionary: dictionary, model:TestPlayerModel())
XCTAssert(state == .Malformed)
}
func test_NotVerifiableModelSpecified_stateMalformed() {
let dictionary: Dictionary<String, Any> = [ "name" : "vladyslav", "level" : 1, "type" : true ]
let state: JSONResponseState = JSONResponseBouncer.validate(dictionary: dictionary, model: InvalidTestModel())
XCTAssert(state == .Malformed)
}
}