Skip to content

Commit 1d47f92

Browse files
committed
add tests
1 parent cbb6eb6 commit 1d47f92

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

src/app/core/reports.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ fn filter_sql(filters: &[DimensionFilter]) -> Result<(String, ParamVec<'_>)> {
182182
Dimension::UtmCampaign => format!("utm_campaign {filter_value}"),
183183
Dimension::UtmContent => format!("utm_content {filter_value}"),
184184
Dimension::UtmTerm => format!("utm_term {filter_value}"),
185-
Dimension::ScreenResolution => format!("concat_ws('x', screen_width::text, screen_height::text) {filter_value}"),
185+
Dimension::ScreenResolution => {
186+
format!("concat_ws('x', screen_width::text, screen_height::text) {filter_value}")
187+
}
186188
})
187189
})
188190
.collect::<Result<Vec<String>>>()?;
@@ -459,7 +461,9 @@ pub fn dimension_report(
459461
Dimension::UtmCampaign => ("utm_campaign", "utm_campaign"),
460462
Dimension::UtmContent => ("utm_content", "utm_content"),
461463
Dimension::UtmTerm => ("utm_term", "utm_term"),
462-
Dimension::ScreenResolution => ("concat_ws('x', screen_width::text, screen_height::text)", "screen_width, screen_height"),
464+
Dimension::ScreenResolution => {
465+
("nullif(concat_ws('x', screen_width::text, screen_height::text), '')", "screen_width, screen_height")
466+
}
463467
};
464468

465469
params.push(range.start);

tests/dashboard.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ async fn test_dashboard() -> Result<()> {
3535
json!({"dimension":"url","filters":[{"dimension":"fqdn","filterType":"equal","value":"example.org"},{"dimension":"url","filterType":"equal","value":"example.org/contact"},{"dimension":"referrer","filterType":"equal","value":"liwan.dev"},{"dimension":"country","filterType":"equal","value":"AU"},{"dimension":"city","filterType":"equal","value":"Sydney"},{"dimension":"platform","filterType":"equal","value":"iOS"},{"dimension":"browser","filterType":"equal","value":"Safari"},{"dimension":"mobile","filterType":"is_true"}],"metric":"views","range":{"start": start_date ,"end": end_date}}),
3636
json!({"dimension":"city","filters":[{"dimension":"fqdn","filterType":"equal","value":"example.org"},{"dimension":"url","filterType":"equal","value":"example.org/contact"},{"dimension":"referrer","filterType":"equal","value":"liwan.dev"},{"dimension":"country","filterType":"equal","value":"AU"},{"dimension":"city","filterType":"equal","value":"Sydney"},{"dimension":"platform","filterType":"equal","value":"iOS"},{"dimension":"browser","filterType":"equal","value":"Safari"},{"dimension":"mobile","filterType":"is_true"}],"metric":"views","range":{"start": start_date ,"end": end_date}}),
3737
json!({"dimension":"browser","filters":[{"dimension":"fqdn","filterType":"equal","value":"example.org"},{"dimension":"url","filterType":"equal","value":"example.org/contact"},{"dimension":"referrer","filterType":"equal","value":"liwan.dev"},{"dimension":"country","filterType":"equal","value":"AU"},{"dimension":"city","filterType":"equal","value":"Sydney"},{"dimension":"platform","filterType":"equal","value":"iOS"},{"dimension":"browser","filterType":"equal","value":"Safari"},{"dimension":"mobile","filterType":"is_true"}],"metric":"views","range":{"start": start_date ,"end": end_date}}),
38+
json!({"dimension":"screen_resolution","filters":[],"metric":"views","range":{"start": start_date ,"end": end_date}}),
39+
json!({"dimension":"screen_resolution","filters":[],"metric":"unique_visitors","range":{"start": start_date ,"end": end_date}}),
40+
json!({"dimension":"url","filters":[{"dimension":"screen_resolution","filterType":"equal","value":"1920x1080"}],"metric":"views","range":{"start": start_date ,"end": end_date}}),
3841
];
3942

