Write RemoveItemsFromRomOfChild tests, fix issues

This commit is contained in:
Matt Nadareski
2025-01-13 15:28:30 -05:00
parent ce05765d06
commit 5e1066d3da
2 changed files with 104 additions and 5 deletions

View File

@@ -205,7 +205,106 @@ namespace SabreTools.DatFiles.Test
#region RemoveItemsFromRomOfChild
// TODO: Implement RemoveItemsFromRomOfChild tests
[Fact]
public void RemoveItemsFromRomOfChild_Items()
{
Source source = new Source(0, source: null);
Machine parentMachine = new Machine();
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "parent");
Machine childMachine = new Machine();
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "child");
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, "parent");
childMachine.SetFieldValue<bool>(Models.Metadata.Machine.IsBiosKey, true);
DatItem parentItem = new Rom();
parentItem.SetName("parent_rom");
parentItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
parentItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "deadbeef");
parentItem.SetFieldValue<Machine>(DatItem.MachineKey, parentMachine);
parentItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatItem matchChildItem = new Rom();
matchChildItem.SetName("match_child_rom");
matchChildItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
matchChildItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "deadbeef");
matchChildItem.SetFieldValue<Machine>(DatItem.MachineKey, childMachine);
matchChildItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatItem noMatchChildItem = new Rom();
noMatchChildItem.SetName("no_match_child_rom");
noMatchChildItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
noMatchChildItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "beefdead");
noMatchChildItem.SetFieldValue<Machine>(DatItem.MachineKey, childMachine);
noMatchChildItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatFile datFile = new Logiqx(datFile: null, deprecated: false);
datFile.AddItem(parentItem, statsOnly: false);
datFile.AddItem(matchChildItem, statsOnly: false);
datFile.AddItem(noMatchChildItem, statsOnly: false);
datFile.BucketBy(ItemKey.Machine, DedupeType.None);
datFile.RemoveItemsFromRomOfChild();
Assert.Single(datFile.GetItemsForBucket("parent"));
DatItem actual = Assert.Single(datFile.GetItemsForBucket("child"));
Machine? actualMachine = actual.GetFieldValue<Machine>(DatItem.MachineKey);
Assert.NotNull(actualMachine);
Assert.Equal("child", actualMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
[Fact]
public void RemoveItemsFromRomOfChild_ItemsDB()
{
Source source = new Source(0, source: null);
Machine parentMachine = new Machine();
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "parent");
Machine childMachine = new Machine();
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "child");
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, "parent");
childMachine.SetFieldValue<bool>(Models.Metadata.Machine.IsBiosKey, true);
DatItem parentItem = new Rom();
parentItem.SetName("parent_rom");
parentItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
parentItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "deadbeef");
parentItem.SetFieldValue<Machine>(DatItem.MachineKey, parentMachine);
parentItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatItem matchChildItem = new Rom();
matchChildItem.SetName("match_child_rom");
matchChildItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
matchChildItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "deadbeef");
matchChildItem.SetFieldValue<Machine>(DatItem.MachineKey, childMachine);
matchChildItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatItem noMatchChildItem = new Rom();
noMatchChildItem.SetName("no_match_child_rom");
noMatchChildItem.SetFieldValue<long>(Models.Metadata.Rom.SizeKey, 12345);
noMatchChildItem.SetFieldValue<string?>(Models.Metadata.Rom.CRCKey, "beefdead");
noMatchChildItem.SetFieldValue<Machine>(DatItem.MachineKey, childMachine);
noMatchChildItem.SetFieldValue<Source>(DatItem.SourceKey, source);
DatFile datFile = new Logiqx(datFile: null, deprecated: false);
long biosMachineIndex = datFile.AddMachineDB(parentMachine);
long deviceMachineIndex = datFile.AddMachineDB(childMachine);
long sourceIndex = datFile.AddSourceDB(source);
_ = datFile.AddItemDB(parentItem, biosMachineIndex, sourceIndex, statsOnly: false);
_ = datFile.AddItemDB(matchChildItem, deviceMachineIndex, sourceIndex, statsOnly: false);
_ = datFile.AddItemDB(noMatchChildItem, deviceMachineIndex, sourceIndex, statsOnly: false);
datFile.BucketBy(ItemKey.Machine, DedupeType.None);
datFile.RemoveItemsFromRomOfChild();
Assert.Single(datFile.GetItemsForBucketDB("parent"));
long actual = Assert.Single(datFile.GetItemsForBucketDB("child")).Key;
Machine? actualMachine = datFile.ItemsDB.GetMachineForItem(actual).Value;
Assert.NotNull(actualMachine);
Assert.Equal("child", actualMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey));
}
#endregion