Skip to content

Commit

Permalink
Fix tests and add mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
amirRamirfatahi committed Dec 5, 2024
1 parent 0b189da commit aed1a86
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/target
.env
docker/.database
docker/.database*
docker/.env
**/.DS_Store
/static
95 changes: 95 additions & 0 deletions docker/db-graph/mocks/hot-tags.cypher
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Generate user keys
:param user1 => 'pyc598poqkdgtx1wc4aeptx67mqg71mmywyh7uzkffzittjmbiuo';

:param user2 => 'qumq6fady4bmw4w5tpsrj1tg36g3qo4tcfedga9p4bg4so4ikyzy';

:param user3 => 'r4irb481b8qspaixq1brwre8o87cxybsbk9iwe1f6f9ukrxxs7bo';

:param user4 => 'r91hi8kc3x6761gwfiigr7yn6nca1z47wm6jadhw1jbx1co93r9y';

:param user5 => 'tkpeqpx3ywoawiw6q8e6kuo9o3egr7fnhx83rudznbrrmqgdmomo';

:param post1 => '0032GZQ335NEJ';

:param post2 => '0032GZQ338BMP';

:param tag1 => 'tag1';

:param tag2 => 'tag2';

:param tag3 => 'tag3';

:param day => 24 * 60 * 60 * 1000;

:param month => 30 * 24 * 60 * 60 * 1000;

// Create the posts
MERGE (p:Post { id: $post1 }) SET p.content = "This is a test post", p.kind = "short", p.indexed_at = 1733380839000;

MERGE (p:Post { id: $post2 }) SET p.content = "This is a second test post", p.kind = "short", p.indexed_at = 1733380849000;

// athors
MATCH (u:User { id: $user1 }), (p:Post {id: $post1})
MERGE (u)-[:AUTHORED]->(p);

MATCH (u:User { id: $user2 }), (p:Post {id: $post2})
MERGE (u)-[:AUTHORED]->(p);

// Today
WITH datetime().epochMillis AS today_millis
MATCH (u:User { id: $user2 }), (p:Post {id: $post1})
MERGE (u)-[:TAGGED { label:$tag1, id: "0032GZRVBRB30", indexed_at: today_millis }]->(p);

WITH datetime().epochMillis AS today_millis
MATCH (u:User { id: $user3 }), (p:Post {id: $post1})
MERGE (u)-[:TAGGED { label: $tag1, id: "0032GZRVBRAR8", indexed_at: today_millis }]->(p);

WITH datetime().epochMillis AS today_millis
MATCH (u:User { id: $user4 }), (p:Post {id: $post1})
MERGE (u)-[:TAGGED { label: $tag1, id: "0032GZRSZTRA8", indexed_at: today_millis }]->(p);

WITH datetime().epochMillis AS today_millis
MATCH (u:User { id: $user4 }), (p:Post {id: $post1})
MERGE (u)-[:TAGGED { label: $tag2, id: "0032GZRSZTV00", indexed_at: today_millis }]->(p);

WITH datetime().epochMillis AS today_millis
MATCH (u:User { id: $user5 }), (p:Post {id: $post1})
MERGE (u)-[:TAGGED { label: $tag2, id: "0032GZS4DWEGM", indexed_at: today_millis }]->(p);

// This month
WITH datetime().epochMillis - (2 * $day) AS this_month_millis
MATCH (u:User { id: $user1 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label:$tag1, id: "0032GZR6TQTSG", indexed_at: this_month_millis }]->(p);

WITH datetime().epochMillis - (2 * $day) AS this_month_millis
MATCH (u:User { id: $user1 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label:$tag2, id: "0032GZRVBRC3M", indexed_at: this_month_millis }]->(p);

WITH datetime().epochMillis - (2 * $day) AS this_month_millis
MATCH (u:User { id: $user3 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag3, id: "0032GZR6TQTT6", indexed_at: this_month_millis }]->(p);

WITH datetime().epochMillis - (2 * $day) AS this_month_millis
MATCH (u:User { id: $user4 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag3, id: "0032GZR6TQV4P", indexed_at: this_month_millis }]->(p);

