Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic tests #584

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
components: cargo, rustc, rustfmt
profile: minimal

- uses: actions-rs/cargo@v1
- name: cargo fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all --check
Expand All @@ -43,19 +44,27 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}

- uses: actions-rs/cargo@v1
- name: cargo clippy --lib
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all --lib --bins --all-features --no-deps --profile=debug-lite
args: --workspace --lib --all-features --no-deps --profile=debug-lite

- name: cargo clippy --bins
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --bins --all-features --no-deps --profile=debug-lite

- name: check lockfile
run: |
diff Cargo.lock <(git show HEAD:Cargo.lock)

- uses: actions-rs/cargo@v1
- name: cargo doc --lib
uses: actions-rs/cargo@v1
with:
command: doc
args: --all --lib --all-features --no-deps --profile=debug-lite
args: --workspace --lib --all-features --no-deps --profile=debug-lite

release-check:
name: cargo check/doc/clippy (release profile)
Expand All @@ -78,12 +87,20 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}

- uses: actions-rs/cargo@v1
- name: cargo clippy --lib --release
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --lib --all-features --profile=release-lite

- name: cargo clippy --bins --release
uses: actions-rs/cargo@v1
with:
command: clippy
args: --all --lib --bins --all-features --profile=release-lite
args: --workspace --bins --all-features --profile=release-lite

- uses: actions-rs/cargo@v1
- name: cargo doc --lib --release
uses: actions-rs/cargo@v1
with:
command: doc
args: --all --lib --all-features --no-deps --profile=release-lite
args: --workspace --lib --all-features --no-deps --profile=release-lite
64 changes: 57 additions & 7 deletions .github/workflows/cargo_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@ name: Cargo test

on:
pull_request:
branches: [master]
branches: [dev, master]
push:
branches: [dev, master]

jobs:
debug-build:
debug-test:
name: cargo build/test (debug profile)
runs-on: indexer-test-runner

services:
postgres:
image: postgres:14.1

env:
POSTGRES_USER: ci
POSTGRES_PASSWORD: ci
POSTGRES_PORT: 5432
POSTGRES_DB: ci

ports:
- 5432:5432

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -28,14 +42,39 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}

- uses: actions-rs/cargo@v1
- name: cargo test --lib
uses: actions-rs/cargo@v1
env:
DATABASE_URL: postgres://ci:[email protected]:5432/ci
with:
command: test
args: --workspace --lib --all-features --profile=debug-lite

- name: cargo test --bins
uses: actions-rs/cargo@v1
env:
DATABASE_URL: postgres://ci:[email protected]:5432/ci
with:
command: test
args: --all --lib --bins --all-features --profile=debug-lite
args: --workspace --bins --all-features --profile=debug-lite

release-build:
release-test:
name: cargo build/test (release profile)
runs-on: indexer-test-runner

services:
postgres:
image: postgres:14.1

env:
POSTGRES_USER: ci
POSTGRES_PASSWORD: ci
POSTGRES_PORT: 5432
POSTGRES_DB: ci

ports:
- 5432:5432

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -54,7 +93,18 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}

- uses: actions-rs/cargo@v1
- name: cargo test --lib --release
uses: actions-rs/cargo@v1
env:
DATABASE_URL: postgres://ci:[email protected]:5432/ci
with:
command: test
args: --workspace --lib --all-features --profile=release-lite

- name: cargo test --bins --release
uses: actions-rs/cargo@v1
env:
DATABASE_URL: postgres://ci:[email protected]:5432/ci
with:
command: test
args: --all --lib --bins --all-features --profile=release-lite
args: --workspace --bins --all-features --profile=release-lite
50 changes: 0 additions & 50 deletions .github/workflows/deploy_to_heroku_production.yml

This file was deleted.

40 changes: 0 additions & 40 deletions .github/workflows/deploy_to_heroku_staging.yml

This file was deleted.

1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ solana-program = { version = "1.9.4", optional = true }
cid = { version = "0.7.0", optional = true }
url = "2.2.2"
md5 = { version = "0.7.0", optional = true }

[dev-dependencies]
lazy_static = "1.4.0"
94 changes: 78 additions & 16 deletions crates/core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ impl From<ConnectMode> for ConnectionType {
}
}

fn migrate<C: diesel::Connection>(
conn: &C,
desc: impl std::fmt::Display,
run: impl FnOnce(&C, &mut dyn std::io::Write) -> Result<(), diesel_migrations::RunMigrationsError>,
) -> Result<()> {
info!("Running {}...", desc);

let mut out = vec![];

run(conn, &mut out).with_context(|| format!("Failed to run {}", desc))?;

match std::str::from_utf8(&out) {
Ok(s) => {
let s = s.trim();

if !s.is_empty() {
info!("Output from {}:\n{}", desc, s);
}
},
Err(e) => warn!("Failed to read output from {}: {}", desc, e),
}

Ok(())
}

/// Create a pooled connection to the Postgres database, using the given CLI
/// arguments and a hint indicating if the database is writable.
///
Expand Down Expand Up @@ -159,8 +184,6 @@ pub fn connect(args: ConnectArgs, mode: ConnectMode) -> Result<ConnectResult> {
.build(man)
.context("Failed to create database connection pool")?;

let mut out = vec![];

if cfg!(not(debug_assertions)) && matches!(ty, ConnectionType::Default) {
warn!("Cannot determine if database is writable; assuming yes");
}
Expand All @@ -187,25 +210,64 @@ pub fn connect(args: ConnectArgs, mode: ConnectMode) -> Result<ConnectResult> {
};

if migrated {
info!("Running database migrations...");
migrate(
&*pool
.get()
.context("Failed to acquire database connection")?,
"database migrations",
embedded_migrations::run_with_output,
)?;
}

Ok(ConnectResult { pool, ty, migrated })
}

#[cfg(test)]
pub mod test {
embed_migrations!("test_migrations");

embedded_migrations::run_with_output(
&pool.get().context("Failed to connect to the database")?,
&mut out,
fn initialize() -> super::ConnectResult {
dotenv::from_filename(".env.dev").expect("Failed to load .env.dev");
dotenv::from_filename(".env").expect("Failed to load .env");
Comment on lines +230 to +231
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also read from .env.local?

Copy link
Contributor

@ray-kast ray-kast Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose not to as an attempt to enforce consistency in the test setup. Unsure if that will pay off, but if we need .env.local at some point I'll cave and add it.


let conn_args = super::ConnectArgs {
database_read_url: None,
database_write_url: Some(
std::env::var("DATABASE_URL")
.expect("Failed to load DATABASE_URL from environment"),
),
database_url: None,
};
let res = super::connect(conn_args, super::ConnectMode::Write { migrate: true })
.expect("Failed to connect to database");

super::migrate(
&*res
.pool
.get()
.expect("Failed to acquire database connection"),
"test seed migrations",
embedded_migrations::run_with_output,
)
.context("Failed to run database migrations")?;
.expect("Seed migrations failed");

res
}

match std::str::from_utf8(&out) {
Ok(s) => {
let s = s.trim();
lazy_static::lazy_static! {
static ref DATABASE: super::ConnectResult = initialize();
}

if !s.is_empty() {
info!("Output from migrations:\n{}", s);
}
},
Err(e) => warn!("Failed to read migration output: {}", e),
#[must_use]
pub fn connect() -> super::PooledConnection {
DATABASE
.pool
.get()
.expect("Failed to acquire database connection")
}

Ok(ConnectResult { pool, ty, migrated })
pub mod prelude {
pub use super::connect;
pub use crate::{db::custom_types::*, prelude::*};
}
}
Loading