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 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(); } }
private void assertData() { assertDC(isAfterDispose: false); DC.DisposeData(); initDC(); assertDC(isAfterDispose: true); }
private void assertData() { assertDL(); DC.DisposeData(); initDL(); assertDL(); }
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 TestDataTypeSample() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); var csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); new DC(csvConfig); var now = DateTime.Now; var nowDate = now.Date; var nowMinute = nowDate.AddMinutes(1); var nowSecond = nowDate.AddSeconds(1); var nowTimeSpan = now - now.Date; var nowTime = TimeSpan.FromSeconds(Math.Floor(nowTimeSpan.TotalSeconds)); var d7 = 1.2345678m; var d5 = 1.23456m; var d4 = 1.2345m; var d2 = 1.23m; var aLong = 123456789012; var oldDts = new DataTypeSample(nowDate, nowDate, nowTime, nowTime, nowMinute, nowMinute, nowSecond, nowSecond, now, now, nowTimeSpan, nowTimeSpan, d7, d7, d2, d2, d4, d4, d5, d5, true, true, 123, 123, aLong, aLong, 'a', 'a', "abc", "abc", SampleStateEnum.None, SampleStateEnum.Some, isStoring: true); DC.DisposeData(); new DC(csvConfig); var newDts = DC.Data.DataTypeSamples[0]; assert(oldDts, newDts); oldDts = new DataTypeSample(nowDate, null, nowTime, null, nowMinute, null, nowSecond, null, now, null, nowTimeSpan, null, d7, null, d2, null, d4, null, d5, null, true, null, 123, null, aLong, null, 'a', null, "abc", null, SampleStateEnum.None, SampleStateEnum.Some, isStoring: true); DC.DisposeData(); new DC(csvConfig); newDts = DC.Data.DataTypeSamples[1]; assert(oldDts, newDts); oldDts = new DataTypeSample(DateTime.MinValue.Date, DateTime.MaxValue.Date, TimeSpan.FromTicks(0), new TimeSpan(0, 23, 59, 59, 0), DateTime.MinValue.Date.AddMinutes(1), DateTime.MaxValue.Date.AddMinutes(-1), DateTime.MinValue.Date.AddSeconds(1), DateTime.MaxValue.Date.AddSeconds(-1), DateTime.MinValue, DateTime.MaxValue, TimeSpan.MinValue, TimeSpan.MaxValue, decimal.MinValue, decimal.MaxValue, -123456789012.12m, 123456789012.12m, -123456789012.1234m, 123456789012.1234m, -123456789012.12345m, 123456789012.12345m, false, true, int.MinValue, int.MaxValue, long.MinValue, long.MaxValue, char.MinValue, char.MaxValue, "", new string('a', 1000), (SampleStateEnum)int.MinValue, (SampleStateEnum)int.MaxValue, isStoring: true); DC.DisposeData(); new DC(csvConfig); newDts = DC.Data.DataTypeSamples[2]; assert(oldDts, newDts); } finally { DC.DisposeData(); } }
private void disposeCreateDC() { assertData(); DC.DisposeData(); //removeReleasedExpectedRecords(); var dc = new DC(csvConfig); location = dc.Locations[0]; expectedPlayinglists.RemoveAll(pl => pl.PlayinglistTrackRecords.Count == 0); assertData(); }
private void assertData() { assertDL(); if (csvConfig is null) { return; } DC.DisposeData(); initDL(); assertDL(); }
private void assertData() { assertDictionaries(); DC.Data.Dispose(); if (bakCsvFileSwapper.UseBackupFiles()) { _ = new DC(csvConfig); assertDictionaries(); DC.DisposeData(); bakCsvFileSwapper.SwapBack(); } _ = new DC(csvConfig); assertDictionaries(); }
private void assertData() { assertDL(); if (csvConfig is null) { return; } DC.DisposeData(); if (bakCsvFileSwapper !.UseBackupFiles()) { initDL(); assertDL(); DC.DisposeData(); bakCsvFileSwapper.SwapBack(); } initDL(); assertDL(); }
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 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 void TestPlayingList() { 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 location = new Location("SomePath", "SomeLocation"); //create and delete playlist var playlistRecordA = createPlayList(); release(playlistRecordA); increaseGeneration(); //create and delete track var trackRecordA = createTrack(); release(trackRecordA); increaseGeneration(); //assign new track to new playlist, delete playlistTrack, track, playlist playlistRecordA = createPlayList(); trackRecordA = createTrack(); var playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); release(playlistTrackRecordA); release(trackRecordA); release(playlistRecordA); increaseGeneration(); //assign new track to new playlist, delete playlistTrack track, playlist playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); release(trackRecordA); release(playlistRecordA); increaseGeneration(); //assign new track to new playlist, delete playlistTrack playlist, track playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); release(playlistRecordA); release(trackRecordA); increaseGeneration(); //create new playlist, track, playlistTrack, playingList //delete playlist, track playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); var playingListRecordA = createPlayingList(playlistRecordA); release(playlistRecordA); release(trackRecordA); increaseGeneration(); //create new playlist, track, playlistTrack, playingList //delete track, playlist playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); playingListRecordA = createPlayingList(playlistRecordA); release(trackRecordA); release(playlistRecordA); increaseGeneration(); //create new playlist, track, playlistTrack, playingList //delete playlistTrack, track, playlist playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); playingListRecordA = createPlayingList(playlistRecordA); release(playlistTrackRecordA); release(trackRecordA); release(playlistRecordA); increaseGeneration(); //create new playlist with 3 tracks, 3 PlaylistTracks, playingList //play 1 track //delete first playlistTrack //delete second playlistTrack playlistRecordA = createPlayList(); trackRecordA = createTrack(); playlistTrackRecordA = createPlaylistTrack(playlistRecordA, trackRecordA); var trackRecordB = createTrack(); var playlistTrackRecordB = createPlaylistTrack(playlistRecordA, trackRecordB); var trackRecordC = createTrack(); var playlistTrackRecordC = createPlaylistTrack(playlistRecordA, trackRecordC); playingListRecordA = createPlayingList(playlistRecordA); playNextTrack(playingListRecordA, trackRecordA); release(playlistTrackRecordA); release(playlistTrackRecordB); //playlist contains only 1 record. Keep playing it playNextTrack(playingListRecordA, trackRecordC); playNextTrack(playingListRecordA, trackRecordC); //create second playinglist with same 3 tracks //delete 3. track var playlistRecordB = createPlayList(); _ = createPlaylistTrack(playlistRecordB, trackRecordA); _ = createPlaylistTrack(playlistRecordB, trackRecordB); _ = createPlaylistTrack(playlistRecordB, trackRecordC); //Testing of playNextTrack from empty PlayingList cannot be tested, because disposeCreateDC() makes empty PlayingList //disapear //release(trackRecordC); //playNextTrack(playingListRecordA, null); //increaseGeneration(); Assert.IsTrue(true);//Just for setting a breakpoint } finally { DC.DisposeData(); } }
public void TestSingleChild() { /* Test explanation: * items can be stored or not stored, legal combinations: * child variable name | parent variable name * --------------------+-------------------- * child_: not stored | parent_: not stored * child__: not stored | parent__:stored * child: stored | parent: stored * child___: stored | parent___: not stored this combination is illegal. Test: is an exception thrown ? * * for each combination above some test code is written * * parent variabls names: * Parent: the parent property in the child is not nullable * ParentN: the parent property in the child is nullable * ParentR: the parent property in the child is readonly * ParentNR: the parent property in the child is nullable and readonly * * each activity like create, update or delete is done first in a rooled back transaction and none of the data should * be changed, then the same activity is executed in a commited transactions and the data should change accordingly. * * After a transaction has been committed, the datacontext gets disposed, opened again and verified, that the data * is still the same. This is done twice, first using the .bak files, then the .csv files. There is one exception: * stored parents might have some not stored children. In the new data context, those parents have no longer those * children. * * For convenience, the variables parent0, parentN1, child1, etc. contain always the data from the latest data context. They * get updated, each time assertDataDisposeDCRecreateDCassertData() gets called. * * child_, child__, parent_ and parent___ are not stored in the data context. They do not get updated by * assertDataDisposeDCRecreateDCassertData() and can therefore contain children or parents stored in previous data * contexts. */ var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); try { csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); bakCsvFileSwapper = new BakCsvFileSwapper(csvConfig); DC.Trace = dcTrace; dc = new DC(csvConfig); assertData(""); // Create // ====== // Create parent // ------------- traceHeader("create not stored parent"); dc.StartTransaction(); var parent0_ = new SingleChildParent("p_Temp", isStoring: false); dc.RollbackTransaction(); assertData(""); dc.StartTransaction(); parent0_ = new SingleChildParent("p_", isStoring: false); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData(""); traceHeader("create stored parent"); dc.StartTransaction(); parent0 = new SingleChildParent("p0Temp", isStoring: true); dc.RollbackTransaction(); assertData(""); dc.StartTransaction(); parent0 = new SingleChildParent("p0", isStoring: true); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|"); // Create child with Parent==parent0, ParentN==null, ParentR==parentR0, ParentNR=parentNR0 // --------------------------------------------------------------------------------------- traceHeader("create not stored child with not stored parent"); var parentR0_ = new SingleChildParentR("pR0_", isStoring: false); var parentNR0_ = new SingleChildParentNR("pRN0_", isStoring: false); dc.StartTransaction(); var child0_ = new SingleChildChild("c0_Temp", parent0_, null, parentR0_, parentNR0_, isStoring: false); dc.RollbackTransaction(); Assert.IsNull(parent0_.Child); Assert.IsNull(parentR0_.Child); Assert.IsNull(parentNR0_.Child); assertData("p0|"); dc.StartTransaction(); child0_ = new SingleChildChild("c0_", parent0_, null, parentR0_, parentNR0_, isStoring: false); dc.CommitTransaction(); Assert.AreEqual("c0_", child0_.Text); Assert.AreEqual(child0_, parent0_.Child); Assert.AreEqual(child0_, parentR0_.Child); Assert.AreEqual(child0_, parentNR0_.Child); assertDataDisposeDCRecreateDCassertData("p0|"); traceHeader("create not stored child with stored parent"); parent0__ = new SingleChildParent("p0__", isStoring: true); parentR0__ = new SingleChildParentR("pR0__", isStoring: true); parentNR0__ = new SingleChildParentNR("pNR0__", isStoring: true); dc.StartTransaction(); var child0__ = new SingleChildChild("c0__Temp", parent0__, null, parentR0__, parentNR0__, isStoring: false); dc.RollbackTransaction(); Assert.IsNull(parent0__.Child); Assert.IsNull(parentR0__.Child); Assert.IsNull(parentNR0__.Child); assertData("p0|p0__|pR0__|pNR0__|"); dc.StartTransaction(); child0__ = new SingleChildChild("c0__", parent0__, null, parentR0__, parentNR0__, isStoring: false); dc.CommitTransaction(); Assert.AreEqual("c0__", child0__.Text); Assert.AreEqual(parent0__, child0__.Parent); Assert.AreEqual(parentR0__, child0__.ParentR); Assert.AreEqual(parentNR0__, child0__.ParentNR); assertDataDisposeDCRecreateDCassertData("p0|p0__:c0__|pR0__:c0__|pNR0__:c0__|", "p0|p0__|pR0__|pNR0__|"); traceHeader("create stored child with stored parent"); parentR0 = new SingleChildParentR("pR0", isStoring: true); parentNR0 = new SingleChildParentNR("pNR0", isStoring: true); dc.StartTransaction(); child0 = new SingleChildChild("c0Temp", parent0, null, parentR0, parentNR0, isStoring: true); dc.RollbackTransaction(); assertData("p0|p0__|pR0__|pR0|pNR0__|pNR0|"); dc.StartTransaction(); child0 = new SingleChildChild("c0", parent0, null, parentR0, parentNR0, isStoring: true); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); //Fail to create stored child with not stored parent traceHeader("fail to create stored child with not stored parents"); try { var parent0___ = new SingleChildParent("p0___", isStoring: false); var parentN0___ = new SingleChildParentN("pN0___", isStoring: false); var parentR0___ = new SingleChildParentR("pR0___", isStoring: false); var parentNR0___ = new SingleChildParentNR("pRN0___", isStoring: false); var child___ = new SingleChildChild("failed child", parent0___, parentN0___, parentR0___, parentNR0___); Assert.Fail(); } catch { } //Todo: Ideally, an exception during create, store or remove should not change any data. Is additional code needed undoing //any potentially changed data ? //Assert.AreEqual(0, parent0_.Children.Count); assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); // Update // ====== // Update child.Parent to parent1, child.ParentN to parent0N // --------------------------------------------------------- traceHeader("not stored child: update with not stored parents"); var parent1_ = new SingleChildParent("p1_", isStoring: false); var parentN0_ = new SingleChildParentN("pN0___", isStoring: false); dc.StartTransaction(); child0_.Update("c0_.1Temp", parent1_, parentN0_); dc.RollbackTransaction(); Assert.AreEqual(child0_, parent0_.Child); Assert.IsNull(parentN0_.Child); Assert.IsNull(parent1_.Child); assertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.1", parent1_, parentN0_); dc.CommitTransaction(); Assert.IsNull(parent0_.Child); Assert.AreEqual(child0_, parentN0_.Child); Assert.AreEqual(child0_, parent1_.Child); assertDataDisposeDCRecreateDCassertData("p0:c0|p0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); traceHeader("not stored child: update with stored parents"); parent1__ = new SingleChildParent("p1__", isStoring: true); parentN0__ = new SingleChildParentN("pN0__", isStoring: true); dc.StartTransaction(); child0__.Update("c0__.1Temp", parent1__, parentN0__); dc.RollbackTransaction(); Assert.AreEqual("c0__", child0__.Text); Assert.AreEqual(parent0__.Text, child0__.Parent.Text); Assert.IsNull(child0__.ParentN); assertData("p0:c0|p0__|p1__|pN0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.1", parent1__, parentN0__); dc.CommitTransaction(); Assert.AreEqual("c0__.1", child0__.Text); Assert.AreEqual(parent1__, child0__.Parent); Assert.AreEqual(parentN0__, child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0:c0|p0__|p1__:c0__.1|pN0__:c0__.1|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|", "p0:c0|p0__|p1__|pN0__|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); traceHeader("stored child: update with stored parents"); parent1 = new SingleChildParent("p1", isStoring: true); parentN0 = new SingleChildParentN("pN0", isStoring: true); dc.StartTransaction(); child0.Update("c0.1Temp", parent1, parentN0); dc.RollbackTransaction(); assertData("p0:c0|p0__|p1__|p1|pN0__|pN0|pR0__|pR0:c0|pNR0__|pNR0:c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.1", parent1, parentN0); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); // Update child.ParentN to parent1N // -------------------------------- traceHeader("not stored child: update not stored ParentN"); var parentN1_ = new SingleChildParentN("pN1_", isStoring: false); dc.StartTransaction(); child0_.Update("c0_.2Temp", parent1_, parentN1_); dc.RollbackTransaction(); Assert.AreEqual("c0_.1", child0_.Text); Assert.AreEqual(child0_, parent1_.Child); Assert.AreEqual(child0_, parentN0_.Child); Assert.IsNull(parentN1_.Child); assertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.2", parent1_, parentN1_); dc.CommitTransaction(); Assert.AreEqual("c0_.2", child0_.Text); Assert.IsNull(parent0_.Child); Assert.IsNull(parentN0_.Child); Assert.AreEqual(child0_, parent1_.Child); Assert.AreEqual(child0_, parentN1_.Child); assertDataDisposeDCRecreateDCassertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); traceHeader("not stored child: update stored ParentN"); parentN1__ = new SingleChildParentN("pN1__", isStoring: true); dc.StartTransaction(); child0__.Update("c0__.2Temp", parent1__, parentN1__); dc.RollbackTransaction(); Assert.AreEqual("c0__.1", child0__.Text); Assert.AreEqual(parent1__.Text, child0__.Parent.Text); Assert.AreEqual(parentN0__.Text, child0__.ParentN !.Text); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.2", parent1__, parentN1__); dc.CommitTransaction(); Assert.AreEqual("c0__.2", child0__.Text); Assert.AreEqual(parent1__, child0__.Parent); Assert.AreEqual(parentN1__, child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__:c0__.2|p1:c0.1|pN0__|pN0:c0.1|pN1__:c0__.2|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|", "p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); traceHeader("stored child: update stored ParentN"); parentN1 = new SingleChildParentN("pN1", isStoring: true); dc.StartTransaction(); child0.Update("c0.2Temp", parent1, parentN1); dc.RollbackTransaction(); assertData("p0|p0__|p1__|p1:c0.1|pN0__|pN0:c0.1|pN1__|pN1|pR0__|pR0:c0.1|pNR0__|pNR0:c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.2", parent1, parentN1); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); // Update child.ParentN to null // ---------------------------- traceHeader("not stored child: update not stored ParentN to null"); dc.StartTransaction(); child0_.Update("c0_.3Temp", parent1_, null); dc.RollbackTransaction(); Assert.AreEqual("c0_.2", child0_.Text); Assert.AreEqual(child0_, parent1_.Child); Assert.AreEqual(child0_, parentN1_.Child); assertData("p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.3", parent1_, null); dc.CommitTransaction(); Assert.AreEqual("c0_.3", child0_.Text); Assert.IsNull(parentN0_.Child); Assert.AreEqual(child0_, parent1_.Child); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); traceHeader("not stored child: update stored ParentN to null"); dc.StartTransaction(); child0__.Update("c0__.3Temp", parent1__, null); dc.RollbackTransaction(); Assert.AreEqual("c0__.2", child0__.Text); Assert.AreEqual(parent1__.Text, child0__.Parent.Text); Assert.AreEqual(parentN1__.Text, child0__.ParentN !.Text); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.3", parent1__, null); dc.CommitTransaction(); Assert.AreEqual("c0__.3", child0__.Text); Assert.AreEqual(parent1__, child0__.Parent); Assert.IsNull(child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__:c0__.3|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|", "p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); traceHeader("stored child: update stored ParentN to null"); dc.StartTransaction(); child0.Update("c0.3Temp", parent1, null); dc.RollbackTransaction(); assertData("p0|p0__|p1__|p1:c0.2|pN0__|pN0|pN1__|pN1:c0.2|pR0__|pR0:c0.2|pNR0__|pNR0:c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.3", parent1, null); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData( "p0|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pNR0__|pNR0:c0.3|c0.3:p1,pR0,pNR0|"); // Release // ======= // Fail to release parent with stored child // ---------------------------------------- traceHeader("stored child: fail to release parent"); try { parent1.Release(); Assert.Fail(); } catch { } assertData("p0|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pNR0__|pNR0:c0.3|c0.3:p1,pR0,pNR0|"); bakCsvFileSwapper.DeleteBakFiles(); //Release child //------------- traceHeader("stored child: release child"); parentR1 = new SingleChildParentR("pR1", isStoring: true); parentNR1 = new SingleChildParentNR("pNR1", isStoring: true); child1 = new SingleChildChild("c1", parent0, parentN1, parentR1, parentNR1, isStoring: true); assertDataDisposeDCRecreateDCassertData( "p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|c1:p0,pN1,pR1,pNR1|"); dc.StartTransaction(); child1.Release(); dc.RollbackTransaction(); assertData("p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|c1:p0,pN1,pR1,pNR1|"); dc.StartTransaction(); child1.Release(); dc.CommitTransaction(); child1 = null; assertDataDisposeDCRecreateDCassertData( "p0:c1|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1:c1|pR0__|pR0:c0.3|pR1:c1|pNR0__|pNR0:c0.3|pNR1:c1|c0.3:p1,pR0,pNR0|", "p0" + "|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1" + "|pR0__|pR0:c0.3|pR1" + "|pNR0__|pNR0:c0.3|pNR1" + "|c0.3:p1,pR0,pNR0|"); //Release parent //-------------- traceHeader("stored parent: release parent"); dc.StartTransaction(); parent0.Release(); dc.RollbackTransaction(); assertData("p0" + "|p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1" + "|pR0__|pR0:c0.3|pR1" + "|pNR0__|pNR0:c0.3|pNR1" + "|c0.3:p1,pR0,pNR0|"); dc.StartTransaction(); parent0.Release(); dc.CommitTransaction(); parent0 = null; assertDataDisposeDCRecreateDCassertData( "p0__|p1__|p1:c0.3|pN0__|pN0|pN1__|pN1|pR0__|pR0:c0.3|pR1|pNR0__|pNR0:c0.3|pNR1|c0.3:p1,pR0,pNR0|"); } finally { DC.DisposeData(); } }
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. public CollectionTestBase() { #pragma warning restore CS8618 #pragma warning disable IDE0045 // Convert to conditional expression if (typeof(TChild) == typeof(ListChild)) { collectionType = CollectionTypeEnum.list; } else if (typeof(TChild) == typeof(DictionaryChild)) { collectionType = CollectionTypeEnum.dictionary; } else if (typeof(TChild) == typeof(SortedListChild)) { collectionType = CollectionTypeEnum.sortedList; } else { throw new NotSupportedException(); } #pragma warning restore IDE0045 var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); //initVariables(); try { csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); bakCsvFileSwapper = new BakCsvFileSwapper(csvConfig); DC.Trace = dcTrace; dc = new DC(csvConfig); initialiseDcParents(); assertData(""); // Create // ====== // Create parent // ------------- traceHeader("create not stored parent"); dc.StartTransaction(); var parent0_ = createParent("p_Temp", isStoring: false); dc.RollbackTransaction(); assertData(""); dc.StartTransaction(); parent0_ = createParent("p_", isStoring: false); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData(""); traceHeader("create stored parent"); dc.StartTransaction(); parent0 = createParent("p0Temp", isStoring: true); dc.RollbackTransaction(); assertData(""); dc.StartTransaction(); parent0 = createParent("p0", isStoring: true); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|"); // Create child with Parent==parent0, ParentN==null, ParentR==parentR0, ParentNR=parentNR0 // --------------------------------------------------------------------------------------- traceHeader("create not stored child with not stored parent"); var parentR0_ = createParentR("pR0_", isStoring: false); var parentNR0_ = createParentNR("pRN0_", isStoring: false); dc.StartTransaction(); var child0_ = createChild("c0_Temp", parent0_, null, parentR0_, parentNR0_, isStoring: false); dc.RollbackTransaction(); Assert.AreEqual(0, parent0_.CountAllChildren); Assert.AreEqual(0, parentR0_.CountAllChildren); Assert.AreEqual(0, parentNR0_.CountAllChildren); assertData("p0|"); dc.StartTransaction(); child0_ = createChild("c0_", parent0_, null, parentR0_, parentNR0_, isStoring: false); dc.CommitTransaction(); Assert.AreEqual("c0_", child0_.Text); Assert.AreEqual(child0_, parent0_.AllChildrenFirst); Assert.AreEqual(child0_, parentR0_.AllChildrenFirst); Assert.AreEqual(child0_, parentNR0_.AllChildrenFirst); assertDataDisposeDCRecreateDCassertData("p0|"); traceHeader("create not stored child with stored parent"); parentR0 = createParentR("pR0", isStoring: true); parentNR0 = createParentNR("pNR0", isStoring: true); dc.StartTransaction(); var child0__ = createChild("c0__Temp", parent0, null, parentR0, parentNR0, isStoring: false); dc.RollbackTransaction(); Assert.AreEqual(0, parent0.CountAllChildren); Assert.AreEqual(0, parentR0.CountAllChildren); Assert.AreEqual(0, parentNR0.CountAllChildren); assertData("p0|pR0|pNR0|"); dc.StartTransaction(); child0__ = createChild("c0__", parent0, null, parentR0, parentNR0, isStoring: false); dc.CommitTransaction(); Assert.AreEqual("c0__", child0__.Text); Assert.AreEqual(parent0, child0__.Parent); Assert.AreEqual(parentR0, child0__.ParentR); Assert.AreEqual(parentNR0, child0__.ParentNR); assertDataDisposeDCRecreateDCassertData("p0:c0__|pR0:c0__|pNR0:c0__|", "p0|pR0|pNR0|"); traceHeader("create stored child with stored parent"); dc.StartTransaction(); child0 = createChild("c0Temp", parent0, null, parentR0, parentNR0, isStoring: true); dc.RollbackTransaction(); assertData("p0|pR0|pNR0|"); dc.StartTransaction(); child0 = createChild("c0", parent0, null, parentR0, parentNR0, isStoring: true); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0:;c0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); //Fail to create stored child with not stored parent traceHeader("fail to create stored child with not stored parents"); try { var parent0___ = createParent("p0___", isStoring: false); var parentN0___ = createParentN("pN0___", isStoring: false); var parentR0___ = createParentR("pR0___", isStoring: false); var parentNR0___ = createParentNR("pRN0___", isStoring: false); var _ = createChild("failed child", parent0___, parentN0___, parentR0___, parentNR0___); Assert.Fail(); } catch { } //Todo: Ideally, an exception during create, store or remove should not change any data. Is additional code needed undoing //any potentially changed data ? //Assert.AreEqual(0, parent0_.CountAllChildren); assertDataDisposeDCRecreateDCassertData("p0:;c0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); // Update // ====== // Update child.Parent to parent1, child.ParentN to parentN0 // --------------------------------------------------------- traceHeader("not stored child: update with not stored parents"); var parent1_ = createParent("p1_", isStoring: false); var parentN0_ = createParentN("pN0___", isStoring: false); //var parentR1_ = createParent("pR1_", isStoring: false); //var parentNR0_ = createParentN("pNR0_", isStoring: false); dc.StartTransaction(); child0_.Update("c0_.1Temp", (TP)parent1_, (TPN)parentN0_); dc.RollbackTransaction(); Assert.AreEqual(child0_, parent0_.AllChildrenFirst); Assert.AreEqual(0, parentN0_.CountAllChildren); Assert.AreEqual(0, parent1_.CountAllChildren); assertData("p0:;c0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.1", (TP)parent1_, (TPN)parentN0_); dc.CommitTransaction(); Assert.AreEqual(0, parent0_.CountAllChildren); Assert.AreEqual(child0_, parentN0_.AllChildrenFirst); Assert.AreEqual(child0_, parent1_.AllChildrenFirst); assertDataDisposeDCRecreateDCassertData("p0:;c0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); traceHeader("not stored child: update with stored parents"); parent1 = createParent("p1", isStoring: true); parentN0 = createParentN("pN0", isStoring: true); dc.StartTransaction(); child0__.Update("c0__.1Temp", (TP)parent1, (TPN)parentN0); dc.RollbackTransaction(); Assert.AreEqual("c0__", child0__.Text); Assert.AreEqual(parent0.Text, child0__.Parent.Text); Assert.IsNull(child0__.ParentN); assertData("p0:;c0|p1|pN0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.1", (TP)parent1, (TPN)parentN0); dc.CommitTransaction(); Assert.AreEqual("c0__.1", child0__.Text); Assert.AreEqual(parent1, child0__.Parent); Assert.AreEqual(parentN0, child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0:;c0|p1:c0__.1|pN0:c0__.1|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|", "p0:;c0|p1|pN0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); traceHeader("stored child: update with stored parents"); dc.StartTransaction(); child0.Update("c0.1Temp", (TP)parent1, (TPN)parentN0); dc.RollbackTransaction(); assertData("p0:;c0|p1|pN0|pR0:;c0|pNR0:;c0|c0:p0,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.1", (TP)parent1, (TPN)parentN0); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.1|pN0:;c0.1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); // Update child.ParentN to parentN1 // -------------------------------- traceHeader("not stored child: update not stored ParentN"); var parentN1_ = createParentN("pN1_", isStoring: false); dc.StartTransaction(); child0_.Update("c0_.2Temp", (TP)parent1_, (TPN)parentN1_); dc.RollbackTransaction(); Assert.AreEqual("c0_.1", child0_.Text); Assert.AreEqual(child0_, parent1_.AllChildrenFirst); Assert.AreEqual(child0_, parentN0_.AllChildrenFirst); Assert.AreEqual(0, parentN1_.CountAllChildren); assertData("p0|p1:;c0.1|pN0:;c0.1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.2", (TP)parent1_, (TPN)parentN1_); dc.CommitTransaction(); Assert.AreEqual("c0_.2", child0_.Text); Assert.AreEqual(0, parent0_.CountAllChildren); Assert.AreEqual(0, parentN0_.CountAllChildren); Assert.AreEqual(child0_, parent1_.AllChildrenFirst); Assert.AreEqual(child0_, parentN1_.AllChildrenFirst); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.1|pN0:;c0.1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); traceHeader("not stored child: update stored ParentN"); parentN1 = createParentN("pN1", isStoring: true); dc.StartTransaction(); child0__.Update("c0__.2Temp", (TP)parent1, (TPN)parentN1); dc.RollbackTransaction(); Assert.AreEqual("c0__.1", child0__.Text); Assert.AreEqual(parent1.Text, child0__.Parent.Text); Assert.AreEqual(parentN0.Text, child0__.ParentN !.Text); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.1|pN0:;c0.1|pN1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.2", (TP)parent1, (TPN)parentN1); dc.CommitTransaction(); Assert.AreEqual("c0__.2", child0__.Text); Assert.AreEqual(parent1, child0__.Parent); Assert.AreEqual(parentN1, child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0|p1:c0__.2;,c0.1|pN0:;c0.1|pN1:c0__.2|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|", "p0|p1:;c0.1|pN0:;c0.1|pN1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); traceHeader("stored child: update stored ParentN"); dc.StartTransaction(); child0.Update("c0.2Temp", (TP)parent1, (TPN)parentN1); dc.RollbackTransaction(); assertData("p0|p1:;c0.1|pN0:;c0.1|pN1|pR0:;c0.1|pNR0:;c0.1|c0.1:p1,pN0,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.2", (TP)parent1, (TPN)parentN1); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); // Update child.ParentN to null // ---------------------------- traceHeader("not stored child: update not stored ParentN to null"); dc.StartTransaction(); child0_.Update("c0_.3Temp", (TP)parent1_, null); dc.RollbackTransaction(); Assert.AreEqual("c0_.2", child0_.Text); Assert.AreEqual(child0_, parent1_.AllChildrenFirst); Assert.AreEqual(child0_, parentN1_.AllChildrenFirst); assertData("p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0_.Update("c0_.3", (TP)parent1_, null); dc.CommitTransaction(); Assert.AreEqual("c0_.3", child0_.Text); Assert.AreEqual(0, parentN0_.CountAllChildren); Assert.AreEqual(child0_, parent1_.AllChildrenFirst); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); traceHeader("not stored child: update stored ParentN to null"); dc.StartTransaction(); child0__.Update("c0__.3Temp", (TP)parent1, null); dc.RollbackTransaction(); Assert.AreEqual("c0__.2", child0__.Text); Assert.AreEqual(parent1.Text, child0__.Parent.Text); Assert.AreEqual(parentN1.Text, child0__.ParentN !.Text); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0__.Update("c0__.3", (TP)parent1, null); dc.CommitTransaction(); Assert.AreEqual("c0__.3", child0__.Text); Assert.AreEqual(parent1, child0__.Parent); Assert.IsNull(child0__.ParentN); assertDataDisposeDCRecreateDCassertData( "p0|p1:c0__.3;,c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|", "p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); traceHeader("stored child: update stored ParentN to null"); dc.StartTransaction(); child0.Update("c0.3Temp", (TP)parent1, null); dc.RollbackTransaction(); assertData("p0|p1:;c0.2|pN0|pN1:;c0.2|pR0:;c0.2|pNR0:;c0.2|c0.2:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child0.Update("c0.3", (TP)parent1, null); dc.CommitTransaction(); assertDataDisposeDCRecreateDCassertData("p0|p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); bakCsvFileSwapper.DeleteBakFiles(); // Release // ======= // Fail to release parent with stored child // ---------------------------------------- traceHeader("stored child: fail to release parent"); try { parent1.Release(); Assert.Fail(); } catch { } assertData("p0|p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); //Release child //------------- traceHeader("stored child: release child"); child1 = createChild("c1", parent1, parentN1, parentR0, parentNR0, isStoring: true); assertDataDisposeDCRecreateDCassertData( "p0|p1:;c0.3,c1|pN0|pN1:;c1|pR0:;c0.3,c1|pNR0:;c0.3,c1|c0.3:p1,pR0,pNR0|c1:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child1.Release(); dc.RollbackTransaction(); assertData("p0|p1:;c0.3,c1|pN0|pN1:;c1|pR0:;c0.3,c1|pNR0:;c0.3,c1|c0.3:p1,pR0,pNR0|c1:p1,pN1,pR0,pNR0|"); dc.StartTransaction(); child1.Release(); dc.CommitTransaction(); child1 = null; assertDataDisposeDCRecreateDCassertData( "p0|p1:;c0.3,c1|pN0|pN1:c1|pR0:;c0.3,c1|pNR0:;c0.3,c1|c0.3:p1,pR0,pNR0|", "p0|p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); //Release parent //-------------- traceHeader("stored parent: release parent"); dc.StartTransaction(); parent0.Release(); dc.RollbackTransaction(); assertData("p0|p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); dc.StartTransaction(); parent0.Release(); dc.CommitTransaction(); parent0 = null; assertDataDisposeDCRecreateDCassertData("p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); //test .bak file with complicated data structure // ============================================= traceHeader("create, update and release complicated data structure"); var parent2 = createParent("p2", isStoring: true); var parent3 = createParent("p3", isStoring: true); var parentN2 = createParentN("pN2", isStoring: true); var parentR2 = createParentR("pR2", isStoring: true); var parentNR2 = createParentNR("pRN2", isStoring: true); var child2 = createChild("c2", parent2, null, parentR2, parentNR2, isStoring: true); child2.Update("c2.0", (TP)parent2, null); parent2.Update("p2.0"); parent3.Update("p3.0"); parentN2.Update("pN2.0"); parentR2.Update("pR2.0"); parentNR2.Update("pRN2.0"); child2.Update("c2.0", (TP)parent3, (TPN)parentN2); child2.Release(); parent2.Release(); parent3.Release(); parentN2.Release(); parentR2.Release(); parentNR2.Release(); assertDataDisposeDCRecreateDCassertData("p1:;c0.3|pN0|pN1|pR0:;c0.3|pNR0:;c0.3|c0.3:p1,pR0,pNR0|"); } finally { DC.DisposeData(); } }
public void TestPlayingList() { try { var directoryInfo = new DirectoryInfo("TestCsv"); if (directoryInfo.Exists) { directoryInfo.Delete(recursive: true); directoryInfo.Refresh(); } directoryInfo.Create(); var csvConfig = new CsvConfig(directoryInfo.FullName, reportException: reportException); _ = new DC(csvConfig); var random = new Random(); //test Playinglist with Playlist var playlist = new Playlist("Name"); var location = new Location("Path", "Name"); var expectedTrackKeys = new HashSet <int>(); var expectedAllTrackKeys = new HashSet <int>(); var trackNo = 0; createTrack(playlist, location, trackNo++, expectedAllTrackKeys); createTrack(playlist, location, trackNo++, expectedAllTrackKeys); createTrack(playlist, location, trackNo++, expectedAllTrackKeys); refill(expectedAllTrackKeys, expectedTrackKeys); var playinglist1 = DC.Data.AddPlayinglist(playlist); assert(expectedTrackKeys); DC.DisposeData(); var iterations = expectedTrackKeys.Count * 2; Playinglist playinglist; for (int i = 0; i < iterations; i++) { if (expectedTrackKeys.Count == 0) { refill(expectedAllTrackKeys, expectedTrackKeys); } _ = new DC(csvConfig); assert(expectedTrackKeys); playlist = DC.Data.Playlists[playlist.Key]; playinglist = DC.Data.Playinglists[playlist]; var trackKey = playinglist.GetNext(random) !.PlaylistTracks[0].Track.Key; expectedTrackKeys.Remove(trackKey); DC.DisposeData(); } //test Playinglist with some tracks _ = new DC(csvConfig); refill(expectedAllTrackKeys, expectedTrackKeys); playinglist = DC.Data.AddPlayinglist(DC.Data.Tracks); iterations = expectedTrackKeys.Count * 2; for (int i = 0; i < iterations; i++) { if (expectedTrackKeys.Count == 0) { refill(expectedAllTrackKeys, expectedTrackKeys); } assert(expectedTrackKeys, playinglist); var trackKey = playinglist.GetNext(random) !.PlaylistTracks[0].Track.Key; expectedTrackKeys.Remove(trackKey); DC.DisposeData(); } Assert.IsTrue(true);//Just for setting a breakpoint } 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(); } }
private void assertDataDisposeDCRecreateDCassertData(string expectedDcString1, string?expectedDcString2 = null) { assertData(expectedDcString1); DC.DisposeData(); if (bakCsvFileSwapper.UseBackupFiles()) { dc = new DC(csvConfig); assertData(expectedDcString2 ?? expectedDcString1); DC.DisposeData(); bakCsvFileSwapper.SwapBack(); } dc = new DC(csvConfig); assertData(expectedDcString2 ?? expectedDcString1); if (parent0__ is not null) { parent0__ = dc.SingleChildParents[1]; } if (parent0 is not null) { parent0 = dc.SingleChildParents[0]; } if (parent1__ is not null) { parent1__ = dc.SingleChildParents[2]; } if (parent1 is not null) { parent1 = dc.SingleChildParents[3]; } if (parentN0__ is not null) { parentN0__ = dc.SingleChildParentNs[0]; } if (parentN0 is not null) { parentN0 = dc.SingleChildParentNs[1]; } if (parentN1__ is not null) { parentN1__ = dc.SingleChildParentNs[2]; } if (parentN1 is not null) { parentN1 = dc.SingleChildParentNs[3]; } if (parentR0__ is not null) { parentR0__ = dc.SingleChildParentRs[0]; } if (parentR0 is not null) { parentR0 = dc.SingleChildParentRs[1]; } if (parentR1 is not null) { parentR1 = dc.SingleChildParentRs[2]; } if (parentNR0__ is not null) { parentNR0__ = dc.SingleChildParentNRs[0]; } if (parentNR0 is not null) { parentNR0 = dc.SingleChildParentNRs[1]; } if (parentNR1 is not null) { parentNR1 = dc.SingleChildParentNRs[2]; } if (child0 is not null) { child0 = dc.SingleChildChildren[0]; } if (child1 is not null) { child1 = dc.SingleChildChildren[1]; } }
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 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(); } }
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(); } }
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; Console.SetWindowSize(Console.LargestWindowWidth * 8 / 10, Console.LargestWindowHeight * 9 / 10); Console.WriteLine("StorageLib Get Started Application"); Console.WriteLine("=================================="); Console.WriteLine(); var directoryInfo = new DirectoryInfo("GetSetartedData"); if (directoryInfo.Exists) { Console.WriteLine("Found existing directory for data: " + directoryInfo.FullName); } else { directoryInfo.Create(); //it is enough just to start with an empty directory. The data context will create //the necessary files if they are missing. Console.WriteLine("Created new directory for data: " + directoryInfo.FullName); } try { var dc = new DC(new CsvConfig(directoryInfo.FullName, reportException: reportException)); Parent parent0; Parent parent1; Child child0; if (dc.IsNew) { //new data context, we come here only the very first time Program runs or when the .csv gets deleted parent0 = new Parent("Parent0"); //per default, new() stores instance in Data Context parent1 = new Parent("Parent1", isStoring: false); //example where new instance is not stored yet parent1.Store(); //this can be done much later, but before application shuts down child0 = new Child("Child0", parent0); //this adds child0 automatically to parent0.Children consoleWriteLine("Newly created data", parent0, parent1, child0); } else { //access already existing data parent0 = DC.Data.Parents[0]; //access without local DC reference, just through static variable DC.Data parent1 = dc.Parents[1]; child0 = dc.Children[0]; consoleWriteLine("Exisiting data", parent0, parent1, child0); } //update without transaction child0.Update(child0.Name + " updated", parent1); consoleWriteLine("After simple update", parent0, parent1, child0); //a normal transaction dc.StartTransaction(); try { child0.Update("Child0", parent0); //could have many more statements dc.CommitTransaction(); } catch (Exception exception) { dc.RollbackTransaction(); reportException(exception); } consoleWriteLine("After update with transaction", parent0, parent1, child0); //showing that transaction rollback really works dc.StartTransaction(); child0.Update(child0.Name + " updated before rollback", parent1); consoleWriteLine("After update, before rollback", parent0, parent1, child0); dc.RollbackTransaction(); //normally, a dc.CommitTransaction() would be here consoleWriteLine("After transaction rollback", parent0, parent1, child0); child0.Release(); // removing child0 from dc.Children consoleWriteLine("After child0.Release", parent0, parent1, child0); //child0 is still in parent0.Children. //Since parent0 is stored, not stored child0 will not show when using: //foreach (var child in parent0.Children) { // To see all children when a parent is stored, use: //foreach (var child in parent0.Children.GetAll()) { //Similarly, parent0.Children.Count will only count stored children when parent is stored. //Use, parent0.Children.Count will only count stored children when parent is stored. child0.Store(); consoleWriteLine("After child0.Store", parent0, parent1, child0); } catch (Exception exception) { Console.WriteLine("Exception occured: " + exception); } finally { //It is important that your application calls DC.DisposeData() when it shuts down. This //flushes any not yet written data to that CSV files. DC.DisposeData(); } }