From 389384136a3021bb8da2dcb24bdad4aef454f5ec Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 23 Oct 2020 00:17:07 +0200 Subject: [PATCH 1/2] Use `insta` for `GET /api/v1/summary` tests --- Cargo.lock | 89 ++++ Cargo.toml | 1 + src/tests/all.rs | 1 + src/tests/insta.rs | 9 + src/tests/krate.rs | 53 +-- .../all__krate__summary_doesnt_die.snap | 14 + .../all__krate__summary_new_crates.snap | 412 ++++++++++++++++++ 7 files changed, 537 insertions(+), 42 deletions(-) create mode 100644 src/tests/insta.rs create mode 100644 src/tests/snapshots/all__krate__summary_doesnt_die.snap create mode 100644 src/tests/snapshots/all__krate__summary_new_crates.snap diff --git a/Cargo.lock b/Cargo.lock index d126d8156e1..cc5addebe6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,6 +302,7 @@ dependencies = [ "hyper", "hyper-tls", "indexmap", + "insta", "jemallocator", "lazy_static", "lettre", @@ -525,6 +526,20 @@ dependencies = [ "conduit", ] +[[package]] +name = "console" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b1aacfaffdbff75be81c15a399b4bedf78aaefe840e8af1d299ac2ade885d2" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "terminal_size", + "termios", + "winapi 0.3.9", +] + [[package]] name = "const_fn" version = "0.4.2" @@ -674,6 +689,12 @@ dependencies = [ "migrations_macros", ] +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" + [[package]] name = "digest" version = "0.8.1" @@ -722,6 +743,12 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "134951f4028bdadb9b84baf4232681efbf277da25144b9b0ad65df75946c422b" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.24" @@ -1240,6 +1267,22 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "insta" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e7528a20113cf7ca90eddfc2439c608188b6eafc0613964da2bd140c92acec" +dependencies = [ + "console", + "difference", + "lazy_static", + "pest", + "pest_derive", + "serde", + "serde_json", + "serde_yaml", +] + [[package]] name = "instant" version = "0.1.7" @@ -1440,6 +1483,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb374efe4669153b6bb170a7c5063a4e570a4eb45e5daf0dab4c1543d4d6f197" +[[package]] +name = "linked-hash-map" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a" + [[package]] name = "lock_api" version = "0.4.1" @@ -2399,6 +2448,18 @@ dependencies = [ "url", ] +[[package]] +name = "serde_yaml" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae3e2dd40a7cdc18ca80db804b7f461a39bb721160a85c9a1fa30134bf3c02a5" +dependencies = [ + "dtoa", + "linked-hash-map", + "serde", + "yaml-rust", +] + [[package]] name = "sha-1" version = "0.8.2" @@ -2714,6 +2775,25 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "terminal_size" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a14cd9f8c72704232f0bfc8455c0e861f0ad4eb60cc9ec8a170e231414c1e13" +dependencies = [ + "libc", + "winapi 0.3.9", +] + +[[package]] +name = "termios" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "411c5bf740737c7918b8b1fe232dca4dc9f8e754b8ad5e20966814001ed0ac6b" +dependencies = [ + "libc", +] + [[package]] name = "thiserror" version = "1.0.21" @@ -3221,3 +3301,12 @@ dependencies = [ "markup5ever", "time 0.1.44", ] + +[[package]] +name = "yaml-rust" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39f0c922f1a334134dc2f7a8b67dc5d25f0735263feec974345ff706bcf20b0d" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index 26a6a22a464..82e22d71a05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,6 +91,7 @@ url = "2.1" conduit-test = "0.9.0-alpha.3" diesel_migrations = { version = "1.3.0", features = ["postgres"] } hyper-tls = "0.4" +insta = { version = "1.1.0", features = ["redactions"] } lazy_static = "1.0" tokio = { version = "0.2", default-features = false, features = ["stream"]} tower-service = "0.3.0" diff --git a/src/tests/all.rs b/src/tests/all.rs index afc99c7f14a..b790470aead 100644 --- a/src/tests/all.rs +++ b/src/tests/all.rs @@ -50,6 +50,7 @@ mod categories; mod category; mod dump_db; mod git; +mod insta; mod keyword; mod krate; mod owners; diff --git a/src/tests/insta.rs b/src/tests/insta.rs new file mode 100644 index 00000000000..05307db29f3 --- /dev/null +++ b/src/tests/insta.rs @@ -0,0 +1,9 @@ +use chrono::DateTime; +use insta::{dynamic_redaction, internals::Redaction}; + +pub fn rfc3339_redaction() -> Redaction { + dynamic_redaction(|value, _| { + assert!(DateTime::parse_from_rfc3339(value.as_str().unwrap()).is_ok()); + "[datetime]" + }) +} diff --git a/src/tests/krate.rs b/src/tests/krate.rs index 516b901dd5f..a95b03146f7 100644 --- a/src/tests/krate.rs +++ b/src/tests/krate.rs @@ -1,15 +1,13 @@ use crate::{ builders::{CrateBuilder, DependencyBuilder, PublishBuilder, VersionBuilder}, + insta::rfc3339_redaction, new_category, new_dependency, new_user, CrateMeta, CrateResponse, GoodCrate, OkBool, RequestHelper, TestApp, }; use cargo_registry::{ models::{krate::MAX_NAME_LENGTH, Category, Crate}, schema::{api_tokens, crates, emails, metadata, versions, versions_published_by}, - views::{ - EncodableCategory, EncodableCrate, EncodableDependency, EncodableKeyword, EncodableVersion, - EncodableVersionDownload, - }, + views::{EncodableDependency, EncodableVersion, EncodableVersionDownload}, }; use std::{ collections::HashMap, @@ -22,6 +20,8 @@ use chrono::Utc; use conduit::StatusCode; use diesel::{dsl::*, prelude::*, update}; use flate2::{write::GzEncoder, Compression}; +use insta::assert_json_snapshot; +use serde_json::Value; #[derive(Deserialize)] struct VersionsList { @@ -42,18 +42,6 @@ struct Downloads { version_downloads: Vec, } -#[derive(Deserialize)] -struct SummaryResponse { - num_downloads: i64, - num_crates: i64, - new_crates: Vec, - most_downloaded: Vec, - most_recently_downloaded: Vec, - just_updated: Vec, - popular_keywords: Vec, - popular_categories: Vec, -} - impl crate::util::MockAnonymousUser { fn reverse_dependencies(&self, krate_name: &str) -> RevDeps { let url = format!("/api/v1/crates/{}/reverse_dependencies", krate_name); @@ -1330,7 +1318,8 @@ fn new_krate_records_verified_email() { #[test] fn summary_doesnt_die() { let (_, anon) = TestApp::init().empty(); - anon.get::("/api/v1/summary").good(); + let json: Value = anon.get("/api/v1/summary").good(); + assert_json_snapshot!(json); } #[test] @@ -1403,31 +1392,11 @@ fn summary_new_crates() { .unwrap(); }); - let json: SummaryResponse = anon.get("/api/v1/summary").good(); - - assert_eq!(json.num_crates, 5); - assert_eq!(json.num_downloads, 6000); - assert_eq!(json.most_downloaded[0].name, "most_recent_downloads"); - assert_eq!(json.most_downloaded[0].downloads, 5000); - assert_eq!(json.most_downloaded[0].recent_downloads, Some(50)); - assert_eq!( - json.most_recently_downloaded[0].name, - "most_recent_downloads" - ); - assert_eq!(json.most_recently_downloaded[0].recent_downloads, Some(50)); - assert_eq!(json.popular_keywords[0].keyword, "popular"); - assert_eq!(json.popular_categories[0].category, "Category 1"); - assert_eq!(json.just_updated.len(), 2); - - assert_eq!(json.just_updated[0].name, "just_updated_patch"); - assert_eq!(json.just_updated[0].max_version, "0.2.0"); - assert_eq!(json.just_updated[0].newest_version, "0.1.1"); - - assert_eq!(json.just_updated[1].name, "just_updated"); - assert_eq!(json.just_updated[1].max_version, "0.1.2"); - assert_eq!(json.just_updated[1].newest_version, "0.1.2"); - - assert_eq!(json.new_crates.len(), 5); + let json: Value = anon.get("/api/v1/summary").good(); + assert_json_snapshot!(json, { + ".**.created_at" => rfc3339_redaction(), + ".**.updated_at" => rfc3339_redaction(), + }); } #[test] diff --git a/src/tests/snapshots/all__krate__summary_doesnt_die.snap b/src/tests/snapshots/all__krate__summary_doesnt_die.snap new file mode 100644 index 00000000000..b409265c0cc --- /dev/null +++ b/src/tests/snapshots/all__krate__summary_doesnt_die.snap @@ -0,0 +1,14 @@ +--- +source: src/tests/krate.rs +expression: json +--- +{ + "just_updated": [], + "most_downloaded": [], + "most_recently_downloaded": [], + "new_crates": [], + "num_crates": 0, + "num_downloads": 0, + "popular_categories": [], + "popular_keywords": [] +} diff --git a/src/tests/snapshots/all__krate__summary_new_crates.snap b/src/tests/snapshots/all__krate__summary_new_crates.snap new file mode 100644 index 00000000000..3f15288b344 --- /dev/null +++ b/src/tests/snapshots/all__krate__summary_new_crates.snap @@ -0,0 +1,412 @@ +--- +source: src/tests/krate.rs +expression: json +--- +{ + "just_updated": [ + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated_patch", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated_patch/owner_team", + "owner_user": "/api/v1/crates/just_updated_patch/owner_user", + "owners": "/api/v1/crates/just_updated_patch/owners", + "reverse_dependencies": "/api/v1/crates/just_updated_patch/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated_patch/downloads", + "versions": "/api/v1/crates/just_updated_patch/versions" + }, + "max_version": "0.2.0", + "name": "just_updated_patch", + "newest_version": "0.1.1", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated/owner_team", + "owner_user": "/api/v1/crates/just_updated/owner_user", + "owners": "/api/v1/crates/just_updated/owners", + "reverse_dependencies": "/api/v1/crates/just_updated/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated/downloads", + "versions": "/api/v1/crates/just_updated/versions" + }, + "max_version": "0.1.2", + "name": "just_updated", + "newest_version": "0.1.2", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + } + ], + "most_downloaded": [ + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 5000, + "exact_match": false, + "homepage": null, + "id": "most_recent_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/most_recent_downloads/owner_team", + "owner_user": "/api/v1/crates/most_recent_downloads/owner_user", + "owners": "/api/v1/crates/most_recent_downloads/owners", + "reverse_dependencies": "/api/v1/crates/most_recent_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/most_recent_downloads/downloads", + "versions": "/api/v1/crates/most_recent_downloads/versions" + }, + "max_version": "0.2.0", + "name": "most_recent_downloads", + "newest_version": "0.2.0", + "recent_downloads": 50, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 1000, + "exact_match": false, + "homepage": null, + "id": "with_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/with_downloads/owner_team", + "owner_user": "/api/v1/crates/with_downloads/owner_user", + "owners": "/api/v1/crates/with_downloads/owners", + "reverse_dependencies": "/api/v1/crates/with_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/with_downloads/downloads", + "versions": "/api/v1/crates/with_downloads/versions" + }, + "max_version": "0.3.0", + "name": "with_downloads", + "newest_version": "0.3.0", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": "description", + "documentation": null, + "downloads": 20, + "exact_match": false, + "homepage": null, + "id": "some_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/some_downloads/owner_team", + "owner_user": "/api/v1/crates/some_downloads/owner_user", + "owners": "/api/v1/crates/some_downloads/owners", + "reverse_dependencies": "/api/v1/crates/some_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/some_downloads/downloads", + "versions": "/api/v1/crates/some_downloads/versions" + }, + "max_version": "0.1.0", + "name": "some_downloads", + "newest_version": "0.1.0", + "recent_downloads": 10, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated_patch", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated_patch/owner_team", + "owner_user": "/api/v1/crates/just_updated_patch/owner_user", + "owners": "/api/v1/crates/just_updated_patch/owners", + "reverse_dependencies": "/api/v1/crates/just_updated_patch/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated_patch/downloads", + "versions": "/api/v1/crates/just_updated_patch/versions" + }, + "max_version": "0.2.0", + "name": "just_updated_patch", + "newest_version": "0.1.1", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated/owner_team", + "owner_user": "/api/v1/crates/just_updated/owner_user", + "owners": "/api/v1/crates/just_updated/owners", + "reverse_dependencies": "/api/v1/crates/just_updated/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated/downloads", + "versions": "/api/v1/crates/just_updated/versions" + }, + "max_version": "0.1.2", + "name": "just_updated", + "newest_version": "0.1.2", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + } + ], + "most_recently_downloaded": [ + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 5000, + "exact_match": false, + "homepage": null, + "id": "most_recent_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/most_recent_downloads/owner_team", + "owner_user": "/api/v1/crates/most_recent_downloads/owner_user", + "owners": "/api/v1/crates/most_recent_downloads/owners", + "reverse_dependencies": "/api/v1/crates/most_recent_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/most_recent_downloads/downloads", + "versions": "/api/v1/crates/most_recent_downloads/versions" + }, + "max_version": "0.2.0", + "name": "most_recent_downloads", + "newest_version": "0.2.0", + "recent_downloads": 50, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": "description", + "documentation": null, + "downloads": 20, + "exact_match": false, + "homepage": null, + "id": "some_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/some_downloads/owner_team", + "owner_user": "/api/v1/crates/some_downloads/owner_user", + "owners": "/api/v1/crates/some_downloads/owners", + "reverse_dependencies": "/api/v1/crates/some_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/some_downloads/downloads", + "versions": "/api/v1/crates/some_downloads/versions" + }, + "max_version": "0.1.0", + "name": "some_downloads", + "newest_version": "0.1.0", + "recent_downloads": 10, + "repository": null, + "updated_at": "[datetime]", + "versions": null + } + ], + "new_crates": [ + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated_patch", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated_patch/owner_team", + "owner_user": "/api/v1/crates/just_updated_patch/owner_user", + "owners": "/api/v1/crates/just_updated_patch/owners", + "reverse_dependencies": "/api/v1/crates/just_updated_patch/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated_patch/downloads", + "versions": "/api/v1/crates/just_updated_patch/versions" + }, + "max_version": "0.2.0", + "name": "just_updated_patch", + "newest_version": "0.1.1", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 0, + "exact_match": false, + "homepage": null, + "id": "just_updated", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/just_updated/owner_team", + "owner_user": "/api/v1/crates/just_updated/owner_user", + "owners": "/api/v1/crates/just_updated/owners", + "reverse_dependencies": "/api/v1/crates/just_updated/reverse_dependencies", + "version_downloads": "/api/v1/crates/just_updated/downloads", + "versions": "/api/v1/crates/just_updated/versions" + }, + "max_version": "0.1.2", + "name": "just_updated", + "newest_version": "0.1.2", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 1000, + "exact_match": false, + "homepage": null, + "id": "with_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/with_downloads/owner_team", + "owner_user": "/api/v1/crates/with_downloads/owner_user", + "owners": "/api/v1/crates/with_downloads/owners", + "reverse_dependencies": "/api/v1/crates/with_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/with_downloads/downloads", + "versions": "/api/v1/crates/with_downloads/versions" + }, + "max_version": "0.3.0", + "name": "with_downloads", + "newest_version": "0.3.0", + "recent_downloads": null, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": null, + "documentation": null, + "downloads": 5000, + "exact_match": false, + "homepage": null, + "id": "most_recent_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/most_recent_downloads/owner_team", + "owner_user": "/api/v1/crates/most_recent_downloads/owner_user", + "owners": "/api/v1/crates/most_recent_downloads/owners", + "reverse_dependencies": "/api/v1/crates/most_recent_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/most_recent_downloads/downloads", + "versions": "/api/v1/crates/most_recent_downloads/versions" + }, + "max_version": "0.2.0", + "name": "most_recent_downloads", + "newest_version": "0.2.0", + "recent_downloads": 50, + "repository": null, + "updated_at": "[datetime]", + "versions": null + }, + { + "badges": null, + "categories": null, + "created_at": "[datetime]", + "description": "description", + "documentation": null, + "downloads": 20, + "exact_match": false, + "homepage": null, + "id": "some_downloads", + "keywords": null, + "links": { + "owner_team": "/api/v1/crates/some_downloads/owner_team", + "owner_user": "/api/v1/crates/some_downloads/owner_user", + "owners": "/api/v1/crates/some_downloads/owners", + "reverse_dependencies": "/api/v1/crates/some_downloads/reverse_dependencies", + "version_downloads": "/api/v1/crates/some_downloads/downloads", + "versions": "/api/v1/crates/some_downloads/versions" + }, + "max_version": "0.1.0", + "name": "some_downloads", + "newest_version": "0.1.0", + "recent_downloads": 10, + "repository": null, + "updated_at": "[datetime]", + "versions": null + } + ], + "num_crates": 5, + "num_downloads": 6000, + "popular_categories": [ + { + "category": "Category 1", + "crates_cnt": 2, + "created_at": "[datetime]", + "description": "Category 1 crates", + "id": "cat1", + "slug": "cat1" + } + ], + "popular_keywords": [ + { + "crates_cnt": 3, + "created_at": "[datetime]", + "id": "popular", + "keyword": "popular" + } + ] +} From 56c7988ea22ad193f148a85fd262e1a5c5d63785 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 23 Oct 2020 00:33:01 +0200 Subject: [PATCH 2/2] Use `insta` for `GET /api/v1/keywords` tests --- src/tests/keyword.rs | 33 ++++++++----------- .../snapshots/all__keyword__index-2.snap | 17 ++++++++++ src/tests/snapshots/all__keyword__index.snap | 10 ++++++ src/tests/snapshots/all__keyword__show.snap | 12 +++++++ .../snapshots/all__keyword__uppercase.snap | 12 +++++++ 5 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/tests/snapshots/all__keyword__index-2.snap create mode 100644 src/tests/snapshots/all__keyword__index.snap create mode 100644 src/tests/snapshots/all__keyword__show.snap create mode 100644 src/tests/snapshots/all__keyword__uppercase.snap diff --git a/src/tests/keyword.rs b/src/tests/keyword.rs index 8f1c9511015..fc1016b4b1e 100644 --- a/src/tests/keyword.rs +++ b/src/tests/keyword.rs @@ -1,15 +1,9 @@ +use crate::insta::rfc3339_redaction; use crate::{builders::CrateBuilder, RequestHelper, TestApp}; use cargo_registry::{models::Keyword, views::EncodableKeyword}; +use insta::assert_json_snapshot; +use serde_json::Value; -#[derive(Deserialize)] -struct KeywordList { - keywords: Vec, - meta: KeywordMeta, -} -#[derive(Deserialize)] -struct KeywordMeta { - total: i32, -} #[derive(Deserialize)] struct GoodKeyword { keyword: EncodableKeyword, @@ -19,18 +13,15 @@ struct GoodKeyword { fn index() { let url = "/api/v1/keywords"; let (app, anon) = TestApp::init().empty(); - let json: KeywordList = anon.get(url).good(); - assert_eq!(json.keywords.len(), 0); - assert_eq!(json.meta.total, 0); + let json: Value = anon.get(url).good(); + assert_json_snapshot!(json); app.db(|conn| { Keyword::find_or_create_all(conn, &["foo"]).unwrap(); }); - let json: KeywordList = anon.get(url).good(); - assert_eq!(json.keywords.len(), 1); - assert_eq!(json.meta.total, 1); - assert_eq!(json.keywords[0].keyword.as_str(), "foo"); + let json: Value = anon.get(url).good(); + assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() }); } #[test] @@ -42,8 +33,9 @@ fn show() { app.db(|conn| { Keyword::find_or_create_all(conn, &["foo"]).unwrap(); }); - let json: GoodKeyword = anon.get(url).good(); - assert_eq!(json.keyword.keyword.as_str(), "foo"); + + let json: Value = anon.get(url).good(); + assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() }); } #[test] @@ -55,8 +47,9 @@ fn uppercase() { app.db(|conn| { Keyword::find_or_create_all(conn, &["UPPER"]).unwrap(); }); - let json: GoodKeyword = anon.get(url).good(); - assert_eq!(json.keyword.keyword.as_str(), "upper"); + + let json: Value = anon.get(url).good(); + assert_json_snapshot!(json, { ".**.created_at" => rfc3339_redaction() }); } #[test] diff --git a/src/tests/snapshots/all__keyword__index-2.snap b/src/tests/snapshots/all__keyword__index-2.snap new file mode 100644 index 00000000000..b7099bdd0fb --- /dev/null +++ b/src/tests/snapshots/all__keyword__index-2.snap @@ -0,0 +1,17 @@ +--- +source: src/tests/keyword.rs +expression: json +--- +{ + "keywords": [ + { + "crates_cnt": 0, + "created_at": "[datetime]", + "id": "foo", + "keyword": "foo" + } + ], + "meta": { + "total": 1 + } +} diff --git a/src/tests/snapshots/all__keyword__index.snap b/src/tests/snapshots/all__keyword__index.snap new file mode 100644 index 00000000000..4028bbf7ff8 --- /dev/null +++ b/src/tests/snapshots/all__keyword__index.snap @@ -0,0 +1,10 @@ +--- +source: src/tests/keyword.rs +expression: json +--- +{ + "keywords": [], + "meta": { + "total": 0 + } +} diff --git a/src/tests/snapshots/all__keyword__show.snap b/src/tests/snapshots/all__keyword__show.snap new file mode 100644 index 00000000000..b5cd177a7c3 --- /dev/null +++ b/src/tests/snapshots/all__keyword__show.snap @@ -0,0 +1,12 @@ +--- +source: src/tests/keyword.rs +expression: json +--- +{ + "keyword": { + "crates_cnt": 0, + "created_at": "[datetime]", + "id": "foo", + "keyword": "foo" + } +} diff --git a/src/tests/snapshots/all__keyword__uppercase.snap b/src/tests/snapshots/all__keyword__uppercase.snap new file mode 100644 index 00000000000..0ae1757f306 --- /dev/null +++ b/src/tests/snapshots/all__keyword__uppercase.snap @@ -0,0 +1,12 @@ +--- +source: src/tests/keyword.rs +expression: json +--- +{ + "keyword": { + "crates_cnt": 0, + "created_at": "[datetime]", + "id": "upper", + "keyword": "upper" + } +}