Write RemoveItemsFromCloneOfChild tests, fix issues

This commit is contained in:
Matt Nadareski
2025-01-13 15:21:24 -05:00
parent d7c962063b
commit ce05765d06
2 changed files with 109 additions and 8 deletions

View File

@@ -85,8 +85,8 @@ namespace SabreTools.DatFiles.Test
long biosMachineIndex = datFile.AddMachineDB(biosMachine); long biosMachineIndex = datFile.AddMachineDB(biosMachine);
long deviceMachineIndex = datFile.AddMachineDB(deviceMachine); long deviceMachineIndex = datFile.AddMachineDB(deviceMachine);
long sourceIndex = datFile.AddSourceDB(source); long sourceIndex = datFile.AddSourceDB(source);
long biosItemId = datFile.AddItemDB(biosItem, biosMachineIndex, sourceIndex, statsOnly: false); _ = datFile.AddItemDB(biosItem, biosMachineIndex, sourceIndex, statsOnly: false);
long deviceItemId = datFile.AddItemDB(deviceItem, deviceMachineIndex, sourceIndex, statsOnly: false); _ = datFile.AddItemDB(deviceItem, deviceMachineIndex, sourceIndex, statsOnly: false);
datFile.BucketBy(ItemKey.Machine, DedupeType.None); datFile.BucketBy(ItemKey.Machine, DedupeType.None);
datFile.RemoveBiosAndDeviceSets(); datFile.RemoveBiosAndDeviceSets();
@@ -98,7 +98,108 @@ namespace SabreTools.DatFiles.Test
#region RemoveItemsFromCloneOfChild #region RemoveItemsFromCloneOfChild
// TODO: Implement RemoveItemsFromCloneOfChild tests [Fact]
public void RemoveItemsFromCloneOfChild_Items()
{
Source source = new Source(0, source: null);
Machine parentMachine = new Machine();
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "parent");
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, "romof");
Machine childMachine = new Machine();
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "child");
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, "parent");
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.RemoveItemsFromCloneOfChild();
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));
Assert.Equal("romof", actualMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey));
}
[Fact]
public void RemoveItemsFromCloneOfChild_ItemsDB()
{
Source source = new Source(0, source: null);
Machine parentMachine = new Machine();
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "parent");
parentMachine.SetFieldValue<string?>(Models.Metadata.Machine.RomOfKey, "romof");
Machine childMachine = new Machine();
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, "child");
childMachine.SetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey, "parent");
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.RemoveItemsFromCloneOfChild();
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));
Assert.Equal("romof", actualMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey));
}
#endregion #endregion
@@ -155,7 +256,7 @@ namespace SabreTools.DatFiles.Test
DatFile datFile = new Logiqx(datFile: null, deprecated: false); DatFile datFile = new Logiqx(datFile: null, deprecated: false);
long machineIndex = datFile.AddMachineDB(machine); long machineIndex = datFile.AddMachineDB(machine);
long sourceIndex = datFile.AddSourceDB(source); long sourceIndex = datFile.AddSourceDB(source);
long itemId = datFile.AddItemDB(datItem, machineIndex, sourceIndex, statsOnly: false); _ = datFile.AddItemDB(datItem, machineIndex, sourceIndex, statsOnly: false);
datFile.BucketBy(ItemKey.Machine, DedupeType.None); datFile.BucketBy(ItemKey.Machine, DedupeType.None);
datFile.RemoveMachineRelationshipTags(); datFile.RemoveMachineRelationshipTags();

View File

@@ -989,10 +989,10 @@ namespace SabreTools.DatFiles
// If the parent exists and has items, we remove the parent items from the current game // If the parent exists and has items, we remove the parent items 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);
} }
} }
@@ -1037,7 +1037,7 @@ namespace SabreTools.DatFiles
// If the parent exists and has items, we remove the parent items from the current game // If the parent exists and has items, we remove the parent items 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);