|
| 1 | +import Foundation |
| 2 | +import XCTest |
| 3 | +@testable import APIRouter |
| 4 | + |
| 5 | +final class QueryBUilderTests: XCTestCase { |
| 6 | + func testGeneratedMultyUrlQueryWithQueryBuilder() { |
| 7 | + let request = Request { |
| 8 | + URL { |
| 9 | + Query { |
| 10 | + Field("firstQuery", forKey: "first") |
| 11 | + Field("secondQuery", forKey: "second") |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + if let queryString = request.urlRequest?.url?.absoluteString { |
| 17 | + XCTAssertEqual(queryString.first, "?") |
| 18 | + XCTAssertEqual(queryString.contains("first=firstQuery"), true) |
| 19 | + XCTAssertEqual(queryString.contains("second=secondQuery"), true) |
| 20 | + XCTAssertEqual(queryString.split(separator: "&").count, 2) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + func testSwitchConditionStatementWorkingForBuildEitherInQueryBuilder() { |
| 25 | + enum QueryOptions { |
| 26 | + case one |
| 27 | + case two |
| 28 | + |
| 29 | + var request: Request { |
| 30 | + Request { |
| 31 | + URL { |
| 32 | + Query { |
| 33 | + switch self { |
| 34 | + case .one: |
| 35 | + Field("firstQuery", forKey: "first") |
| 36 | + case .two: |
| 37 | + Field("secondQuery", forKey: "second") |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if let optionOneQueryFields = QueryOptions.one.request.urlComponents?.queryItems, |
| 46 | + let optionTwoQueryFields = QueryOptions.two.request.urlComponents?.queryItems { |
| 47 | + XCTAssertEqual(optionOneQueryFields.first?.debugDescription, "first=firstQuery") |
| 48 | + XCTAssertEqual(optionTwoQueryFields.first?.debugDescription, "second=secondQuery") |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func testIfConditionalStatementWorkingForBuildEitherInQueryBuilder() { |
| 53 | + enum QueryOptions { |
| 54 | + case one |
| 55 | + case two |
| 56 | + |
| 57 | + var request: Request { |
| 58 | + Request { |
| 59 | + URL { |
| 60 | + Query { |
| 61 | + if self == .one { |
| 62 | + Field("firstQuery", forKey: "first") |
| 63 | + } else { |
| 64 | + Field("secondQuery", forKey: "second") |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if let optionOneQueryFields = QueryOptions.one.request.urlComponents?.queryItems, |
| 73 | + let optionTwoQueryFields = QueryOptions.two.request.urlComponents?.queryItems { |
| 74 | + XCTAssertEqual(optionOneQueryFields.first?.debugDescription, "first=firstQuery") |
| 75 | + XCTAssertEqual(optionTwoQueryFields.first?.debugDescription, "second=secondQuery") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + func testForLoopStatementWorkingForBuildEitherInQueryBuilder() { |
| 80 | + let fields = [ |
| 81 | + "first": "firstQuery", |
| 82 | + "second": "secondQuery", |
| 83 | + "third": "thirdQuery" |
| 84 | + ] |
| 85 | + |
| 86 | + let request = Request { |
| 87 | + URL { |
| 88 | + Query { |
| 89 | + for field in fields { |
| 90 | + Field(field.value, forKey: field.key) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + var mockQueryItems: [URLQueryItem] = [] |
| 97 | + for field in fields { |
| 98 | + mockQueryItems.append(URLQueryItem(name: field.key, value: field.value)) |
| 99 | + } |
| 100 | + |
| 101 | + if let queryItems = request.urlComponents?.queryItems { |
| 102 | + XCTAssertEqual(queryItems, mockQueryItems) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments