|
@@ -803,3 +803,85 @@ mod query_equivalence {
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+mod injective_test {
|
|
|
+ use microrm::prelude::*;
|
|
|
+ use test_log::test;
|
|
|
+
|
|
|
+ struct AuthorBookRelation;
|
|
|
+ impl microrm::Relation for AuthorBookRelation {
|
|
|
+ type Domain = Author;
|
|
|
+ type Range = Book;
|
|
|
+
|
|
|
+ const NAME: &'static str = "AuthorBook";
|
|
|
+ const INJECTIVE: bool = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ struct Author {
|
|
|
+ #[key]
|
|
|
+ name: String,
|
|
|
+
|
|
|
+ works: microrm::AssocDomain<AuthorBookRelation>,
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ struct Book {
|
|
|
+ #[key]
|
|
|
+ title: String,
|
|
|
+
|
|
|
+ creator: microrm::AssocRange<AuthorBookRelation>,
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Database)]
|
|
|
+ struct ABDB {
|
|
|
+ authors: microrm::IDMap<Author>,
|
|
|
+ books: microrm::IDMap<Book>,
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn schema_creation_test() {
|
|
|
+ let db = ABDB::open_path(":memory:").expect("couldn't open in-memory database");
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn single_connection() {
|
|
|
+ let db = ABDB::open_path(":memory:").expect("couldn't open in-memory database");
|
|
|
+
|
|
|
+ let a1 = db.authors.insert_and_return(Author {
|
|
|
+ name: "Homer".to_string(),
|
|
|
+ works: Default::default(),
|
|
|
+ }).expect("couldn't insert author");
|
|
|
+
|
|
|
+ let a2 = db.authors.insert_and_return(Author {
|
|
|
+ name: "Virgil".to_string(),
|
|
|
+ works: Default::default(),
|
|
|
+ }).expect("couldn't insert author");
|
|
|
+
|
|
|
+ let b1_id = db.books.insert(Book {
|
|
|
+ title: "Odyssey".to_string(),
|
|
|
+ creator: Default::default(),
|
|
|
+ }).expect("couldn't insert book");
|
|
|
+ let b2_id = db.books.insert(Book {
|
|
|
+ title: "Aeneid".to_string(),
|
|
|
+ creator: Default::default(),
|
|
|
+ }).expect("couldn't insert book");
|
|
|
+
|
|
|
+ a1.works.connect_to(b1_id).expect("couldn't connect a1 and b1");
|
|
|
+ a2.works.connect_to(b2_id).expect("couldn't connect a2 and b2");
|
|
|
+
|
|
|
+ // we can't claim that Homer wrote the Aeneid because it's an injective relationship and
|
|
|
+ // only one Author can claim the Book as their work
|
|
|
+ match a1.works.connect_to(b2_id) {
|
|
|
+ Err(microrm::Error::ConstraintViolation(_)) => {
|
|
|
+ // all good
|
|
|
+ },
|
|
|
+ Err(_) => {
|
|
|
+ panic!("Unexpected error while testing injective connection");
|
|
|
+ },
|
|
|
+ Ok(_) => {
|
|
|
+ panic!("Unexpected success while testing injective connection");
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|