use super::{Column, Describe, EdgeShape, EdgeTable, SideTable, Table, describes};
use crate::{ColumnName, ColumnShape, FieldName, RecordAddress};
struct Db;
impl crate::Habitat for Db {
type Blueprint = Table;
}
struct Media;
struct Clip;
const MEDIA_COLUMNS: &[ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("id")),
ColumnShape::Plain(ColumnName::new("title")),
];
const CLIP_COLUMNS: &[ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("media_id")),
ColumnShape::Plain(ColumnName::new("id")),
ColumnShape::Plain(ColumnName::new("title")),
ColumnShape::Plain(ColumnName::new("bit_rate")),
];
impl Describe<Db> for Media {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]),
&[
Column::new(FieldName::new("id"), "INTEGER", false),
Column::new(FieldName::new("title"), "TEXT", true),
],
)
.with_indexes(&[FieldName::new("title")]);
}
impl Describe<Db> for Clip {
const BLUEPRINT: Table = Table::new(
RecordAddress::new(
FieldName::new("clip"),
&[FieldName::new("media_id"), FieldName::new("id")],
),
&[
Column::new(FieldName::new("media_id"), "INTEGER", false),
Column::new(FieldName::new("id"), "INTEGER", false),
Column::new(FieldName::new("title"), "TEXT", true),
Column::new(FieldName::new("bit_rate"), "INTEGER", false),
],
)
.with_edges(&[
EdgeTable::new(&[FieldName::new("tags")], EdgeShape::Sequence, "TEXT"),
EdgeTable::new(&[FieldName::new("notes")], EdgeShape::Set, "TEXT"),
]);
}
const _: () = assert!(describes(
&<Media as Describe<Db>>::BLUEPRINT,
MEDIA_COLUMNS
));
const _: () = assert!(describes(&<Clip as Describe<Db>>::BLUEPRINT, CLIP_COLUMNS));
#[test]
fn a_bare_descriptor_carries_no_index_no_edge_and_no_discriminant() {
let table = <Media as Describe<Db>>::BLUEPRINT;
assert_eq!(table.edges(), &[]);
assert_eq!(table.source(), None);
let clip = <Clip as Describe<Db>>::BLUEPRINT;
assert_eq!(clip.indexes(), &[] as &[&FieldName]);
}
#[test]
fn the_address_is_the_rows_own_and_not_a_second_spelling() {
let clip = <Clip as Describe<Db>>::BLUEPRINT;
assert_eq!(clip.address().table(), FieldName::new("clip"));
assert_eq!(
clip.address().keys(),
[FieldName::new("media_id"), FieldName::new("id")]
);
}
#[test]
fn a_key_column_is_typed_by_the_column_list_that_carries_it() {
let clip = <Clip as Describe<Db>>::BLUEPRINT;
assert_eq!(clip.stored(FieldName::new("media_id")), "INTEGER");
assert_eq!(clip.stored(FieldName::new("title")), "TEXT");
}
#[test]
#[should_panic(expected = "this table carries no such column")]
fn a_column_the_table_does_not_carry_has_no_stored_type() {
let _ = <Media as Describe<Db>>::BLUEPRINT.stored(FieldName::new("bit_rate"));
}
#[test]
fn the_audit_reads_the_column_order_and_not_only_the_names() {
const SWAPPED: &[ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("title")),
ColumnShape::Plain(ColumnName::new("id")),
];
assert!(!describes(&<Media as Describe<Db>>::BLUEPRINT, SWAPPED));
assert!(describes(
&<Media as Describe<Db>>::BLUEPRINT,
MEDIA_COLUMNS
));
}
#[test]
fn the_audit_reads_the_width() {
assert!(!describes(
&<Clip as Describe<Db>>::BLUEPRINT,
&[ColumnShape::Plain(ColumnName::new("media_id"))]
));
}
#[test]
fn a_sequence_carries_an_ordinal_and_a_set_does_not() {
assert!(EdgeShape::Sequence.ordered());
assert!(!EdgeShape::Set.ordered());
}
const SPLIT: Table = Table::new(
RecordAddress::new(FieldName::new("caption"), &[FieldName::new("id")]),
&[
Column::new(FieldName::new("id"), "INTEGER", false),
Column::new(FieldName::new("__ig_source__"), "TEXT", false),
],
)
.with_provenance(
FieldName::new("__ig_source__"),
&[SideTable::new(
FieldName::new("caption_frame"),
&[Column::new(FieldName::new("media_id"), "INTEGER", false)],
)],
);
#[test]
fn the_split_layout_is_read_back_through_the_names_it_was_declared_with() {
assert_eq!(SPLIT.source(), Some(FieldName::new("__ig_source__")));
assert_eq!(
SPLIT
.sides()
.iter()
.map(|side| side.table().as_str())
.collect::<Vec<_>>(),
["caption_frame"]
);
assert_eq!(<Media as Describe<Db>>::BLUEPRINT.source(), None);
assert!(<Media as Describe<Db>>::BLUEPRINT.sides().is_empty());
}
#[test]
fn the_discriminant_is_one_of_the_tables_own_columns() {
assert!(describes(
&SPLIT,
&[
ColumnShape::Plain(ColumnName::new("id")),
ColumnShape::Plain(ColumnName::new("__ig_source__"))
]
));
}
#[test]
fn the_id_a_provenance_row_points_at_is_the_keys_last_column() {
assert_eq!(SPLIT.id(), FieldName::new("id"));
assert_eq!(<Clip as Describe<Db>>::BLUEPRINT.id(), FieldName::new("id"));
assert_eq!(
<Media as Describe<Db>>::BLUEPRINT.id(),
FieldName::new("id")
);
}
const AT_PLAIN: &[ColumnShape] = &[ColumnShape::Plain(ColumnName::new("at"))];
const AT_NULLABLE: &[ColumnShape] = &[ColumnShape::Nullable(ColumnName::new("at"))];
const AT_DERIVED: &[ColumnShape] = &[ColumnShape::Derived(ColumnName::new("at"))];
const _: () = assert!(super::same_shapes(AT_PLAIN, AT_PLAIN));
const _: () = assert!(super::same_shapes(AT_NULLABLE, AT_NULLABLE));
const _: () = assert!(super::same_shapes(AT_DERIVED, AT_DERIVED));
const _: () = assert!(!super::same_shapes(AT_PLAIN, AT_NULLABLE));
const _: () = assert!(!super::same_shapes(AT_NULLABLE, AT_PLAIN));
const _: () = assert!(!super::same_shapes(AT_PLAIN, AT_DERIVED));
const _: () = assert!(!super::same_shapes(AT_DERIVED, AT_PLAIN));
const _: () = assert!(!super::same_shapes(AT_NULLABLE, AT_DERIVED));
const _: () = assert!(!super::same_shapes(AT_DERIVED, AT_NULLABLE));
const ON_PLAIN: &[ColumnShape] = &[ColumnShape::Nullable(ColumnName::new("on"))];
const ON_DERIVED: &[ColumnShape] = &[ColumnShape::Derived(ColumnName::new("on"))];
const _: () = assert!(!super::same_shapes(AT_NULLABLE, ON_PLAIN));
const _: () = assert!(!super::same_shapes(AT_DERIVED, ON_DERIVED));
const STAMPED_COLUMNS: &[ColumnShape] = &[
ColumnShape::Plain(ColumnName::new("id")),
ColumnShape::Nullable(ColumnName::new("at_nanos")),
ColumnShape::Derived(ColumnName::new("area")),
];
const STAMPED: Table = Table::shaped(
RecordAddress::new(FieldName::new("stamped"), &[FieldName::new("id")]),
STAMPED_COLUMNS,
&[&["INTEGER"], &["INTEGER"], &["INTEGER"]],
&[false, false, false],
);
const _: () = assert!(describes(&STAMPED, STAMPED_COLUMNS));
const _: () = assert!(!describes(
&STAMPED,
&[
ColumnShape::Plain(ColumnName::new("id")),
ColumnShape::Plain(ColumnName::new("at_nanos")),
ColumnShape::Plain(ColumnName::new("area")),
]
));