From 98412eb013e6e7c64b08794b3904f5e01261d979 Mon Sep 17 00:00:00 2001 From: jbell Date: Sun, 6 Oct 2024 15:53:59 -0400 Subject: [PATCH] Initial commit --- Cargo.toml | 18 ++ contracts/puff_pastry/Cargo.toml | 15 ++ contracts/puff_pastry/src/test.rs | 306 ++++++++++++++++++++++++++++++ 3 files changed, 339 insertions(+) create mode 100644 Cargo.toml create mode 100644 contracts/puff_pastry/Cargo.toml create mode 100644 contracts/puff_pastry/src/test.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..49ef214 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[workspace] +resolver = "2" +members = [ + "contracts/*", +] + +[workspace.dependencies] +soroban-sdk = "21.6.0" + +[profile.release] +opt-level = "z" +overflow-checks = true +debug = 0 +strip = "symbols" +debug-assertions = false +panic = "abort" +codegen-units = 1 +lto = true \ No newline at end of file diff --git a/contracts/puff_pastry/Cargo.toml b/contracts/puff_pastry/Cargo.toml new file mode 100644 index 0000000..6c1c333 --- /dev/null +++ b/contracts/puff_pastry/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "puffpastry" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] +doctest = false + +[dependencies] +soroban-sdk = { workspace = true } + +[dev-dependencies] +soroban-sdk = { workspace = true, features = ["testutils"] } \ No newline at end of file diff --git a/contracts/puff_pastry/src/test.rs b/contracts/puff_pastry/src/test.rs new file mode 100644 index 0000000..bf2ba0e --- /dev/null +++ b/contracts/puff_pastry/src/test.rs @@ -0,0 +1,306 @@ +#![cfg(test)] + +use soroban_sdk::{Address, String}; +use super::*; +use soroban_sdk::Env; + +macro_rules! add_issue { + ($client:expr, $env:expr, $id:expr, $title:expr, $paragraphs:expr, $author:expr) => { + $client.add_issue( + &BytesN::from_array(&$env, $id), + &String::from_str(&$env, $title), + &{ + let mut paragraphs = Vec::new(&$env); + for ¶graph in $paragraphs.iter() { + paragraphs.push_back(String::from_str(&$env, paragraph)); + } + paragraphs + }, + &String::from_str(&$env, $author) + ) + }; +} + +fn setup() -> (Env, Address, PuffPastryClient<'static>) { + let env = Env::default(); + let contract_id = env.clone().register_contract(None, PuffPastry); + let client = PuffPastryClient::new(&env.clone(), &contract_id); + (env, contract_id, client) +} + +#[test] +fn test_add_and_list_issues() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + + let result = client.list_issues(); + + assert_eq!(result.len(), 1) +} + +#[test] +fn test_vote_on_nonexistent_issue() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 91]; + + let result = client.increase_positive_vote(&48017u64, &BytesN::from_array(&env, id)); + + assert_eq!(result, false) +} + +#[test] +fn test_get_issue_by_id() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 91]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + + let result = client.get_issue(&BytesN::from_array(&env, id)); + + assert_eq!(result.unwrap().title, String::from_str(&env, "Lorem Ipsum")) +} + +#[test] +fn test_list_issues_with_no_votes() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 91]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + add_issue!(client, env, id, "Look for the union label", &["123"], "johncena"); + + let result = client.list_issues(); + + assert_eq!(result.len(), 2) +} + +#[test] +fn test_list_issues_after_positive_vote() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 91]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.increase_positive_vote(&48017u64, &BytesN::from_array(&env, id)); + + let result = client.list_issues(); + + assert_eq!(result.len(), 1) +} + +#[test] +fn test_list_issues_after_negative_vote() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 91]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.increase_negative_vote(&48017u64, &BytesN::from_array(&env, id)); + + let result = client.list_issues(); + + assert_eq!(result.len(), 1) +} + +#[test] +fn test_get_paragraphs_for_issue() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + + let result = client.get_paragraphs_for_issue(&BytesN::from_array(&env, id)); + + assert_eq!(result.unwrap(), Vec::from_array( + &env, + [String::from_str(&env, "lorem"), String::from_str(&env, "ipsum")]) + ) +} + +#[test] +fn test_get_paragraphs_for_nonexistent_issue() { + let (env, _, client) = setup(); + + let id1 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let id2 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, id1, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + + let result = client.get_paragraphs_for_issue(&BytesN::from_array(&env, id2)); + + assert_eq!(result, None) +} + +#[test] +fn test_add_comment_for_nonexistent_issue() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let issue_id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + let result = client.add_comment( + &BytesN::from_array(&env, id), + &BytesN::from_array(&env, issue_id), + &String::from_str(&env, "Look for the union label") + ); + + assert_eq!(result, false) +} + +#[test] +fn test_add_and_retrieve_comment_for_issue() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let issue_id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, issue_id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + + client.add_comment( + &BytesN::from_array(&env, id), + &BytesN::from_array(&env, issue_id), + &String::from_str(&env, "Look for the union label") + ); + + let result = client.get_comments_for_issue(&BytesN::from_array(&env, issue_id)); + + assert_eq!(result.len(), 1); +} + +#[test] +fn test_add_and_retrieve_multiple_comments_for_issue() { + let (env, _, client) = setup(); + + let id1 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 94]; + let id2 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let issue_id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, issue_id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.add_comment( + &BytesN::from_array(&env, id1), + &BytesN::from_array(&env, issue_id), + &String::from_str(&env, "Look for the union label") + ); + client.add_comment( + &BytesN::from_array(&env, id2), + &BytesN::from_array(&env, issue_id), + &String::from_str(&env, "Lorem ipsum") + ); + + let result = client.get_comments_for_issue(&BytesN::from_array(&env, issue_id)); + + assert_eq!(result.len(), 2); +} + +#[test] +fn test_get_comments_after_negative_vote() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let comment_id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.add_comment( + &BytesN::from_array(&env, comment_id), + &BytesN::from_array(&env, id), + &String::from_str(&env, "Look for the union label") + ); + client.increase_negative_comment_vote(&BytesN::from_array(&env, comment_id)); + + let result = client.get_comments_for_issue(&BytesN::from_array(&env, id)); + + assert_eq!(result.len(), 1) +} + +#[test] +fn test_get_comments_after_positive_vote() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let comment_id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.add_comment( + &BytesN::from_array(&env, comment_id), + &BytesN::from_array(&env, id), + &String::from_str(&env, "Look for the union label") + ); + client.increase_positive_comment_vote(&BytesN::from_array(&env, comment_id)); + + let result = client.get_comments_for_issue(&BytesN::from_array(&env, id)); + + assert_eq!(result.len(), 1) +} + +#[test] +fn test_positive_vote_addition() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.increase_positive_vote(&48017u64, &BytesN::from_array(&env, id)); + + let result = client.get_positive_votes_for_user(&48017u64); + + assert_eq!(result.unwrap().len(), 1) +} + +#[test] +fn test_multiple_positive_vote_additions() { + let (env, _, client) = setup(); + + let id1 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let id2 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, id1, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + add_issue!(client, env, id2, "Look for the union label", &["123"], "johncena"); + client.increase_positive_vote(&48017u64, &BytesN::from_array(&env, id1)); + client.increase_positive_vote(&48017u64, &BytesN::from_array(&env, id2)); + + let result = client.get_positive_votes_for_user(&48017u64); + + assert_eq!(result.unwrap().len(), 2) +} + +#[test] +fn test_negative_vote_addition() { + let (env, _, client) = setup(); + + let id = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + + add_issue!(client, env, id, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + client.increase_negative_vote(&48017u64, &BytesN::from_array(&env, id)); + + let result = client.get_negative_votes_for_user(&48017u64); + + assert_eq!(result.unwrap().len(), 1) +} + +#[test] +fn test_multiple_negative_vote_additions() { + let (env, _, client) = setup(); + + let id1 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 92]; + let id2 = &[176, 218, 132, 194, 224, 182, 150, 172, 253, 22, 224, 106, 232, 199, 67, 93]; + + add_issue!(client, env, id1, "Lorem Ipsum", &["lorem", "ipsum"], "johncena"); + add_issue!(client, env, id2, "Look for the union label", &["123"], "johncena"); + client.increase_negative_vote(&48017u64, &BytesN::from_array(&env, id1)); + client.increase_negative_vote(&48017u64, &BytesN::from_array(&env, id2)); + + let result = client.get_negative_votes_for_user(&48017u64); + + assert_eq!(result.unwrap().len(), 2) +} + +#[test] +fn test_user_attempts_multiple_votes() { + +}