Skip to content

Commit d451fff

Browse files
committed
add unit tests
1 parent 984920c commit d451fff

File tree

11 files changed

+98
-2
lines changed

11 files changed

+98
-2
lines changed

crates/backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ opendal = { version = "0.45", features = ["services-b2", "services-swift", "laye
9696

9797
[dev-dependencies]
9898
rstest = { workspace = true }
99+
toml = "0.8.12"
99100

100101
[lints]
101102
workspace = true

crates/backend/src/opendal.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn runtime() -> &'static Runtime {
3838
/// Throttling parameters
3939
///
4040
/// Note: Throttle implements [`FromStr`] to read it from something like "10kiB,10MB"
41-
#[derive(Debug, Clone, Copy)]
41+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4242
pub struct Throttle {
4343
bandwidth: u32,
4444
burst: u32,
@@ -49,7 +49,7 @@ impl FromStr for Throttle {
4949
fn from_str(s: &str) -> Result<Self> {
5050
let mut values = s
5151
.split(',')
52-
.map(|s| ByteSize::from_str(s).map_err(|err| anyhow!("Error: {err}")))
52+
.map(|s| ByteSize::from_str(s.trim()).map_err(|err| anyhow!("Error: {err}")))
5353
.map(|b| -> Result<u32> { Ok(b?.as_u64().try_into()?) });
5454
let bandwidth = values
5555
.next()
@@ -287,3 +287,47 @@ impl WriteBackend for OpenDALBackend {
287287
Ok(())
288288
}
289289
}
290+
291+
#[cfg(test)]
292+
mod tests {
293+
use std::fs;
294+
295+
use super::*;
296+
use rstest::rstest;
297+
use serde::Deserialize;
298+
299+
#[rstest]
300+
#[case("10kB,10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
301+
#[case("10 kB,10 MB", Throttle{bandwidth:10_000, burst:10_000_000})]
302+
#[case("10kB, 10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
303+
#[case(" 10kB, 10MB", Throttle{bandwidth:10_000, burst:10_000_000})]
304+
#[case("10kiB,10MiB", Throttle{bandwidth:10_240, burst:10_485_760})]
305+
fn correct_throttle(#[case] input: &str, #[case] expected: Throttle) {
306+
assert_eq!(Throttle::from_str(input).unwrap(), expected);
307+
}
308+
309+
#[rstest]
310+
#[case("")]
311+
#[case("10kiB")]
312+
#[case("no_number,10MiB")]
313+
#[case("10kB;10MB")]
314+
fn invalid_throttle(#[case] input: &str) {
315+
assert!(Throttle::from_str(input).is_err());
316+
}
317+
318+
#[rstest]
319+
fn new_opendal_backend(
320+
#[files("tests/fixtures/opendal/*.toml")] test_case: PathBuf,
321+
) -> Result<()> {
322+
#[derive(Deserialize)]
323+
struct TestCase {
324+
path: String,
325+
options: HashMap<String, String>,
326+
}
327+
328+
let test: TestCase = toml::from_str(&fs::read_to_string(test_case)?)?;
329+
330+
_ = OpenDALBackend::new(test.path, test.options)?;
331+
Ok(())
332+
}
333+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
path = "b2"
2+
[options]
3+
application_key_id = "my_id" # B2 application key ID
4+
application_key = "my_key" # B2 application key secret. Can be also set using OPENDAL_APPLICATION_KEY
5+
bucket = "bucket_name" # B2 bucket name
6+
bucket_id = "bucket_id" # B2 bucket ID
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
path = "s3"
2+
[options]
3+
region = "test_region"
4+
bucket = "bucket_name"
5+
connections = "20"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
path = "s3"
2+
[options]
3+
region = "test_region"
4+
bucket = "bucket_name"
5+
retry = "20"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
path = "s3"
2+
[options]
3+
region = "test_region"
4+
bucket = "bucket_name"
5+
retry = "default"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
path = "s3"
2+
[options]
3+
region = "test_region"
4+
bucket = "bucket_name"
5+
retry = "off"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
path = "s3"
2+
[options]
3+
access_key_id = "xxx"
4+
secret_access_key = "xxx"
5+
region = "test_region"
6+
bucket = "bucket_name"
7+
root = "/path/to/repo"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
path = "s3"
2+
[options]
3+
root = "/"
4+
bucket = "bucket_name"
5+
endpoint = "https://p7v1.ldn.idrivee2-40.com"
6+
region = "auto"
7+
access_key_id = "xxx"
8+
secret_access_key = "xxx"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
path = "s3"
2+
[options]
3+
region = "test_region"
4+
bucket = "bucket_name"
5+
throttle = "20kB,5MiB"

0 commit comments

Comments
 (0)