Skip to content

Commit

Permalink
test: move snapshots into test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git authored and pawarherschel committed Feb 9, 2024
1 parent d89ff90 commit 5f02af1
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 134 deletions.
46 changes: 45 additions & 1 deletion src/tool/core/helpers/code_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,48 @@ fn external_request_code_snippet(title_slug: &str) -> anyhow::Result<String> {
}

#[cfg(test)]
mod tests;
mod tests {
use std::io::Write as _;

use anyhow::Context;
use rstest::rstest;

use crate::tool::core::helpers::{
code_snippet::{external_request_code_snippet, get_code_snippets_response},
local_store::tests::{get_rnd_request_delay, insta_settings, title_slugs, SlugList},
};

#[rstest]
#[ignore = "Only use for downloading responses"]
fn download_response_from_leetcode(title_slugs: SlugList) {
for title_slug in title_slugs {
let sleep_delay = std::time::Duration::from_millis(get_rnd_request_delay());
println!(
"Going to sleep for {} milliseconds before requesting and trying to save {title_slug}",
sleep_delay.as_millis()
);
std::thread::sleep(sleep_delay); // Sleep to not go too hard on leetcode API
let response_string = external_request_code_snippet(title_slug).unwrap();
let path = super::path_local_store_code_snippet(title_slug);
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&path)
.with_context(|| format!("failed to save response to {path:?}"))
.unwrap();
file.write_all(response_string.as_bytes()).unwrap();
println!("Save of {title_slug:?} completed\n");
}
println!("Successfully saved all responses");
}

#[rstest]
fn conversion_from_leetcode_response(title_slugs: SlugList, insta_settings: insta::Settings) {
for title_slug in title_slugs {
insta_settings.bind(|| {
insta::assert_debug_snapshot!(get_code_snippets_response(title_slug).unwrap());
});
}
}
}
41 changes: 0 additions & 41 deletions src/tool/core/helpers/code_snippet/tests.rs

This file was deleted.

41 changes: 40 additions & 1 deletion src/tool/core/helpers/daily_challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,43 @@ fn external_request_daily_challenge(_needed_to_match_signature: &str) -> anyhow:
}

#[cfg(test)]
mod tests;
mod tests {
use std::io::Write as _;

use anyhow::Context;
use rstest::rstest;

use crate::tool::core::helpers::{
daily_challenge::{external_request_daily_challenge, get_daily_challenge_response},
local_store::tests::{get_rnd_request_delay, insta_settings},
};

#[rstest]
#[ignore = "Only use for downloading responses"]
fn download_response_from_leetcode() {
let sleep_delay = std::time::Duration::from_millis(get_rnd_request_delay());
println!(
"Going to sleep for {} milliseconds before requesting and trying to save daily_challenge",
sleep_delay.as_millis()
);
std::thread::sleep(sleep_delay); // Sleep to not go too hard on leetcode API
let response_string = external_request_daily_challenge("unneeded").unwrap();
let path = super::path_local_store_daily_challenge();
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&path)
.with_context(|| format!("failed to save response to {path:?}"))
.unwrap();
file.write_all(response_string.as_bytes()).unwrap();
println!("Successfully saved daily response, possibly not be useful to commit the new one");
}

#[rstest]
fn conversion_from_leetcode_response(insta_settings: insta::Settings) {
insta_settings.bind(|| {
insta::assert_debug_snapshot!(get_daily_challenge_response().unwrap());
});
}
}
36 changes: 0 additions & 36 deletions src/tool/core/helpers/daily_challenge/tests.rs

This file was deleted.

13 changes: 13 additions & 0 deletions src/tool/core/helpers/local_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub(crate) mod tests {
use rand::{thread_rng, Rng};
use rstest::fixture;

const NAME_SNAPSHOT_FOLDER: &str = "snapshots";

use super::NAME_TEST_FOLDER;

pub(crate) type SlugList = &'static [&'static str];

