Skip to content

Commit

Permalink
fix: integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed Nov 6, 2024
1 parent d6cb81c commit b60742e
Show file tree
Hide file tree
Showing 23 changed files with 899 additions and 1,844 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Rust CI

on: [push, pull_request]

jobs:
setup:
name: Set up and Cache
runs-on: ubuntu-latest
outputs:
cache-hit: ${{ steps.cache.outputs.cache-hit }}
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Rust
uses: actions/setup-rust@v1
with:
rust-version: stable

- name: Cache Cargo registry and build
id: cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Build project
run: cargo build --release

unit-tests:
name: Run Unit Tests
runs-on: ubuntu-latest
needs: setup
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Restore Cargo cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Run unit tests from root
run: cargo test --release

integration-tests:
name: Run integration tests
runs-on: ubuntu-latest
needs: setup
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Restore Cargo cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Run integration tests in `tests`
run: |
cd tests
cargo test --release
90 changes: 69 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
## Name pending (currently DB sets)

Inpsired a little bit by Toasty rs and my work on sql-gen.
Idea is to implement the most common SQLX queries, but allow you to still ultimately use SQLX for anything more complex than a basic query.

Currently it's only for PostgresQL, so that I can dogfood. But PRs welcome.

### Why not X

**_Why not SQLX_?**
I _love_ sqlx. It's a delight to work with. I like writing SQL.
That's why _this is sqlx_. This macro allows me to continue writing SQLX, but just shaves off some `sqlx prepare` cycles, and some boring same queries you write over and over again.
Ultimately I still just want to write SQLX, but just save myself all the `by id` style queries.


*Why not SeaORM?*
I've used SeaORM before at a job. I find it verbose.

*Why not Diesel?*
I like the look of diesel. I don't like dedicated single-use config filetypes.
Might end up having to write one though for my codegen tool, so maybe I'll eat those words.

*Why not Toasy?*
Same as diesel. And it's not out yet. But I must like the look of it a little, as I took inspiration from its model definitions.

### Current features

Can currently:
- [x] Query one
- [x] Query many
- [x] Insert (ignoring auto fields)
- [x] Update
- [x] Delete


See the following:
### Roadmap

TODO:
- [ ] Allow for multiple-field primary keys for query-one
- [ ] Allow query many by one key field when there are two key fields
- [ ] Update
- [ ] Delete
- [ ] Release early version!
- [x] Allow for multiple-field primary keys for query-one
- [x] Allow query many by one key field when there are two key fields
- [x] Update
- [x] Delete
- [~] Release early version!
- [ ] Limit / Offset
- [ ] Create a version of https://github.com/jayy-lmao/sql-gen for generating these
- [ ] Do more than just `eq` to match fields (map of ops for each type)

### Examples

```rs
#[derive(DbSet, Debug)] // DbSet also implements sqlx::FromRow by default
#[dbset(table_name = "users")] // Used for queries, will be used for codegen
Expand All @@ -33,43 +60,64 @@ pub struct User {
}

// Fetch one user
let user = UserDbSet::one()
let user: User = UserDbSet::one()
.id_eq("user-1".to_string()) // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.fetch_one(pool)
.await
.expect("could not run query");
.await?;

let user_maybe: Option<User> = UserDbSet::one()
.id_eq("user-1".to_string()) // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.fetch_optional(pool)
.await?;

// We can also just write regular SQLX queries.
// DbSet implements FromRow for your struct also.
let same_user_again = sqlx::query_as!(
User,
"SELECT id, name, email, details FROM users WHERE id = 'user-1';"
)
.fetch_one(pool)
.await?;

// Fetch all users
let users = UserDbSet::many()
.fetch(pool) // Can call without setting fields to match to get all results
.await
.expect("Could not fetch users");

.fetch_all(pool) // Can call without setting fields to match to get all results
.await?;

// Fetch many users with one field
let users = UserDbSet::many()
.name_eq("bob".to_string()) // Can set fields to match on
.fetch(pool)
.await
.expect("Could not fetch users");
.fetch_all(pool)
.await?;

// Fetch many users with multiple fields
let users = UserDbSet::many()
.name_eq("bob".to_string())
.details_eq("the best bob".to_string()) // Can set multiple fields to match on
.fetch(pool)
.await
.expect("Could not fetch users");
.fetch_all(pool)
.await?;

// Insert a user
let inserted_user = UserDbSet::insert()
.id("id-3".to_string())
.email("steven@stevenson.com".to_string())
.name("steven".to_string())
.insert(pool) // Due to type-state insert can't be called until all non-nullable (besides auto) fields have been set
.await
.expect("Could not insert");
.await?;

// Update a user
user.details = Some("Updated details!".to_string());
user.email = String::from("mynewemail@bigpond.com.au");
UserDbSet::update()
.data(user.clone())
.update(pool)
.await?;

// Delete a user
UserDbSet::one()
.id_eq("user-1".to_string()) // type-state pattern, you must provide a key or unique field to be able to call fetch_one
.delete(pool)
.await?;
```


This file was deleted.

This file was deleted.

Loading

0 comments on commit b60742e

Please sign in to comment.