Skip to content

Add tests #1

Description

@mleonhard

Hi Vlad, Thanks for doing this. I'm interested in adding some tests. Would you like to receive some PRs? I would adapt this TestDb struct that I wrote for another project.
-Michael

//! test_db.rs
use diesel::{sql_query, Connection, PgConnection, RunQueryDsl};
use servlin::log::error;
use std::sync::atomic::AtomicU32;
use url::Url;

static TEST_DB_COUNTER: AtomicU32 = AtomicU32::new(0);

pub struct TestDb {
    default_db_url: String,
    pub url: String,
    name: String,
    delete_on_drop: bool,
}
impl TestDb {
    pub fn new() -> Self {
        let name = format!(
            "test_db_{}_{}",
            std::process::id(),
            TEST_DB_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
        );
        let default_db_url = std::env::var("DATABASE_URL")
            .unwrap_or_else(|_| "postgres://user1:pass1@localhost:5432/db1".to_string());
        let mut conn: PgConnection = Connection::establish(&default_db_url).unwrap();
        sql_query(format!("CREATE DATABASE {name};"))
            .execute(&mut conn)
            .unwrap();
        let mut url = Url::parse(&default_db_url).unwrap();
        url.set_path(&name);
        Self {
            default_db_url,
            url: url.to_string(),
            name,
            delete_on_drop: true,
        }
    }

    #[allow(dead_code)]
    pub fn conn(&self) -> PgConnection {
        Connection::establish(self.url.as_str()).unwrap()
    }

    #[allow(dead_code)]
    pub fn leak(&mut self) {
        self.delete_on_drop = false;
    }
}
impl Drop for TestDb {
    fn drop(&mut self) {
        if !self.delete_on_drop {
            error(format!("TestDb leaking database {}", self.name), ()).unwrap();
            return;
        }
        let mut conn: PgConnection = Connection::establish(&self.default_db_url).unwrap();
        sql_query(format!(
            "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}'",
            self.name
        ))
        .execute(&mut conn)
        .unwrap();
        sql_query(format!("DROP DATABASE {}", self.name))
            .execute(&mut conn)
            .unwrap();
    }
}
# docker-compose.yml
# https://docs.docker.com/compose/gettingstarted/
# https://docs.docker.com/reference/compose-file/
version: '4'
name: 'postgres_sync_test'
services:
  postgres:
    image: "postgres:17.0-alpine"
    command: "-c max_connections=100"
    environment:
      POSTGRES_HOST: localhost
      POSTGRES_PORT: 5432
      POSTGRES_USER: user1
      POSTGRES_PASSWORD: pass1
      POSTGRES_DB: db1
    ports:
      - "5432:5432"

I also like to set up CI to run a script. Then I can run the same script on my laptop:

#!/usr/bin/env bash
# check.sh
set -e
set -x
cd "$(dirname "$0")"
docker-compose up --detach
time cargo check --package=arlo --all-targets --all-features
time cargo build --package=arlo --all-targets --all-features
time cargo fmt --package=arlo --all
time cargo clippy --package=arlo --all-targets --all-features --allow-dirty --allow-staged --fix -- -D clippy::pedantic
time cargo test --package=arlo --all-targets --all-features -- --include-ignored
time cargo test --package=arlo --doc --all-features
echo "$0 finished"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions