public void DeviceOfDrive() { int[] dvSavedId = new int[2]; Drive dr1 = new Drive("Drive 1"); Drive dr2 = new Drive("Drive 2"); Drive dr3 = new Drive("Drive 3"); Device dv1 = new Device("Device 1"); Device dv2 = new Device("Device 2"); using (ISession s = sessions.OpenSession()) { s.Save(dr1); s.Save(dr2); s.Save(dr3); dvSavedId[0] = (int) s.Save(dv1); dvSavedId[1] = (int) s.Save(dv2); s.Flush(); } dv1.Drives.Add(dr1); dv1.Drives.Add(dr2); dv2.Drives.Add(dr1); dv2.Drives.Add(dr3); using (ISession s = sessions.OpenSession()) { dvSavedId[0] = (int) s.Save(dv1); dvSavedId[1] = (int) s.Save(dv2); s.Flush(); } dv1 = null; dv2 = null; using (ISession s = sessions.OpenSession()) { s.Delete(dr3); s.Flush(); dv1 = (Device) s.Load(typeof(Device), dvSavedId[0]); dv2 = (Device) s.Load(typeof(Device), dvSavedId[1]); } Assert.AreEqual(2, dv1.Drives.Count); Assert.AreEqual(2, dv2.Drives.Count); // Verify dv1 unchanged Assert.IsTrue(dv1.Drives.Contains(dr1)); Assert.IsTrue(dv1.Drives.Contains(dr2)); // Verify dv2 Assert.IsTrue(dv2.Drives.Contains(dr1)); Assert.IsFalse(dv2.Drives.Contains(dr3)); // Verify one null int nullCount = 0; for (int i = 0; i < dv2.Drives.Count; i++) { if (dv2.Drives[i] == null) nullCount++; } Assert.AreEqual(1, nullCount); }
public async Task DeviceOfDriveAsync() { int[] dvSavedId = new int[2]; Drive dr1 = new Drive("Drive 1"); Drive dr2 = new Drive("Drive 2"); Drive dr3 = new Drive("Drive 3"); Device dv1 = new Device("Device 1"); Device dv2 = new Device("Device 2"); using (ISession s = Sfi.OpenSession()) { await(s.SaveAsync(dr1)); await(s.SaveAsync(dr2)); await(s.SaveAsync(dr3)); dvSavedId[0] = (int)await(s.SaveAsync(dv1)); dvSavedId[1] = (int)await(s.SaveAsync(dv2)); await(s.FlushAsync()); } dv1.Drives.Add(dr1); dv1.Drives.Add(dr2); dv2.Drives.Add(dr1); dv2.Drives.Add(dr3); using (ISession s = Sfi.OpenSession()) { dvSavedId[0] = (int)await(s.SaveAsync(dv1)); dvSavedId[1] = (int)await(s.SaveAsync(dv2)); await(s.FlushAsync()); } dv1 = null; dv2 = null; using (ISession s = Sfi.OpenSession()) { await(s.DeleteAsync(dr3)); await(s.FlushAsync()); dv1 = (Device)await(s.LoadAsync(typeof(Device), dvSavedId[0])); dv2 = (Device)await(s.LoadAsync(typeof(Device), dvSavedId[1])); } Assert.AreEqual(2, dv1.Drives.Count); // Verify one is missing Assert.AreEqual(1, dv2.Drives.Count); // Verify dv1 unchanged Assert.IsTrue(dv1.Drives.Contains(dr1)); Assert.IsTrue(dv1.Drives.Contains(dr2)); // Verify dv2 Assert.IsTrue(dv2.Drives.Contains(dr1)); Assert.IsFalse(dv2.Drives.Contains(dr3)); //Make sure that flush didn't touch not-found="ignore" records for not modified collection using (var s = Sfi.OpenSession()) using (var t = s.BeginTransaction()) { dv2 = await(s.GetAsync <Device>(dv2.Id)); await(s.FlushAsync()); await(t.CommitAsync()); } await(VerifyResultAsync(expectedInCollection: 1, expectedInDb: 2, msg: "not modified collection")); //Many-to-many clears collection and recreates it so not-found ignore records are lost using (var s = Sfi.OpenSession()) using (var t = s.BeginTransaction()) { dv2 = await(s.GetAsync <Device>(dv2.Id)); dv2.Drives.Add(dr2); await(t.CommitAsync()); } await(VerifyResultAsync(2, 2, msg: "modified collection")); async Task VerifyResultAsync(int expectedInCollection, int expectedInDb, string msg) { using (var s = Sfi.OpenSession()) { var realCound = Convert.ToInt32( await(s.CreateSQLQuery("select count(*) from DriveOfDevice where DeviceId = :id ") .SetParameter("id", dv2.Id) .UniqueResultAsync <object>())); dv2 = await(s.GetAsync <Device>(dv2.Id)); Assert.That(dv2.Drives.Count, Is.EqualTo(expectedInCollection), msg); Assert.That(realCound, Is.EqualTo(expectedInDb), msg); } } }