aboutsummaryrefslogtreecommitdiff
path: root/examples/tutorial02
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tutorial02')
-rw-r--r--examples/tutorial02/main.rs37
-rw-r--r--examples/tutorial02/polls/models.rs4
2 files changed, 39 insertions, 2 deletions
diff --git a/examples/tutorial02/main.rs b/examples/tutorial02/main.rs
index 8f0eebe..bac20e1 100644
--- a/examples/tutorial02/main.rs
+++ b/examples/tutorial02/main.rs
@@ -25,3 +25,40 @@ fn settings() -> Settings<impl Connectable> {
}
tosin::main!(urls(), settings());
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use diesel::prelude::*;
+
+ #[test]
+ fn test_models() {
+ use polls::models::{Choice, Question, choice, question};
+
+ let settings = settings();
+ let connection = settings.database.connect().unwrap();
+
+ // get the list of all questions
+ let all_questions = question::table.load::<Question>(&connection).unwrap();
+ assert!(all_questions.is_empty());
+
+ // make a new one
+ let mut q = Question::new(
+ "What's new?".to_string(),
+ time::PrimitiveDateTime::now(),
+ );
+ // save it
+ q.save_mut();
+ // it's got an id now!
+ assert!(q.id().is_some());
+ // it's still got all the same fields!
+ assert_eq!(q.question_text(), "What's new?");
+ // we can change them!
+ q.set_question_text("What's up?");
+ q.save_mut();
+
+ // it should be in the list now, too!!
+ let all_questions = question::table.load::<Question>(&connection).unwrap();
+ assert_eq!(all_questions, vec![q]);
+ }
+}
diff --git a/examples/tutorial02/polls/models.rs b/examples/tutorial02/polls/models.rs
index fb8be8f..d62f219 100644
--- a/examples/tutorial02/polls/models.rs
+++ b/examples/tutorial02/polls/models.rs
@@ -1,6 +1,6 @@
use tosin::db::models::{Model, Id, gather};
-#[derive(Model)]
+#[derive(Model, PartialEq, Debug)]
pub struct Question {
id: Option<Id>,
#[model(max_length=200)]
@@ -9,7 +9,7 @@ pub struct Question {
pub_date: time::PrimitiveDateTime,
}
-#[derive(Model)]
+#[derive(Model, PartialEq, Debug)]
pub struct Choice {
id: Option<Id>,
#[model(Question, on_delete=Cascade)]