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

WIP: Fake relationship members #142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
76 changes: 70 additions & 6 deletions butane/tests/fake.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use butane::db::Connection;
use butane::{find, DataObject, ForeignKey};

use butane::{find, model, DataObject, ForeignKey, Many};
use butane_test_helper::*;

use fake::{Dummy, Fake, Faker};

mod common;
use common::blog::{Blog, Post, Tag};

fn fake_blog_post(conn: Connection) {
use fake::{Fake, Faker};

use common::blog::{Blog, Post, Tag};

let mut fake_blog: Blog = Faker.fake();
fake_blog.save(&conn).unwrap();

Expand All @@ -33,3 +31,69 @@ fn fake_blog_post(conn: Connection) {
assert_eq!(post_from_db.tags.load(&conn).unwrap().count(), 3);
}
testall!(fake_blog_post);

// We dont want the main struct's to have `Clone`, lest
// that becomes necessary without being noticed.
#[model]
#[derive(Clone, Debug, Dummy)]
struct ClonableBlog {
pub id: i64,
pub name: String,
}

#[model]
#[derive(Clone, Debug, Dummy)]
struct ClonablePost {
pub id: i64,
pub title: String,
pub tags: Many<ClonableTag>,
pub blog: ForeignKey<ClonableBlog>,
}

#[model]
#[table = "tags"]
#[derive(Clone, Debug, Dummy)]
pub struct ClonableTag {
#[pk]
pub tag: String,
}

/// Fake ForeignKey values can be accessed, but will not be saved
/// resulting in inability to load them from the database.
fn fake_auto_relationship_values(conn: Connection) {
let mut post: ClonablePost = Faker.fake();

// The ForeignKey value can be accessed before being saved
assert!(post.blog.get().is_ok());
// The Many has a value in it
assert_eq!(post.tags.get().unwrap().count(), 1);

let mut blog: ClonableBlog = post.blog.get().unwrap().clone();
blog.save(&conn).unwrap();
let blog_name = post.blog.get().unwrap().name.clone();
assert!(post.blog.load(&conn).is_ok());
assert_eq!(post.blog.load(&conn).unwrap().name, blog_name);

assert_ne!(post.blog.pk(), 0);

let mut tag: ClonableTag = post.tags.get().unwrap().next().unwrap().clone();
eprintln!("tag: {:?}", tag);
tag.save(&conn).unwrap();

let tag_from_db = find!(ClonableTag, tag == { tag.tag }, &conn).unwrap();
assert_eq!(tag_from_db.tag, tag_from_db.tag);

// With the Blog & Tag saved, we can save the Post,
// however the Many<Tag> wont be saved because they are in `all_values`.
post.save(&conn).unwrap();

let post_from_db = find!(ClonablePost, id == { post.id }, &conn).unwrap();
assert_eq!(post_from_db.title, post.title);

assert!(post_from_db.blog.load(&conn).is_ok());
assert_eq!(post_from_db.blog.load(&conn).unwrap().name, blog_name);

// The Many<T> were not saved
assert_eq!(post_from_db.tags.load(&conn).unwrap().count(), 0);
}
testall!(fake_auto_relationship_values);
15 changes: 10 additions & 5 deletions butane_core/src/fkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::borrow::Cow;
use std::fmt::{Debug, Formatter};

#[cfg(feature = "fake")]
use fake::{Dummy, Faker};
use fake::{Dummy, Faker, Fake};

/// Used to implement a relationship between models.
///
Expand Down Expand Up @@ -206,9 +206,14 @@ where
}

#[cfg(feature = "fake")]
/// Fake data support is currently limited to empty ForeignKey relationships.
impl<T: DataObject> Dummy<Faker> for ForeignKey<T> {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, _rng: &mut R) -> Self {
Self::new_raw()
/// A dummy value for T will be created, however it also has to have the Clone
/// trait in order for the mutable value to be obtained so that it can be saved.
impl<T: DataObject + Dummy<Faker>> Dummy<Faker> for ForeignKey<T> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we proceed with this, and .clone() is needed to get the T to be saved, IMO we should do:

-impl<T: DataObject + Dummy<Faker>>...
+impl<T: Clone + DataObject + Dummy<Faker>>...

and then document how to save the fake data.

fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, _rng: &mut R) -> Self
{
let obj = Faker.fake::<T>();
let ret = Self::new_raw();
ret.val.set(Box::new(obj)).ok();
ret
}
}
14 changes: 10 additions & 4 deletions butane_core/src/many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use std::borrow::Cow;

#[cfg(feature = "fake")]
use fake::{Dummy, Faker};
use fake::{Dummy, Fake, Faker};

fn default_oc<T>() -> OnceCell<Vec<T>> {
OnceCell::default()
Expand Down Expand Up @@ -215,9 +215,15 @@ impl<T: DataObject> Default for Many<T> {
}

#[cfg(feature = "fake")]
/// Fake data support is currently limited to empty Many relationships.
impl<T: DataObject> Dummy<Faker> for Many<T> {
/// Fake data support generates one value,
/// however it is impossible to the many relationship to it.
/// If we put them in `new_values`, we can only store the `.pk()`
/// which makes it impossible to save the value being related to.
impl<T: DataObject + Dummy<Faker>> Dummy<Faker> for Many<T> {
fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, _rng: &mut R) -> Self {
Self::new()
let obj = Faker.fake::<T>();
let ret = Self::new();
ret.all_values.set(vec![obj]).ok();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Values in all_values are presumed to be already saved, and thus not savable.

The only way I can think to get this working is: a pub fn which loads from the db, and compares it with all parts of this Many to determine which DML is needed to "sync" to the db. This could be of value if there are multiple threads, and the current thread wants to make its version of the Many<T> to be authoritative, despite changes that other threads may have made to the same Many<T>. There are several minefields down that path.

ret
}
}