aboutsummaryrefslogtreecommitdiff
path: root/examples/tutorial02/main.rs
blob: 5d78e5ba21b6f141bbc52122981bd4ed9030ca67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#[macro_use]
extern crate diesel;

use tosin::contrib::admin;
use tosin::urls::{url_map, UrlMap};
use tosin::Settings;

mod polls;

fn urls() -> UrlMap {
    url_map! {
        "polls" / ..polls::urls(),
        "admin" / ..admin::site::urls(),
    }
}

fn settings() -> Settings {
    Settings {
        installed_apps: &[&polls::APP, &admin::APP],
        ..Settings::default()
    }
}

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(),
            chrono::Local::now().naive_local(),
        );
        // 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]);
    }
}