Skip to content

Commit 2a83951

Browse files
authored
feat(ads-client): add simple integration test (#7055)
1 parent ef01bca commit 2a83951

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ads-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ sql-support = { path = "../support/sql" }
3333
mockall = "0.12"
3434
mockito = { version = "0.31", default-features = false }
3535
viaduct-dev = { path = "../support/viaduct-dev" }
36+
viaduct-reqwest = { path = "../support/viaduct-reqwest" }
3637

3738
[build-dependencies]
3839
uniffi = { version = "0.29.0", features = ["build"] }

components/ads-client/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,32 @@ This component is currently still under construction.
1414

1515
## Tests
1616

17-
Tests are run with
17+
### Unit Tests
18+
19+
Unit tests are run with
1820

1921
```shell
2022
cargo test -p ads-client
2123
```
2224

25+
### Integration Tests
26+
27+
Integration tests make real HTTP calls to the Mozilla Ads Routing Service (MARS) and are not run automatically in CI. They are marked with `#[ignore]` and must be run manually.
28+
29+
To run integration tests:
30+
31+
```shell
32+
cargo test -p ads-client --test integration_test -- --ignored
33+
```
34+
35+
To run a specific integration test:
36+
37+
```shell
38+
cargo test -p ads-client --test integration_test -- --ignored test_mock_pocket_billboard_1_placement
39+
```
40+
41+
**Note:** Integration tests require network access and will make real HTTP requests to the MARS API.
42+
2343
## Usage
2444

2545
Please refer to `./docs/usage.md` for information on using the component.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
*/
5+
6+
use ads_client::{MozAdsClient, MozAdsPlacementRequest, MozAdsPlacementRequestWithCount};
7+
8+
#[test]
9+
#[ignore]
10+
fn test_mock_pocket_billboard_1_placement() {
11+
viaduct_reqwest::use_reqwest_backend();
12+
13+
let client = MozAdsClient::new(None);
14+
15+
let placement_request = MozAdsPlacementRequest {
16+
placement_id: "mock_pocket_billboard_1".to_string(),
17+
iab_content: None,
18+
};
19+
20+
let result = client.request_ads(vec![placement_request], None);
21+
println!("result: {:?}", result);
22+
23+
assert!(result.is_ok(), "Failed to request ads: {:?}", result.err());
24+
25+
let placements = result.unwrap();
26+
27+
assert!(
28+
placements.contains_key("mock_pocket_billboard_1"),
29+
"Response should contain placement_id 'mock_pocket_billboard_1'"
30+
);
31+
32+
let placement = placements
33+
.get("mock_pocket_billboard_1")
34+
.expect("Placement should exist");
35+
36+
assert!(!placement.url.is_empty(), "Ad URL should not be empty");
37+
assert!(
38+
!placement.image_url.is_empty(),
39+
"Ad image URL should not be empty"
40+
);
41+
assert!(
42+
!placement.format.is_empty(),
43+
"Ad format should not be empty"
44+
);
45+
assert!(
46+
!placement.block_key.is_empty(),
47+
"Ad block_key should not be empty"
48+
);
49+
}
50+
51+
#[test]
52+
#[ignore]
53+
fn test_request_ads_multiset_count() {
54+
viaduct_reqwest::use_reqwest_backend();
55+
56+
let client = MozAdsClient::new(None);
57+
58+
let requested_count = 3;
59+
let placement_request = MozAdsPlacementRequestWithCount {
60+
placement_id: "mock_pocket_billboard_1".to_string(),
61+
count: requested_count,
62+
iab_content: None,
63+
};
64+
65+
let result = client.request_ads_multiset(vec![placement_request], None);
66+
println!("result: {:?}", result);
67+
68+
assert!(result.is_ok(), "Failed to request ads: {:?}", result.err());
69+
70+
let placements = result.unwrap();
71+
72+
assert!(
73+
placements.contains_key("mock_pocket_billboard_1"),
74+
"Response should contain placement_id 'mock_pocket_billboard_1'"
75+
);
76+
77+
let ads = placements
78+
.get("mock_pocket_billboard_1")
79+
.expect("Placement should exist");
80+
81+
assert_eq!(
82+
ads.len(),
83+
requested_count as usize,
84+
"Should have {} ads, but got {}",
85+
requested_count,
86+
ads.len()
87+
);
88+
89+
for (index, ad) in ads.iter().enumerate() {
90+
assert!(!ad.url.is_empty(), "Ad {} URL should not be empty", index);
91+
assert!(
92+
!ad.image_url.is_empty(),
93+
"Ad {} image URL should not be empty",
94+
index
95+
);
96+
assert!(
97+
!ad.format.is_empty(),
98+
"Ad {} format should not be empty",
99+
index
100+
);
101+
assert!(
102+
!ad.block_key.is_empty(),
103+
"Ad {} block_key should not be empty",
104+
index
105+
);
106+
}
107+
}

0 commit comments

Comments
 (0)