WITH datetime().epochMillis - (2 * $day) AS this_month_millis
MATCH (u:User { id: $user5 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag3, id: "0032GZS4DWF62", indexed_at: this_month_millis }]->(p);

// All time (outside this month period)
WITH datetime().epochMillis - (7 * $day + $month) AS all_time_millis
MATCH (u:User { id: $user1 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label:$tag3, id: "0032GZRG46YPY", indexed_at: all_time_millis }]->(p);

WITH datetime().epochMillis - (7 * $day + $month) AS all_time_millis
MATCH (u:User { id: $user3 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag2, id: "0032GZRG46ZH0", indexed_at: all_time_millis }]->(p);

WITH datetime().epochMillis - (7 * $day + $month) AS all_time_millis
MATCH (u:User { id: $user4 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag1, id: "0032GZRG46ZH6", indexed_at: all_time_millis }]->(p);

WITH datetime().epochMillis - (7 * $day + $month) AS all_time_millis
MATCH (u:User { id: $user5 }), (p:Post {id: $post2})
MERGE (u)-[:TAGGED { label: $tag1, id: "0032GZRG46ZH6", indexed_at: all_time_millis }]->(p);
39 changes: 0 additions & 39 deletions docker/db-graph/mocks/hot_tags.cypher

This file was deleted.

3 changes: 3 additions & 0 deletions docker/db-graph/run-queries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
QUERIES_FILE="/db-graph/skunk.cypher"
# To achieve deterministic tests, each domain will have its own testing graph
TAGS_TEST_FILE="/db-graph/mocks/tags.cypher"
HOT_TAGS_TEST_FILE="/db-graph/mocks/hot-tags.cypher"
POSTS_TEST_FILE="/db-graph/mocks/posts.cypher"
FILES_TEST_FILE="/db-graph/mocks/files.cypher"
MUTES_TEST_FILE="/db-graph/mocks/mutes.cypher"
Expand All @@ -20,6 +21,8 @@ fi
time cypher-shell -u neo4j -p 12345678 -f "$QUERIES_FILE"
echo "Importing TAGs test graph..."
time cypher-shell -u neo4j -p 12345678 -f "$TAGS_TEST_FILE"
echo "Importing HOT TAGs test graph..."
time cypher-shell -u neo4j -p 12345678 -f "$HOT_TAGS_TEST_FILE"
echo "Importing POSTs test graph..."
time cypher-shell -u neo4j -p 12345678 -f "$POSTS_TEST_FILE"
echo "Importing FILEs test graph..."
Expand Down
2 changes: 2 additions & 0 deletions src/db/graph/queries/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::models::tag::stream::HotTagsInput;
use crate::models::tag::stream::TagStreamReach;
use crate::types::Pagination;
use crate::types::StreamSorting;
use log::debug;
use neo4rs::{query, Query};
use pubky_app_specs::PubkyAppPostKind;

