From 66b22c1c3021303d009d9017c49f217e10221a29 Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Tue, 4 Feb 2025 23:45:53 +0000 Subject: [PATCH] Added a very poorly written test thingy Started fixing a couple of mistakes --- scryfall_deser/.gitignore | 1 + scryfall_deser/src/lib.rs | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/scryfall_deser/.gitignore b/scryfall_deser/.gitignore index 6985cf1..77524ba 100644 --- a/scryfall_deser/.gitignore +++ b/scryfall_deser/.gitignore @@ -2,6 +2,7 @@ # will have compiled files and executables debug/ target/ +test_files/all-cards.json # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/scryfall_deser/src/lib.rs b/scryfall_deser/src/lib.rs index 8de8193..35e961f 100644 --- a/scryfall_deser/src/lib.rs +++ b/scryfall_deser/src/lib.rs @@ -35,7 +35,7 @@ struct ScryfallCard { // NOTE: Probably a bad idea to rename color -> colour just for the sake pub cmc: Option, // TODO: Make this a proper Decimal - see "Little Girl" card for example of cmc of 0.5 #[serde(rename = "color_identity")] - pub colour_identity: Vec, + pub colour_identity: Option>, #[serde(rename = "color_indicator")] pub colour_indicator: Option>, #[serde(rename = "colors")] @@ -81,7 +81,7 @@ struct ScryfallCard { pub highres_image: bool, pub illustration_id: Option, pub image_status: ImageStatus, - pub image_uris: ImageURIs, + pub image_uris: Option, pub oversized: bool, pub prices: Prices, pub printed_name: Option, @@ -124,7 +124,7 @@ struct ScryfallCardFaceObject { pub artist_id: Option, // UUID pub cmc: Option, // TODO: Make this a proper Decimal - see "Little Girl" card for example of cmc of 0.5 #[serde(rename = "color_identity")] - pub colour_identity: Vec, + pub colour_identity: Option>, #[serde(rename = "color_indicator")] pub colour_indicator: Option>, #[serde(rename = "colors")] @@ -132,7 +132,7 @@ struct ScryfallCardFaceObject { pub defence: Option, pub flavour_text: Option, pub illustration_id: Option, - pub image_uris: ImageURIs, + pub image_uris: Option, pub layout: Option, pub loyalty: Option, pub mana_cost: Option, @@ -419,6 +419,7 @@ mod tests { use super::*; use std::path::PathBuf; use std::fs; + use std::io::{BufRead, BufReader}; #[test] fn deserialise_nissa() { @@ -426,8 +427,7 @@ mod tests { f.push("test_files/nissa.json"); assert!(f.exists()); let fc = fs::read_to_string(f).unwrap(); - let nissa: ScryfallCard = serde_json::from_str(&fc).unwrap(); - println!("{:#?}", nissa); + let _nissa: ScryfallCard = serde_json::from_str(&fc).unwrap(); } #[test] @@ -436,8 +436,7 @@ mod tests { f.push("test_files/black_lotus.json"); assert!(f.exists()); let fc = fs::read_to_string(f).unwrap(); - let bl: ScryfallCard = serde_json::from_str(&fc).unwrap(); - println!("{:#?}", bl); + let _bl: ScryfallCard = serde_json::from_str(&fc).unwrap(); } #[test] @@ -446,12 +445,10 @@ mod tests { f.push("test_files/little_girl.json"); assert!(f.exists()); let fc = fs::read_to_string(f).unwrap(); - let lg: ScryfallCard = serde_json::from_str(&fc).unwrap(); - println!("{:#?}", lg); + let _lg: ScryfallCard = serde_json::from_str(&fc).unwrap(); } #[test] - #[ignore] fn try_deserialise_all_cards() { // TODO Actually write this funtion. // The idea will be to run over one of the Scryfall dumps of all the cards so I can be @@ -463,5 +460,25 @@ mod tests { // I think that's what it should do at least. I bet it'll still take a while tho. // If that doesn't work - I should try somehow use the serde from_reader function // I don't 100% know how I'll use that though. + let mut f = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + f.push("test_files/all-cards.json"); + assert!(f.exists(), "You need to download the all-cards-... file from Scryfall bulk data. Can be found here: https://scryfall.com/docs/api/bulk-data and rename to all-cards.json"); + let ac = fs::File::open(f).unwrap(); + let reader = BufReader::new(ac); + for line in reader.lines().skip(1) { + let mut line = line.unwrap(); + line.pop(); + let a_card: Result = serde_json::from_str(&line.as_ref()); + match a_card { + Err(error) => { + println!("{:#?}", line); + println!("{:#?}", error); + panic!(); + }, + Ok(_) => (), + } + + } + // FIXME: Skip the last line } }