aboutsummaryrefslogtreecommitdiff
path: root/examples/tutorial02/main.rs
blob: bac20e132b0cb419e31b66fb73edfa1c902d7474 (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
62
63
64
#[macro_use] extern crate diesel;

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

mod polls;

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

fn settings() -> Settings<impl Connectable> {
    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(),
            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]);
    }
}