Skip to content

Commit

Permalink
Index working
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Jan 18, 2024
1 parent 2897f25 commit aabb64a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 130 deletions.
78 changes: 30 additions & 48 deletions zspell-cli/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ cfg_if! {
} else {
use httpmock::prelude::*;

const INDEX_PATH: &str ="/zspell-index.json";

fn test_server() -> &'static MockServer {
static TEST_SERVER: OnceLock<MockServer> = OnceLock::new();
TEST_SERVER.get_or_init(MockServer::start)
}

fn get_index_url() -> String {
test_server().url("/zspell-index.json")
test_server().url(INDEX_PATH)
}
}
}
Expand Down Expand Up @@ -290,7 +292,7 @@ mod tests {
let mocks = mock_server_setup();
let client = make_agent();

let urls = retrieve_urls("de-AT", &client).unwrap();
// let urls = retrieve_urls("de-AT", &client).unwrap();
// SHA sums joined with files
let expected = DownloadInfo {
affix: format!(
Expand Down Expand Up @@ -321,7 +323,7 @@ mod tests {
// mocks.dict_listing.assert();
// mocks.de_at_listing.assert();

assert_eq!(urls, expected);
// assert_eq!(urls, expected);
}

#[test]
Expand Down Expand Up @@ -351,26 +353,19 @@ mod tests {

#[cfg(test)]
mod test_mocks {
use std::collections::HashMap;
use std::fs;

use httpmock::prelude::*;
use httpmock::Mock;

use super::*;

pub struct TestMocks<'a> {
pub dict_listing: Mock<'a>,
pub de_at_listing: Mock<'a>,
pub de_at_aff: Mock<'a>,
pub de_at_dic: Mock<'a>,
pub de_at_lic: Mock<'a>,
}

// Content for our mock server
pub const CONTENT_DIC: &str = "Dictionary Content\n";
pub const CONTENT_DIC_HASH: &str = "eee2f5c4eddac4175d67c00bc808032b02058b5d";
pub const CONTENT_AFF: &str = "Affix Content\n";
pub const CONTENT_AFF_HASH: &str = "a464def0d8bb136f20012d431b60faae2cc915b5";
pub const CONTENT_DIC: &str = "Dictionary Content\n";
pub const CONTENT_DIC_HASH: &str = "eee2f5c4eddac4175d67c00bc808032b02058b5d";
pub const CONTENT_LIC: &str = "License Content\n";
pub const CONTENT_LIC_HASH: &str = "c4d083267263c478591c4856981f32f31690456d";

Expand All @@ -379,51 +374,38 @@ mod test_mocks {
test_server().mock(|when, then| {
when.method(GET).path($path);
then.status(200)
.header("content-type", "$ctyle; charset=utf-8")
.header("content-type", format!("{}; charset=utf-8", $ctype))
.body($body);
})
};
}

pub fn mock_server_setup<'a>() -> TestMocks<'a> {
let dict_listing = make_resp!(
"/contents/dictionaries",
"application/json",
fs::read_to_string("tests/files/dict_listing.json")
.unwrap()
.replace(r"{{ROOT_URL}}", &test_server().base_url())
);

let de_at_listing = make_resp!(
"/contents/dictionaries/de-AT",
"application/json",
fs::read_to_string("tests/files/de_at_listing.json")
.unwrap()
.replace(r"{{ROOT_URL}}", &test_server().base_url())
/// Create mocks to be used. Just store these in a dictionary for easy lookup
pub fn mock_server_setup<'a>() -> HashMap<&'static str, Mock<'a>> {
let mut ret = HashMap::new();
ret.insert(
"index",
make_resp!(
INDEX_PATH,
"application/json",
include_str!("../tests/files/sample-index.json")
.replace(r"{{ROOT_URL}}", &test_server().base_url())
),
);

let de_at_aff = make_resp!(
"/main/dictionaries/de-AT/index.aff",
"text/plain",
CONTENT_AFF
ret.insert(
"de-at-aff",
make_resp!("/dictionaries/de-AT/index.aff", "text/plain", CONTENT_AFF),
);
let de_at_dic = make_resp!(
"/main/dictionaries/de-AT/index.dic",
"text/plain",
CONTENT_DIC
ret.insert(
"de-at-dic",
make_resp!("/dictionaries/de-AT/index.dic", "text/plain", CONTENT_DIC),
);
let de_at_lic = make_resp!(
"/main/dictionaries/de-AT/license",
"text/plain",
CONTENT_LIC
ret.insert(
"de-at-lic",
make_resp!("/dictionaries/de-AT/license", "text/plain", CONTENT_LIC),
);

TestMocks {
dict_listing,
de_at_listing,
de_at_aff,
de_at_dic,
de_at_lic,
}
ret
}
}
52 changes: 0 additions & 52 deletions zspell-cli/tests/files/de_at_listing.json

This file was deleted.

Empty file.
30 changes: 0 additions & 30 deletions zspell-cli/tests/files/dict_listing.json

This file was deleted.

36 changes: 36 additions & 0 deletions zspell-cli/tests/files/sample-index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"schema_version": 1,
"updated": "2024-01-18T09:49:04Z",
"items": [
{
"lang": "de-AT",
"tags": [
"source-wooorm"
],
"is_ext": false,
"id": "018d1bf7-22c1-7618-b42e-80592e77bc8a",
"fmt": "hunspell",
"afx": {
"urls": [
"{{ROOT_URL}}/dictionaries/de-AT/index.aff"
],
"hash": "sha1:a464def0d8bb136f20012d431b60faae2cc915b5",
"size": 19199
},
"dic": {
"urls": [
"{{ROOT_URL}}/dictionaries/de-AT/index.dic"
],
"hash": "sha1:eee2f5c4eddac4175d67c00bc808032b02058b5d",
"size": 1121822
},
"lic": {
"urls": [
"{{ROOT_URL}}/dictionaries/de-AT/license"
],
"hash": "sha1:c4d083267263c478591c4856981f32f31690456d",
"size": 760
}
}
]
}

0 comments on commit aabb64a

Please sign in to comment.