Skip to content

Commit 56c7988

Browse files
committed
Use insta for GET /api/v1/keywords tests
1 parent 3893841 commit 56c7988

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

src/tests/keyword.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1+
use crate::insta::rfc3339_redaction;
12
use crate::{builders::CrateBuilder, RequestHelper, TestApp};
23
use cargo_registry::{models::Keyword, views::EncodableKeyword};
4+
use insta::assert_json_snapshot;
5+
use serde_json::Value;
36

4-
#[derive(Deserialize)]
5-
struct KeywordList {
6-
keywords: Vec<EncodableKeyword>,
7-
meta: KeywordMeta,
8-
}
9-
#[derive(Deserialize)]
10-
struct KeywordMeta {
11-
total: i32,
12-
}
137
#[derive(Deserialize)]
148
struct GoodKeyword {
159
keyword: EncodableKeyword,
@@ -19,18 +13,15 @@ struct GoodKeyword {
1913
fn index() {
2014
let url = "/api/v1/keywords";
2115
let (app, anon) = TestApp::init().empty();
22-
let json: KeywordList = anon.get(url).good();
23-
assert_eq!(json.keywords.len(), 0);
24-
assert_eq!(json.meta.total, 0);
16+
let json: Value = anon.get(url).good();
17+
assert_json_snapshot!(json);
2518

2619
app.db(|conn| {
2720
Keyword::find_or_create_all(conn, &["foo"]).unwrap();
2821
});
2922

30-
let json: KeywordList = anon.get(url).good();
31-
assert_eq!(json.keywords.len(), 1);
32-
assert_eq!(json.meta.total, 1);
33-
assert_eq!(json.keywords[0].keyword.as_str(), "foo");
23+
let json: Value = anon.get(url).good();
24+
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
3425
}
3526

3627
#[test]
@@ -42,8 +33,9 @@ fn show() {
4233
app.db(|conn| {
4334
Keyword::find_or_create_all(conn, &["foo"]).unwrap();
4435
});
45-
let json: GoodKeyword = anon.get(url).good();
46-
assert_eq!(json.keyword.keyword.as_str(), "foo");
36+
37+
let json: Value = anon.get(url).good();
38+
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
4739
}
4840

4941
#[test]
@@ -55,8 +47,9 @@ fn uppercase() {
5547
app.db(|conn| {
5648
Keyword::find_or_create_all(conn, &["UPPER"]).unwrap();
5749
});
58-
let json: GoodKeyword = anon.get(url).good();
59-
assert_eq!(json.keyword.keyword.as_str(), "upper");
50+
51+
let json: Value = anon.get(url).good();
52+
assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() });
6053
}
6154

6255
#[test]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: src/tests/keyword.rs
3+
expression: json
4+
---
5+
{
6+
"keywords": [
7+
{
8+
"crates_cnt": 0,
9+
"created_at": "[datetime]",
10+
"id": "foo",
11+
"keyword": "foo"
12+
}
13+
],
14+
"meta": {
15+
"total": 1
16+
}
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: src/tests/keyword.rs
3+
expression: json
4+
---
5+
{
6+
"keywords": [],
7+
"meta": {
8+
"total": 0
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: src/tests/keyword.rs
3+
expression: json
4+
---
5+
{
6+
"keyword": {
7+
"crates_cnt": 0,
8+
"created_at": "[datetime]",
9+
"id": "foo",
10+
"keyword": "foo"
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: src/tests/keyword.rs
3+
expression: json
4+
---
5+
{
6+
"keyword": {
7+
"crates_cnt": 0,
8+
"created_at": "[datetime]",
9+
"id": "upper",
10+
"keyword": "upper"
11+
}
12+
}

0 commit comments

Comments
 (0)