Skip to content

Commit 54c5a30

Browse files
update
1 parent f9240f4 commit 54c5a30

File tree

8 files changed

+149
-106
lines changed

8 files changed

+149
-106
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ jobs:
1717
- name: Build
1818
run: swift build
1919
- name: Run tests
20-
run: swift test
20+
run: swift test --no-parallel
2121
test-linux:
2222
runs-on: ubuntu-22.04
2323
strategy:
2424
matrix:
25-
swift: ["6.0"]
25+
swift: ["6.1"]
2626
container: swift:${{ matrix.swift }}
2727
steps:
2828
- uses: actions/checkout@v3
@@ -31,4 +31,4 @@ jobs:
3131
- name: Build
3232
run: swift build
3333
- name: Run tests
34-
run: swift test
34+
run: swift test --no-parallel

Alchemy/Sources/Queue/QueueWorker.swift

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ actor QueueWorker: Service {
1919
}
2020

2121
func run() async throws {
22+
// 0. run immediately - `AsyncTimerSequence` waits one `interval` before
23+
// firing the first time.
24+
try await runNext()
25+
26+
// 1. then run after every interval
2227
for try await _ in timer {
2328
try await runNext()
2429
}

Alchemy/Tests/Database/Query/QueryWhereTests.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ struct QueryWhereTests {
5757
}
5858

5959
@Test func whereColumn() {
60-
let query = DB.table("foo")
60+
let query = Database.stub
61+
.table("foo")
6162
.whereColumn("foo", .equals, "bar")
6263
.orWhereColumn("baz", .like, "fiz")
6364
#expect(query.wheres == [

Alchemy/Tests/Database/Rune/Relations/EagerLoadableTests.swift

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import AlchemyTesting
22

3-
@Suite(.serialized)
4-
struct EagerLoadableTests {
5-
init() async throws {
6-
try await DB.shutdown()
7-
try await DB.fake(migrations: [TestModelMigration(), TestParentMigration()])
8-
}
9-
10-
func testWith() async throws {
11-
try await TestParent.seed()
12-
let child = try await TestModel.seed()
13-
_ = try await child.testParent()
14-
let fetchedChild = try await TestModel.query().with(\.testParent).first()
15-
#expect(fetchedChild == child)
3+
struct EagerLoadableTests: AppSuite {
4+
let app = TestApp()
5+
6+
@Test func with() async throws {
7+
try await withApp { _ in
8+
try await DB.fake(migrations: [TestModelMigration(), TestParentMigration()])
9+
try await TestParent.seed()
10+
let child = try await TestModel.seed()
11+
_ = try await child.testParent()
12+
let fetchedChild = try await TestModel.query().with(\.testParent).first()
13+
#expect(fetchedChild == child)
14+
}
1615
}
1716
}
1817

Alchemy/Tests/Database/Rune/Relations/RelationshipTests.swift

+27-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ struct RelationshipTests {
1111
private var job: Job!
1212

1313
init() async throws {
14-
try await DB.shutdown()
15-
try await DB.fake(migrations: [WorkflowMigration()])
14+
let db = Database.memory
15+
try await db.migrate([WorkflowMigration()])
16+
Container.main.set(db)
1617

1718
/*
1819
========== STRUCTURE ==========
@@ -51,31 +52,43 @@ struct RelationshipTests {
5152
@Test func hasMany() async throws {
5253
let repositories = try await user.refresh().repositories
5354
#expect(repositories.map(\.id) == [5, 6])
55+
56+
try await _cleanup()
5457
}
5558

5659
@Test func hasOne() async throws {
5760
let manager = try await user.report
5861
#expect(manager?.id == 4)
62+
63+
try await _cleanup()
5964
}
6065

6166
@Test func belongsTo() async throws {
6267
let user = try await repository.user
6368
#expect(user.id == 3)
69+
70+
try await _cleanup()
6471
}
6572

6673
@Test func through() async throws {
6774
let jobs = try await user.jobs.get()
6875
#expect(jobs.map(\.id) == [9, 10])
76+
77+
try await _cleanup()
6978
}
7079

7180
@Test func pivot() async throws {
7281
let organizations = try await user.organizations
7382
#expect(organizations.map(\.id) == [1, 2])
83+
84+
try await _cleanup()
7485
}
7586

7687
@Test func fetchWhere() async throws {
7788
let organizations = try await organization.usersOver30.get()
7889
#expect(organizations.map(\.id) == [4])
90+
91+
try await _cleanup()
7992
}
8093

8194
// MARK: - Eager Loading
@@ -84,19 +97,25 @@ struct RelationshipTests {
8497
let user = try await User.where("id" == 3).with(\.$repositories).first()
8598
#expect(user != nil)
8699
#expect(throws: Never.self) { try user?.$repositories.require() }
100+
101+
try await _cleanup()
87102
}
88103

89104
@Test func autoCache() async throws {
90105
#expect(throws: Error.self) { try user.$repositories.require() }
91106
_ = try await user.$repositories.value()
92107
#expect(user.$repositories.isLoaded == true)
93108
#expect(throws: Never.self) { try user.$repositories.require() }
109+
110+
try await _cleanup()
94111
}
95112

96113
@Test func whereCache() async throws {
97114
_ = try await organization.users
98115
#expect(organization.$users.isLoaded == true)
99116
#expect(organization.usersOver30.isLoaded == false)
117+
118+
try await _cleanup()
100119
}
101120

102121
@Test func sync() async throws {
@@ -106,6 +125,8 @@ struct RelationshipTests {
106125
#expect(user.$report.isLoaded == true)
107126
#expect(try await user.report?.id == 4)
108127
#expect(try await user.$report.load() == nil)
128+
129+
try await _cleanup()
109130
}
110131

111132
// MARK: - CRUD
@@ -125,6 +146,10 @@ struct RelationshipTests {
125146
@Test(.disabled())
126147
func pivotReplace() async throws {
127148
}
149+
150+
private func _cleanup() async throws {
151+
try await DB.shutdown()
152+
}
128153
}
129154

130155
@Model

Alchemy/Tests/Database/Seeding/SeedingTests.swift

+38-35
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,59 @@
22
import Alchemy
33
import AlchemyTesting
44

5-
@Suite(.serialized)
6-
struct SeederTests {
7-
init() async throws {
8-
try await DB.shutdown()
9-
}
5+
struct SeederTests: AppSuite {
6+
let app = TestApp()
107

118
@Test func seedable() async throws {
12-
try await DB.fake(migrations: [SeedModel.Migrate()])
9+
try await withApp { _ in
10+
try await DB.fake(migrations: [SeedModel.Migrate()])
1311

14-
try await SeedModel.seed()
15-
#expect(try await SeedModel.all().count == 1)
12+
try await SeedModel.seed()
13+
#expect(try await SeedModel.all().count == 1)
1614

17-
try await SeedModel.seed(10)
18-
#expect(try await SeedModel.all().count == 11)
15+
try await SeedModel.seed(10)
16+
#expect(try await SeedModel.all().count == 11)
17+
}
1918
}
2019

2120
@Test func seeder() async throws {
22-
try await DB.fake(
23-
migrations: [
24-
SeedModel.Migrate(),
25-
OtherSeedModel.Migrate()
26-
],
27-
seeders: [TestSeeder()]
28-
)
21+
try await withApp { _ in
22+
try await DB.fake(
23+
migrations: [
24+
SeedModel.Migrate(),
25+
OtherSeedModel.Migrate()
26+
],
27+
seeders: [TestSeeder()]
28+
)
2929

30-
#expect(try await SeedModel.all().count == 10)
31-
#expect(try await OtherSeedModel.all().count == 0)
30+
#expect(try await SeedModel.all().count == 10)
31+
#expect(try await OtherSeedModel.all().count == 0)
3232

33-
try await DB.seed(with: OtherSeeder())
34-
#expect(try await OtherSeedModel.all().count == 11)
33+
try await DB.seed(with: OtherSeeder())
34+
#expect(try await OtherSeedModel.all().count == 11)
35+
}
3536
}
3637

3738
@Test func seedWithNames() async throws {
38-
try await DB.fake(
39-
migrations: [
40-
SeedModel.Migrate(),
41-
OtherSeedModel.Migrate()
42-
]
43-
)
39+
try await withApp { _ in
40+
try await DB.fake(
41+
migrations: [
42+
SeedModel.Migrate(),
43+
OtherSeedModel.Migrate()
44+
]
45+
)
4446

45-
DB.seeders = [
46-
TestSeeder(),
47-
OtherSeeder()
48-
]
47+
DB.seeders = [
48+
TestSeeder(),
49+
OtherSeeder()
50+
]
4951

50-
try await DB.seed(names: ["otherseeder"])
51-
#expect(try await SeedModel.all().count == 0)
52-
#expect(try await OtherSeedModel.all().count == 11)
52+
try await DB.seed(names: ["otherseeder"])
53+
#expect(try await SeedModel.all().count == 0)
54+
#expect(try await OtherSeedModel.all().count == 11)
5355

54-
await #expect(throws: Error.self) { try await DB.seed(names: ["foo"]) }
56+
await #expect(throws: Error.self) { try await DB.seed(names: ["foo"]) }
57+
}
5558
}
5659
}
5760

0 commit comments

Comments
 (0)