From e1b86ee6c84e73d6a0ed2e1a9ef1aa0b273094e3 Mon Sep 17 00:00:00 2001 From: Victor Bodinaud Date: Sun, 14 Jan 2024 20:58:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20unit=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 19 +++++++++ Tests/HermesTests/HermesTests.swift | 61 ++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..483ebe9 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,19 @@ +name: Hermes CI + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Swift + uses: fwal/setup-swift@v1 + - name: Build + run: swift build + - name: Run tests + run: swift test diff --git a/Tests/HermesTests/HermesTests.swift b/Tests/HermesTests/HermesTests.swift index b1cef3e..cef5c7e 100644 --- a/Tests/HermesTests/HermesTests.swift +++ b/Tests/HermesTests/HermesTests.swift @@ -1,12 +1,61 @@ import XCTest @testable import Hermes -final class HermesTests: XCTestCase { - func testExample() throws { - // XCTest Documentation - // https://developer.apple.com/documentation/xctest +struct SomeCodableStruct: Codable, Equatable { + let url: String + var data: String? = nil + var json: SomeCodableJsonStruct? = nil +} - // Defining Test Cases and Test Methods - // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods +struct SomeCodableJsonStruct: Codable, Equatable { + var test: String +} + +final class HermesTests: XCTestCase { + func testResourceInitialization() { + let url = URL(string: "https://httpbin.org/get")! + let resource = Resource(url: url, method: .get([]), modelType: SomeCodableStruct.self) + + XCTAssertEqual(resource.url, url) + XCTAssertEqual(resource.method.name, "GET") + XCTAssert(resource.modelType == SomeCodableStruct.self) + } + + func testGetRequest() async throws { + let url = URL(string: "https://httpbin.org/get")! + let resource = Resource(url: url, method: .get([]), modelType: SomeCodableStruct.self) + + let response = try await Hermes().load(resource) + + XCTAssertNotNil(response) + XCTAssertEqual(response.url, "https://httpbin.org/get") + XCTAssertNil(response.data) + XCTAssertNil(response.json) + } + + func testPostRequest() async throws { + let postDatas = ["test": "test"] + + let url = URL(string: "https://httpbin.org/post")! + let resource = try Resource(url: url, method: .post(JSONEncoder().encode(postDatas)), modelType: SomeCodableStruct.self) + + let response = try await Hermes().load(resource) + + XCTAssertNotNil(response) + XCTAssertEqual(response.url, "https://httpbin.org/post") + XCTAssertEqual(response.data, "{\"test\":\"test\"}") + XCTAssert(response.json == SomeCodableJsonStruct(test: "test")) + } + + func testDeleteRequest() async throws { + let url = URL(string: "https://httpbin.org/delete")! + let resource = Resource(url: url, method: .delete, modelType: SomeCodableStruct.self) + + let response = try await Hermes().load(resource) + + XCTAssertNotNil(response) + XCTAssertEqual(response.url, "https://httpbin.org/delete") + XCTAssertEqual(response.data, "") + XCTAssertNil(response.json) } }