summaryrefslogtreecommitdiffstats
path: root/tests/defaults.rs
blob: f740259649aa0317893564986a7ac9c1d41d9cd4 (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
use serde_derive::{Deserialize, Serialize};

use config::Config;

#[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
    pub db_host: String,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            db_host: String::from("default"),
        }
    }
}

#[test]
fn set_defaults() {
    let c = Config::default();
    let s: Settings = c.try_deserialize().expect("Deserialization failed");

    assert_eq!(s.db_host, "default");
}

#[test]
fn try_from_defaults() {
    let c = Config::try_from(&Settings::default()).expect("Serialization failed");
    let s: Settings = c.try_deserialize().expect("Deserialization failed");
    assert_eq!(s.db_host, "default");
}