Skip to content

Commit

Permalink
Refactor TUI tests significantly
Browse files Browse the repository at this point in the history
Add two new types, TestHarness and TestComponent, that make it much easier to test components. This should make TUI test code much simpler and more consistent, and it's much easier to right *correct* test code now.
  • Loading branch information
LucasPickering committed May 25, 2024
1 parent c578998 commit fbfd576
Show file tree
Hide file tree
Showing 30 changed files with 856 additions and 788 deletions.
2 changes: 1 addition & 1 deletion src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ async fn load_collection(path: PathBuf) -> anyhow::Result<Collection> {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::{assert_err, temp_dir, TempDir};
use rstest::rstest;
use std::fs::File;

Expand Down
2 changes: 1 addition & 1 deletion src/collection/insomnia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{collection::CollectionFile, test_util::*};
use crate::{collection::CollectionFile, test_util::test_data_dir};
use indexmap::indexmap;
use pretty_assertions::assert_eq;
use rstest::rstest;
Expand Down
83 changes: 83 additions & 0 deletions src/collection/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,27 +327,92 @@ impl Collection {
}
}

#[cfg(test)]
impl crate::test_util::Factory for Collection {
fn factory(_: ()) -> Self {
let recipe = Recipe::factory(());
let profile = Profile::factory(());
Collection {
recipes: indexmap::indexmap! {recipe.id.clone() => recipe}.into(),
profiles: indexmap::indexmap! {profile.id.clone() => profile},
..Collection::default()
}
}
}

#[cfg(test)]
impl crate::test_util::Factory for ProfileId {
fn factory(_: ()) -> Self {
uuid::Uuid::new_v4().to_string().into()
}
}

#[cfg(test)]
impl crate::test_util::Factory for RecipeId {
fn factory(_: ()) -> Self {
uuid::Uuid::new_v4().to_string().into()
}
}

impl Profile {
/// Get a presentable name for this profile
pub fn name(&self) -> &str {
self.name.as_deref().unwrap_or(&self.id)
}
}

#[cfg(test)]
impl crate::test_util::Factory for Profile {
fn factory(_: ()) -> Self {
Self {
id: "profile1".into(),
name: None,
data: IndexMap::new(),
}
}
}

impl Folder {
/// Get a presentable name for this folder
pub fn name(&self) -> &str {
self.name.as_deref().unwrap_or(&self.id)
}
}

#[cfg(test)]
impl crate::test_util::Factory for Folder {
fn factory(_: ()) -> Self {
Self {
id: "folder1".into(),
name: None,
children: IndexMap::new(),
}
}
}

impl Recipe {
/// Get a presentable name for this recipe
pub fn name(&self) -> &str {
self.name.as_deref().unwrap_or(&self.id)
}
}

#[cfg(test)]
impl crate::test_util::Factory for Recipe {
fn factory(_: ()) -> Self {
Self {
id: "recipe1".into(),
name: None,
method: Method::Get,
url: "http://localhost/url".into(),
body: None,
authentication: None,
query: IndexMap::new(),
headers: IndexMap::new(),
}
}
}

/// For deserialization
impl TryFrom<String> for Method {
type Error = anyhow::Error;
Expand All @@ -369,3 +434,21 @@ impl From<Method> for String {
method.to_string()
}
}

