Skip to content

Commit 0c60e2d

Browse files
authored
test: add rstest and fixtures (#632)
test: add rstest and fixtures
1 parent 5388907 commit 0c60e2d

File tree

9 files changed

+281
-234
lines changed

9 files changed

+281
-234
lines changed

Cargo.lock

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

crates/monitor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ env_logger = { version = "0.11.0", default-features = false }
2525
test-log = { version = "0.2.12", default-features = false }
2626
wiremock.workspace = true
2727
test-assets = { path = "../test-assets" }
28+
rstest = "0.24.0"
2829
test-with = "0.14.6"

crates/monitor/src/client/monitor.rs

Lines changed: 23 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -72,47 +72,14 @@ mod tests {
7272

7373
use super::*;
7474

75-
#[tokio::test]
76-
async fn test_parses_synced_and_healthy_response() {
77-
let mock_server = MockServer::start().await;
78-
let status_url: Url = mock_server
79-
.uri()
80-
.parse::<Url>()
81-
.unwrap()
82-
.join("/status")
83-
.unwrap();
84-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
85-
86-
Mock::given(method("POST"))
87-
.and(path("/status"))
88-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
89-
"data": {
90-
"indexingStatuses": [
91-
{
92-
"synced": true,
93-
"health": "healthy"
94-
}
95-
]
96-
}
97-
})))
98-
.mount(&mock_server)
99-
.await;
100-
101-
let status = monitor_deployment_status(deployment, status_url)
102-
.await
103-
.unwrap();
104-
105-
assert_eq!(
106-
status.borrow().clone(),
107-
DeploymentStatus {
108-
synced: true,
109-
health: "healthy".to_string()
110-
}
111-
);
75+
struct MonitorMock {
76+
mock_server: MockServer,
77+
status_url: Url,
78+
deployment: DeploymentId,
11279
}
11380

114-
#[tokio::test]
115-
async fn test_parses_not_synced_and_healthy_response() {
81+
#[rstest::fixture]
82+
async fn monitor_mock() -> MonitorMock {
11683
let mock_server = MockServer::start().await;
11784
let status_url: Url = mock_server
11885
.uri()
@@ -121,109 +88,44 @@ mod tests {
12188
.join("/status")
12289
.unwrap();
12390
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
124-
125-
Mock::given(method("POST"))
126-
.and(path("/status"))
127-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
128-
"data": {
129-
"indexingStatuses": [
130-
{
131-
"synced": false,
132-
"health": "healthy"
133-
}
134-
]
135-
}
136-
})))
137-
.mount(&mock_server)
138-
.await;
139-
140-
let status = monitor_deployment_status(deployment, status_url)
141-
.await
142-
.unwrap();
143-
144-
assert_eq!(
145-
status.borrow().clone(),
146-
DeploymentStatus {
147-
synced: false,
148-
health: "healthy".to_string()
149-
}
150-
);
151-
}
152-
153-
#[tokio::test]
154-
async fn test_parses_synced_and_unhealthy_response() {
155-
let mock_server = MockServer::start().await;
156-
let status_url: Url = mock_server
157-
.uri()
158-
.parse::<Url>()
159-
.unwrap()
160-
.join("/status")
161-
.unwrap();
162-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
163-
164-
Mock::given(method("POST"))
165-
.and(path("/status"))
166-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
167-
"data": {
168-
"indexingStatuses": [
169-
{
170-
"synced": true,
171-
"health": "unhealthy"
172-
}
173-
]
174-
}
175-
})))
176-
.mount(&mock_server)
177-
.await;
178-
179-
let status = monitor_deployment_status(deployment, status_url)
180-
.await
181-
.unwrap();
182-
183-
assert_eq!(
184-
status.borrow().clone(),
185-
DeploymentStatus {
186-
synced: true,
187-
health: "unhealthy".to_string()
188-
}
189-
);
91+
MonitorMock {
92+
mock_server,
93+
status_url,
94+
deployment,
95+
}
19096
}
19197

98+
#[rstest::rstest]
19299
#[tokio::test]
193-
async fn test_parses_synced_and_failed_response() {
194-
let mock_server = MockServer::start().await;
195-
let status_url: Url = mock_server
196-
.uri()
197-
.parse::<Url>()
198-
.unwrap()
199-
.join("/status")
200-
.unwrap();
201-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
202-
100+
async fn test_parses_health_and_sync_status_response(
101+
#[future(awt)] monitor_mock: MonitorMock,
102+
#[values(true, false)] synced: bool,
103+
#[values("healthy", "unhealthy", "failed")] health: &str,
104+
) {
203105
Mock::given(method("POST"))
204106
.and(path("/status"))
205107
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
206108
"data": {
207109
"indexingStatuses": [
208110
{
209-
"synced": true,
210-
"health": "failed"
111+
"synced": synced,
112+
"health": health
211113
}
212114
]
213115
}
214116
})))
215-
.mount(&mock_server)
117+
.mount(&monitor_mock.mock_server)
216118
.await;
217119

218-
let status = monitor_deployment_status(deployment, status_url)
120+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
219121
.await
220122
.unwrap();
221123

222124
assert_eq!(
223125
status.borrow().clone(),
224126
DeploymentStatus {
225-
synced: true,
226-
health: "failed".to_string()
127+
synced,
128+
health: health.to_string()
227129
}
228130
);
229131
}

crates/tap-agent/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ wiremock-grpc = "0.0.3-alpha3"
6666
test-assets = { path = "../test-assets" }
6767
test-log.workspace = true
6868
rstest = "0.24.0"
69+
stdext = "0.3.3"

