Skip to content

Commit 3e52d96

Browse files
Merge pull request #7 from theseus-rs/improve-code-coverage
test: add tests to improve code coverage
2 parents c85ef6a + 78423b4 commit 3e52d96

File tree

9 files changed

+169
-44
lines changed

9 files changed

+169
-44
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ bundled with your application, or downloaded on demand.
1616

1717
### Example
1818
```rust
19-
use postgresql_embedded::PostgreSQL;
19+
use postgresql_embedded::{PostgreSQL, Result};
2020

2121
#[tokio::main]
22-
async fn main() {
22+
async fn main() -> Result<()> {
2323
let mut postgresql = PostgreSQL::default();
24-
postgresql.setup().await.unwrap();
25-
postgresql.start().await.unwrap();
24+
postgresql.setup().await?;
25+
postgresql.start().await?;
2626

2727
let database_name = "test";
28-
postgresql.create_database(database_name).await.unwrap();
29-
postgresql.database_exists(database_name).await.unwrap();
30-
postgresql.drop_database(database_name).await.unwrap();
28+
postgresql.create_database(database_name).await?;
29+
postgresql.database_exists(database_name).await?;
30+
postgresql.drop_database(database_name).await?;
3131

32-
postgresql.stop().await.unwrap();
32+
postgresql.stop().await
3333
}
3434
```
3535

postgresql_archive/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ A library for downloading and extracting PostgreSQL archives from
1515
### Asynchronous API
1616

1717
```rust
18-
use postgresql_archive::{extract, get_archive, LATEST};
18+
use postgresql_archive::{extract, get_archive, Result, LATEST};
1919

2020
#[tokio::main]
21-
async fn main() {
22-
let (archive_version, archive, hash) = get_archive(&LATEST).await.unwrap();
21+
async fn main() -> Result<()> {
22+
let (archive_version, archive, hash) = get_archive(&LATEST).await?;
2323
let out_dir = std::env::temp_dir();
24-
let result = extract(&archive, &out_dir).await.unwrap();
24+
extract(&archive, &out_dir).await;
2525
}
2626
```
2727

2828
### Synchronous API
2929
```rust
30-
use postgresql_archive::LATEST;
30+
use postgresql_archive::{Result, LATEST};
3131
use postgresql_archive::blocking::{extract, get_archive};
3232

33-
fn main() {
34-
let (archive_version, archive, hash) = get_archive(&LATEST).unwrap();
33+
fn main() -> Result<()> {
34+
let (archive_version, archive, hash) = get_archive(&LATEST);
3535
let out_dir = std::env::temp_dir();
36-
let result = extract(&archive, &out_dir).unwrap();
36+
extract(&archive, &out_dir)
3737
}
3838
```
3939

postgresql_archive/src/error.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,64 @@ impl From<anyhow::Error> for ArchiveError {
7070
ArchiveError::Unexpected(error.to_string())
7171
}
7272
}
73+
74+
/// These are relatively low value tests; they are here to reduce the coverage gap and
75+
/// ensure that the error conversions are working as expected.
76+
#[cfg(test)]
77+
mod test {
78+
use super::*;
79+
use std::path::PathBuf;
80+
use std::str::FromStr;
81+
82+
#[test]
83+
fn test_from_regex_error() {
84+
let regex_error = regex::Error::Syntax("test".to_string());
85+
let error = ArchiveError::from(regex_error);
86+
assert_eq!(error.to_string(), "test");
87+
}
88+
89+
#[tokio::test]
90+
async fn test_from_reqwest_error() {
91+
let result = reqwest::get("https://a.com").await;
92+
assert!(result.is_err());
93+
if let Err(error) = result {
94+
let error = ArchiveError::from(error);
95+
assert!(error.to_string().contains("https://a.com"));
96+
}
97+
}
98+
99+
#[test]
100+
fn test_from_io_error() {
101+
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "test");
102+
let error = ArchiveError::from(io_error);
103+
assert_eq!(error.to_string(), "test");
104+
}
105+
106+
#[test]
107+
fn test_from_parse_int_error() {
108+
let result = u64::from_str("test");
109+
assert!(result.is_err());
110+
if let Err(error) = result {
111+
let error = ArchiveError::from(error);
112+
assert_eq!(error.to_string(), "invalid digit found in string");
113+
}
114+
}
115+
116+
#[test]
117+
fn test_from_strip_prefix_error() {
118+
let path = PathBuf::from("test");
119+
let result = path.strip_prefix("foo");
120+
assert!(result.is_err());
121+
if let Err(error) = result {
122+
let error = ArchiveError::from(error);
123+
assert_eq!(error.to_string(), "prefix not found");
124+
}
125+
}
126+
127+
#[test]
128+
fn test_from_anyhow_error() {
129+
let anyhow_error = anyhow::Error::msg("test");
130+
let error = ArchiveError::from(anyhow_error);
131+
assert_eq!(error.to_string(), "test");
132+
}
133+
}

