Skip to content

Commit

Permalink
Merge pull request #1 from 5-pebbles/better_tests
Browse files Browse the repository at this point in the history
Better Tests & Github Actions
  • Loading branch information
5-pebbles authored Mar 2, 2024
2 parents bd42424 + 45d261e commit 7cbab1a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 47 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "**" ]

env:
CARGO_TERM_COLOR: always

jobs:
app_tests:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Rust Toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install Hurl
uses: gacts/install-hurl@v1
with:
version: 4.1.0
- name: Run Tests
uses: actions-rs/cargo@v1
with:
command: test

4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rocket = { version = "0.5.0", features = ["json"] }
rusqlite-from-row = "0.2.2"
strum = { version = "0.26.1", features = ["derive"] }

sqlvec = { path = "../sqlvec", features = ["serde"] }
sqlvec = { version = "0.0.2", features = ["serde"] }
bcrypt = "0.15.0"
uuid = { version = "1.7.0", features = ["v4"] }

Expand Down
20 changes: 0 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@ Tuna is an open source music api, designed to allow client side automation & con

> Warning: this is currently in a work in progress...
## Testing

**Application Testing:**

```
cargo run
```

Then:

```
./tests/tests.sh
```

**Unit Testing:**

```
I don't have any unit tests yet...
```

<details><summary><h2>Api End Points</h2></summary>

<details><summary><code>POST /init</code></summary>
Expand Down
3 changes: 2 additions & 1 deletion Rocket.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[default.databases.user_db]
# Fun Fact: In-memory Databases don't support transactions...
[default.databases.db]
url = "file:database/sqlite/db.sqlite?cache=shared"
3 changes: 1 addition & 2 deletions src/database/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rocket_sync_db_pools::{
rusqlite::{params, Connection, Error},
};

#[database("user_db")]
#[database("db")]
pub struct Database(Connection);

impl Database {
Expand Down Expand Up @@ -135,7 +135,6 @@ CREATE TABLE IF NOT EXISTS track_genres (
params![],
)?;


tx.execute(
"
CREATE TABLE IF NOT EXISTS album_track (
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[macro_use]
extern crate rocket;

use std::{fs, path::Path};

mod api;
mod database;

Expand All @@ -9,8 +11,17 @@ fn index() -> &'static str {
"Hello, world!"
}

fn create_working_dir() {
let db_dir = Path::new("./database/sqlite");
if !db_dir.exists() {
fs::create_dir_all(db_dir).expect("Failed to create database directory");
}
}

#[launch]
fn rocket() -> _ {
create_working_dir();

rocket::build()
.attach(database::fairing())
.attach(api::fairing())
Expand Down
77 changes: 77 additions & 0 deletions tests/app_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::env;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};

#[test]
fn hurl_tests() {
// Set the HURL_url environment variable
env::set_var("HURL_url", "http://127.0.0.1:8000");

// Start cargo in the background
let mut cargo_process = Command::new("cargo")
.arg("run")
.stdout(Stdio::piped())
.spawn()
.expect("Failed to start cargo");

// capture the PID
let cargo_pid = cargo_process.id();

// wait for rocket
if let Some(ref mut stdout) = cargo_process.stdout {
let reader = BufReader::new(stdout);
for line in reader.lines() {
match line {
Ok(line) => {
print!("{}", line);
if line.contains("Rocket has launched") {
break;
}
}
Err(e) => {
cargo_process
.kill()
.unwrap_or_else(|_| panic!("Failed to kill cargo: pid = {}", cargo_pid));
panic!("Failed to read line: {}", e);
}
}
}
} else {
cargo_process
.kill()
.unwrap_or_else(|_| panic!("Failed to kill cargo: pid = {}", cargo_pid));
panic!("No stdout to parse...");
}

// Define the test files
let tests = vec![
"tests/users.hurl",
"tests/invites.hurl",
"tests/permissions.hurl",
"tests/sessions.hurl",
"tests/genres.hurl",
"tests/artists.hurl",
];

// Run all application tests
for test in tests {
let status = Command::new("hurl")
.arg("--very-verbose")
.arg(test)
.status()
.expect("Failed to execute hurl command");

// Check if the test failed
if !status.success() {
cargo_process
.kill()
.unwrap_or_else(|_| panic!("Failed to kill cargo: pid = {}", cargo_pid));
panic!("Test failed: {}", test);
}
}

// Kill the cargo process
cargo_process
.kill()
.unwrap_or_else(|_| panic!("Failed to kill cargo: pid = {}", cargo_pid));
}
22 changes: 0 additions & 22 deletions tests/tests.sh

This file was deleted.

0 comments on commit 7cbab1a

Please sign in to comment.