Skip to content

Commit

Permalink
adds more debug tests for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Jan 26, 2024
1 parent 4c3a5a2 commit 1e3cded
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
22 changes: 20 additions & 2 deletions src/web/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::util::read_file_utf8;
pub const CONFIG_PATH: &str = "event_config.json";
const TEMPLATE_REPLACE_REGEX: &str = "<[^<>]+>";

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Criterion
{
#[serde(default)]
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn expand_template(template: String, data: HashMap<String, serde_json::Value

pub fn satisfied(criterion: Criterion, data: &HashMap<String, serde_json::Value>) -> bool
{

crate::debug(format!("testing criterion {:?}", criterion), None);
if criterion.check_value_path == ""
{
return true
Expand Down Expand Up @@ -221,20 +221,36 @@ pub fn satisfied(criterion: Criterion, data: &HashMap<String, serde_json::Value>
}
};

crate::debug(format!("extraced {:?} from path {:?}", extracted_value, path), None);

if extracted_value.is_none()
{
crate::debug(format!("got none: return false"), None);
return false
}

let string_value = extracted_value.unwrap().to_string().replace("\"", "");

crate::debug(format!("string value {}, check ins {:?}, check not ins {:?}", string_value, criterion.check_value_in, criterion.check_value_not_in), None);

crate::debug(
format!("in emtpy {} in contains {}, not in empty, {} not in contains {}",
criterion.check_value_in.is_empty(),
criterion.check_value_in.contains(&string_value),
criterion.check_value_not_in.is_empty(),
criterion.check_value_not_in.contains(&string_value) ),
None
);

if (criterion.check_value_in.is_empty() || criterion.check_value_in.contains(&string_value)) &&
(criterion.check_value_not_in.is_empty() || !criterion.check_value_not_in.contains(&string_value))
{
crate::debug(format!("return true"), None);
return true
}
else
{
crate::debug(format!("return false"), None);
return false
}
}
Expand All @@ -248,8 +264,10 @@ pub fn select_template(templates: Vec<Template>, data: HashMap<String, serde_jso

for template in templates
{
crate::debug(format!("testing template {}", template.body), None);
if template.criteria.into_iter().all(|c| satisfied(c, &data))
{
crate::debug(format!("selected template {}", template.body), None);
return template.body
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ pub const RELEASE_PAYLOAD: &str = "

pub const RELEASE_PAYLOAD_PRIVATE: &str = "
{
\"action\": \"published\",
\"action\": \"created\",
\"release\": {
\"url\": \"https://api.github.com/repos/JerboaBurrow/jGL/releases/135576460\",
\"assets_url\": \"https://api.github.com/repos/JerboaBurrow/jGL/releases/135576460/assets\",
Expand Down
44 changes: 42 additions & 2 deletions tests/test_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod test_template
use std::collections::HashMap;
use serde_json;

use crate::common::{ISSUE_PAYLOAD, STAR_CREATED_PAYLOAD, STAR_DELETED_PAYLOAD};
use pulse::web::event::{self, Template, select_template};
use crate::common::{ISSUE_PAYLOAD, RELEASE_PAYLOAD, RELEASE_PAYLOAD_PRIVATE, STAR_CREATED_PAYLOAD, STAR_DELETED_PAYLOAD};
use pulse::{util::strip_control_characters, web::event::{self, Template, select_template}};

const ISSUE_TEMPLATE_OK: &str = "this is an issue template string the action was <action> the repository url was <issue/repository_url>";
const ISSUE_TEMPLATE_BAD: &str = "this is an issue template string the action was <action> the repository url was <issue/a_non_existant_value>";
Expand Down Expand Up @@ -110,6 +110,22 @@ mod test_template
]
"#;

const ONLY_PUBLISHED: &str = r#"
[
{
"criteria":
[
{
"check_value_path": "action",
"check_value_in": ["published"],
"check_value_not_in": []
}
],
"body": "New release just droppped!"
}
]
"#;

#[test]
fn issue_format_ok()
{
Expand Down Expand Up @@ -215,4 +231,28 @@ mod test_template

assert_eq!(selected, "".to_string())
}

#[test]
fn only_published_is_published()
{
let templates: Vec<Template> = serde_json::from_str(ONLY_PUBLISHED).unwrap();

let parsed_data: HashMap<String, serde_json::Value> = serde_json::from_str(&strip_control_characters(RELEASE_PAYLOAD.to_string())).unwrap();

let selected = select_template(templates, parsed_data);

assert_eq!(selected, "New release just droppped!".to_string())
}

#[test]
fn only_published_is_created()
{
let templates: Vec<Template> = serde_json::from_str(ONLY_PUBLISHED).unwrap();

let parsed_data: HashMap<String, serde_json::Value> = serde_json::from_str(&strip_control_characters(RELEASE_PAYLOAD_PRIVATE.to_string())).unwrap();

let selected = select_template(templates, parsed_data);

assert_eq!(selected, "".to_string())
}
}

0 comments on commit 1e3cded

Please sign in to comment.