epoch_forge/lib_efo/tests/migration_fixtures.rs

159 lines
5.4 KiB
Rust

use std::collections::{BTreeMap, BTreeSet};
use lib_efo::{
entity::Entity,
epoch::{EpochId, EpochInfo},
repository_format::{DetectedManifest, LegacyManifest, detect_manifest},
stats::{EntityType, Role, TypedLink},
};
use serde::Deserialize;
use uuid::Uuid;
#[derive(Debug, Deserialize)]
struct Fixture {
name: String,
current_epoch: u64,
epoch_ids: Vec<u64>,
tags: BTreeMap<String, u64>,
feature: String,
}
fn fixtures() -> Vec<Fixture> {
serde_json::from_str(include_str!("fixtures/v1/catalog.json")).expect("valid fixture catalog")
}
#[test]
fn version_one_migration_matrix_covers_every_legacy_shape() {
let fixtures = fixtures();
let names = fixtures
.iter()
.map(|fixture| fixture.name.as_str())
.collect::<BTreeSet<_>>();
assert_eq!(
names,
BTreeSet::from([
"empty",
"legacy-monster",
"malformed-missing-selected-epoch",
"populated",
"relationships",
"switched-back-with-future",
"tags",
])
);
for fixture in fixtures {
let legacy = LegacyManifest {
epoch_id: EpochId::new(fixture.current_epoch),
version: "fixture-v1".to_owned(),
tags: fixture
.tags
.into_iter()
.map(|(tag, id)| (tag, EpochId::new(id)))
.collect(),
};
let bytes = serde_json::to_vec(&legacy).expect("serialize fixture manifest");
assert!(matches!(
detect_manifest(&bytes).expect("detect fixture manifest"),
DetectedManifest::Legacy(_)
));
let known = fixture.epoch_ids.into_iter().collect::<BTreeSet<_>>();
if fixture.feature == "malformed" {
assert!(!known.contains(&fixture.current_epoch));
} else {
assert!(
known.contains(&fixture.current_epoch),
"{} must contain its selected epoch",
fixture.name
);
}
if fixture.feature == "future" {
assert!(known.iter().any(|id| *id > fixture.current_epoch));
}
if fixture.feature == "tags" {
assert!(!legacy.tags.is_empty());
}
for id in known {
let mut snapshot = EpochInfo {
epoch_id: EpochId::new(id),
summary: fixture.feature.clone(),
..EpochInfo::default()
};
populate_feature(&fixture.feature, &mut snapshot);
let encoded = serde_json::to_vec(&snapshot).expect("serialize fixture snapshot");
let decoded: EpochInfo =
serde_json::from_slice(&encoded).expect("load fixture snapshot");
assert_eq!(decoded.epoch_id, EpochId::new(id));
match fixture.feature.as_str() {
"populated" => assert_eq!(decoded.characters.len(), 1),
"relationships" => {
assert_eq!(decoded.organizations.len(), 1);
assert_eq!(decoded.towns.len(), 1);
let value = serde_json::to_value(&decoded.organizations[0])
.expect("serialize related organisation");
assert_eq!(
value["metadata"]["links"]
.as_array()
.expect("relationship list")
.len(),
1
);
}
"monster" => assert_eq!(decoded.calamities.len(), 1),
_ => {}
}
}
}
}
fn populate_feature(feature: &str, snapshot: &mut EpochInfo) {
match feature {
"populated" => {
let mut character = Entity::default();
character.name = "Ada".to_owned();
character.entity_type = EntityType::Character;
snapshot.characters.push(character);
}
"relationships" => {
let town_id = Uuid::from_u128(1);
let mut town = Entity::default();
town.id = town_id;
town.name = "Beacon".to_owned();
town.entity_type = EntityType::Town;
let mut organization = Entity::default();
organization.id = Uuid::from_u128(2);
organization.name = "Council".to_owned();
organization.entity_type = EntityType::Organization;
organization.add_to_links(TypedLink::new(town_id, Role::Capital, EntityType::Town));
snapshot.towns.push(town);
snapshot.organizations.push(organization);
}
"monster" => {
let mut calamity = Entity::default();
calamity.name = "Old Blight".to_owned();
calamity.entity_type = EntityType::Calamity;
snapshot.calamities.push(calamity);
}
_ => {}
}
}
#[test]
fn legacy_monster_spelling_remains_loadable() {
let mut entity = lib_efo::entity::Entity::default();
entity.name = "Old Blight".to_owned();
entity.entity_type = lib_efo::stats::EntityType::Calamity;
let mut value = serde_json::to_value(entity).expect("serialize calamity");
value["entity_type"] = serde_json::Value::String("Monster".to_owned());
let decoded: lib_efo::entity::Entity =
serde_json::from_value(value).expect("load legacy Monster spelling");
assert_eq!(decoded.entity_type, lib_efo::stats::EntityType::Calamity);
}