From e8af0050b9351c360dc5d08f4b6b410b7fce0d1b Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Wed, 24 Apr 2024 15:13:49 -0700 Subject: [PATCH] add unit test --- ee/tabby-webserver/src/schema/mod.rs | 10 +++- ee/tabby-webserver/src/schema/user_event.rs | 2 +- ee/tabby-webserver/src/service/user_event.rs | 63 +++++++++++++++++++- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/ee/tabby-webserver/src/schema/mod.rs b/ee/tabby-webserver/src/schema/mod.rs index 5ca10772ee05..2f8dfe8a2c39 100644 --- a/ee/tabby-webserver/src/schema/mod.rs +++ b/ee/tabby-webserver/src/schema/mod.rs @@ -443,7 +443,15 @@ impl Query { |after, before, first, last| async move { ctx.locator .user_event() - .list(after, before, first, last, users.unwrap_or_default(), start, end) + .list( + after, + before, + first, + last, + users.unwrap_or_default(), + start, + end, + ) .await }, ) diff --git a/ee/tabby-webserver/src/schema/user_event.rs b/ee/tabby-webserver/src/schema/user_event.rs index 545e42c40609..c4256da89589 100644 --- a/ee/tabby-webserver/src/schema/user_event.rs +++ b/ee/tabby-webserver/src/schema/user_event.rs @@ -5,7 +5,7 @@ use juniper::{GraphQLEnum, GraphQLObject, ID}; use super::Context; use crate::{juniper::relay::NodeType, schema::Result}; -#[derive(GraphQLEnum)] +#[derive(GraphQLEnum, Debug)] pub enum EventKind { Completion, Select, diff --git a/ee/tabby-webserver/src/service/user_event.rs b/ee/tabby-webserver/src/service/user_event.rs index 1e1ff6556482..5fc1e820d926 100644 --- a/ee/tabby-webserver/src/service/user_event.rs +++ b/ee/tabby-webserver/src/service/user_event.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; -use tabby_db::DbConn; use juniper::ID; +use tabby_db::DbConn; use tracing::warn; use super::{graphql_pagination_to_filter, AsRowid}; @@ -53,4 +53,63 @@ fn convert_ids(ids: Vec) -> Vec { } }) .collect() -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use assert_matches::assert_matches; + use chrono::{Days, Duration}; + + use super::*; + use crate::{schema::user_event::EventKind, service::AsID}; + + fn timestamp() -> u128 { + use std::time::{SystemTime, UNIX_EPOCH}; + let start = SystemTime::now(); + start + .duration_since(UNIX_EPOCH) + .expect("Time went backwards") + .as_millis() + } + + #[tokio::test] + async fn test_list_user_events() { + let db = DbConn::new_in_memory().await.unwrap(); + let user1 = db + .create_user("test@example.com".into(), Some("pass".into()), true) + .await + .unwrap(); + + db.create_user_event(user1, "view".into(), timestamp(), "".into()) + .await + .unwrap(); + + let user2 = db + .create_user("test2@example.com".into(), Some("pass".into()), true) + .await + .unwrap(); + + db.create_user_event(user2, "select".into(), timestamp(), "".into()) + .await + .unwrap(); + + let svc = create(db); + let end = Utc::now() + Duration::days(1); + let start = end.checked_sub_days(Days::new(100)).unwrap(); + + // List without users should return all events + let events = svc + .list(None, None, None, None, vec![], start, end) + .await + .unwrap(); + assert_eq!(events.len(), 2); + + // Filter with user should return only events for that user + let events = svc + .list(None, None, None, None, vec![user1.as_id()], start, end) + .await + .unwrap(); + assert_eq!(events.len(), 1); + assert_matches!(events[0].kind, EventKind::View); + } +}