Skip to content

Commit 9dec386

Browse files
Use In-Memory Database for Tests and Run migration.sql at Startup (#79)
- Use in-memory SQLite database for tests, rather than temp files - Make `SqliteLayer` run `migration.sql` at startup, rather than embedding a pre-built database file - Add tests to ensure that the persistence of the database works correctly.
1 parent cc997d0 commit 9dec386

14 files changed

Lines changed: 258 additions & 365 deletions

File tree

.github/workflows/ci.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ jobs:
4848
- uses: actions/checkout@v4
4949
with:
5050
submodules: true
51-
- name: install dependencies (ubuntu only)
52-
run: |
53-
sudo apt-get update
54-
sudo apt-get install protobuf-compiler
51+
- uses: actions/cache@v5.0.4
52+
with:
53+
path: target
54+
key: ${{ runner.os }}-target
5555
- name: Install ${{ matrix.toolchain }}
5656
uses: dtolnay/rust-toolchain@master
5757
with:
@@ -82,10 +82,10 @@ jobs:
8282
- uses: actions/checkout@v4
8383
with:
8484
submodules: true
85-
- name: install dependencies (ubuntu only)
86-
run: |
87-
sudo apt-get update
88-
sudo apt-get install protobuf-compiler
85+
- uses: actions/cache@v5.0.4
86+
with:
87+
path: target
88+
key: ${{ runner.os }}-target
8989
- uses: dtolnay/rust-toolchain@master
9090
with:
9191
toolchain: stable

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basalt-server-lib/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version.workspace = true
55
rust-version.workspace = true
66

77
[features]
8+
testing = ["dep:tracing-subscriber"]
89
doc-gen = []
910
webhooks = ["dep:reqwest"]
1011
scripting = ["dep:rustyscript"]
@@ -17,7 +18,6 @@ uninlined_format_args = "allow"
1718
[dependencies]
1819
anyhow.workspace = true
1920
argon2.workspace = true
20-
async-tempfile.workspace = true
2121
axum-extra.workspace = true
2222
axum.workspace = true
2323
bedrock.workspace = true
@@ -45,10 +45,11 @@ rustyscript = { git = "https://github.com/rscarson/rustyscript.git", branch = "m
4545
reqwest = { version = "0.13.2", features = ["json"], optional = true }
4646
futures = "0.3.31"
4747
ident-str = "0.1.0"
48+
tracing-subscriber = { workspace = true, optional = true }
4849

4950
[dev-dependencies]
50-
async-tempfile = { workspace = true, features = ["uuid"] }
5151
tracing-subscriber.workspace = true
52+
async-tempfile.workspace = true
5253

5354
[build-dependencies]
5455
tokio = { version = "1.43.0", features = ["full"] }

basalt-server-lib/build.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::path::Path;
22

33
use anyhow::Context;
44

@@ -8,14 +8,7 @@ pub async fn main() -> anyhow::Result<()> {
88
let cargo_target_dir =
99
std::env::var("OUT_DIR").context("Failed to get cargo target directory")?;
1010

11-
let path = PathBuf::from(cargo_target_dir)
12-
.join("initial_data")
13-
.with_extension("db");
14-
15-
println!(
16-
"cargo::rustc-env=INITIAL_DATA_PATH={}",
17-
path.to_str().unwrap()
18-
);
11+
let path = Path::new(&cargo_target_dir).join("initial_data.db");
1912

2013
let sqlite_uri = format!("sqlite:{}", path.to_str().unwrap());
2114

basalt-server-lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ mod services;
55
pub mod storage;
66
mod utils;
77

8-
#[cfg(test)]
9-
mod testing;
8+
#[cfg(any(test, feature = "testing"))]
9+
pub mod testing;

basalt-server-lib/src/repositories/announcements.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,19 @@ mod tests {
100100

101101
#[tokio::test]
102102
async fn create_announcement() {
103-
let (f, sql) = mock_db().await;
103+
let sql = mock_db().await;
104104
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
105105
let announcement = super::create_announcement(&sql, &user.id, "hello world")
106106
.await
107107
.unwrap();
108108

109109
assert_eq!(announcement.sender, user.id);
110110
assert_eq!(&announcement.message, "hello world");
111-
drop(f)
112111
}
113112

114113
#[tokio::test]
115114
async fn get_announcements() {
116-
let (f, sql) = mock_db().await;
115+
let sql = mock_db().await;
117116
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
118117
super::create_announcement(&sql, &user.id, "foo")
119118
.await
@@ -126,12 +125,11 @@ mod tests {
126125

127126
assert!(ann.iter().any(|a| a.message == "foo"));
128127
assert!(ann.iter().any(|a| a.message == "bar"));
129-
drop(f)
130128
}
131129

132130
#[tokio::test]
133131
async fn delete_announcement() {
134-
let (f, sql) = mock_db().await;
132+
let sql = mock_db().await;
135133
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
136134
let Announcement { id, .. } = super::create_announcement(&sql, &user.id, "foo")
137135
.await
@@ -145,6 +143,5 @@ mod tests {
145143

146144
let ann = super::get_announcements(&sql).await.unwrap();
147145
assert!(ann.is_empty());
148-
drop(f)
149146
}
150147
}

basalt-server-lib/src/repositories/submissions.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ mod tests {
525525

526526
#[tokio::test]
527527
async fn create_submission() {
528-
let (f, sql) = mock_db().await;
528+
let sql = mock_db().await;
529529
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
530530
let history = create_submission_history(
531531
&sql,
@@ -553,12 +553,11 @@ mod tests {
553553
assert_eq!(history.question_index, 42);
554554
assert_eq!(history.score, 42.);
555555
assert!(!history.success);
556-
drop(f)
557556
}
558557

559558
#[tokio::test]
560559
async fn create_submission_test() {
561-
let (f, sql) = mock_db().await;
560+
let sql = mock_db().await;
562561
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
563562
let history = create_submission_history(
564563
&sql,
@@ -595,12 +594,11 @@ mod tests {
595594
assert_eq!(test.stdout, "stdout");
596595
assert_eq!(test.stderr, "stderr");
597596
assert_eq!(test.exit_status, 1);
598-
drop(f)
599597
}
600598

601599
#[tokio::test]
602600
async fn other_submissions() {
603-
let (f, sql) = mock_db().await;
601+
let sql = mock_db().await;
604602

605603
for i in 0..5 {
606604
let user = dummy_user(
@@ -652,13 +650,11 @@ mod tests {
652650

653651
let n = count_other_submissions(&sql, 1).await.unwrap();
654652
assert_eq!(n, 5);
655-
656-
drop(f)
657653
}
658654

659655
#[tokio::test]
660656
async fn previous_submissions() {
661-
let (f, sql) = mock_db().await;
657+
let sql = mock_db().await;
662658

663659
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
664660
for _ in 0..5 {
@@ -685,13 +681,11 @@ mod tests {
685681

686682
let n = count_previous_submissions(&sql, &user.id, 1).await.unwrap();
687683
assert_eq!(n, 5);
688-
689-
drop(f)
690684
}
691685

692686
#[tokio::test]
693687
async fn user_score() {
694-
let (f, sql) = mock_db().await;
688+
let sql = mock_db().await;
695689

696690
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
697691
for i in 0..5 {
@@ -716,13 +710,11 @@ mod tests {
716710

717711
let n = get_user_score(&sql, &user.id).await.unwrap();
718712
assert_eq!(n, 42. * 5.);
719-
720-
drop(f)
721713
}
722714

723715
#[tokio::test]
724716
async fn latest_submissions() {
725-
let (f, sql) = mock_db().await;
717+
let sql = mock_db().await;
726718

727719
let user = dummy_user(&sql, "dummy_user", "foobar", Role::Competitor).await;
728720
for i in 0..5 {
@@ -772,7 +764,5 @@ mod tests {
772764
for s in submissions {
773765
assert_eq!(s.code, "latest");
774766
}
775-
776-
drop(f)
777767
}
778768
}

basalt-server-lib/src/repositories/users.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,14 @@ mod tests {
220220
use super::*;
221221
#[tokio::test]
222222
async fn get_nonexistent_user() {
223-
let (f, sql) = mock_db().await;
223+
let sql = mock_db().await;
224224
let response = get_user_by_id(&sql, &UserId::new()).await;
225225
assert!(response.is_err());
226-
drop(f)
227226
}
228227

229228
#[tokio::test]
230229
async fn get_existing_user_by_id() {
231-
let (f, sql) = mock_db().await;
230+
let sql = mock_db().await;
232231
let dummy_user = create_user(
233232
&sql,
234233
"awesome_user".to_string(),
@@ -242,12 +241,11 @@ mod tests {
242241
.await
243242
.expect("Failed to find user");
244243
assert_eq!(user.username, dummy_user.username);
245-
drop(f)
246244
}
247245

248246
#[tokio::test]
249247
async fn get_correct_user() {
250-
let (f, sql) = mock_db().await;
248+
let sql = mock_db().await;
251249
let dummy_user = crate::testing::users_repositories::dummy_user(
252250
&sql,
253251
"awesome_user".to_string(),
@@ -266,6 +264,5 @@ mod tests {
266264
.await
267265
.expect("Failed to find user");
268266
assert_eq!(user.username, dummy_user.username);
269-
drop(f)
270267
}
271268
}

0 commit comments

Comments
 (0)