Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys committed Apr 24, 2024
1 parent 791ffa2 commit e8af005
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
10 changes: 9 additions & 1 deletion ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Check warning on line 454 in ee/tabby-webserver/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/mod.rs#L446-L454

Added lines #L446 - L454 were not covered by tests
.await
},
)
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-webserver/src/schema/user_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use juniper::{GraphQLEnum, GraphQLObject, ID};
use super::Context;
use crate::{juniper::relay::NodeType, schema::Result};

#[derive(GraphQLEnum)]
#[derive(GraphQLEnum, Debug)]

Check warning on line 8 in ee/tabby-webserver/src/schema/user_event.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/user_event.rs#L8

Added line #L8 was not covered by tests
pub enum EventKind {
Completion,
Select,
Expand Down
63 changes: 61 additions & 2 deletions ee/tabby-webserver/src/service/user_event.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -53,4 +53,63 @@ fn convert_ids(ids: Vec<ID>) -> Vec<i64> {
}
})
.collect()
}
}

#[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("[email protected]".into(), Some("pass".into()), true)
.await
.unwrap();

db.create_user_event(user1, "view".into(), timestamp(), "".into())
.await
.unwrap();

let user2 = db
.create_user("[email protected]".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);
}
}

0 comments on commit e8af005

Please sign in to comment.