Giter Site home page Giter Site logo

testfixtures's Introduction

testfixtures

testfixtures is a Rust library for preparing test data from yaml files.

Install

This crate is compatible with the async-std and tokio runtimes.

async-std

[dependencies]
testfixtures = "0.1"
sqlx = "0.3"
chrono = "0.4.11"

tokio

[dependencies]
testfixtures = { version = "0.1", default-features = false, features = [ "runtime-tokio" ] }
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "macros" ] }
chrono = "0.4.11"

Usage

Create fixture files like the following. Fixture files should have the name <table_name>.yml.

todos.yml

- id: 1
  description: buy a new camera
  done: true
  progress: 10.5
  created_at: 2020/01/01 01:01:01
- id: 2
  description: meeting
  done: false
  progress: 30.0
  created_at: 2020/01/01 02:02:02
Click and see the datetime format example
2020-01-01 01:01
2020-01-01 01:01:01
20200101 01:01
20200101 01:01:01
01012020 01:01
01012020 01:01:01
2020/01/01 01:01
2020/01/01 01:01:01

If you need to write raw SQL, probably to call a function, prefix the value of the column with RAW=.

- id: 1
  description: fizz
  done: true
  progress: 10.5
  created_at: RAW=NOW()

Your tests would look like this.

#[cfg(test)]
mod tests {
    use chrono::Utc;
    use sqlx::MySqlPool;
    use std::env;
    use testfixtures::MySqlLoader;

    #[async_std::test]
    async fn test_something() -> anyhow::Result<()> {
        let pool = MySqlPool::new(&env::var("DATABASE_URL")?).await?;
        let loader = MySqlLoader::new(|cfg| {
            cfg.location(Utc);
            cfg.database(pool);
            cfg.skip_test_database_check();
            cfg.paths(vec!["fixtures/todos.yml"]);
        })
        .await?;

        // load your fixtures
        loader.load().await.unwrap();

        // run your tests

        Ok(())
    }
}

PgLoader and SqliteLoader are under development.

Options

database(required)

database is a option for passing db connection pool to a Loader.

let pool = MySqlPool::new(&env::var("DATABASE_URL")?).await?;
let loader = MySqlLoader::new(|cfg| {
    cfg.database(pool);
    // ...
})
.await?;

location(required)

location is a option for setting timezone.

use chrono::Utc;

let loader = MySqlLoader::new(|cfg| {
    cfg.location(Utc);
    // or cfg.location(Local);
    // ...
})
.await?;

skip_test_database_check(optional)

skip_test_database_check is a option for setting a flag for checking if database name ends with "test".

let loader = MySqlLoader::new(|cfg| {
    cfg.skip_test_database_check();
    // ...
})
.await?;

files(optional)

files is a option for reading your fixture files.

let loader = MySqlLoader::new(|cfg| {
    cfg.files(vec!["fizz.yml"]);
    // ...
})
.await?;

directory(optional)

files is a option for reading your fixture files in a directory.

let loader = MySqlLoader::new(|cfg| {
    cfg.directory("fixture");
    // ...
})
.await?;

paths(optional)

paths is a option that is a combination of files option and directory option.

let loader = MySqlLoader::new(|cfg| {
    cfg.paths(vec!["fizz", "buzz/todos.yml"]);
    // ...
})
.await?;

Implemation status

Database

  • MySQL and MariaDB
  • Postgres
  • SQLite

Options

  • database
  • load files
  • skip_test_database_check
  • location
  • directory
  • paths
  • template

Contribution

# setup test db
$ make db

# load environment variables
$ make env
$ direnv allow # https://github.com/direnv/direnv

# run unit tests
$ make test

Show your support

Give a ⭐️ if this project helped you!

Reference

https://github.com/go-testfixtures/testfixtures

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.