postgresql_archive/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
//! ### Asynchronous API
2323
//!
2424
//! ```no_run
25-
//! use postgresql_archive::{extract, get_archive, LATEST};
25+
//! use postgresql_archive::{extract, get_archive, Result, LATEST};
2626
//!
2727
//! #[tokio::main]
28-
//! async fn main() {
29-
//! let (archive_version, archive, hash) = get_archive(&LATEST).await.unwrap();
28+
//! async fn main() -> Result<()> {
29+
//! let (archive_version, archive, hash) = get_archive(&LATEST).await?;
3030
//! let out_dir = std::env::temp_dir();
31-
//! let result = extract(&archive, &out_dir).await.unwrap();
31+
//! extract(&archive, &out_dir).await
3232
//! }
3333
//! ```
3434
//!

postgresql_embedded/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,39 @@ bundled with your application, or downloaded on demand.
1515
### Asynchronous API
1616

1717
```rust
18-
use postgresql_embedded::PostgreSQL;
18+
use postgresql_embedded::{PostgreSQL, Result};
1919

2020
#[tokio::main]
21-
async fn main() {
21+
async fn main() -> Result<()> {
2222
let mut postgresql = PostgreSQL::default();
23-
postgresql.setup().await.unwrap();
24-
postgresql.start().await.unwrap();
23+
postgresql.setup().await?;
24+
postgresql.start().await?;
2525

2626
let database_name = "test";
27-
postgresql.create_database(database_name).await.unwrap();
28-
postgresql.database_exists(database_name).await.unwrap();
29-
postgresql.drop_database(database_name).await.unwrap();
27+
postgresql.create_database(database_name).await?;
28+
postgresql.database_exists(database_name).await?;
29+
postgresql.drop_database(database_name).await?;
3030

31-
postgresql.stop().await.unwrap();
31+
postgresql.stop().await;
3232
}
3333
```
3434

3535
### Synchronous API
3636
```rust
37+
use postgresql_embedded::Result;
3738
use postgresql_embedded::blocking::PostgreSQL;
3839

39-
fn main() {
40+
fn main() -> Result<()> {
4041
let mut postgresql = PostgreSQL::default();
41-
postgresql.setup().unwrap();
42-
postgresql.start().unwrap();
42+
postgresql.setup()?;
43+
postgresql.start()?;
4344

4445
let database_name = "test";
45-
postgresql.create_database(database_name).unwrap();
46-
postgresql.database_exists(database_name).unwrap();
47-
postgresql.drop_database(database_name).unwrap();
46+
postgresql.create_database(database_name)?;
47+
postgresql.database_exists(database_name)?;
48+
postgresql.drop_database(database_name)?;
4849

49-
postgresql.stop().unwrap();
50+
postgresql.stop()
5051
}
5152
```
5253

