Skip to content

Commit 6d619b8

Browse files
Merge pull request #16 from theseus-rs/improve-test-coverage
test: improve lifecycle test coverage
2 parents f596df5 + feeeeb6 commit 6d619b8

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

postgresql_embedded/src/error.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ pub enum Error {
99
/// Error when PostgreSQL archive operations fail
1010
#[error(transparent)]
1111
ArchiveError(postgresql_archive::Error),
12-
/// Error when the archive is not found for a specific version
13-
#[error("Archive not found for version [{0}]")]
14-
ArchiveNotFound(String),
1512
/// Error when a command fails
1613
#[error("Command error: stdout={stdout}; stderr={stderr}")]
1714
CommandError { stdout: String, stderr: String },
@@ -108,7 +105,7 @@ mod test {
108105
#[tokio::test]
109106
async fn test_from_elapsed_error() {
110107
let result = tokio::time::timeout(std::time::Duration::from_nanos(1), async {
111-
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
108+
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
112109
})
113110
.await;
114111
assert!(result.is_err());

postgresql_embedded/src/postgresql.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::command::pg_ctl::Mode::{Start, Stop};
33
use crate::command::pg_ctl::PgCtlBuilder;
44
use crate::command::pg_ctl::ShutdownMode::Fast;
55
use crate::command::traits::{CommandBuilder, CommandExecutor};
6-
use crate::error::Error::{
7-
ArchiveNotFound, DatabaseInitializationError, DatabaseStartError, DatabaseStopError,
8-
};
6+
use crate::error::Error::{DatabaseInitializationError, DatabaseStartError, DatabaseStopError};
97
use crate::error::Result;
108
use crate::settings::Settings;
119
use bytes::Bytes;
@@ -94,7 +92,7 @@ impl PostgreSQL {
9492
postgresql
9593
}
9694

97-
/// Get the default version used by if not otherwise specified
95+
/// Get the default version used if not otherwise specified
9896
pub fn default_version() -> Version {
9997
if cfg!(feature = "bundled") {
10098
*ARCHIVE_VERSION
@@ -168,19 +166,8 @@ impl PostgreSQL {
168166
/// already exists, the archive will not be extracted. If the archive is not found, an error will be
169167
/// returned.
170168
async fn install(&mut self) -> Result<()> {
171-
let mut archive_bytes: Option<Bytes> = None;
172-
173169
debug!("Starting installation process for version {}", self.version);
174170

175-
#[cfg(feature = "bundled")]
176-
// If the requested version is the same as the version of the bundled archive, use the bundled
177-
// archive. This avoids downloading the archive in environments where internet access is
178-
// restricted or undesirable.
179-
if ARCHIVE_VERSION.deref() == &self.version {
180-
debug!("Using bundled installation archive");
181-
archive_bytes = Some(Bytes::copy_from_slice(ARCHIVE));
182-
}
183-
184171
// If the minor and release version are not set, determine the latest version and update the
185172
// version and installation directory accordingly. This is an optimization to avoid downloading
186173
// the archive if the latest version is already installed.
@@ -191,35 +178,33 @@ impl PostgreSQL {
191178
.settings
192179
.installation_dir
193180
.join(self.version.to_string());
194-
195-
if self.settings.installation_dir.exists() {
196-
debug!("Installation directory already exists");
197-
self.update_status();
198-
return Ok(());
199-
}
200181
}
201182

202-
if archive_bytes.is_none() {
203-
let (version, bytes) = get_archive(&self.version).await?;
204-
self.version = version;
205-
archive_bytes = Some(bytes);
183+
if self.settings.installation_dir.exists() {
184+
debug!("Installation directory already exists");
185+
self.update_status();
186+
return Ok(());
206187
}
207188

208-
if !self.settings.installation_dir.exists() {
209-
self.status = Status::Installing;
210-
create_dir_all(&self.settings.installation_dir)?;
211-
212-
match archive_bytes {
213-
Some(bytes) => {
214-
extract(&bytes, &self.settings.installation_dir).await?;
215-
self.status = Status::Installed;
216-
}
217-
None => {
218-
self.update_status();
219-
return Err(ArchiveNotFound(self.version.to_string()));
220-
}
221-
}
222-
}
189+
#[cfg(feature = "bundled")]
190+
// If the requested version is the same as the version of the bundled archive, use the bundled
191+
// archive. This avoids downloading the archive in environments where internet access is
192+
// restricted or undesirable.
193+
let (version, bytes) = if ARCHIVE_VERSION.deref() == &self.version {
194+
debug!("Using bundled installation archive");
195+
(self.version, Bytes::copy_from_slice(ARCHIVE))
196+
} else {
197+
get_archive(&self.version).await?
198+
};
199+
200+
#[cfg(not(feature = "bundled"))]
201+
let (version, bytes) = { get_archive(&self.version).await? };
202+
203+
self.version = version;
204+
self.status = Status::Installing;
205+
create_dir_all(&self.settings.installation_dir)?;
206+
extract(&bytes, &self.settings.installation_dir).await?;
207+
self.status = Status::Installed;
223208

224209
debug!(
225210
"Installed PostgreSQL version {} to {}",

postgresql_embedded/tests/blocking.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ use test_log::test;
99
#[test]
1010
fn test_lifecycle() -> Result<()> {
1111
let mut postgresql = PostgreSQL::default();
12+
let settings = postgresql.settings();
13+
14+
// Verify that an ephemeral instance is created by default
15+
assert_eq!(0, settings.port);
16+
assert!(settings.temporary);
1217

1318
let initial_statuses = [Status::NotInstalled, Status::Installed, Status::Stopped];
1419
assert!(initial_statuses.contains(&postgresql.status()));
@@ -20,6 +25,7 @@ fn test_lifecycle() -> Result<()> {
2025
assert_eq!(Status::Started, postgresql.status());
2126

2227
let database_name = "test";
28+
assert!(!postgresql.database_exists(database_name)?);
2329
postgresql.create_database(database_name)?;
2430
assert!(postgresql.database_exists(database_name)?);
2531
postgresql.drop_database(database_name)?;

postgresql_embedded/tests/postgresql.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use test_log::test;
66

77
async fn lifecycle() -> Result<()> {
88
let mut postgresql = PostgreSQL::default();
9+
let settings = postgresql.settings();
10+
11+
// Verify that an ephemeral instance is created by default
12+
assert_eq!(0, settings.port);
13+
assert!(settings.temporary);
914

1015
let initial_statuses = [Status::NotInstalled, Status::Installed, Status::Stopped];
1116
assert!(initial_statuses.contains(&postgresql.status()));
@@ -17,6 +22,7 @@ async fn lifecycle() -> Result<()> {
1722
assert_eq!(Status::Started, postgresql.status());
1823

1924
let database_name = "test";
25+
assert!(!postgresql.database_exists(database_name).await?);
2026
postgresql.create_database(database_name).await?;
2127
assert!(postgresql.database_exists(database_name).await?);
2228
postgresql.drop_database(database_name).await?;

0 commit comments

Comments
 (0)