Skip to content
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
119 changes: 64 additions & 55 deletions Tests/AppTests/AuthorControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,89 +12,98 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import App

import Dependencies
import Testing
import Vapor


class AuthorControllerTests: AppTestCase {

func test_query() async throws {
// setup
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
@Suite struct AuthorControllerTests {

// MUT
let pkg = try await AuthorController.query(on: app.db, owner: "owner")

// validate
XCTAssertEqual(pkg.map(\.model.id), [p.id])
}
@Test func query() async throws {
try await withApp { app in
// setup
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)

func test_query_no_version() async throws {
// setup
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
// MUT
let pkg = try await AuthorController.query(on: app.db, owner: "owner")

// MUT
do {
_ = try await AuthorController.query(on: app.db, owner: "owner")
XCTFail("Expected Abort.notFound")
} catch let error as Abort {
// validate
XCTAssertEqual(error.status, .notFound)
} catch {
XCTFail("Unexpected error: \(error)")
#expect(pkg.map(\.model.id) == [p.id])
}
}

func test_query_sort_alphabetically() async throws {
// setup
for packageName in ["gamma", "alpha", "beta"] {
let p = Package(url: "\(packageName)".url)
try await p.save(on: app.db)
@Test func query_no_version() async throws {
try await withApp { app in
// setup
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)

// MUT
do {
_ = try await AuthorController.query(on: app.db, owner: "owner")
Issue.record("Expected Abort.notFound")
} catch let error as Abort {
// validate
#expect(error.status == .notFound)
} catch {
Issue.record("Unexpected error: \(error)")
}
}
}

@Test func query_sort_alphabetically() async throws {
try await withApp { app in
// setup
for packageName in ["gamma", "alpha", "beta"] {
let p = Package(url: "\(packageName)".url)
try await p.save(on: app.db)
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)
}

// MUT
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
// MUT
let pkg = try await AuthorController.query(on: app.db, owner: "owner")

// validate
XCTAssertEqual(pkg.map(\.model.url), ["alpha", "beta", "gamma"])
// validate
#expect(pkg.map(\.model.url) == ["alpha", "beta", "gamma"])
}
}

func test_show_owner() async throws {
@Test func show_owner() async throws {
try await withDependencies {
$0.environment.dbId = { nil }
} operation: {
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)

// MUT
try await app.test(.GET, "/owner", afterResponse: { response async in
XCTAssertEqual(response.status, .ok)
})
try await withApp { app in
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)

// MUT
try await app.test(.GET, "/owner", afterResponse: { response async in
#expect(response.status == .ok)
})
}
}
}

func test_show_owner_empty() async throws {
@Test func show_owner_empty() async throws {
try await withDependencies {
$0.environment.dbId = { nil }
} operation: {
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)

// MUT
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
XCTAssertEqual(response.status, .notFound)
})
try await withApp { app in
let p = try await savePackage(on: app.db, "1")
try await Repository(package: p, owner: "owner").save(on: app.db)
try await Version(package: p, latest: .defaultBranch).save(on: app.db)

// MUT
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
#expect(response.status == .notFound)
})
}
}
}

Expand Down
17 changes: 8 additions & 9 deletions Tests/AppTests/BadgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@

@testable import App

import XCTest
import Testing


class BadgeTests: AppTestCase {
@Suite struct BadgeTests {

func test_badgeMessage_swiftVersions() throws {
XCTAssertEqual(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]), "6.0 | 5.10 | 5.9 | 5.8")
XCTAssertNil(Badge.badgeMessage(swiftVersions: []))
@Test func badgeMessage_swiftVersions() throws {
#expect(Badge.badgeMessage(swiftVersions: [.v1, .v2, .v3, .v4]) == "6.0 | 5.10 | 5.9 | 5.8")
#expect(Badge.badgeMessage(swiftVersions: []) == nil)
}

func test_badgeMessage_platforms() throws {
XCTAssertEqual(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]),
"iOS | macOS | Linux")
XCTAssertNil(Badge.badgeMessage(platforms: []))
@Test func badgeMessage_platforms() throws {
#expect(Badge.badgeMessage(platforms: [.linux, .iOS, .macosXcodebuild, .macosSpm]) == "iOS | macOS | Linux")
#expect(Badge.badgeMessage(platforms: []) == nil)
}

}
41 changes: 22 additions & 19 deletions Tests/AppTests/BlogActionsModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
import Foundation

@testable import App

import Dependencies
import Testing


class BlogActionsModelTests: AppTestCase {
@Suite struct BlogActionsModelTests {

func test_init_loadSummaries() async throws {
@Test func init_loadSummaries() async throws {
try withDependencies {
$0.fileManager.contents = { @Sendable _ in
"""
Expand All @@ -45,19 +46,21 @@ class BlogActionsModelTests: AppTestCase {
// MUT
let summaries = try BlogActions.Model().summaries

XCTAssertEqual(summaries.count, 2)
XCTAssertEqual(summaries.map(\.slug), ["post-2", "post-1"])
XCTAssertEqual(summaries.map(\.published), [false, true])
#expect(summaries.count == 2)
#expect(summaries.map(\.slug) == ["post-2", "post-1"])
#expect(summaries.map(\.published) == [false, true])

let firstSummary = try XCTUnwrap(summaries).first
let firstSummary = try #require(summaries).first

// Note that we are testing that the first item in this list is the *last* item in the source YAML
// as the init should reverse the order of posts so that they display in reverse chronological order
XCTAssertEqual(firstSummary, BlogActions.Model.PostSummary(slug: "post-2",
title: "Blog post title two",
summary: "Summary of blog post two",
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
published: false))
#expect(firstSummary == BlogActions.Model.PostSummary(
slug: "post-2",
title: "Blog post title two",
summary: "Summary of blog post two",
publishedAt: DateFormatter.yearMonthDayDateFormatter.date(from: "2024-01-02")!,
published: false)
)
}

try withDependencies { // Ensure prod shows only published summaries
Expand All @@ -67,13 +70,13 @@ class BlogActionsModelTests: AppTestCase {
let summaries = try BlogActions.Model().summaries

// validate
XCTAssertEqual(summaries.map(\.slug), ["post-1"])
XCTAssertEqual(summaries.map(\.published), [true])
#expect(summaries.map(\.slug) == ["post-1"])
#expect(summaries.map(\.published) == [true])
}
}
}

func test_postSummary_postMarkdown_load() async throws {
@Test func postSummary_postMarkdown_load() async throws {
withDependencies {
$0.fileManager.contents = { @Sendable _ in
"""
Expand All @@ -86,21 +89,21 @@ class BlogActionsModelTests: AppTestCase {
// MUT
let markdown = summary.postMarkdown

XCTAssertEqual(markdown, "<p>This is some Markdown with <a href=\"https://example.com\">a link</a> and some <em>formatting</em>.</p>")
#expect(markdown == "<p>This is some Markdown with <a href=\"https://example.com\">a link</a> and some <em>formatting</em>.</p>")
}
}

func test_decode_posts_yml() async throws {
@Test func decode_posts_yml() async throws {
// setup
try withDependencies {
$0.fileManager.contents = FileManagerClient.liveValue.contents(atPath:)
$0.timeZone = .utc
} operation: {
// MUT
let summaries = try BlogActions.Model.allSummaries()

// validate
XCTAssert(summaries.count > 0)
#expect(summaries.count > 0)
}
}

Expand Down
Loading
Loading