postgresql_embedded/src/blocking/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,17 @@ impl PostgreSQL {
8282
.block_on(async move { self.inner.drop_database(database_name).await })
8383
}
8484
}
85+
86+
#[cfg(test)]
87+
mod test {
88+
use super::*;
89+
90+
#[test]
91+
fn test_postgresql() {
92+
let version = Version::new(16, Some(2), Some(0));
93+
let postgresql = PostgreSQL::new(version, Settings::default());
94+
let initial_statuses = [Status::NotInstalled, Status::Installed, Status::Stopped];
95+
assert!(initial_statuses.contains(&postgresql.status()));
96+
assert_eq!(postgresql.version(), &version);
97+
}
98+
}

postgresql_embedded/src/error.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,52 @@ impl From<tokio::time::error::Elapsed> for EmbeddedError {
7474
EmbeddedError::TimeoutError(error.into())
7575
}
7676
}
77+
78+
/// These are relatively low value tests; they are here to reduce the coverage gap and
79+
/// ensure that the error conversions are working as expected.
80+
#[cfg(test)]
81+
mod test {
82+
use super::*;
83+
84+
#[test]
85+
fn test_from_archive_error() {
86+
let archive_error = ArchiveError::ReleaseNotFound("test".to_string());
87+
let error = EmbeddedError::from(archive_error);
88+
assert_eq!(error.to_string(), "release not found for version [test]");
89+
}
90+
91+
#[test]
92+
fn test_from_io_error() {
93+
let io_error = std::io::Error::new(std::io::ErrorKind::Other, "test");
94+
let error = EmbeddedError::from(io_error);
95+
assert_eq!(error.to_string(), "test");
96+
}
97+
98+
#[test]
99+
fn test_from_utf8_error() {
100+
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
101+
let result = String::from_utf8(invalid_utf8);
102+
assert!(result.is_err());
103+
if let Err(error) = result {
104+
let error = EmbeddedError::from(error);
105+
assert_eq!(
106+
error.to_string(),
107+
"invalid utf-8 sequence of 1 bytes from index 1"
108+
);
109+
}
110+
}
111+
112+
#[cfg(feature = "tokio")]
113+
#[tokio::test]
114+
async fn test_from_elapsed_error() {
115+
let result = tokio::time::timeout(std::time::Duration::from_nanos(1), async {
116+
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
117+
})
118+
.await;
119+
assert!(result.is_err());
120+
if let Err(elapsed_error) = result {
121+
let error = EmbeddedError::from(elapsed_error);
122+
assert_eq!(error.to_string(), "deadline has elapsed");
123+
}
124+
}
125+
}

postgresql_embedded/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@
2323
//! ### Asynchronous API
2424
//!
2525
//! ```no_run
26-
//! use postgresql_embedded::PostgreSQL;
26+
//! use postgresql_embedded::{PostgreSQL, Result};
2727
//!
2828
//! #[tokio::main]
29-
//! async fn main() {
29+
//! async fn main() -> Result<()> {
3030
//! let mut postgresql = PostgreSQL::default();
31-
//! postgresql.setup().await.unwrap();
32-
//! postgresql.start().await.unwrap();
31+
//! postgresql.setup().await?;
32+
//! postgresql.start().await?;
3333
//!
3434
//! let database_name = "test";
35-
//! postgresql.create_database(database_name).await.unwrap();
36-
//! postgresql.database_exists(database_name).await.unwrap();
37-
//! postgresql.drop_database(database_name).await.unwrap();
35+
//! postgresql.create_database(database_name).await?;
36+
//! postgresql.database_exists(database_name).await?;
37+
//! postgresql.drop_database(database_name).await?;
3838
//!
39-
//! postgresql.stop().await.unwrap();
39+
//! postgresql.stop().await
4040
//! }
4141
//! ```
4242
//!

postgresql_embedded/src/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::path::PathBuf;
77
use std::time::Duration;
88

99
/// Database settings
10-
#[derive(Clone, Debug)]
10+
#[derive(Clone, Debug, PartialEq, PartialOrd)]
1111
pub struct Settings {
1212
/// PostgreSQL installation directory
1313
pub installation_dir: PathBuf,

0 commit comments

Comments
 (0)