Skip to content

Commit 0684ca1

Browse files
committed
Add test
1 parent 329367a commit 0684ca1

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

lib/src/config.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ impl ConfigBuilder {
6868
|| self.user.is_none()
6969
|| self.password.is_none()
7070
|| self.fetch_size.is_none()
71+
|| self.max_connections.is_none()
72+
|| self.db.is_none()
7173
{
7274
Err(Error::InvalidConfig)
7375
} else {
76+
//The config attributes are validated before unwrapping
7477
Ok(Config {
7578
uri: self.uri.unwrap(),
7679
user: self.user.unwrap(),
@@ -94,3 +97,64 @@ pub fn config() -> ConfigBuilder {
9497
fetch_size: Some(DEFAULT_FETCH_SIZE),
9598
}
9699
}
100+
101+
#[cfg(test)]
102+
mod tests {
103+
use super::*;
104+
105+
#[tokio::test]
106+
async fn should_build_config() {
107+
let config = config()
108+
.uri("127.0.0.1:7687")
109+
.user("some_user")
110+
.password("some_password")
111+
.db("some_db")
112+
.fetch_size(10)
113+
.max_connections(5)
114+
.build()
115+
.unwrap();
116+
assert_eq!(config.uri, "127.0.0.1:7687");
117+
assert_eq!(config.user, "some_user");
118+
assert_eq!(config.password, "some_password");
119+
assert_eq!(config.db, "some_db");
120+
assert_eq!(config.fetch_size, 10);
121+
assert_eq!(config.max_connections, 5);
122+
}
123+
124+
#[tokio::test]
125+
async fn should_build_with_defaults() {
126+
let config = config()
127+
.uri("127.0.0.1:7687")
128+
.user("some_user")
129+
.password("some_password")
130+
.build()
131+
.unwrap();
132+
assert_eq!(config.uri, "127.0.0.1:7687");
133+
assert_eq!(config.user, "some_user");
134+
assert_eq!(config.password, "some_password");
135+
assert_eq!(config.db, "");
136+
assert_eq!(config.fetch_size, 200);
137+
assert_eq!(config.max_connections, 16);
138+
}
139+
140+
#[tokio::test]
141+
async fn should_reject_invalid_config() {
142+
assert!(config()
143+
.user("some_user")
144+
.password("some_password")
145+
.build()
146+
.is_err());
147+
148+
assert!(config()
149+
.uri("127.0.0.1:7687")
150+
.password("some_password")
151+
.build()
152+
.is_err());
153+
154+
assert!(config()
155+
.uri("127.0.0.1:7687")
156+
.user("some_user")
157+
.build()
158+
.is_err());
159+
}
160+
}

0 commit comments

Comments
 (0)