Expand Down Expand Up @@ -503,6 +504,7 @@ pub fn get_global_hot_tags(tags_query: &HotTagsInput) -> Query {
None => String::from("Post|User"),
};
let (from, to) = tags_query.timeframe.to_timestamp_range();
debug!("get_global_hot_tags query: {:?} {:?}", from, to);
query(
format!(
"
Expand Down
2 changes: 1 addition & 1 deletion src/routes/v0/tag/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn tag_taggers_handler(
tag = "Tags",
params(
("user_id" = Option<String>, Query, description = "User Pubky ID"),
("reach" = Option<TagStreamReach>, Query, description = "Reach type: follower | following | friends | wot"),
("reach" = Option<TagStreamReach>, Query, description = "Reach type: follower | following | friends"),
("taggers_limit" = Option<usize>, Query, description = "Retrieve N user_id for each tag"),
("skip" = Option<usize>, Query, description = "Skip N tags"),
("limit" = Option<usize>, Query, description = "Retrieve N tag"),
Expand Down
7 changes: 4 additions & 3 deletions tests/service/stream/post/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ pub const POST_ED: &str = "2Z1N9M56X7DG0";
pub const POST_EF: &str = "2Z1N9M56W8D00";
pub const POST_EG: &str = "2Z1N9M56X5VG0";
pub const POST_EH: &str = "2Z1N9M56WJKG0";
pub const POST_EI: &str = "2Z1N8QBETDS00";
pub const POST_EJ: &str = "2Z1N8QBERHB00";
pub const POST_EK: &str = "2Z1N8QBETHK00";
pub const POST_EI: &str = "0032GZQ338BMP";
pub const POST_EJ: &str = "2Z1N8QBETDS00";
pub const POST_EK: &str = "2Z1N8QBERHB00";
pub const POST_EL: &str = "2Z1N8QBETHK00";

pub const POST_E0: &str = "2Z1PBYS0F90G0";
pub const POST_E1: &str = "2ZECRNM66G900";
Expand Down
52 changes: 44 additions & 8 deletions tests/service/tags/hot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,44 @@ async fn test_global_hot_tags() -> Result<()> {
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 4th index
let hot_tag = StreamTagMockup::new(String::from("pubky"), 14, 26, 14);
compare_unit_hot_tag(&tags[3], hot_tag);
let hot_tag = StreamTagMockup::new(String::from("pubky"), 9, 15, 9);
compare_unit_hot_tag(&tags[4], hot_tag);

Ok(())
}

#[tokio::test]
async fn test_global_hot_tags_with_today_timeframe() -> Result<()> {
let body = make_request("/v0/tags/hot?timeframe=today").await?;

assert!(body.is_array());

let tags = body.as_array().expect("Stream tags should be an array");

// Validate that the posts belong to the specified user's bookmarks
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 4th index
let hot_tag = StreamTagMockup::new(String::from("tag2"), 2, 1, 2);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
}

#[tokio::test]
async fn test_global_hot_tags_with_this_month_timeframe() -> Result<()> {
let body = make_request("/v0/tags/hot?timeframe=this_month").await?;

assert!(body.is_array());

let tags = body.as_array().expect("Stream tags should be an array");

// Validate that the posts belong to the specified user's bookmarks
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 4th index
let hot_tag = StreamTagMockup::new(String::from("tag2"), 3, 2, 3);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
}
Expand Down Expand Up @@ -106,7 +142,7 @@ async fn test_hot_tags_by_following_reach() -> Result<()> {
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 0 index
let hot_tag = StreamTagMockup::new(String::from("pubky"), 5, 12, 5);
let hot_tag = StreamTagMockup::new(String::from("pubky"), 4, 5, 4);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
Expand Down Expand Up @@ -146,7 +182,7 @@ async fn test_hot_tags_by_following_using_taggers_limit() -> Result<()> {
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 0 index
let hot_tag = StreamTagMockup::new(String::from("pubky"), 3, 12, 5);
let hot_tag = StreamTagMockup::new(String::from("pubky"), 3, 5, 4);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
Expand All @@ -165,8 +201,8 @@ async fn test_hot_tags_by_followers_reach() -> Result<()> {
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 1st index
let hot_tag = StreamTagMockup::new(String::from("pubky"), 3, 10, 3);
compare_unit_hot_tag(&tags[1], hot_tag);
let hot_tag = StreamTagMockup::new(String::from("pubky"), 2, 3, 2);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
}
Expand All @@ -184,8 +220,8 @@ async fn test_hot_tags_by_friends_reach() -> Result<()> {
analyse_hot_tags_structure(tags);

// Analyse the tag that is in the 1st index
let hot_tag = StreamTagMockup::new(String::from("pubky"), 3, 10, 3);
compare_unit_hot_tag(&tags[1], hot_tag);
let hot_tag = StreamTagMockup::new(String::from("pubky"), 2, 3, 2);
compare_unit_hot_tag(&tags[0], hot_tag);

Ok(())
}

0 comments on commit aed1a86

Please sign in to comment.