Skip to content

Commit

Permalink
feat(#141): Created better tests structure (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjovanc authored Oct 7, 2024
2 parents 4869e4b + 6b1416f commit 6aafc5e
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 194 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ A lightweight and extensible ORM library for Rust.
- [Update data](#update-data)
- [Delete data](#delete-data)
- [Select data](#select-data)
- [Testing](#testing)
- [Running tests](#running-tests)
- [Adding tests](#adding-tests)
- [Getting Help](#getting-help)
- [Reporting Issues](#reporting-issues)
- [Contributing](#contributing)
Expand Down Expand Up @@ -322,6 +325,36 @@ ORDER BY
email DESC
```

## Testing

### Running tests

**Run all tests**

```bash
cargo test
```

**Run specific test folders**

```bash
cargo test --test sqlite_tests
```

**Run specific test files**

```bash
cargo test --test sqlite_tests -- update_test
```

### Adding tests

Once your new test folder and tests have been created:

1. Create a `mod.rs` file in your test folder.
2. Add your tests to the `mod.rs` file.
3. Update `Cargo.toml` with a new `[[test]]` section containing your desired name and the test path.

## Getting Help

Are you having trouble with Njord? We want to help!
Expand Down Expand Up @@ -369,6 +402,7 @@ being awesome contributors to this project. **We'd like to take a moment to reco
[<img src="https://github.com/SvMak.png?size=72" alt="SvMak" width="72">](https://github.com/SvMak)
[<img src="https://github.com/TomasWild.png?size=72" alt="TomasWild" width="72">](https://github.com/TomasWild)
[<img src="https://github.com/chaseWillden.png?size=72" alt="chaseWillden" width="72">](https://github.com/chaseWillden)
[<img src="https://github.com/Hiccup-za.png?size=72" alt="Hiccup-za" width="72">](https://github.com/Hiccup-za)

## License

Expand Down
4 changes: 4 additions & 0 deletions njord/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ njord_derive = { version = "0.3.0", path = "../njord_derive" }
[features]
default = ["sqlite"]
sqlite = []

[[test]]
name = "sqlite_tests"
path = "tests/sqlite/mod.rs"
34 changes: 34 additions & 0 deletions njord/tests/sqlite/delete_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use njord::condition::Condition;
use njord::sqlite;
use std::collections::HashMap;
use std::path::Path;
use super::User;

#[test]
fn delete() {
let db_relative_path = "./db/insert.db";
let db_path = Path::new(&db_relative_path);
let conn = sqlite::open(db_path);

let condition = Condition::Eq("address".to_string(), "Some Random Address 1".to_string());

let mut order = HashMap::new();
order.insert(vec!["id".to_string()], "DESC".to_string());

match conn {
Ok(c) => {
let result = sqlite::delete(c)
.from(User::default())
.where_clause(condition)
.order_by(order)
.limit(20)
.offset(0)
.build();
println!("{:?}", result);
assert!(result.is_ok());
}
Err(e) => {
panic!("Failed to DELETE: {:?}", e);
}
}
}
28 changes: 28 additions & 0 deletions njord/tests/sqlite/insert_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use njord::keys::AutoIncrementPrimaryKey;
use njord::sqlite;
use std::path::Path;
use super::User;

#[test]
fn insert_row() {
let db_relative_path = "./db/insert.db";
let db_path = Path::new(&db_relative_path);
let conn = sqlite::open(db_path);

let table_row: User = User {
id: AutoIncrementPrimaryKey::default(),
username: "mjovanc".to_string(),
email: "[email protected]".to_string(),
address: "Some Random Address 1".to_string(),
};

match conn {
Ok(c) => {
let result = sqlite::insert(c, vec![table_row]);
assert!(result.is_ok());
}
Err(e) => {
panic!("Failed to INSERT: {:?}", e);
}
}
}
47 changes: 47 additions & 0 deletions njord/tests/sqlite/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
mod open_test;
mod insert_test;
mod update_test;
mod delete_test;
mod select_test;

use njord::keys::{AutoIncrementPrimaryKey, PrimaryKey};
use njord::table::Table;
use njord_derive::Table;

#[derive(Table, Clone)]
#[table_name = "users"]
pub struct User {
pub id: AutoIncrementPrimaryKey<usize>,
pub username: String,
pub email: String,
pub address: String,
}

#[derive(Table)]
#[table_name = "users"]
pub struct UserWithSubQuery {
pub id: AutoIncrementPrimaryKey<usize>,
pub username: String,
pub email: String,
pub address: String,
pub additional_address: String,
}

#[derive(Table)]
#[table_name = "categories"]
pub struct Category {
pub id: PrimaryKey<usize>,
pub name: String,
}

#[derive(Table)]
#[table_name = "products"]
pub struct Product {
pub id: PrimaryKey<usize>,
pub name: String,
pub description: String,
pub price: f64,
pub stock_quantity: usize,
pub category: Category, // one-to-one relationship
pub discount: f64,
}
11 changes: 11 additions & 0 deletions njord/tests/sqlite/open_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use njord::sqlite;
use std::path::Path;

#[test]
fn open_db() {
let db_relative_path = "./db/open.db";
let db_path = Path::new(&db_relative_path);

let result = sqlite::open(db_path);
assert!(result.is_ok());
}
Loading

0 comments on commit 6aafc5e

Please sign in to comment.