Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: Migrate Networking tests to Swift Testing #193

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
uses: actions/checkout@v4

- name: Build
run: swift build --build-tests -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
run: swift build --build-tests -Xswiftc -warnings-as-errors

- name: Test
run: swift test --filter TMDbTests --enable-code-coverage -Xswiftc -strict-concurrency=complete
run: swift test --filter TMDbTests --enable-code-coverage

- name: Prepare Code Coverage
run: |
Expand All @@ -48,7 +48,7 @@ jobs:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

- name: Build for Release
run: swift build -c release -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
run: swift build -c release -Xswiftc -warnings-as-errors

build-and-test-platforms:
name: Build and Test (${{ matrix.name }})
Expand Down Expand Up @@ -84,10 +84,10 @@ jobs:
# uses: actions/checkout@v4

# - name: Build
# run: swift build --build-tests -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
# run: swift build --build-tests -Xswiftc -warnings-as-errors

# - name: Test
# run: swift test --skip-build --filter TMDbTests -Xswiftc -strict-concurrency=complete
# run: swift test --skip-build --filter TMDbTests

# - name: Build for Release
# run: swift build -c release -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
# run: swift build -c release -Xswiftc -warnings-as-errors
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
build:
name: Build
runs-on: ubuntu-latest
container: swift:5.9.2-jammy
container: swiftlang/swift:nightly-6.0-jammy
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ identifier_name:
- id
- job
- key
- l
- lhs
- r
- rhs
- tv
- url
Expand Down
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ lint-markdown:

.PHONY: build
build:
swift build -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
swift build -Xswiftc -warnings-as-errors

.PHONY: build-tests
build-tests:
swift build --build-tests -Xswiftc -warnings-as-errors

.PHONY: build-linux
build-linux:
docker run --rm -v "$${PWD}:/workspace" -w /workspace $(SWIFT_CONTAINER_IMAGE) /bin/bash -cl "swift build -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete"
docker run --rm -v "$${PWD}:/workspace" -w /workspace $(SWIFT_CONTAINER_IMAGE) /bin/bash -cl "swift build -Xswiftc -warnings-as-errors

.PHONY: build-release
build-release:
swift build -c release -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
swift build -c release -Xswiftc -warnings-as-errors

.PHONY: build-linux-release
build-linux-release:
docker run --rm -v "$${PWD}:/workspace" -w /workspace $(SWIFT_CONTAINER_IMAGE) /bin/bash -cl "swift build -c release -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete"
docker run --rm -v "$${PWD}:/workspace" -w /workspace $(SWIFT_CONTAINER_IMAGE) /bin/bash -cl "swift build -c release -Xswiftc -warnings-as-errors"

.PHONY: build-docs
build-docs:
Expand All @@ -65,8 +69,8 @@ generate-docs:

.PHONY: test
test:
swift build --build-tests -Xswiftc -warnings-as-errors -Xswiftc -strict-concurrency=complete
swift test --skip-build --filter $(TEST_TARGET) -Xswiftc -strict-concurrency=complete
swift build --build-tests -Xswiftc -warnings-as-errors
swift test --skip-build --filter $(TEST_TARGET)

.PHONY: test-ios
test-ios:
Expand Down Expand Up @@ -94,8 +98,8 @@ test-linux:

.PHONY: integration-test
integration-test: .check-env-vars
swift build --build-tests -Xswiftc -strict-concurrency=complete
swift test --skip-build --filter $(INTEGRATION_TEST_TARGET) -Xswiftc -strict-concurrency=complete
swift build --build-tests
swift test --skip-build --filter $(INTEGRATION_TEST_TARGET)