#[cfg(test)]
impl crate::test_util::Factory for Chain {
fn factory(_: ()) -> Self {
Self {
id: "chain1".into(),
source: ChainSource::Request {
recipe: "recipe1".into(),
trigger: Default::default(),
section: Default::default(),
},
sensitive: false,
selector: None,
content_type: None,
trim: ChainOutputTrim::default(),
}
}
}
2 changes: 1 addition & 1 deletion src/collection/recipe_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl From<&Vec<&RecipeId>> for RecipeLookupKey {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::{assert_err, Factory};
use indexmap::indexmap;
use itertools::Itertools;
use rstest::{fixture, rstest};
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl<'a, 'b> TryFrom<&'a Row<'b>> for RequestRecordSummary {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::Factory;
use itertools::Itertools;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ mod tests {
use super::*;
use crate::{
collection::{Authentication, Collection, Profile},
test_util::*,
test_util::{header_map, Factory},
};
use indexmap::indexmap;
use pretty_assertions::assert_eq;
Expand Down
2 changes: 1 addition & 1 deletion src/http/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ContentType {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::{assert_err, Factory};
use reqwest::header::{
HeaderMap, HeaderValue, InvalidHeaderValue, CONTENT_TYPE,
};
Expand Down
2 changes: 1 addition & 1 deletion src/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Query {
#[cfg(test)]
mod tests {
use super::*;
use crate::{http::Json, test_util::*};
use crate::{http::Json, test_util::assert_err};
use rstest::rstest;
use serde_json::json;

Expand Down
77 changes: 76 additions & 1 deletion src/http/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,81 @@ impl Request {
}
}

#[cfg(test)]
impl crate::test_util::Factory for Request {
fn factory(_: ()) -> Self {
Self {
id: RequestId::new(),
profile_id: None,
recipe_id: "recipe1".into(),
method: reqwest::Method::GET,
url: "http://localhost/url".parse().unwrap(),
headers: HeaderMap::new(),
body: None,
}
}
}

/// Customize profile and recipe ID
#[cfg(test)]
impl crate::test_util::Factory<(Option<ProfileId>, RecipeId)> for Request {
fn factory((profile_id, recipe_id): (Option<ProfileId>, RecipeId)) -> Self {
Self {
id: RequestId::new(),
profile_id,
recipe_id,
method: reqwest::Method::GET,
url: "http://localhost/url".parse().unwrap(),
headers: HeaderMap::new(),
body: None,
}
}
}

#[cfg(test)]
impl crate::test_util::Factory for Response {
fn factory(_: ()) -> Self {
Self {
status: StatusCode::OK,
headers: HeaderMap::new(),
body: Body::default(),
}
}
}

#[cfg(test)]
impl crate::test_util::Factory for RequestRecord {
fn factory(_: ()) -> Self {
let request = Request::factory(());
let response = Response::factory(());
Self {
id: request.id,
request: request.into(),
response: response.into(),
start_time: Utc::now(),
end_time: Utc::now(),
}
}
}

/// Customize profile and recipe ID
#[cfg(test)]
impl crate::test_util::Factory<(Option<ProfileId>, RecipeId)>
for RequestRecord
{
fn factory(params: (Option<ProfileId>, RecipeId)) -> Self {
let request = Request::factory(params);
let response = Response::factory(());
Self {
id: request.id,
request: request.into(),
response: response.into(),
start_time: Utc::now(),
end_time: Utc::now(),
}
}
}

/// A resolved HTTP response, with all content loaded and ready to be displayed
/// to the user. A simpler alternative to [reqwest::Response], because there's
/// no way to access all resolved data on that type at once. Resolving the
Expand Down Expand Up @@ -395,7 +470,7 @@ impl PartialEq for RequestError {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::{header_map, Factory};
use indexmap::indexmap;
use rstest::rstest;
use serde_json::json;
Expand Down
20 changes: 19 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ impl<T> TemplateKey<T> {
}
}

#[cfg(test)]
impl crate::test_util::Factory for TemplateContext {
fn factory(_: ()) -> Self {
use crate::test_util::TestPrompter;
Self {
collection: Collection::default(),
selected_profile: None,
http_engine: None,
database: CollectionDatabase::factory(()),
overrides: IndexMap::new(),
prompter: Box::<TestPrompter>::default(),
recursion_count: 0.into(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -185,7 +201,9 @@ mod tests {
},
config::Config,
http::{ContentType, Request, RequestRecord, Response},
test_util::*,
test_util::{
assert_err, header_map, temp_dir, Factory, TempDir, TestPrompter,
},
};
use chrono::Utc;
use indexmap::indexmap;
Expand Down
2 changes: 1 addition & 1 deletion src/template/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn take_until_or_eof<'a>(
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::*;
use crate::test_util::assert_err;
use itertools::Itertools;
use rstest::rstest;

Expand Down
Loading

0 comments on commit fbfd576

Please sign in to comment.