crates/tap-agent/src/agent/sender_account.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,10 +1423,11 @@ pub mod tests {
14231423
use serde_json::json;
14241424
use sqlx::PgPool;
14251425
use test_assets::{
1426-
flush_messages, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
1426+
flush_messages, pgpool, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
14271427
TAP_SIGNER as SIGNER,
14281428
};
14291429
use thegraph_core::alloy::{hex::ToHexExt, primitives::U256};
1430+
use tokio::sync::mpsc;
14301431
use wiremock::{
14311432
matchers::{body_string_contains, method},
14321433
Mock, MockServer, ResponseTemplate,
@@ -1467,6 +1468,22 @@ pub mod tests {
14671468
.await;
14681469
mock_escrow_subgraph_server
14691470
}
1471+
struct TestSenderAccount {
1472+
sender_account: ActorRef<SenderAccountMessage>,
1473+
msg_receiver: mpsc::Receiver<SenderAccountMessage>,
1474+
prefix: String,
1475+
}
1476+
1477+
#[rstest::fixture]
1478+
async fn basic_sender_account(#[future(awt)] pgpool: PgPool) -> TestSenderAccount {
1479+
let (sender_account, msg_receiver, prefix, _) =
1480+
create_sender_account().pgpool(pgpool).call().await;
1481+
TestSenderAccount {
1482+
sender_account,
1483+
msg_receiver,
1484+
prefix,
1485+
}
1486+
}
14701487

14711488
#[rstest::rstest]
14721489
#[sqlx::test(migrations = "../../migrations")]
@@ -1688,20 +1705,22 @@ pub mod tests {
16881705
.as_nanos() as u64
16891706
}
16901707

1691-
#[sqlx::test(migrations = "../../migrations")]
1692-
async fn test_update_receipt_fees_no_rav(pgpool: PgPool) {
1693-
let (sender_account, _, prefix, _) = create_sender_account().pgpool(pgpool).call().await;
1694-
1708+
#[rstest::rstest]
1709+
#[tokio::test]
1710+
async fn test_update_receipt_fees_no_rav(
1711+
#[future(awt)] basic_sender_account: TestSenderAccount,
1712+
) {
16951713
// create a fake sender allocation
16961714
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1697-
prefix,
1715+
basic_sender_account.prefix,
16981716
SENDER.1,
16991717
ALLOCATION_ID_0,
1700-
sender_account.clone(),
1718+
basic_sender_account.sender_account.clone(),
17011719
)
17021720
.await;
17031721

1704-
sender_account
1722+
basic_sender_account
1723+
.sender_account
17051724
.cast(SenderAccountMessage::UpdateReceiptFees(
17061725
ALLOCATION_ID_0,
17071726
ReceiptFees::NewReceipt(TRIGGER_VALUE - 1, get_current_timestamp_u64_ns()),
@@ -1714,40 +1733,42 @@ pub mod tests {
17141733
assert_not_triggered!(&triggered_rav_request);
17151734
}
17161735

1717-
#[sqlx::test(migrations = "../../migrations")]
1718-
async fn test_update_receipt_fees_trigger_rav(pgpool: PgPool) {
1719-
let (sender_account, mut msg_receiver, prefix, _) =
1720-
create_sender_account().pgpool(pgpool).call().await;
1721-
1736+
#[rstest::rstest]
1737+
#[tokio::test]
1738+
async fn test_update_receipt_fees_trigger_rav(
1739+
#[future(awt)] mut basic_sender_account: TestSenderAccount,
1740+
) {
17221741
// create a fake sender allocation
17231742
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1724-
prefix,
1743+
basic_sender_account.prefix,
17251744
SENDER.1,
17261745
ALLOCATION_ID_0,
1727-
sender_account.clone(),
1746+
basic_sender_account.sender_account.clone(),
17281747
)
17291748
.await;
17301749

1731-
sender_account
1750+
basic_sender_account
1751+
.sender_account
17321752
.cast(SenderAccountMessage::UpdateReceiptFees(
17331753
ALLOCATION_ID_0,
17341754
ReceiptFees::NewReceipt(TRIGGER_VALUE, get_current_timestamp_u64_ns()),
17351755
))
17361756
.unwrap();
17371757

1738-
flush_messages(&mut msg_receiver).await;
1758+
flush_messages(&mut basic_sender_account.msg_receiver).await;
17391759
assert_not_triggered!(&triggered_rav_request);
17401760

17411761
// wait for it to be outside buffer
17421762
tokio::time::sleep(BUFFER_DURATION).await;
17431763

1744-
sender_account
1764+
basic_sender_account
1765+
.sender_account
17451766
.cast(SenderAccountMessage::UpdateReceiptFees(
17461767
ALLOCATION_ID_0,
17471768
ReceiptFees::Retry,
17481769
))
17491770
.unwrap();
1750-
flush_messages(&mut msg_receiver).await;
1771+
flush_messages(&mut basic_sender_account.msg_receiver).await;
17511772

17521773
assert_triggered!(&triggered_rav_request);
17531774
}
@@ -1837,8 +1858,9 @@ pub mod tests {
18371858
}
18381859

18391860
/// Test that the deny status is correctly loaded from the DB at the start of the actor
1840-
#[sqlx::test(migrations = "../../migrations")]
1841-
async fn test_init_deny(pgpool: PgPool) {
1861+
#[rstest::rstest]
1862+
#[tokio::test]
1863+
async fn test_init_deny(#[future(awt)] pgpool: PgPool) {
18421864
sqlx::query!(
18431865
r#"
18441866
INSERT INTO scalar_tap_denylist (sender_address)

0 commit comments

Comments
 (0)