Skip to content

Commit

Permalink
test: Add tests for schema and check helper methods (#289)
Browse files Browse the repository at this point in the history
Also fix a minor issue with how `TestCase` parses thread names to create
a description
  • Loading branch information
spencewenski authored Jul 24, 2024
1 parent d1cc47a commit 93c5bb6
Show file tree
Hide file tree
Showing 19 changed files with 246 additions and 4 deletions.
50 changes: 50 additions & 0 deletions src/migration/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,53 @@ where
{
Expr::expr(Func::char_length(Expr::col(name))).gte(len)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::testing::snapshot::TestCase;
use insta::assert_snapshot;
use rstest::{fixture, rstest};
use sea_orm_migration::schema::string;

#[derive(DeriveIden)]
pub(crate) enum Foo {
Table,
Bar,
}

#[fixture]
fn case() -> TestCase {
Default::default()
}

#[fixture]
#[cfg_attr(coverage_nightly, coverage(off))]
fn table_stmt() -> TableCreateStatement {
Table::create().table(Foo::Table).to_owned()
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn str_not_empty(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(string(Foo::Bar).check(super::str_not_empty(Foo::Bar)));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn str_len_gt(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(string(Foo::Bar).check(super::str_len_gt(Foo::Bar, 1)));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn str_len_gte(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(string(Foo::Bar).check(super::str_len_gte(Foo::Bar, 1)));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}
}
84 changes: 84 additions & 0 deletions src/migration/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,87 @@ where
{
pk_uuid(name).default(default).to_owned()
}

#[cfg(test)]
mod tests {
use super::*;
use crate::testing::snapshot::TestCase;
use insta::assert_snapshot;
use rstest::{fixture, rstest};

#[derive(DeriveIden)]
pub(crate) enum Foo {
Table,
Bar,
}

#[fixture]
fn case() -> TestCase {
Default::default()
}

#[fixture]
#[cfg_attr(coverage_nightly, coverage(off))]
fn table_stmt() -> TableCreateStatement {
Table::create().table(Foo::Table).to_owned()
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn table() {
let statement = super::table(Foo::Table);

assert_snapshot!(statement.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn timestamps(_case: TestCase, table_stmt: TableCreateStatement) {
let table_stmt = super::timestamps(table_stmt);

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_bigint_auto(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_bigint_auto(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuid(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuid(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuidv4(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuidv4(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuidv7(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuidv7(Foo::Bar));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}

#[rstest]
#[cfg_attr(coverage_nightly, coverage(off))]
fn pk_uuid_default(_case: TestCase, mut table_stmt: TableCreateStatement) {
table_stmt.col(super::pk_uuid_default(
Foo::Bar,
Expr::cust("custom_uuid_fn()"),
));

assert_snapshot!(table_stmt.to_string(PostgresQueryBuilder));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/check.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" varchar NOT NULL CHECK (CHAR_LENGTH("bar") > 1) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/check.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" varchar NOT NULL CHECK (CHAR_LENGTH("bar") >= 1) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/check.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" varchar NOT NULL CHECK (CHAR_LENGTH("bar") > 0) )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" bigserial NOT NULL PRIMARY KEY )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT custom_uuid_fn() )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT uuid_generate_v4() )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "bar" uuid PRIMARY KEY DEFAULT uuid_generate_v7() )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: statement.to_string(PostgresQueryBuilder)
---
CREATE TABLE IF NOT EXISTS "foo" ( "created_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP )
5 changes: 5 additions & 0 deletions src/migration/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/migration/schema.rs
expression: table_stmt.to_string(PostgresQueryBuilder)
---
CREATE TABLE "foo" ( "created_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP )
2 changes: 2 additions & 0 deletions src/migration/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use insta::assert_debug_snapshot;
use itertools::Itertools;

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn user_migrator_migrations() {
let user_migrations = UserMigrator::migrations()
.into_iter()
Expand All @@ -12,6 +13,7 @@ fn user_migrator_migrations() {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn user_migrator_migrations_no_int_pk() {
let user_migrations = UserMigrator::migrations()
.into_iter()
Expand Down
39 changes: 35 additions & 4 deletions src/testing/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::util::regex::UUID_REGEX;
use insta::internals::SettingsBindDropGuard;
use insta::Settings;
use itertools::Itertools;
use std::thread::current;
use typed_builder::TypedBuilder;

Expand Down Expand Up @@ -201,22 +202,38 @@ pub fn snapshot_redact_uuid(settings: &mut Settings) -> &mut Settings {
/// See: <https://github.com/la10736/rstest/issues/177>
fn description_from_current_thread() -> String {
let thread_name = current().name().unwrap_or("").to_string();
let description = thread_name
description_from_thread_name(&thread_name)
}

fn description_from_thread_name(name: &str) -> String {
let description = name
.split("::")
.map(|item| item.split('_').skip(2).collect::<Vec<&str>>().join("_"))
.map(|item| {
if item.starts_with("case_") {
item.split('_').skip(2).join("_")
} else {
item.to_string()
}
})
.last()
.filter(|s| !s.is_empty())
.unwrap_or(thread_name.split("::").last().unwrap().to_string());
.unwrap_or(name.split("::").last().unwrap().to_string());
description
}

#[cfg(test)]
mod tests {
use super::*;
use insta::assert_snapshot;
use rstest::rstest;
use rstest::{fixture, rstest};
use uuid::Uuid;

#[fixture]
#[cfg_attr(coverage_nightly, coverage(off))]
fn case() -> TestCase {
Default::default()
}

#[rstest]
#[case(0, false)]
#[case::rstest_description(1, false)]
Expand Down Expand Up @@ -260,11 +277,25 @@ mod tests {
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn uuid() {
let _case = TestCase::new();

let uuid = Uuid::new_v4();

assert_snapshot!(format!("Foo '{uuid}' bar"));
}

#[rstest]
#[case("")]
#[case("foo")]
#[case("foo::bar")]
#[case("foo::bar::x_y_z_1_2_3")]
#[case("foo::bar::case_1_x_y_z_1_2_3")]
#[cfg_attr(coverage_nightly, coverage(off))]
fn description_from_thread_name(_case: TestCase, #[case] name: &str) {
let description = super::description_from_thread_name(name);

assert_snapshot!(description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: description
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: description
---
foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: description
---
bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: description
---
x_y_z_1_2_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/testing/snapshot.rs
expression: description
---
x_y_z_1_2_3

0 comments on commit 93c5bb6

Please sign in to comment.