// Don't want to send them too fast and not at the same time for all tests so random in range
Expand All @@ -50,4 +54,13 @@ pub(crate) mod tests {
pub(crate) fn title_slugs() -> SlugList {
&["two-sum", "add-two-numbers", "validate-binary-search-tree"]
}

#[fixture]
pub(crate) fn insta_settings() -> insta::Settings {
let mut result = insta::Settings::clone_current();
let cwd = std::env::current_dir().expect("failed to get cwd");
let path = cwd.join(NAME_TEST_FOLDER).join(NAME_SNAPSHOT_FOLDER);
result.set_snapshot_path(path);
result
}
}
49 changes: 48 additions & 1 deletion src/tool/core/helpers/problem_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,51 @@ fn external_request_problem_metadata(title_slug: &str) -> anyhow::Result<String>
}

#[cfg(test)]
mod tests;
mod tests {
use std::io::Write as _;

use anyhow::Context;
use rstest::rstest;

use crate::tool::core::helpers::{
local_store::{
path_local_store_problem_metadata,
tests::{get_rnd_request_delay, insta_settings, title_slugs, SlugList},
},
problem_metadata::{external_request_problem_metadata, get_problem_metadata},
};

#[rstest]
#[ignore = "Only use for downloading responses"]
fn download_response_from_leetcode(title_slugs: SlugList) {
for title_slug in title_slugs {
let sleep_delay = std::time::Duration::from_millis(get_rnd_request_delay());
println!(
"Going to sleep for {} milliseconds before requesting and trying to save {title_slug}",
sleep_delay.as_millis()
);
std::thread::sleep(sleep_delay); // Sleep to not go too hard on leetcode API
let response_string = external_request_problem_metadata(title_slug).unwrap();
let path = path_local_store_problem_metadata(title_slug);
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&path)
.with_context(|| format!("failed to save response to {path:?}"))
.unwrap();
file.write_all(response_string.as_bytes()).unwrap();
println!("Save of {title_slug:?} completed\n");
}
println!("Successfully saved all responses");
}

#[rstest]
fn conversion_from_leetcode_response(title_slugs: SlugList, insta_settings: insta::Settings) {
for title_slug in title_slugs {
insta_settings.bind(|| {
insta::assert_debug_snapshot!(get_problem_metadata(title_slug).unwrap());
});
}
}
}
44 changes: 0 additions & 44 deletions src/tool/core/helpers/problem_metadata/tests.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/tool/core/helpers/problem_code.rs
assertion_line: 617
expression: actual
---
/* Error: expected number of augments (2) to match the number of lines downloaded (1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/tool/core/helpers/code_snippet/tests.rs
assertion_line: 39
source: src/tool/core/helpers/code_snippet.rs
expression: get_code_snippets_response(title_slug).unwrap()
---
CodeSnippetResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/tool/core/helpers/code_snippet/tests.rs
source: src/tool/core/helpers/code_snippet.rs
expression: get_code_snippets_response(title_slug).unwrap()
---
CodeSnippetResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/tool/core/helpers/code_snippet/tests.rs
source: src/tool/core/helpers/code_snippet.rs
expression: get_code_snippets_response(title_slug).unwrap()
---
CodeSnippetResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: src/tool/core/helpers/daily_challenge/tests.rs
assertion_line: 35
source: src/tool/core/helpers/daily_challenge.rs
expression: get_daily_challenge_response().unwrap()
---
DailyChallengeResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: src/tool/core/helpers/problem_metadata/tests.rs
source: src/tool/core/helpers/problem_metadata.rs
assertion_line: 94
expression: get_problem_metadata(title_slug).unwrap()
---
ProblemMetadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/tool/core/helpers/problem_metadata/tests.rs
source: src/tool/core/helpers/problem_metadata.rs
expression: get_problem_metadata(title_slug).unwrap()
---
ProblemMetadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/tool/core/helpers/problem_metadata/tests.rs
source: src/tool/core/helpers/problem_metadata.rs
expression: get_problem_metadata(title_slug).unwrap()
---
ProblemMetadata {
Expand Down

0 comments on commit 5f02af1

Please sign in to comment.