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 #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 #endregion

View File

@@ -1101,10 +1101,10 @@ namespace SabreTools.DatFiles
// If the parent exists and has items, we remove the items that are in the parent from the current game // If the parent exists and has items, we remove the items that are in the parent from the current game
foreach (DatItem item in parentItems) foreach (DatItem item in parentItems)
{ {
DatItem datItem = (DatItem)item.Clone(); var matchedItems = items.FindAll(i => i.Equals(item));
while (items.Contains(datItem)) foreach (var match in matchedItems)
{ {
Items.Remove(bucket, datItem); Items.Remove(bucket, match);
} }
} }
} }
@@ -1147,7 +1147,7 @@ namespace SabreTools.DatFiles
// If the parent exists and has items, we remove the items that are in the parent from the current game // If the parent exists and has items, we remove the items that are in the parent from the current game
foreach (var item in parentItems) foreach (var item in parentItems)
{ {
var matchedItems = items.Where(i => i.Value == item.Value); var matchedItems = items.Where(i => i.Value.Equals(item.Value));
foreach (var match in matchedItems) foreach (var match in matchedItems)
{ {
ItemsDB.RemoveItem(match.Key); ItemsDB.RemoveItem(match.Key);