4043
for request in stats_requests.iter() {

tests/event.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,138 @@ async fn test_event() -> Result<()> {
2626

2727
Ok(())
2828
}
29+
30+
#[tokio::test]
31+
async fn test_event_screen_size_accepted() -> Result<()> {
32+
let app = common::app();
33+
let (tx, _rx) = common::events();
34+
let client = common::TestClient::new(app.clone(), tx);
35+
app.entities.create(&Entity { display_name: "Entity 1".to_string(), id: "entity-1".to_string() }, &[])?;
36+
37+
let user_agent = vec![("user-agent".to_string(), "Mozilla/5.0 (Linux x86_64)".to_string())];
38+
let event_desktop_screen_size = json!({
39+
"entity_id": "entity-1",
40+
"name": "pageview",
41+
"url": "https://example.com/",
42+
"screen_width": 1920,
43+
"screen_height": 1080
44+
});
45+
46+
let res = client.post_with_headers("/api/event", event_desktop_screen_size.clone(), user_agent.clone()).await;
47+
res.assert_status_success();
48+
49+
let event_mobile_screen_size = json!({
50+
"entity_id": "entity-1",
51+
"name": "pageview",
52+
"url": "https://example.com/",
53+
"screen_width": 390,
54+
"screen_height": 844
55+
});
56+
57+
let res = client.post_with_headers("/api/event", event_mobile_screen_size.clone(), user_agent.clone()).await;
58+
res.assert_status_success();
59+
60+
Ok(())
61+
}
62+
63+
#[tokio::test]
64+
async fn test_event_screen_size_read_write() -> Result<()> {
65+
use chrono::Utc;
66+
use liwan::app::models::Event;
67+
68+
let app = common::app();
69+
let (tx, _rx) = common::events();
70+
let client = common::TestClient::new(app.clone(), tx);
71+
72+
app.seed_database(0)?;
73+
74+
let events_to_insert = vec![
75+
Event {
76+
entity_id: "entity-1".to_string(),
77+
visitor_id: "visitor-desktop".to_string(),
78+
event: "pageview".to_string(),
79+
created_at: Utc::now(),
80+
fqdn: Some("example.com".to_string()),
81+
path: Some("/".to_string()),
82+
referrer: None,
83+
platform: None,
84+
browser: None,
85+
mobile: Some(false),
86+
country: None,
87+
city: None,
88+
utm_source: None,
89+
utm_medium: None,
90+
utm_campaign: None,
91+
utm_content: None,
92+
utm_term: None,
93+
screen_width: Some(1920),
94+
screen_height: Some(1080),
95+
},
96+
Event {
97+
entity_id: "entity-1".to_string(),
98+
visitor_id: "visitor-mobile".to_string(),
99+
event: "pageview".to_string(),
100+
created_at: Utc::now(),
101+
fqdn: Some("example.com".to_string()),
102+
path: Some("/".to_string()),
103+
referrer: None,
104+
platform: None,
105+
browser: None,
106+
mobile: Some(true),
107+
country: None,
108+
city: None,
109+
utm_source: None,
110+
utm_medium: None,
111+
utm_campaign: None,
112+
utm_content: None,
113+
utm_term: None,
114+
screen_width: Some(390),
115+
screen_height: Some(844),
116+
},
117+
Event {
118+
entity_id: "entity-1".to_string(),
119+
visitor_id: "visitor-old".to_string(),
120+
event: "pageview".to_string(),
121+
created_at: Utc::now(),
122+
fqdn: Some("example.com".to_string()),
123+
path: Some("/".to_string()),
124+
referrer: None,
125+
platform: None,
126+
browser: None,
127+
mobile: None,
128+
country: None,
129+
city: None,
130+
utm_source: None,
131+
utm_medium: None,
132+
utm_campaign: None,
133+
utm_content: None,
134+
utm_term: None,
135+
screen_width: None,
136+
screen_height: None,
137+
},
138+
];
139+
app.events.append(events_to_insert.into_iter())?;
140+
141+
let start = (Utc::now() - chrono::Duration::hours(1)).to_rfc3339();
142+
let end = Utc::now().to_rfc3339();
143+
144+
let query = json!({
145+
"dimension": "screen_resolution",
146+
"filters": [],
147+
"metric": "views",
148+
"range": { "start": start, "end": end }
149+
});
150+
151+
let res = client.post("/api/dashboard/project/public-project/dimension", query.clone()).await;
152+
res.assert_status_success();
153+
154+
let body: serde_json::Value = res.json();
155+
let rows = body["data"].as_array().expect("expected data array");
156+
let values: Vec<&str> = rows.iter().filter_map(|r| r["dimensionValue"].as_str()).collect();
157+
158+
assert!(values.contains(&"1920x1080"), "expected 1920x1080 in results, got: {values:?}");
159+
assert!(values.contains(&"390x844"), "expected 390x844 in results, got: {values:?}");
160+
assert!(values.contains(&"Unknown"), "expected Unknown for NULL screen data, got: {values:?}");
161+
162+
Ok(())
163+
}

0 commit comments

Comments
 (0)