Skip to content

Commit 365814c

Browse files
metamejoepio
authored andcommitted
tests passing
1 parent a7dab2e commit 365814c

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

server/src/files.rs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ use actix_multipart::Field;
44
use futures::StreamExt;
55
use opendal::{services::S3, Operator};
66

7-
use crate::{appstate::AppState, config::Config, errors::AtomicServerResult};
7+
use crate::{config::Config, errors::AtomicServerResult};
88

9-
#[derive(Clone, Debug)]
9+
#[derive(Clone, Debug, PartialEq)]
1010
pub enum FileStore {
1111
S3(S3Config),
1212
FS(FSConfig),
1313
}
1414

15-
#[derive(Clone, Debug)]
15+
#[derive(Clone, Debug, PartialEq)]
1616
pub struct S3Config {
1717
bucket: String,
1818
path: String,
1919
endpoint: Option<String>,
2020
region: Option<String>,
2121
}
2222

23-
#[derive(Clone, Debug)]
23+
#[derive(Clone, Debug, PartialEq)]
2424
pub struct FSConfig {
2525
pub path: PathBuf,
2626
}
@@ -50,11 +50,11 @@ impl FileStore {
5050
}
5151
}
5252

53-
pub fn get_subject_file_store<'a>(appstate: &'a AppState, subject: &str) -> &'a FileStore {
53+
pub fn get_subject_file_store<'a>(file_store: &'a FileStore, fs_file_store: &'a FileStore, subject: &str) -> &'a FileStore {
5454
if subject.contains(Self::S3_PREFIX) {
55-
&appstate.file_store
55+
file_store
5656
} else {
57-
&appstate.fs_file_store
57+
fs_file_store
5858
}
5959
}
6060

@@ -172,3 +172,66 @@ pub async fn get_s3_signed_url(
172172

173173
Ok(uri)
174174
}
175+
176+
#[cfg(test)]
177+
mod test {
178+
use super::*;
179+
180+
use crate::config::{self, Opts};
181+
182+
#[actix_rt::test]
183+
async fn file_store_tests() {
184+
let unique_string = atomic_lib::utils::random_string(10);
185+
use clap::Parser;
186+
let mut opts = Opts::parse_from([
187+
"atomic-server",
188+
"--initialize",
189+
"--data-dir",
190+
&format!("./.temp/{}/db", unique_string),
191+
"--config-dir",
192+
&format!("./.temp/{}/config", unique_string),
193+
]);
194+
195+
let mut config = config::build_config(opts.clone())
196+
.map_err(|e| format!("Initialization failed: {}", e))
197+
.expect("failed init config");
198+
199+
let appstate = crate::appstate::init(config.clone()).expect("failed init appstate");
200+
201+
let fs_store = FileStore::init_fs_from_config(&config);
202+
if let FileStore::FS(fs_config) = fs_store.clone() {
203+
assert!(fs_config.path.to_str().unwrap().contains("uploads"));
204+
} else {
205+
panic!("fs FileStore not initiated");
206+
}
207+
let store = FileStore::init_from_config(&config, fs_store.clone());
208+
assert_eq!(fs_store, store);
209+
assert_eq!("fs:", fs_store.prefix());
210+
assert_eq!("fs%3A", fs_store.encoded());
211+
212+
assert!(fs_store.get_fs_file_path("my-great-file").unwrap().to_str().unwrap().contains("uploads/my-great-file"));
213+
assert!(fs_store.get_fs_file_path("fs:my-great-file").unwrap().to_str().unwrap().contains("uploads/my-great-file"));
214+
//assert_eq!(file_path, fs_store.get_fs_file_path("my-great-file").unwrap());
215+
//assert_eq!(file_path, fs_store.get_fs_file_path("fs:my-great-file").unwrap());
216+
217+
// FileStore::S3 init
218+
opts.s3_bucket = Some("test-bucket".to_string());
219+
opts.s3_path = Some("uploads".to_string());
220+
config.opts = opts;
221+
222+
let s3_store = FileStore::init_from_config(&config, fs_store.clone());
223+
if let FileStore::S3(s3_config) = s3_store.clone() {
224+
assert_eq!(s3_config.bucket, "test-bucket");
225+
assert_eq!(s3_config.path, "uploads");
226+
} else {
227+
panic!("s3 FileStore not initiated");
228+
}
229+
230+
assert_eq!("s3:", s3_store.prefix());
231+
assert_eq!("s3%3A", s3_store.encoded());
232+
233+
assert_eq!(&fs_store, FileStore::get_subject_file_store(&s3_store, &fs_store, "my-great-file"));
234+
assert_eq!(&fs_store, FileStore::get_subject_file_store(&s3_store, &fs_store, "fs:my-great-file"));
235+
assert_eq!(&s3_store, FileStore::get_subject_file_store(&s3_store, &fs_store, "s3:my-great-file"));
236+
}
237+
}

server/src/handlers/download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub async fn handle_download(
3333

3434
let for_agent = get_client_agent(headers, &appstate, subject.clone())?;
3535
tracing::info!("handle_download: {}", subject);
36-
let file_store = FileStore::get_subject_file_store(&appstate, &subject);
36+
let file_store = FileStore::get_subject_file_store(&appstate.file_store, &appstate.fs_file_store, &subject);
3737
let encoded = subject.replace(file_store.prefix(), &file_store.encoded());
3838
let resource = store.get_resource_extended(&encoded, false, &for_agent)?;
3939
let file_id = resource

0 commit comments

Comments
 (0)