Skip to content

Commit

Permalink
rename neo4j client to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
isidzukuri committed Jan 2, 2025
1 parent 4d4e975 commit be98c54
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
92 changes: 46 additions & 46 deletions src/neo4j_repository.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use neo4rs::*;

pub async fn prepare() -> Client {
let client = Client::new().await;
client.create_constraints().await;
client
pub async fn prepare() -> Repository {
let repository = Repository::new().await;
repository.create_constraints().await;
repository
}

pub struct Client {
pub struct Repository {
graph: Graph,
}

impl Client {
impl Repository {
pub async fn new() -> Self {
let host = std::env::var("NEO4J_HOST").unwrap_or_else(|_| "localhost".into());
let port = std::env::var("NEO4J_PORT").unwrap_or_else(|_| "7687".into());
Expand Down Expand Up @@ -207,9 +207,9 @@ use serial_test::serial;

#[tokio::test]
async fn test_connect_to_db() {
let client = Client::new().await;
let repository = Repository::new().await;

let mut result = client.graph.execute(query("RETURN 1")).await.unwrap();
let mut result = repository.graph.execute(query("RETURN 1")).await.unwrap();
let row = result.next().await.unwrap().unwrap();
let value: i64 = row.get("1").unwrap();
assert_eq!(1, value);
Expand All @@ -219,115 +219,115 @@ async fn test_connect_to_db() {
#[tokio::test]
#[serial]
async fn test_uniqueness_constraint() {
let client = prepare().await;
let _ = client.delete_all().await;
let repository = prepare().await;
let _ = repository.delete_all().await;

let entity_id = "999".to_string();

assert!(client.create(&entity_id).await.is_ok());
assert!(client.create(&entity_id).await.is_err());
let _ = client.delete_all().await;
assert!(repository.create(&entity_id).await.is_ok());
assert!(repository.create(&entity_id).await.is_err());
let _ = repository.delete_all().await;
}

#[tokio::test]
#[serial]
async fn test_crud() {
let client = prepare().await;
let _ = client.delete_all().await;
let repository = prepare().await;
let _ = repository.delete_all().await;

let entity_id = "1111".to_string();
let count = client.count_by_entity_id(&entity_id).await;
let count = repository.count_by_entity_id(&entity_id).await;
assert_eq!(count, 0);
let _ = client.create(&entity_id).await;
let count = client.count_by_entity_id(&entity_id).await;
let _ = repository.create(&entity_id).await;
let count = repository.count_by_entity_id(&entity_id).await;
assert_eq!(count, 1);
let entity_in_db = client.find(&entity_id).await;
let entity_in_db = repository.find(&entity_id).await;
assert_eq!(entity_in_db.is_some(), true);
let _ = client.delete_all().await;
let entity_in_db = client.find(&entity_id).await;
let _ = repository.delete_all().await;
let entity_in_db = repository.find(&entity_id).await;
assert_eq!(entity_in_db.is_some(), false);
let count = client.count_by_entity_id(&entity_id).await;
let count = repository.count_by_entity_id(&entity_id).await;
assert_eq!(count, 0);
}

#[tokio::test]
#[serial]
async fn test_create_similarity_relation() {
let client = prepare().await;
let _ = client.delete_all().await;
let repository = prepare().await;
let _ = repository.delete_all().await;

let a_entity_id = "1111".to_string();
let b_entity_id = "2222".to_string();
let similarity_estimation: f32 = 0.33;
let count = client.count_by_entity_id(&a_entity_id).await;
let count = repository.count_by_entity_id(&a_entity_id).await;
assert_eq!(count, 0);
let count = client.count_by_entity_id(&b_entity_id).await;
let count = repository.count_by_entity_id(&b_entity_id).await;
assert_eq!(count, 0);

let _ = client
let _ = repository
.create_similarity_relation(&a_entity_id, &b_entity_id, similarity_estimation)
.await;

let count = client.count_by_entity_id(&a_entity_id).await;
let count = repository.count_by_entity_id(&a_entity_id).await;
assert_eq!(count, 1);
let count = client.count_by_entity_id(&b_entity_id).await;
let count = repository.count_by_entity_id(&b_entity_id).await;
assert_eq!(count, 1);

let estimation_from_db = client
let estimation_from_db = repository
.get_similarity_estimation(&a_entity_id, &b_entity_id)
.await;
assert_eq!(estimation_from_db, 0.33);

let _ = client
let _ = repository
.create_similarity_relation(&a_entity_id, &b_entity_id, 0.7)
.await;

let count = client.count_by_entity_id(&a_entity_id).await;
let count = repository.count_by_entity_id(&a_entity_id).await;
assert_eq!(count, 1);
let count = client.count_by_entity_id(&b_entity_id).await;
let count = repository.count_by_entity_id(&b_entity_id).await;
assert_eq!(count, 1);

let estimation_from_db = client
let estimation_from_db = repository
.get_similarity_estimation(&a_entity_id, &b_entity_id)
.await;
assert_eq!(estimation_from_db, 0.7);

let _ = client.delete_all().await;
let _ = repository.delete_all().await;
}

#[tokio::test]
#[serial]
async fn test_find_missing_relations() {
let client = prepare().await;
let _ = client.delete_all().await;
let repository = prepare().await;
let _ = repository.delete_all().await;

let a_entity_id = "11111".to_string();
let b_entity_id = "22222".to_string();
let c_entity_id = "33333".to_string();
let d_entity_id = "44444".to_string();
let e_entity_id = "55555".to_string();
let _ = client.create(&a_entity_id).await;
let _ = client.create(&b_entity_id).await;
let _ = client.create(&c_entity_id).await;
let _ = client.create(&d_entity_id).await;
let _ = client.create(&e_entity_id).await;
let _ = repository.create(&a_entity_id).await;
let _ = repository.create(&b_entity_id).await;
let _ = repository.create(&c_entity_id).await;
let _ = repository.create(&d_entity_id).await;
let _ = repository.create(&e_entity_id).await;

let missing_ids = client.find_missing_relations(&a_entity_id).await;
let missing_ids = repository.find_missing_relations(&a_entity_id).await;

assert!(!missing_ids.contains(&a_entity_id));
assert!(missing_ids.contains(&b_entity_id));
assert!(missing_ids.contains(&c_entity_id));
assert!(missing_ids.contains(&d_entity_id));
assert!(missing_ids.contains(&e_entity_id));

let _ = client
let _ = repository
.create_similarity_relation(&a_entity_id, &b_entity_id, 0.1)
.await;
let _ = client
let _ = repository
.create_similarity_relation(&c_entity_id, &d_entity_id, 0.1)
.await;

let missing_ids = client.find_missing_relations(&a_entity_id).await;
let missing_ids = repository.find_missing_relations(&a_entity_id).await;

assert!(!missing_ids.contains(&a_entity_id));
assert!(!missing_ids.contains(&b_entity_id));
Expand Down
18 changes: 9 additions & 9 deletions src/text_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use futures::stream::StreamExt;

pub struct Processor {
bow_mapper: bow_data_mapper::Mapper,
graph_client: neo4j_repository::Client,
graph_repository: neo4j_repository::Repository,
}

impl Processor {
pub async fn new() -> Self {
Self {
bow_mapper: bow_data_mapper::Mapper::new().await,
graph_client: neo4j_repository::Client::new().await,
graph_repository: neo4j_repository::Repository::new().await,
}
}

Expand All @@ -36,14 +36,14 @@ impl Processor {
}
let similarity = compute_similarity(&new_bow, &current_bow);

self.graph_client
self.graph_repository
.create_similarity_relation(&new_bow.entity_id, &current_bow.entity_id, similarity)
.await;
}

async fn update_missing_similarities(&self, new_bow: &BOW) {
let missing_ids = self
.graph_client
.graph_repository
.find_missing_relations(&new_bow.entity_id)
.await;
for entity_id in missing_ids.iter() {
Expand All @@ -59,7 +59,7 @@ use serial_test::serial;
#[tokio::test]
#[serial]
async fn test_process() {
let graph_client = neo4j_repository::Client::new().await;
let graph_repository = neo4j_repository::Repository::new().await;
let collection = bow_data_mapper::Mapper::new().await;
collection.delete_all().await;

Expand Down Expand Up @@ -93,21 +93,21 @@ async fn test_process() {
let new_bow = collection.find(&entity_id).await.unwrap();
assert_eq!(new_bow.words_count, 3);

let estimation_from_db = graph_client
let estimation_from_db = graph_repository
.get_similarity_estimation(&entity_id, &bow1.entity_id)
.await;
assert_eq!(estimation_from_db, 0.725);

let estimation_from_db = graph_client
let estimation_from_db = graph_repository
.get_similarity_estimation(&entity_id, &bow2.entity_id)
.await;
assert_eq!(estimation_from_db, 0.33333334);

let estimation_from_db = graph_client
let estimation_from_db = graph_repository
.get_similarity_estimation(&entity_id, &bow3.entity_id)
.await;
assert_eq!(estimation_from_db, 0.0);

collection.delete_all().await;
let _ = graph_client.delete_all().await;
let _ = graph_repository.delete_all().await;
}

0 comments on commit be98c54

Please sign in to comment.