Skip to content

Commit f68a8bd

Browse files
committed
test: 커버리지 보정
1 parent a69138e commit f68a8bd

4 files changed

Lines changed: 101 additions & 5 deletions

File tree

crates/vespertide-cli/src/commands/export.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,29 @@ mod tests {
859859
assert_eq!(out_py, Path::new("src/models/users.py"));
860860
}
861861

862+
#[test]
863+
fn build_output_path_prisma_uses_prisma_extension() {
864+
use std::path::Path;
865+
let root = Path::new("src/models");
866+
let out = build_output_path(root, Path::new("user.json"), Orm::Prisma);
867+
assert_eq!(out, Path::new("src/models/user.prisma"));
868+
}
869+
870+
#[tokio::test]
871+
#[serial]
872+
async fn clean_export_dir_removes_stale_prisma_files() {
873+
let tmp = tempdir().unwrap();
874+
let root = tmp.path().join("out");
875+
std_fs::create_dir_all(&root).unwrap();
876+
std_fs::write(root.join("schema.prisma"), "stale").unwrap();
877+
std_fs::write(root.join("keep.txt"), "keep").unwrap();
878+
879+
clean_export_dir(&root, Orm::Prisma).await.unwrap();
880+
881+
assert!(!root.join("schema.prisma").exists());
882+
assert!(root.join("keep.txt").exists());
883+
}
884+
862885
#[test]
863886
fn build_output_path_handles_special_path_components() {
864887
use std::path::Path;

crates/vespertide-exporter/src/prisma/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,17 @@ mod tests {
201201
assert!(schema.contains("provider = \"postgresql\""));
202202
}
203203

204+
#[test]
205+
fn render_schema_emits_shared_enum_block_once() {
206+
let config = PrismaConfig::default();
207+
let t1 = crate::tests::fixtures::enum_shared();
208+
let mut t2 = crate::tests::fixtures::enum_shared();
209+
t2.name = "archived_documents".into();
210+
let schema = PrismaExporterWithConfig::new(&config).render_schema(&[t1, t2]);
211+
212+
assert_eq!(schema.matches("enum DocStatus {").count(), 1);
213+
}
214+
204215
#[test]
205216
fn render_schema_emits_relation_mode_when_configured() {
206217
// `PrismaConfig` is `#[non_exhaustive]` in a different crate, so it can't be

crates/vespertide-exporter/src/prisma/render.rs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,11 @@ pub(super) fn render_model(
351351
for c in &table.constraints {
352352
if let TableConstraint::Index { name, columns } = c {
353353
let cols = columns.join(", ");
354-
if let Some(n) = name {
355-
lines.push(format!(" @@index([{cols}], map: \"{n}\")"));
356-
} else {
357-
lines.push(format!(" @@index([{cols}])"));
354+
// `match` instead of `if let Some`: LLVM coverage attributes match
355+
// arms reliably where this if/else was misattributed as uncovered.
356+
match name {
357+
Some(n) => lines.push(format!(" @@index([{cols}], map: \"{n}\")")),
358+
None => lines.push(format!(" @@index([{cols}])")),
358359
}
359360
}
360361
}
@@ -467,10 +468,65 @@ fn infer_relation_field_name(fk_col: &str) -> String {
467468
#[cfg(test)]
468469
mod tests {
469470
use rstest::rstest;
470-
use vespertide_core::schema::column::SimpleColumnType;
471+
use vespertide_core::schema::column::{NumValue, SimpleColumnType};
471472

472473
use super::*;
473474

475+
#[rstest]
476+
#[case::cascade(ReferenceAction::Cascade, "Cascade")]
477+
#[case::restrict(ReferenceAction::Restrict, "Restrict")]
478+
#[case::set_null(ReferenceAction::SetNull, "SetNull")]
479+
#[case::set_default(ReferenceAction::SetDefault, "SetDefault")]
480+
#[case::no_action(ReferenceAction::NoAction, "NoAction")]
481+
fn reference_actions_map_to_prisma(#[case] action: ReferenceAction, #[case] expected: &str) {
482+
assert_eq!(reference_action_to_prisma(&action), expected);
483+
}
484+
485+
#[test]
486+
fn integer_enum_default_resolves_numeric_value_to_variant_name() {
487+
let ty = ColumnType::Complex(ComplexColumnType::Enum {
488+
name: "priority".into(),
489+
values: EnumValues::Integer(vec![
490+
NumValue {
491+
name: "low".into(),
492+
value: 100,
493+
},
494+
NumValue {
495+
name: "high".into(),
496+
value: 200,
497+
},
498+
]),
499+
});
500+
assert_eq!(
501+
prisma_default_attr("100", &ty, PrismaProvider::Postgres),
502+
"@default(LOW)"
503+
);
504+
}
505+
506+
#[test]
507+
fn fk_on_update_action_is_rendered() {
508+
let mut table = crate::tests::fixtures::table_with_fk();
509+
for c in &mut table.constraints {
510+
if let TableConstraint::ForeignKey { on_update, .. } = c {
511+
*on_update = Some(ReferenceAction::Cascade);
512+
}
513+
}
514+
let rendered = render_model(
515+
&table,
516+
std::slice::from_ref(&table),
517+
PrismaProvider::default(),
518+
);
519+
assert!(rendered.contains("onUpdate: Cascade"));
520+
}
521+
522+
#[test]
523+
fn named_index_is_rendered_with_map() {
524+
let table = crate::tests::fixtures::table_with_indexes();
525+
let rendered = render_model(&table, &[], PrismaProvider::default());
526+
assert!(rendered.contains("@@index([created_at], map: \"idx_articles_created_at\")"));
527+
assert!(rendered.contains("@@index([title])"));
528+
}
529+
474530
#[rstest]
475531
#[case::postgres(PrismaProvider::Postgres, "@default(uuid())")]
476532
#[case::mysql(PrismaProvider::MySql, "@default(dbgenerated(\"(uuid())\"))")]

crates/vespertide-exporter/src/prisma/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub(super) fn column_type_to_prisma(
5252
("String", None, Some("@db.Text"))
5353
}
5454
// Unknown/future simple types fall back to a plain String column.
55+
// `SimpleColumnType` is #[non_exhaustive]; every current variant is
56+
// matched above, so this arm is currently unreachable.
57+
#[cfg(not(tarpaulin_include))]
5558
_ => ("String", None, None),
5659
};
5760
let native = match provider {
@@ -82,6 +85,9 @@ pub(super) fn column_type_to_prisma(
8285
(format!("{pascal}{q}"), None)
8386
}
8487
// Unknown/future complex types fall back to a plain String column.
88+
// `ComplexColumnType` is #[non_exhaustive]; every current variant is
89+
// matched above, so this arm is currently unreachable.
90+
#[cfg(not(tarpaulin_include))]
8591
_ => (format!("String{q}"), None),
8692
},
8793
}

0 commit comments

Comments
 (0)