Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.

Commit 5bea038

Browse files
committed
add integration tests
1 parent 23ef91e commit 5bea038

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

capture-server/tests/events.rs

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ async fn it_captures_one_event() -> Result<()> {
1313
setup_tracing();
1414
let token = random_string("token", 16);
1515
let distinct_id = random_string("id", 16);
16-
let topic = EphemeralTopic::new().await;
17-
let server = ServerHandle::for_topic(&topic).await;
16+
17+
let main_topic = EphemeralTopic::new().await;
18+
let histo_topic = EphemeralTopic::new().await;
19+
let server = ServerHandle::for_topics(&main_topic, &histo_topic).await;
1820

1921
let event = json!({
2022
"token": token,
@@ -24,7 +26,7 @@ async fn it_captures_one_event() -> Result<()> {
2426
let res = server.capture_events(event.to_string()).await;
2527
assert_eq!(StatusCode::OK, res.status());
2628

27-
let event = topic.next_event()?;
29+
let event = main_topic.next_event()?;
2830
assert_json_include!(
2931
actual: event,
3032
expected: json!({
@@ -37,14 +39,15 @@ async fn it_captures_one_event() -> Result<()> {
3739
}
3840

3941
#[tokio::test]
40-
async fn it_captures_a_batch() -> Result<()> {
42+
async fn it_captures_a_posthogjs_array() -> Result<()> {
4143
setup_tracing();
4244
let token = random_string("token", 16);
4345
let distinct_id1 = random_string("id", 16);
4446
let distinct_id2 = random_string("id", 16);
4547

46-
let topic = EphemeralTopic::new().await;
47-
let server = ServerHandle::for_topic(&topic).await;
48+
let main_topic = EphemeralTopic::new().await;
49+
let histo_topic = EphemeralTopic::new().await;
50+
let server = ServerHandle::for_topics(&main_topic, &histo_topic).await;
4851

4952
let event = json!([{
5053
"token": token,
@@ -59,14 +62,98 @@ async fn it_captures_a_batch() -> Result<()> {
5962
assert_eq!(StatusCode::OK, res.status());
6063

6164
assert_json_include!(
62-
actual: topic.next_event()?,
65+
actual: main_topic.next_event()?,
6366
expected: json!({
6467
"token": token,
6568
"distinct_id": distinct_id1
6669
})
6770
);
6871
assert_json_include!(
69-
actual: topic.next_event()?,
72+
actual: main_topic.next_event()?,
73+
expected: json!({
74+
"token": token,
75+
"distinct_id": distinct_id2
76+
})
77+
);
78+
79+
Ok(())
80+
}
81+
82+
#[tokio::test]
83+
async fn it_captures_a_batch() -> Result<()> {
84+
setup_tracing();
85+
let token = random_string("token", 16);
86+
let distinct_id1 = random_string("id", 16);
87+
let distinct_id2 = random_string("id", 16);
88+
89+
let main_topic = EphemeralTopic::new().await;
90+
let histo_topic = EphemeralTopic::new().await;
91+
let server = ServerHandle::for_topics(&main_topic, &histo_topic).await;
92+
93+
let event = json!({
94+
"token": token,
95+
"batch": [{
96+
"event": "event1",
97+
"distinct_id": distinct_id1
98+
},{
99+
"event": "event2",
100+
"distinct_id": distinct_id2
101+
}]
102+
});
103+
let res = server.capture_events(event.to_string()).await;
104+
assert_eq!(StatusCode::OK, res.status());
105+
106+
assert_json_include!(
107+
actual: main_topic.next_event()?,
108+
expected: json!({
109+
"token": token,
110+
"distinct_id": distinct_id1
111+
})
112+
);
113+
assert_json_include!(
114+
actual: main_topic.next_event()?,
115+
expected: json!({
116+
"token": token,
117+
"distinct_id": distinct_id2
118+
})
119+
);
120+
121+
Ok(())
122+
}
123+
#[tokio::test]
124+
async fn it_captures_a_historical_batch() -> Result<()> {
125+
setup_tracing();
126+
let token = random_string("token", 16);
127+
let distinct_id1 = random_string("id", 16);
128+
let distinct_id2 = random_string("id", 16);
129+
130+
let main_topic = EphemeralTopic::new().await;
131+
let histo_topic = EphemeralTopic::new().await;
132+
let server = ServerHandle::for_topics(&main_topic, &histo_topic).await;
133+
134+
let event = json!({
135+
"token": token,
136+
"historical_migration": true,
137+
"batch": [{
138+
"event": "event1",
139+
"distinct_id": distinct_id1
140+
},{
141+
"event": "event2",
142+
"distinct_id": distinct_id2
143+
}]
144+
});
145+
let res = server.capture_events(event.to_string()).await;
146+
assert_eq!(StatusCode::OK, res.status());
147+
148+
assert_json_include!(
149+
actual: histo_topic.next_event()?,
150+
expected: json!({
151+
"token": token,
152+
"distinct_id": distinct_id1
153+
})
154+
);
155+
assert_json_include!(
156+
actual: histo_topic.next_event()?,
70157
expected: json!({
71158
"token": token,
72159
"distinct_id": distinct_id2

0 commit comments

Comments
 (0)