Skip to content

Commit 695119e

Browse files
metamejoepio
authored andcommitted
move test to tests.rs
1 parent c118905 commit 695119e

File tree

3 files changed

+78
-70
lines changed

3 files changed

+78
-70
lines changed

server/src/files.rs

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ pub enum FileStore {
1414

1515
#[derive(Clone, Debug, PartialEq)]
1616
pub struct S3Config {
17-
bucket: String,
18-
path: String,
19-
endpoint: Option<String>,
20-
region: Option<String>,
17+
pub bucket: String,
18+
pub path: String,
19+
pub endpoint: Option<String>,
20+
pub region: Option<String>,
2121
}
2222

2323
#[derive(Clone, Debug, PartialEq)]
@@ -172,63 +172,3 @@ 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 fs_store = FileStore::init_fs_from_config(&config);
200-
if let FileStore::FS(fs_config) = &fs_store {
201-
assert!(fs_config.path.to_str().unwrap().contains("uploads"));
202-
} else {
203-
panic!("fs FileStore not initiated");
204-
}
205-
let store = FileStore::init_from_config(&config, fs_store.clone());
206-
assert_eq!(fs_store, store);
207-
assert_eq!("fs:", fs_store.prefix());
208-
assert_eq!("fs%3A", fs_store.encoded());
209-
210-
assert!(fs_store.get_fs_file_path("my-great-file").unwrap().to_str().unwrap().contains("uploads/my-great-file"));
211-
assert!(fs_store.get_fs_file_path("fs:my-great-file").unwrap().to_str().unwrap().contains("uploads/my-great-file"));
212-
213-
// FileStore::S3 init
214-
opts.s3_bucket = Some("test-bucket".to_string());
215-
opts.s3_path = Some("uploads".to_string());
216-
config.opts = opts;
217-
let appstate = crate::appstate::init(config.clone()).expect("failed init appstate");
218-
219-
let s3_store = FileStore::init_from_config(&config, fs_store.clone());
220-
if let FileStore::S3(s3_config) = &s3_store {
221-
assert_eq!(s3_config.bucket, "test-bucket");
222-
assert_eq!(s3_config.path, "uploads");
223-
} else {
224-
panic!("s3 FileStore not initiated");
225-
}
226-
227-
assert_eq!("s3:", s3_store.prefix());
228-
assert_eq!("s3%3A", s3_store.encoded());
229-
230-
assert_eq!(&fs_store, FileStore::get_subject_file_store(&appstate, "my-great-file"));
231-
assert_eq!(&fs_store, FileStore::get_subject_file_store(&appstate, "fs:my-great-file"));
232-
assert_eq!(&s3_store, FileStore::get_subject_file_store(&appstate, "s3:my-great-file"));
233-
}
234-
}

server/src/handlers/upload.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ use atomic_lib::{
99
use futures::TryStreamExt;
1010
use serde::Deserialize;
1111

12-
use crate::{
13-
appstate::AppState,
14-
errors::AtomicServerResult,
15-
helpers::get_client_agent,
16-
};
12+
use crate::{appstate::AppState, errors::AtomicServerResult, helpers::get_client_agent};
1713

1814
#[derive(Deserialize, Debug)]
1915
pub struct UploadQuery {

server/src/tests.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! Most of the more rigorous testing is done in the end-to-end tests:
33
//! https://github.com/atomicdata-dev/atomic-data-browser/tree/main/data-browser/tests
44
5-
use crate::{appstate::AppState, config::Opts};
5+
use crate::{appstate::AppState, config::Opts, files::FileStore};
66

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

0 commit comments

Comments
 (0)