Skip to content

Commit

Permalink
add test for default config
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Dec 8, 2024
1 parent 95307b4 commit ae0f0e9
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nghe-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ xxhash-rust = { version = "0.8.12", features = ["xxh3"] }
[dev-dependencies]
bon = { workspace = true }
fake = { workspace = true }
figment = { version = "0.10.19", features = ["test"] }
url = { workspace = true }
rstest = { workspace = true }
serde_json = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/config/cover_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use typed_path::Utf8NativePathBuf;
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct CoverArt {
#[serde(with = "crate::filesystem::path::serde::option")]
#[educe(Default(
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/config/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::database::Key;
#[serde_as]
#[derive(Deserialize, Educe)]
#[educe(Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Database {
#[educe(Debug(ignore))]
pub url: String,
Expand Down
4 changes: 4 additions & 0 deletions nghe-backend/src/config/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_with::serde_as;
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Scan {
// 100 KiB in bytes
#[educe(Default(expression = 100 * 1024))]
Expand All @@ -18,6 +19,7 @@ pub struct Scan {

#[derive(Debug, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Tls {
#[educe(Default(expression = false))]
pub accept_invalid_certs: bool,
Expand All @@ -27,6 +29,7 @@ pub struct Tls {

#[derive(Debug, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct S3 {
#[educe(Default(expression =
std::env::var("AWS_ACCESS_KEY_ID").is_ok() && std::env::var("AWS_SECRET_ACCESS_KEY").is_ok()
Expand All @@ -43,6 +46,7 @@ pub struct S3 {
}

#[derive(Debug, Serialize, Deserialize, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Filesystem {
pub scan: Scan,
pub tls: Tls,
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/config/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{database, error, Error};

#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Index {
#[serde(with = "split")]
#[educe(Default(expression = Index::split("The An A Die Das Ein Eine Les Le La")))]
Expand Down
2 changes: 2 additions & 0 deletions nghe-backend/src/config/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use typed_path::Utf8NativePathBuf;
#[serde_as]
#[derive(Clone, Serialize, Deserialize, Educe)]
#[educe(Debug, Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Spotify {
#[educe(Debug(ignore))]
pub id: Option<String>,
Expand All @@ -20,6 +21,7 @@ pub struct Spotify {
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Integration {
pub spotify: Spotify,
}
Expand Down
39 changes: 39 additions & 0 deletions nghe-backend/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use server::Server;
pub use transcode::Transcode;

#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Config {
pub server: Server,
pub database: Database,
Expand All @@ -47,3 +48,41 @@ impl Default for Config {
.expect("Could not parse config")
}
}

#[cfg(test)]
#[coverage(off)]
mod tests {
use fake::{Fake, Faker};

use super::*;
use crate::database::Key;

#[test]
fn test_default_config() {
figment::Jail::expect_with(|jail| {
let database_url: String = Faker.fake();
let database_key: Key = Faker.fake();

jail.clear_env();
jail.set_env("NGHE_DATABASE__URL", &database_url);
jail.set_env("NGHE_DATABASE__KEY", faster_hex::hex_string(&database_key));

let config = Config::default();
assert_eq!(
config,
Config {
server: Server::default(),
database: Database { url: database_url, key: database_key },
filesystem: Filesystem::default(),
parsing: Parsing::default(),
index: Index::default(),
transcode: Transcode::default(),
cover_art: CoverArt::default(),
integration: Integration::default()
}
);

Ok(())
});
}
}
1 change: 1 addition & 0 deletions nghe-backend/src/config/parsing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use vorbis_comments::VorbisComments;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Parsing {
pub vorbis_comments: VorbisComments,
}
Expand Down
5 changes: 5 additions & 0 deletions nghe-backend/src/config/parsing/vorbis_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_with::serde_as;

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Common {
pub name: String,
#[serde_as(as = "serde_with::NoneAsEmptyString")]
Expand All @@ -16,13 +17,15 @@ pub struct Common {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Artist {
pub name: String,
pub mbz_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Artists {
#[educe(Default(expression = Artist::default_song()))]
pub song: Artist,
Expand All @@ -32,6 +35,7 @@ pub struct Artists {

#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct TrackDisc {
#[educe(Default(expression = "TRACKNUMBER".into()))]
pub track_number: String,
Expand All @@ -45,6 +49,7 @@ pub struct TrackDisc {

#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct VorbisComments {
#[educe(Default(expression = Common::default_song()))]
pub song: Common,
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use typed_path::Utf8NativePathBuf;

#[derive(Debug, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Server {
#[educe(Default(expression = [127u8, 0u8, 0u8, 1u8].into()))]
pub host: IpAddr,
Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/config/transcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use typed_path::Utf8NativePathBuf;
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Transcode {
#[educe(Default(expression = 32 * 1024))]
pub buffer_size: usize,
Expand Down

0 comments on commit ae0f0e9

Please sign in to comment.