public void TestCreateOnly() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); initDL(); assertDL(); //no transaction is used to have also testing if everything works without transactions. addParent("1"); addChild(0, "11"); addParent("2"); addChild(1, "21"); addChild(1, "22"); } finally { DC.DisposeData(); } }
public void TestSample() { var directoryInfo = new DirectoryInfo("TestCsv"); for (int configurationIndex = 0; configurationIndex < 2; configurationIndex++) { switch (configurationIndex) { case 0: csvConfig = null; bakCsvFileSwapper = null; break; case 1: csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); bakCsvFileSwapper = new BakCsvFileSwapper(csvConfig); break; } try { directoryInfo.Refresh(); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); expectedData = new expectedDataClass(); initDL(); assertDL(); addMaster(0); addMaster(1); addMaster(2); addMaster(3); addSample(0, null); addSample(1, 1); addSample(2, 2); addDetail(0, 1); addDetail(1, 1); addDetail(2, 2); updateMaster(1, "a"); //todo: What should happen if the child has two parent property for the same parent. Presently, updateSample() doesn't work //updateSample(1, "b", DC.Data.SampleMasters[0]); //updateSample(1, "c", null); //updateSample(1, "d", DC.Data.SampleMasters[1]); //updateDetail(1, "a"); //removeDetail(2); need to find out why this is not working. Maybe because some code above got commented out ? //removeSample(2); //removeMaster(2); //removeMaster(1); //showStructure() } finally { DC.DisposeData(); } } }
public void TestDataStoreReadonlyCSV() { var directoryInfo = new DirectoryInfo("TestCsv"); try { dataStore?.Dispose(); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); directoryInfo.Refresh(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); dataStore = createDataStoreReadonly(); var expectedList = new List <string>(); assertRewriteReadonly(expectedList, cont, ref dataStore); addReadonly(dataStore, expectedList, 0, "0"); addReadonly(dataStore, expectedList, 1, "1"); addReadonly(dataStore, expectedList, 2, "2"); addReadonly(dataStore, expectedList, 3, "3"); } finally { dataStore?.Dispose(); } }
public void TestNotMatchingChildrenListName() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); _ = new DC(csvConfig); var p0 = new NotMatchingChildrenListName_Parent("P0", isStoring: false); Assert.AreEqual(0, p0.Children.Count); Assert.AreEqual(0, p0.Children.CountStoredItems); var c0 = new NotMatchingChildrenListName_Child("c0.0", p0, isStoring: false); Assert.AreEqual(0, p0.Children.CountStoredItems); Assert.AreEqual(1, p0.Children.Count); Assert.AreEqual(c0, p0.Children[0]); var c1 = new NotMatchingChildrenListName_Child("c1.0", p0, isStoring: false); Assert.AreEqual(0, p0.Children.CountStoredItems); Assert.AreEqual(2, p0.Children.Count); Assert.AreEqual(c0, p0.Children[0]); Assert.AreEqual(c1, p0.Children[1]); p0.Store(); Assert.AreEqual(0, p0.Children.CountStoredItems); Assert.AreEqual(2, p0.Children.Count); Assert.AreEqual(c0, p0.Children[0]); Assert.AreEqual(c1, p0.Children[1]); c0.Store(); Assert.AreEqual(1, p0.Children.CountStoredItems); Assert.AreEqual(2, p0.Children.Count); Assert.AreEqual(c0, p0.Children.GetStoredItems().First()); Assert.AreEqual(c0, p0.Children[0]); Assert.AreEqual(c1, p0.Children[1]); var p1 = new NotMatchingChildrenListName_Parent("P1", isStoring: true); c0.Update("c0.1", p1); Assert.AreEqual(0, p0.Children.CountStoredItems); Assert.AreEqual(1, p0.Children.Count); Assert.AreEqual(1, p1.Children.CountStoredItems); Assert.AreEqual(1, p1.Children.Count); Assert.AreEqual(c0, p1.Children.GetStoredItems().First()); Assert.AreEqual(c0, p1.Children[0]); Assert.AreEqual(c1, p0.Children[0]); c0.Release(); } finally { DC.DisposeData(); } }
public void TestStorageDataStoreCSV() { var directoryInfo = new DirectoryInfo("TestCsv"); for (int configurationIndex = 0; configurationIndex < 2; configurationIndex++) { try { directoryInfo.Refresh(); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); directoryInfo.Refresh(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); dataStore = createDataStore(); var expectedList = new List <string>(); assertRewrite(expectedList, cont, ref dataStore); add(dataStore, expectedList, 0, "0", cont); add(dataStore, expectedList, 1, "1", cont); add(dataStore, expectedList, 2, "2", cont); add(dataStore, expectedList, 3, "3", cont); add(dataStore, expectedList, 4, "4", cont); remove(dataStore, expectedList, 3, notC); add(dataStore, expectedList, 5, "5", notC); remove(dataStore, expectedList, 4, notC); remove(dataStore, expectedList, 5, cont); add(dataStore, expectedList, 3, "3a", cont); update(dataStore, expectedList, 2, "2a", cont); remove(dataStore, expectedList, 2, notC); remove(dataStore, expectedList, 3, cont); remove(dataStore, expectedList, 0, cont); remove(dataStore, expectedList, 1, cont); add(dataStore, expectedList, 0, "0a", cont); add(dataStore, expectedList, 1, "1a", cont); } finally { dataStore?.Dispose(); directoryInfo.Delete(recursive: true); } } }
public void TestChildrenListPerformance() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); GC.Collect(); //in case garbage collector would run soon because of previous tests GC.WaitForPendingFinalizers(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); var activitiesCount = 1_000; var childrenPerParent = 10; var sw = new Stopwatch(); Debug.WriteLine($"{activitiesCount} activitiesCount"); Debug.WriteLine($"{childrenPerParent} childrenPerParent"); //testNormal(activitiesCount, childrenPerParent, csvConfig, sw); testCommit(activitiesCount, childrenPerParent, csvConfig, sw); directoryInfo.Delete(recursive: true); directoryInfo.Create(); GC.Collect(); GC.WaitForPendingFinalizers(); testRollback(activitiesCount, childrenPerParent, csvConfig, sw); directoryInfo.Delete(recursive: true); directoryInfo.Create(); GC.Collect(); GC.WaitForPendingFinalizers(); //testCommit(activitiesCount, childrenPerParent, csvConfig, sw); testNormal(activitiesCount, childrenPerParent, csvConfig, sw); } finally { DC.DisposeData(); } }
public void TestChildrenList() { try { parents = new DataStoreStats(); parentReadonlies = new DataStoreStats(); parentNullables = new DataStoreStats(); parentNullableReadonlies = new DataStoreStats(); coParents = new DataStoreStats(); coParentReadonlies = new DataStoreStats(); coParentNullables = new DataStoreStats(); coParentNullableReadonlies = new DataStoreStats(); children = new DataStoreStats(); coChildren = new DataStoreStats(); childrenTestString = new Dictionary <int, string>(); var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); DC.Trace = dcTrace; initDC(); assertData(); traceHeader("--- construct and store parent P0, PN0, CP0, CPN0 ---"); DC.Data.StartTransaction(); var p0 = new ChildrenList_Parent("P0"); var p0r = new ChildrenList_ParentReadonly("P0r"); var pn0 = new ChildrenList_ParentNullable("PN0", isStoring: false); var pn0r = new ChildrenList_ParentNullableReadonly("PN0r", isStoring: false); var cp0 = new ChildrenList_CreateOnlyParent("CP0"); var cp0r = new ChildrenList_CreateOnlyParentReadonly("CP0r"); var cpn0 = new ChildrenList_CreateOnlyParentNullable("CPN0", isStoring: false); var cpn0r = new ChildrenList_CreateOnlyParentNullableReadonly("CPN0r", isStoring: false); DC.Data.RollbackTransaction(); assertData(); DC.Data.StartTransaction(); p0.Store(); var p0Key = p0.Key; p0r.Store(); var p0rKey = p0r.Key; pn0.Store(); var pn0Key = pn0.Key; pn0r.Store(); var pn0rKey = pn0r.Key; cp0.Store(); var cp0Key = cp0.Key; cp0r.Store(); var cp0rKey = cp0r.Key; cpn0.Store(); var cpn0Key = cpn0.Key; cpn0r.Store(); var cpn0rKey = cpn0r.Key; DC.Data.CommitTransaction(); parents.Items[p0Key] = p0.ToString(); parents.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); parentReadonlies.Items[p0rKey] = p0r.ToString(); parentReadonlies.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); coParents.Items[cp0Key] = cp0.ToString(); coParents.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); coParentReadonlies.Items[cp0rKey] = cp0r.ToString(); coParentReadonlies.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); parentNullables.Items[pn0Key] = pn0.ToString(); parentNullables.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); parentNullableReadonlies.Items[pn0rKey] = pn0r.ToString(); parentNullableReadonlies.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); coParentNullables.Items[cpn0Key] = cpn0.ToString(); coParentNullables.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); coParentNullableReadonlies.Items[cpn0rKey] = cpn0r.ToString(); coParentNullableReadonlies.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); assertData(); traceHeader("--- construct and store children C0 and CC0 ---"); p0 = DC.Data.ChildrenList_Parents[p0Key]; p0r = DC.Data.ChildrenList_ParentReadonlys[p0rKey]; pn0 = DC.Data.ChildrenList_ParentNullables[pn0Key]; pn0r = DC.Data.ChildrenList_ParentNullableReadonlys[pn0rKey]; cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; cp0r = DC.Data.ChildrenList_CreateOnlyParentReadonlys[cp0rKey]; cpn0 = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key]; cpn0r = DC.Data.ChildrenList_CreateOnlyParentNullableReadonlys[cpn0rKey]; DC.Data.StartTransaction(); var c0 = new ChildrenList_Child("C0", p0, p0r, pn0, pn0r, cp0, cp0r, cpn0, cpn0r); var cc0 = new ChildrenList_CreateOnlyChild("CC0", cp0, cp0r, cpn0, cpn0r); DC.Data.RollbackTransaction(); assertData(); p0 = DC.Data.ChildrenList_Parents[p0Key]; p0r = DC.Data.ChildrenList_ParentReadonlys[p0rKey]; pn0 = DC.Data.ChildrenList_ParentNullables[pn0Key]; pn0r = DC.Data.ChildrenList_ParentNullableReadonlys[pn0rKey]; cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; cp0r = DC.Data.ChildrenList_CreateOnlyParentReadonlys[cp0rKey]; cpn0 = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key]; cpn0r = DC.Data.ChildrenList_CreateOnlyParentNullableReadonlys[cpn0rKey]; DC.Data.StartTransaction(); c0 = new ChildrenList_Child("C0", p0, p0r, pn0, pn0r, cp0, cp0r, cpn0, cpn0r); var c0Key = c0.Key; cc0 = new ChildrenList_CreateOnlyChild("CC0", cp0, cp0r, cpn0, cpn0r); var cc0Key = cc0.Key; DC.Data.CommitTransaction(); parents.Items[p0Key] = p0.ToString(); parentReadonlies.Items[p0rKey] = p0r.ToString(); coParents.Items[cp0Key] = cp0.ToString(); coParentReadonlies.Items[cp0rKey] = cp0r.ToString(); parentNullables.Items[pn0Key] = pn0.ToString(); parentNullableReadonlies.Items[pn0rKey] = pn0r.ToString(); coParentNullables.Items[cpn0Key] = cpn0.ToString(); coParentNullableReadonlies.Items[cpn0rKey] = cpn0r.ToString(); children.Items[c0Key] = c0.ToString(); childrenTestString[c0Key] = c0.ToTestString(); coChildren.Items[cc0Key] = cc0.ToString(); children.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); coChildren.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); assertData(); traceHeader("--- update child C0 with null parents ---"); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); c0.Update("C0", c0.Parent, null, c0.CreateOnlyParent, null); DC.Data.RollbackTransaction(); assertData(); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); c0.Update("C0", c0.Parent, null, c0.CreateOnlyParent, null); DC.Data.CommitTransaction(); pn0 = DC.Data.ChildrenList_ParentNullables[pn0Key]; parentNullables.Items[pn0Key] = pn0.ToString(); cpn0 = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key]; coParentNullables.Items[cpn0Key] = cpn0.ToString(); children.Items[c0Key] = c0.ToString(); childrenTestString[c0Key] = c0.ToTestString(); children.Set(isContinuous: true, isUpdated: true, isDeleted: false, firstIndex: 0, lastIndex: 0); assertData(); children.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); traceHeader("--- update child C0 with parents 1 ---"); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); var p1 = new ChildrenList_Parent("P1"); var pn1 = new ChildrenList_ParentNullable("PN1"); var cp1 = new ChildrenList_CreateOnlyParent("CP1"); var cpn1 = new ChildrenList_CreateOnlyParentNullable("CPN1"); c0.Update("C0U", p1, pn1, cp1, cpn1); DC.Data.RollbackTransaction(); assertData(); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); p1 = new ChildrenList_Parent("P1"); var p1Key = p1.Key; pn1 = new ChildrenList_ParentNullable("PN1"); var pn1Key = pn1.Key; cp1 = new ChildrenList_CreateOnlyParent("CP1"); var cp1Key = cp1.Key; cpn1 = new ChildrenList_CreateOnlyParentNullable("CPN1"); var cpn1Key = cpn1.Key; c0.Update("C0U", p1, pn1, cp1, cpn1); DC.Data.CommitTransaction(); p0 = DC.Data.ChildrenList_Parents[p0Key]; parents.Items[p0Key] = p0.ToString(); parents.Items[p1Key] = p1.ToString(); parents.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); pn0 = DC.Data.ChildrenList_ParentNullables[pn0Key]; parentNullables.Items[pn0Key] = pn0.ToString(); parentNullables.Items[pn1Key] = pn1.ToString(); parentNullables.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; coParents.Items[cp0Key] = cp0.ToString(); coParents.Items[cp1Key] = cp1.ToString(); coParents.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); cpn0 = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key]; coParentNullables.Items[cpn0Key] = cpn0.ToString(); coParentNullables.Items[cpn1Key] = cpn1.ToString(); coParentNullables.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); children.Items[c0Key] = c0.ToString(); childrenTestString[c0Key] = c0.ToTestString(); children.Set(isContinuous: true, isUpdated: true, isDeleted: false, firstIndex: 0, lastIndex: 0); assertData(); children.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 0); traceHeader("--- add children C1, CC1 with parents 0 ---"); p0 = DC.Data.ChildrenList_Parents[p0Key]; p0r = DC.Data.ChildrenList_ParentReadonlys[p0rKey]; cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; cp0r = DC.Data.ChildrenList_CreateOnlyParentReadonlys[cp0rKey]; DC.Data.StartTransaction(); var c1 = new ChildrenList_Child("C1", p0, p0r, null, null, cp0, cp0r, null, null); var c1Key = c1.Key; var cc1 = new ChildrenList_CreateOnlyChild("CC1", cp0, cp0r, null, null); var cc1Key = cc1.Key; DC.Data.RollbackTransaction(); assertData(); p0 = DC.Data.ChildrenList_Parents[p0Key]; p0r = DC.Data.ChildrenList_ParentReadonlys[p0rKey]; cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; cp0r = DC.Data.ChildrenList_CreateOnlyParentReadonlys[cp0rKey]; DC.Data.StartTransaction(); c1 = new ChildrenList_Child("C1", p0, p0r, null, null, cp0, cp0r, null, null); cc1 = new ChildrenList_CreateOnlyChild("CC1", cp0, cp0r, null, null); DC.Data.CommitTransaction(); children.Items[c1Key] = c1.ToString(); childrenTestString[c1Key] = c1.ToTestString(); coChildren.Items[cc1Key] = cc1.ToString(); parents.Items[p0Key] = p0.ToString(); coParents.Items[cp0Key] = cp0.ToString(); children.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); coChildren.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 1); assertData(); traceHeader("--- test reader and writer ---"); DC.DisposeData(); var fromFilePath = csvConfig.DirectoryPath + "\\ChildrenList_Child.csv"; var toFilePath = csvConfig.DirectoryPath + "\\ChildrenList_Child.new"; var c2Key = 2; using (var reader = new ChildrenList_ChildReader(fromFilePath, csvConfig)) { using var writer = new ChildrenList_ChildWriter(toFilePath, csvConfig); var childIndex = 0; while (reader.ReadLine(out var childRaw)) { Assert.AreEqual(childrenTestString[childIndex++], childRaw.ToTestString()); writer.Write(childRaw); } var newChildRaw = new ChildrenList_ChildRaw { Key = c2Key, Text = "C2", ParentKey = 0, ParentNullableKey = 0, CreateOnlyParentKey = 0, CreateOnlyParentNullableKey = 0 }; writer.Write(newChildRaw); childrenTestString[newChildRaw.Key] = newChildRaw.ToTestString(); } File.Delete(fromFilePath); File.Move(toFilePath, fromFilePath); initDC(); children.Items[2] = DC.Data.ChildrenList_Children[2].ToString(); p0 = DC.Data.ChildrenList_Parents[p0Key]; pn0 = DC.Data.ChildrenList_ParentNullables[pn0Key]; cp0 = DC.Data.ChildrenList_CreateOnlyParents[cp0Key]; cpn0 = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key]; parents.Items[p0Key] = p0.ToString(); parentNullables.Items[pn0Key] = pn0.ToString(); coParents.Items[cp0Key] = cp0.ToString(); coParentNullables.Items[cpn0Key] = cpn0.ToString(); children.Set(isContinuous: true, isUpdated: false, isDeleted: false, firstIndex: 0, lastIndex: 2); assertData(); //some tests without disposing DC between tests //--------------------------------------------- traceHeader("--- release child C1 ---"); c1 = DC.Data.ChildrenList_Children[c1Key]; DC.Data.StartTransaction(); c1.Release(); DC.Data.RollbackTransaction(); assertData(); c1 = DC.Data.ChildrenList_Children[c1Key]; DC.Data.StartTransaction(); c1.Release(); DC.Data.CommitTransaction(); children.Items.Remove(c1Key); childrenTestString.Remove(c1Key); children.Set(isContinuous: false, isUpdated: false, isDeleted: true, firstIndex: 0, lastIndex: 2); var v = parents.Items[p0Key] == DC.Data.ChildrenList_Parents[p0Key].ToString(); parents.Items[p0Key] = DC.Data.ChildrenList_Parents[p0Key].ToString(); coParents.Items[cp0Key] = DC.Data.ChildrenList_CreateOnlyParents[cp0Key].ToString(); //assertData(); doesn't work, because before closing the DC, the parent has still the released child in Children, but nor in new DC assertDC(isAfterDispose: false); traceHeader("--- release child C0 ---"); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); c0.Release(); DC.Data.RollbackTransaction(); assertDC(isAfterDispose: false); c0 = DC.Data.ChildrenList_Children[c0Key]; DC.Data.StartTransaction(); c0.Release(); DC.Data.CommitTransaction(); children.Items.Remove(c0Key); childrenTestString.Remove(c0Key); children.Set(isContinuous: true, isUpdated: false, isDeleted: true, firstIndex: 2, lastIndex: 2); parents.Items[p1Key] = DC.Data.ChildrenList_Parents[p1Key].ToString(); parentNullables.Items[pn1Key] = DC.Data.ChildrenList_ParentNullables[pn1Key].ToString(); coParents.Items[cp1Key] = DC.Data.ChildrenList_CreateOnlyParents[cp1Key].ToString(); coParentNullables.Items[cpn1Key] = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn1Key].ToString(); assertDC(isAfterDispose: false); traceHeader("--- release child C2 ---"); var c2 = DC.Data.ChildrenList_Children[2]; DC.Data.StartTransaction(); c2.Release(); DC.Data.RollbackTransaction(); assertDC(isAfterDispose: false); c2 = DC.Data.ChildrenList_Children[c2.Key]; DC.Data.StartTransaction(); c2.Release(); DC.Data.CommitTransaction(); children.Items.Remove(c2Key); childrenTestString.Remove(c2Key); children.Set(isContinuous: true, isUpdated: false, isDeleted: true, firstIndex: -1, lastIndex: -1); parents.Items[p0Key] = DC.Data.ChildrenList_Parents[p0Key].ToString(); parentNullables.Items[pn0Key] = DC.Data.ChildrenList_ParentNullables[pn0Key].ToString(); coParents.Items[cp0Key] = DC.Data.ChildrenList_CreateOnlyParents[cp0Key].ToString(); coParentNullables.Items[cpn0Key] = DC.Data.ChildrenList_CreateOnlyParentNullables[cpn0Key].ToString(); assertDC(isAfterDispose: false); } finally { DC.DisposeData(); } }
public void TestLookup() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); initDL(); assertDL(); //stored immediately var now = DateTime.Now.Date; var dayIndex = 1; var parent1Key = addParent(now, 1, isStoring: true).Key; var parent1NullableKey = addParentNullable(now, 1, isStoring: true).Key; var child1Key = addChild("1", parent1Key, parent1NullableKey, isStoring: true).Key; var parent2Key = addParent(now.AddDays(dayIndex), 2, isStoring: true).Key; var parent2NullableKey = addParentNullable(now.AddDays(dayIndex++), 2, isStoring: true).Key; addChild("2", parent2Key, parent2NullableKey, isStoring: true); addChild("3", parent2Key, parent2NullableKey, isStoring: true); //not stored var parent3 = addParent(now.AddDays(dayIndex++), 3, isStoring: false); var child4 = addChild("4", parent3, null, isStoring: false); DC.Data.StartTransaction(); parent3.Store(); child4.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent3); store(child4); DC.Data.CommitTransaction(); assertData(); var parent4 = addParent(now.AddDays(dayIndex), 4, isStoring: false); var parent4Nullable = addParentNullable(now.AddDays(dayIndex++), 4, isStoring: false); var child5 = addChild("5", parent4, parent4Nullable, isStoring: false); var child6 = addChild("6", parent4, parent4Nullable, isStoring: false); DC.Data.StartTransaction(); parent4.Store(); parent4Nullable.Store(); child5.Store(); child6.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent4); store(parent4Nullable); store(child5); store(child6); DC.Data.CommitTransaction(); assertData(); //test update() updateParent(parent2Key, 2.1m); updateParentNullable(parent2NullableKey, 2.1m); updateChild(child1Key, parent2Key, parent2NullableKey, "11.U1"); updateChild(child1Key, parent2Key, parent2NullableKey, "11.U2"); updateChild(child1Key, parent1Key, parent1NullableKey, "11.U3"); updateChild(child1Key, parent1Key, null, "11.U4"); updateChild(child1Key, parent1Key, parent1NullableKey, "11.U5"); removeChild(child1Key); } finally { DC.DisposeData(); } }
public void TestChildrenDictionary() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); initDL(); assertDL(); //stored immediately var now = DateTime.Now.Date; var dayIndex = 1; var parent1Key = addParent("1", isStoring: true).Key; var parent1NullableKey = addParentNullable("1N", isStoring: true).Key; var child1Key = addChild(parent1Key, null, now, "11", isStoring: true).Key; var parent2Key = addParent("2", isStoring: true).Key; var parent2NullableKey = addParentNullable("2N", isStoring: true).Key; var child2Key = addChild(parent2Key, parent2NullableKey, now.AddDays(dayIndex++), "21", isStoring: true).Key; var child3Key = addChild(parent2Key, parent2NullableKey, now.AddDays(dayIndex++), "22", isStoring: true).Key; //constructed and stored later var parent3 = addParent("3", isStoring: false); var parent3Nullable = addParentNullable("3N", isStoring: false); var child4 = addChild(parent3, null, now.AddDays(dayIndex++), "31", isStoring: false); DC.Data.StartTransaction(); parent3.Store(); parent3Nullable.Store(); child4.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent3); store(parent3Nullable); store(child4); DC.Data.CommitTransaction(); assertData(); var parent4 = addParent("4", isStoring: false); var parent4Nullable = addParentNullable("4N", isStoring: false); var child5 = addChild(parent4, parent4Nullable, now.AddDays(dayIndex++), "41", isStoring: false); var child6 = addChild(parent4, parent4Nullable, now.AddDays(dayIndex++), "42", isStoring: false); DC.Data.StartTransaction(); parent4.Store(); parent4Nullable.Store(); child5.Store(); child6.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent4); store(parent4Nullable); store(child5); store(child6); DC.Data.CommitTransaction(); assertData(); updateParent(parent2Key, "2.1"); updateParentNullable(parent2NullableKey, "2N.1"); updateChild(child1Key, parent2Key, parent2NullableKey, now, "11.U1"); updateChild(child1Key, parent2Key, parent2NullableKey, now.AddDays(-1), "11.U2"); updateChild(child1Key, parent1Key, parent1NullableKey, now.AddDays(-1), "11.U3"); updateChild(child1Key, parent1Key, null, now.AddDays(-1), "11.U4"); updateChild(child1Key, parent1Key, parent1NullableKey, now.AddDays(-1), "11.U5"); removeChild(child1Key); //updating a not stored child parent3 = DC.Data.ChildrenDictionary_Parents[parent3.Key]; parent4 = DC.Data.ChildrenDictionary_Parents[parent4.Key]; parent4Nullable = DC.Data.ChildrenDictionary_ParentNullables[parent4Nullable.Key]; var child7 = addChild(parent4, parent4Nullable, now.AddDays(dayIndex++), "43", isStoring: false); Assert.IsTrue(parent4.ChildrenDictionary_Children.ContainsKey(child7.DateKey)); Assert.IsTrue(parent4Nullable.ChildrenDictionary_Children.ContainsKey(child7.DateKey)); var oldDate = child7.DateKey; child7.Update(now.AddDays(dayIndex++), "33U", parent3, null); Assert.IsFalse(parent4.ChildrenDictionary_Children.ContainsKey(oldDate)); Assert.IsFalse(parent4Nullable.ChildrenDictionary_Children.ContainsKey(oldDate)); Assert.IsTrue(parent3.ChildrenDictionary_Children.ContainsKey(child7.DateKey)); oldDate = child7.DateKey; child7.Update(now.AddDays(dayIndex++), "33U", parent3, null); Assert.IsFalse(parent3.ChildrenDictionary_Children.ContainsKey(oldDate)); Assert.IsTrue(parent3.ChildrenDictionary_Children.ContainsKey(child7.DateKey)); } finally { DC.DisposeData(); } }
public DC(CsvConfig?csvConfig) : base(DataStoresCount: 5) { data = this; IsInitialised = false; string?backupResult = null; if (csvConfig != null) { backupResult = Csv.Backup(csvConfig, DateTime.Now); } CsvConfig = csvConfig; onConstructing(backupResult); _LocationsByPathLower = new Dictionary <string, Location>(); _PlayinglistTracksByPlaylistTrackKey = new Dictionary <int, PlayinglistTrack>(); _PlaylistsByNameLower = new Dictionary <string, Playlist>(); _TracksByTitleArtists = new Dictionary <string, Track>(); if (csvConfig == null) { _Locations = new DataStore <Location>( this, 0, Location.SetKey, Location.RollbackItemNew, Location.RollbackItemStore, Location.RollbackItemUpdate, Location.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); DataStores[0] = _Locations; onLocationsFilled(); _Tracks = new DataStore <Track>( this, 1, Track.SetKey, Track.RollbackItemNew, Track.RollbackItemStore, Track.RollbackItemUpdate, Track.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); DataStores[1] = _Tracks; onTracksFilled(); _Playlists = new DataStore <Playlist>( this, 2, Playlist.SetKey, Playlist.RollbackItemNew, Playlist.RollbackItemStore, Playlist.RollbackItemUpdate, Playlist.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); DataStores[2] = _Playlists; onPlaylistsFilled(); _PlaylistTracks = new DataStore <PlaylistTrack>( this, 3, PlaylistTrack.SetKey, PlaylistTrack.RollbackItemNew, PlaylistTrack.RollbackItemStore, PlaylistTrack.RollbackItemUpdate, PlaylistTrack.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); DataStores[3] = _PlaylistTracks; onPlaylistTracksFilled(); _PlayinglistTracks = new DataStore <PlayinglistTrack>( this, 4, PlayinglistTrack.SetKey, PlayinglistTrack.RollbackItemNew, PlayinglistTrack.RollbackItemStore, PlayinglistTrack.RollbackItemUpdate, PlayinglistTrack.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); DataStores[4] = _PlayinglistTracks; onPlayinglistTracksFilled(); } else { IsPartiallyNew = false; _Locations = new DataStoreCSV <Location>( this, 0, csvConfig !, Location.EstimatedLineLength, Location.Headers, Location.SetKey, Location.Create, null, Location.Update, Location.Write, Location.RollbackItemNew, Location.RollbackItemStore, Location.RollbackItemUpdate, Location.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); IsPartiallyNew |= _Locations.IsNew; IsNew &= _Locations.IsNew; DataStores[0] = _Locations; onLocationsFilled(); _Tracks = new DataStoreCSV <Track>( this, 1, csvConfig !, Track.EstimatedLineLength, Track.Headers, Track.SetKey, Track.Create, Track.Verify, Track.Update, Track.Write, Track.RollbackItemNew, Track.RollbackItemStore, Track.RollbackItemUpdate, Track.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); IsPartiallyNew |= _Tracks.IsNew; IsNew &= _Tracks.IsNew; DataStores[1] = _Tracks; onTracksFilled(); _Playlists = new DataStoreCSV <Playlist>( this, 2, csvConfig !, Playlist.EstimatedLineLength, Playlist.Headers, Playlist.SetKey, Playlist.Create, null, Playlist.Update, Playlist.Write, Playlist.RollbackItemNew, Playlist.RollbackItemStore, Playlist.RollbackItemUpdate, Playlist.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); IsPartiallyNew |= _Playlists.IsNew; IsNew &= _Playlists.IsNew; DataStores[2] = _Playlists; onPlaylistsFilled(); _PlaylistTracks = new DataStoreCSV <PlaylistTrack>( this, 3, csvConfig !, PlaylistTrack.EstimatedLineLength, PlaylistTrack.Headers, PlaylistTrack.SetKey, PlaylistTrack.Create, PlaylistTrack.Verify, PlaylistTrack.Update, PlaylistTrack.Write, PlaylistTrack.RollbackItemNew, PlaylistTrack.RollbackItemStore, PlaylistTrack.RollbackItemUpdate, PlaylistTrack.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); IsPartiallyNew |= _PlaylistTracks.IsNew; IsNew &= _PlaylistTracks.IsNew; DataStores[3] = _PlaylistTracks; onPlaylistTracksFilled(); _PlayinglistTracks = new DataStoreCSV <PlayinglistTrack>( this, 4, csvConfig !, PlayinglistTrack.EstimatedLineLength, PlayinglistTrack.Headers, PlayinglistTrack.SetKey, PlayinglistTrack.Create, null, PlayinglistTrack.Update, PlayinglistTrack.Write, PlayinglistTrack.RollbackItemNew, PlayinglistTrack.RollbackItemStore, PlayinglistTrack.RollbackItemUpdate, PlayinglistTrack.RollbackItemRelease, areInstancesUpdatable: true, areInstancesReleasable: true); IsPartiallyNew |= _PlayinglistTracks.IsNew; IsNew &= _PlayinglistTracks.IsNew; DataStores[4] = _PlayinglistTracks; onPlayinglistTracksFilled(); } onConstructed(); IsInitialised = true; }
public void TestCreateOnlyParentChangeableChild() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); initDL(); assertDL(); //stored immediately var now = DateTime.Now.Date; var parent1Key = addParent("1", isStoring: true).Key; var parent1NullableKey = addParentNullable("1N", isStoring: true).Key; var child1Key = addChild(parent1Key, null, "11", "11U", isStoring: true).Key; var parent2Key = addParent("2", isStoring: true).Key; var parent2NullableKey = addParentNullable("2N", isStoring: true).Key; var child2Key = addChild(parent2Key, parent2NullableKey, "21", "21U", isStoring: true).Key; var child3Key = addChild(parent2Key, parent2NullableKey, "22", "22U", isStoring: true).Key; //not stored var parent3 = addParent("3", isStoring: false); var parent3Nullable = addParent("3N", isStoring: false); var child4 = addChild(parent3, null, "31", "31U", isStoring: false); DC.Data.StartTransaction(); parent3.Store(); parent3Nullable.Store(); child4.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent3); store(parent3Nullable); store(child4); DC.Data.CommitTransaction(); assertData(); var parent4 = addParent("4", isStoring: false); var parent4Nullable = addParentNullable("4N", isStoring: false); var child5 = addChild(parent4, parent4Nullable, "41", "41U", isStoring: false); var child6 = addChild(parent4, parent4Nullable, "42", "42U", isStoring: false); DC.Data.StartTransaction(); parent4.Store(); parent4Nullable.Store(); child5.Store(); child6.Store(); DC.Data.RollbackTransaction(); DC.Data.StartTransaction(); store(parent4); store(parent4Nullable); store(child5); store(child6); DC.Data.CommitTransaction(); assertData(); updateChild(child1Key, parent2Key, parent2NullableKey, now, "12"); updateChild(child1Key, parent2Key, parent2NullableKey, now.AddDays(-1), "13"); updateChild(child1Key, parent1Key, parent1NullableKey, now.AddDays(-1), "14"); updateChild(child1Key, parent1Key, null, now.AddDays(-1), "15"); updateChild(child1Key, parent1Key, parent1NullableKey, now.AddDays(-1), "16"); DC.Data.StartTransaction(); child5.Release(); DC.Data.RollbackTransaction(); assertData(); DC.Data.StartTransaction(); expectedChildren.Remove(child5.Key); child5.Release(); DC.Data.CommitTransaction(); //assertData(); does not work, because once data context gets created again, parent has lost the not stored child assertDL(); } finally { DC.DisposeData(); } }
public void TestParentOneChild() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); initDL(); assertDL(); var parent = new ParentOneChild_Parent("P0"); expectedParents[parent.Key] = parent.ToString(); assertData(); //create child DC.Data.StartTransaction(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; new ParentOneChild_Child("C0", parent, null); DC.Data.RollbackTransaction(); assertData(); DC.Data.StartTransaction(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; var child = new ParentOneChild_Child("C0", parent, null); expectedChildren[child.Key] = child.ToString(); expectedParents[parent.Key] = parent.ToString(); DC.Data.CommitTransaction(); assertData(); //update child //the following 2 parents get stored immediately without transaction var parent1 = new ParentOneChild_Parent("P1"); expectedParents[parent1.Key] = parent1.ToString(); var parentNullable = new ParentOneChild_ParentNullable("P0N"); expectedParentsNullable[parentNullable.Key] = parentNullable.ToString(); child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Update("C0U", parent1, parentNullable); DC.Data.RollbackTransaction(); assertData(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; parent1 = DC.Data.ParentOneChild_Parents[parent1.Key]; parentNullable = DC.Data.ParentOneChild_ParentNullables[parentNullable.Key]; child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Update("C0U", parent1, parentNullable); DC.Data.CommitTransaction(); expectedChildren[child.Key] = child.ToString(); expectedParents[parent.Key] = parent.ToString(); expectedParents[parent1.Key] = parent1.ToString(); expectedParentsNullable[parentNullable.Key] = parentNullable.ToString(); assertData(); //update child var parentNullable1 = new ParentOneChild_ParentNullable("P1N"); expectedParentsNullable[parentNullable1.Key] = parentNullable1.ToString(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Update("C0U1", parent, parentNullable1); DC.Data.RollbackTransaction(); assertData(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; parent1 = DC.Data.ParentOneChild_Parents[parent1.Key]; parentNullable = DC.Data.ParentOneChild_ParentNullables[parentNullable.Key]; parentNullable1 = DC.Data.ParentOneChild_ParentNullables[parentNullable1.Key]; child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Update("C0U1", parent, parentNullable1); DC.Data.CommitTransaction(); expectedChildren[child.Key] = child.ToString(); expectedParents[parent.Key] = parent.ToString(); expectedParents[parent1.Key] = parent1.ToString(); expectedParentsNullable[parentNullable.Key] = parentNullable.ToString(); expectedParentsNullable[parentNullable1.Key] = parentNullable1.ToString(); assertData(); //delete children child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Release(); DC.Data.RollbackTransaction(); assertData(); parent = DC.Data.ParentOneChild_Parents[parent.Key]; parentNullable1 = DC.Data.ParentOneChild_ParentNullables[parentNullable1.Key]; child = DC.Data.ParentOneChild_Children[child.Key]; DC.Data.StartTransaction(); child.Release(); DC.Data.CommitTransaction(); expectedChildren.Remove(child.Key); expectedParents[parent.Key] = parent.ToString(); expectedParentsNullable[parentNullable1.Key] = parentNullable1.ToString(); } finally { DC.DisposeData(); } }