.PHONY: ci
ci: .check-env-vars lint lint-markdown test test-ios test-watchos test-tvos test-visionos integration-test build-release build-docs
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.9
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import class Foundation.ProcessInfo
Expand Down
63 changes: 62 additions & 1 deletion Sources/TMDb/Networking/TMDbAPIError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
///
/// A model representing a TMDb API error.
///
enum TMDbAPIError: Error {
enum TMDbAPIError: Error, Equatable {

///
/// Invalid URL.
Expand Down Expand Up @@ -114,4 +114,65 @@
///
case unknown

static func == (lhs: TMDbAPIError, rhs: TMDbAPIError) -> Bool {
switch (lhs, rhs) {
case let (.invalidURL(l), .invalidURL(r)):
l == r

case (.network, .network):
true

Check warning on line 123 in Sources/TMDb/Networking/TMDbAPIError.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Networking/TMDbAPIError.swift#L123

Added line #L123 was not covered by tests

case let (.badRequest(l), .badRequest(r)):
l == r

case let (.unauthorised(l), .unauthorised(r)):
l == r

case let (.forbidden(l), .forbidden(r)):
l == r

case let (.notFound(l), .notFound(r)):
l == r

case let (.methodNotAllowed(l), .methodNotAllowed(r)):
l == r

case let (.notAcceptable(l), .notAcceptable(r)):
l == r

case let (.unprocessableContent(l), .unprocessableContent(r)):
l == r

case let (.tooManyRequests(l), .tooManyRequests(r)):
l == r

case let (.internalServerError(l), .internalServerError(r)):
l == r

case let (.notImplemented(l), .notImplemented(r)):
l == r

case let (.badGateway(l), .badGateway(r)):
l == r

case let (.serviceUnavailable(l), .serviceUnavailable(r)):
l == r

case let (.gatewayTimeout(l), .gatewayTimeout(r)):
l == r

case (.encode, .encode):
true

Check warning on line 165 in Sources/TMDb/Networking/TMDbAPIError.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Networking/TMDbAPIError.swift#L165

Added line #L165 was not covered by tests

case (.decode, .decode):
true

Check warning on line 168 in Sources/TMDb/Networking/TMDbAPIError.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Networking/TMDbAPIError.swift#L168

Added line #L168 was not covered by tests

case (.unknown, .unknown):
true

default:
false

Check warning on line 174 in Sources/TMDb/Networking/TMDbAPIError.swift

View check run for this annotation

Codecov / codecov/patch

Sources/TMDb/Networking/TMDbAPIError.swift#L174

Added line #L174 was not covered by tests
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,86 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class APIRequestQueryItemNameTests: XCTestCase {
@Suite(.tags(.apiClient, .domain))
struct APIRequestQueryItemNameTests {

func testPageName() {
XCTAssertEqual(APIRequestQueryItem.Name.page, "page")
@Test("pageName")
func pageQueryItemName() {
#expect(APIRequestQueryItem.Name.page == "page")
}

func testSortByName() {
XCTAssertEqual(APIRequestQueryItem.Name.sortBy, "sort_by")
@Test("sortBy")
func sortByQueryItemName() {
#expect(APIRequestQueryItem.Name.sortBy == "sort_by")
}

func testWithPeopleName() {
XCTAssertEqual(APIRequestQueryItem.Name.withPeople, "with_people")
@Test("withPeople")
func withPeopleQueryItemName() {
#expect(APIRequestQueryItem.Name.withPeople == "with_people")
}

func testWatchRegionName() {
XCTAssertEqual(APIRequestQueryItem.Name.watchRegion, "watch_region")
@Test("watchRegion")
func watchRegionQueryItemName() {
#expect(APIRequestQueryItem.Name.watchRegion == "watch_region")
}

func testIncludeImageLanguageName() {
XCTAssertEqual(APIRequestQueryItem.Name.includeImageLanguage, "include_image_language")
@Test("includeImageLanguage")
func includeImageLanguageQueryItemName() {
#expect(APIRequestQueryItem.Name.includeImageLanguage == "include_image_language")
}

func testIncludeVideoLanguageName() {
XCTAssertEqual(APIRequestQueryItem.Name.includeVideoLanguage, "include_video_language")
@Test("includeVideoLanguage")
func includeVideoLanguageQueryItemName() {
#expect(APIRequestQueryItem.Name.includeVideoLanguage == "include_video_language")
}

func testIncludeAdultName() {
XCTAssertEqual(APIRequestQueryItem.Name.includeAdult, "include_adult")
@Test("includeAdult")
func includeAdultQueryItemName() {
#expect(APIRequestQueryItem.Name.includeAdult == "include_adult")
}

func testQueryName() {
XCTAssertEqual(APIRequestQueryItem.Name.query, "query")
@Test("query")
func queryQueryItemName() {
#expect(APIRequestQueryItem.Name.query == "query")
}

func testYearName() {
XCTAssertEqual(APIRequestQueryItem.Name.year, "year")
@Test("year")
func yearQueryItemName() {
#expect(APIRequestQueryItem.Name.year == "year")
}

func testPrimaryReleaseYearName() {
XCTAssertEqual(APIRequestQueryItem.Name.primaryReleaseYear, "primary_release_year")
@Test("primaryReleaseYear")
func primaryReleaseYearQueryItemName() {
#expect(APIRequestQueryItem.Name.primaryReleaseYear == "primary_release_year")
}

func testFirstAirDateYearName() {
XCTAssertEqual(APIRequestQueryItem.Name.firstAirDateYear, "first_air_date_year")
@Test("firstAirDateYear")
func firstAirDateYearQueryItemName() {
#expect(APIRequestQueryItem.Name.firstAirDateYear == "first_air_date_year")
}

func testSessionIDName() {
XCTAssertEqual(APIRequestQueryItem.Name.sessionID, "session_id")
@Test("sesionID")
func sessionIDQueryItemName() {
#expect(APIRequestQueryItem.Name.sessionID == "session_id")
}

func testLanguageName() {
XCTAssertEqual(APIRequestQueryItem.Name.language, "language")
@Test("language")
func languageQueryItemName() {
#expect(APIRequestQueryItem.Name.language == "language")
}

func testRegionName() {
XCTAssertEqual(APIRequestQueryItem.Name.region, "region")
@Test("region")
func regionQueryItemName() {
#expect(APIRequestQueryItem.Name.region == "region")
}

func testAPIKeyName() {
XCTAssertEqual(APIRequestQueryItem.Name.apiKey, "api_key")
@Test("apiKey")
func apiKeyQueryItemName() {
#expect(APIRequestQueryItem.Name.apiKey == "api_key")
}

}
2 changes: 1 addition & 1 deletion Tests/TMDbTests/Domain/APIClient/MockAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// limitations under the License.
//

import Foundation
@testable import TMDb
import XCTest

final class MockAPIClient: APIClient, @unchecked Sendable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@
// limitations under the License.
//

import Foundation
import Testing
@testable import TMDb
import XCTest

final class HTTPRequestMethodTests: XCTestCase {
@Suite(.tags(.networking))
struct HTTPRequestMethodTests {

func testGetMethod() {
XCTAssertEqual(HTTPRequest.Method.get.rawValue, "GET")
@Test("get method should be equal to \"GET\"")
func getMethod() {
#expect(HTTPRequest.Method.get.rawValue == "GET")
}

func testPostMethod() {
XCTAssertEqual(HTTPRequest.Method.post.rawValue, "POST")
@Test("post method should be equal to \"POST\"")
func postMethod() {
#expect(HTTPRequest.Method.post.rawValue == "POST")
}

func testDeleteMethod() {
XCTAssertEqual(HTTPRequest.Method.delete.rawValue, "DELETE")
@Test("delete method should be equal to \"DELETE\"")
func deleteMethod() {
#expect(HTTPRequest.Method.delete.rawValue == "DELETE")
}

}
3 changes: 0 additions & 3 deletions Tests/TMDbTests/Networking/HTTPClient/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
//

import Foundation
#if canImport(FoundationNetworking)
@preconcurrency import FoundationNetworking
#endif

final class MockURLProtocol: URLProtocol, @unchecked Sendable {

Expand Down
Loading