Esempio n. 1
0
		public void ReadOnlyMode()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			for (int i = 0; i < 100; i++)
			{
				DataPoint dp = new DataPoint();
				dp.X = i * 0.1M;
				dp.Y = (decimal)System.Math.Cos((double)dp.X);
				s.Save(dp);
			}
			t.Commit();
			s.Close();
	
			AssertInsertCount(100);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();

			// NH-specific: Replace use of Scroll with List
			IList<DataPoint> sr = s.CreateQuery("from DataPoint dp order by dp.X asc")
					.SetReadOnly(true)
					.List<DataPoint>();
			
			int index = 0;

			foreach (DataPoint dp in sr)
			{
				if (++index == 50)
				{
					s.SetReadOnly(dp, false);
				}
				dp.Description = "done!";
			}
			t.Commit();
	
			AssertUpdateCount(1);
			ClearCounts();
	
			s.Clear();
			t = s.BeginTransaction();
			IList single = s.CreateQuery("from DataPoint where description='done!'").List();
			Assert.That(single.Count, Is.EqualTo(1));
			Assert.That(s.CreateQuery("delete from DataPoint").ExecuteUpdate(), Is.EqualTo(100));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			//deletes from Query.executeUpdate() are not tracked
			//AssertDeleteCount(100);
		}
		public void ReadOnlyOnProxies()
		{
			DataPoint dp = null;
			long dpId = -1;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				s.BeginTransaction();
			
				dp = new DataPoint();
				dp.X = 0.1M;
				dp.Y = (decimal)System.Math.Cos((double)dp.X);
				dp.Description = "original";
				s.Save(dp);
				dpId = dp.Id;
				s.Transaction.Commit();
			}
		
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				s.BeginTransaction();
				s.DefaultReadOnly = true;
				Assert.That(s.DefaultReadOnly, Is.True);
				dp = (DataPoint)s.Load<DataPoint>(dpId);
				s.DefaultReadOnly = false;
				Assert.That(NHibernateUtil.IsInitialized(dp), Is.False, "was initialized");
				Assert.That(s.IsReadOnly(dp), Is.True);
				Assert.That(NHibernateUtil.IsInitialized(dp), Is.False, "was initialized during isReadOnly");
				dp.Description = "changed";
				Assert.That(NHibernateUtil.IsInitialized(dp), Is.True, "was not initialized during mod");
				Assert.That(dp.Description, Is.StringMatching("changed"), "desc not changed in memory");
				s.Flush();
				s.Transaction.Commit();
			}
	
			using (ISession s = OpenSession())
			{
				s.BeginTransaction();
				IList list = s.CreateQuery("from DataPoint where description = 'changed'").List();
				Assert.That(list.Count, Is.EqualTo(0), "change written to database");
				s.CreateQuery("delete from DataPoint").ExecuteUpdate();
				s.Transaction.Commit();
			}
		}
Esempio n. 3
0
		public void ReadOnlyOnProxies()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			s.BeginTransaction();
			DataPoint dp = new DataPoint();
			dp.X = 0.1M;
			dp.Y = (decimal)System.Math.Cos((double)dp.X);
			dp.Description = "original";
			s.Save(dp);
			long dpId = dp.Id;
			s.Transaction.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			s.BeginTransaction();
			dp = s.Load<DataPoint>(dpId);
			Assert.That(NHibernateUtil.IsInitialized(dp), Is.False, "was initialized");
			s.SetReadOnly(dp, true);
			Assert.That(NHibernateUtil.IsInitialized(dp), Is.False, "was initialized during SetReadOnly");
			dp.Description = "changed";
			Assert.That(NHibernateUtil.IsInitialized(dp), Is.True, "was not initialized during mod");
			Assert.That(dp.Description, Is.EqualTo("changed"), "desc not changed in memory");
			s.Flush();
			s.Transaction.Commit();
			s.Close();
	
			AssertUpdateCount(0);
	
			s = OpenSession();
			s.BeginTransaction();
			IList list = s.CreateQuery("from DataPoint where Description = 'changed'").List();
			Assert.That(list.Count, Is.EqualTo(0), "change written to database");
			Assert.That(s.CreateQuery("delete from DataPoint").ExecuteUpdate(), Is.EqualTo(1));
			s.Transaction.Commit();
			s.Close();
			
			AssertUpdateCount(0);
			//deletes from Query.executeUpdate() are not tracked
			//AssertDeleteCount(1);
		}
		public void MergeWithReadOnlyProxy()
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					s.Save(dp);
					t.Commit();
				}
			}
	
			dp.Description = "description";
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					s.DefaultReadOnly = true;
					DataPoint dpProxy = s.Load<DataPoint>(dp.Id);
					Assert.That(s.IsReadOnly(dpProxy), Is.True);
					Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.False);
					s.Evict(dpProxy);
					dpProxy = (DataPoint)s.Merge(dpProxy);
					Assert.That(s.IsReadOnly(dpProxy), Is.True);
					Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.False);
					dpProxy = (DataPoint)s.Merge(dp);
					Assert.That(s.IsReadOnly(dpProxy), Is.True);
					Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
					Assert.That(dpProxy.Description, Is.EqualTo("description"));
					s.Evict(dpProxy);
					dpProxy = (DataPoint)s.Merge(dpProxy);
					Assert.That(s.IsReadOnly(dpProxy), Is.True);
					Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
					Assert.That(dpProxy.Description, Is.EqualTo("description"));
					dpProxy.Description = null;
					dpProxy = (DataPoint)s.Merge(dp);
					Assert.That(s.IsReadOnly(dpProxy), Is.True);
					Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
					Assert.That(dpProxy.Description, Is.EqualTo("description"));
					t.Commit();
				}
			}
	
			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					dp = s.Get<DataPoint>(dp.Id);
					Assert.That(dp.Description, Is.Null);
					s.Delete(dp);
					t.Commit();
				}
			}
		}
		public void MergeWithReadOnlyEntity()
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					s.Save(dp);
					t.Commit();
				}
			}
	
			dp.Description = "description";
	
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					s.DefaultReadOnly = true;
					DataPoint dpManaged = s.Get<DataPoint>(dp.Id);
					DataPoint dpMerged = (DataPoint)s.Merge(dp);
					Assert.That(dpManaged, Is.SameAs(dpMerged));
					t.Commit();
				}
			}
	
			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					DataPoint dpManaged = s.Get<DataPoint>(dp.Id);
					Assert.That(dpManaged.Description, Is.Null);
					s.Delete(dpManaged);
					t.Commit();
				}
			}
	
		}
Esempio n. 6
0
		public void MergeWithReadOnlyEntity()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			DataPoint dp = new DataPoint();
			dp.X = 0.1M;
			dp.Y = (decimal)System.Math.Cos((double)dp.X);
			s.Save(dp);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			dp.Description = "description";
	
			s = OpenSession();
			t = s.BeginTransaction();
			DataPoint dpManaged = s.Get<DataPoint>(dp.Id);
			s.SetReadOnly(dpManaged, true);
			DataPoint dpMerged = (DataPoint)s.Merge(dp);
			Assert.That(dpManaged, Is.SameAs(dpMerged));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
	
			s = OpenSession();
			t = s.BeginTransaction();
			dpManaged = s.Get<DataPoint>(dp.Id);
			Assert.That(dpManaged.Description, Is.Null);
			s.Delete(dpManaged);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(1);
		}
Esempio n. 7
0
        public async Task MergeWithReadOnlyProxyAsync()
        {
            DataPoint dp = null;

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    dp   = new DataPoint();
                    dp.X = 0.1M;
                    dp.Y = (decimal)System.Math.Cos((double)dp.X);
                    await(s.SaveAsync(dp));
                    await(t.CommitAsync());
                }
            }

            dp.Description = "description";

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    s.DefaultReadOnly = true;
                    DataPoint dpProxy = await(s.LoadAsync <DataPoint>(dp.Id));
                    Assert.That(s.IsReadOnly(dpProxy), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.False);
                    await(s.EvictAsync(dpProxy));
                    dpProxy = (DataPoint)await(s.MergeAsync(dpProxy));
                    Assert.That(s.IsReadOnly(dpProxy), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.False);
                    dpProxy = (DataPoint)await(s.MergeAsync(dp));
                    Assert.That(s.IsReadOnly(dpProxy), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
                    Assert.That(dpProxy.Description, Is.EqualTo("description"));
                    await(s.EvictAsync(dpProxy));
                    dpProxy = (DataPoint)await(s.MergeAsync(dpProxy));
                    Assert.That(s.IsReadOnly(dpProxy), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
                    Assert.That(dpProxy.Description, Is.EqualTo("description"));
                    dpProxy.Description = null;
                    dpProxy             = (DataPoint)await(s.MergeAsync(dp));
                    Assert.That(s.IsReadOnly(dpProxy), Is.True);
                    Assert.That(NHibernateUtil.IsInitialized(dpProxy), Is.True);
                    Assert.That(dpProxy.Description, Is.EqualTo("description"));
                    await(t.CommitAsync());
                }
            }

            using (ISession s = OpenSession())
            {
                using (ITransaction t = s.BeginTransaction())
                {
                    dp = await(s.GetAsync <DataPoint>(dp.Id));
                    Assert.That(dp.Description, Is.Null);
                    await(s.DeleteAsync(dp));
                    await(t.CommitAsync());
                }
            }
        }
		public void ReadOnlyProxyRefreshDetached()
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.Description = "original";
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					s.Save(dp);
					t.Commit();
				}
			}
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					s.DefaultReadOnly = true;
					
					dp = s.Load<DataPoint>(dp.Id);
					Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
					Assert.That(s.IsReadOnly(dp), Is.True);
					s.Evict(dp);

					s.Refresh(dp);
					Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
					s.DefaultReadOnly = false;
					Assert.That(s.IsReadOnly(dp), Is.True);
					s.Evict(dp);
	
					s.Refresh(dp);
					Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
					Assert.That(s.IsReadOnly(dp), Is.False);
					Assert.That(s.IsReadOnly(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementation()), Is.False);
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));
					Assert.That(NHibernateUtil.IsInitialized(dp), Is.True);
					s.Evict(dp);

					s.Refresh(dp);
					Assert.That(dp.Description, Is.EqualTo("original"));
					Assert.That(s.IsReadOnly(dp), Is.False);
					Assert.That(s.IsReadOnly(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementation()), Is.False);
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));
					s.DefaultReadOnly = true;
					s.Evict(dp);

					s.Refresh(dp);
					Assert.That(dp.Description, Is.EqualTo("original"));
					Assert.That(s.IsReadOnly(dp), Is.True);
					Assert.That(s.IsReadOnly(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementation()), Is.True);
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));

					t.Commit();
				}

				s.Clear();
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = s.Get<DataPoint>(dp.Id);
					Assert.That(dp.Description, Is.EqualTo("original"));
					s.Delete(dp);
					t.Commit();
				}
			}
		}
		public void QueryModifiableIterate()
		{
			long lastDataPointId = 0;
			int nExpectedChanges = 0;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					DataPoint dp = null;
					
					for (int i = 0; i < 100; i++)
					{
						dp = new DataPoint();
						dp.X = 0.1M * i;
						dp.Y = (decimal)System.Math.Cos((double)dp.X);
						s.Save(dp);
					}
					t.Commit();
					
					lastDataPointId = dp.Id;
				}
			}
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					s.DefaultReadOnly = true;

					IQuery query = s.CreateQuery("from DataPoint dp order by dp.X asc");
					
					Assert.That(query.IsReadOnly, Is.True);
					s.DefaultReadOnly = false;
					Assert.That(query.IsReadOnly, Is.False);
					s.DefaultReadOnly = true;
					Assert.That(query.IsReadOnly, Is.True);
					query.SetReadOnly(false);
					Assert.That(query.IsReadOnly, Is.False);
					s.DefaultReadOnly = false;
					Assert.That(query.IsReadOnly, Is.False);
					s.DefaultReadOnly = true;
					Assert.That(query.IsReadOnly, Is.False);
					query.SetReadOnly(true);
					Assert.That(query.IsReadOnly, Is.True);
					s.DefaultReadOnly = false;
					Assert.That(query.IsReadOnly, Is.True);
					
					query.SetReadOnly(false);
					Assert.That(query.IsReadOnly, Is.False);
					s.DefaultReadOnly = true;
					Assert.That(s.DefaultReadOnly, Is.True);
					
					IEnumerator<DataPoint> it = query.Enumerable<DataPoint>().GetEnumerator();
					Assert.That(query.IsReadOnly, Is.False);
					DataPoint dpLast = s.Get<DataPoint>(lastDataPointId);
					Assert.That(s.IsReadOnly(dpLast), Is.True);
					query.SetReadOnly(true);
					Assert.That(query.IsReadOnly, Is.True);
					Assert.That(s.DefaultReadOnly, Is.True);
		
					int i = 0;
					
					while (it.MoveNext())
					{
						Assert.That(s.DefaultReadOnly, Is.True);
						DataPoint dp = it.Current;
						Assert.That(s.DefaultReadOnly, Is.True);
						
						if (dp.Id == dpLast.Id)
						{
							//dpLast existed in the session before executing the read-only query
							Assert.That(s.IsReadOnly(dp), Is.True);
						}
						else
						{
							Assert.That(s.IsReadOnly(dp), Is.False);
						}
						
						if (++i == 50)
						{
							s.SetReadOnly(dp, true);
							nExpectedChanges = (dp == dpLast ? 99 : 98);
						}
						
						dp.Description = "done!";
					}
					
					Assert.That(s.DefaultReadOnly, Is.True);
							
					t.Commit();
				}
				
				s.Clear();
			
				using (ITransaction t = s.BeginTransaction())
				{
					try
					{
						IList list = s.CreateQuery("from DataPoint where Description = 'done!'").List();
						Assert.That(list.Count, Is.EqualTo(nExpectedChanges));
					}
					finally
					{
						// cleanup
						s.CreateQuery("delete from DataPoint").ExecuteUpdate();
					}
					
					t.Commit();
				}
			}
		}
Esempio n. 10
0
		public void ReadOnlyModeAutoFlushOnQuery()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();

			for (int i = 0; i < 100; i++)
			{
				DataPoint dp = new DataPoint();
				dp.X = i * 0.1M;
				dp.Y = (decimal)System.Math.Cos((double)dp.X);
				s.Save(dp);
			}
	
			AssertInsertCount(0);
			AssertUpdateCount(0);

			// NH-specific: Replace use of Scroll with List
			IList<DataPoint> sr = s.CreateQuery("from DataPoint dp order by dp.X asc")
					.SetReadOnly(true)
					.List<DataPoint>();
	
			AssertInsertCount(100);
			AssertUpdateCount(0);
			ClearCounts();
	
			foreach(DataPoint dp in sr)
			{
				Assert.That(s.IsReadOnly(dp), Is.False);
				s.Delete(dp);
			}
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(100);
		}
		private DataPoint CreateDataPoint(CacheMode mode)
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					dp.Description = "original";
					s.Save(dp);
					t.Commit();
				}
			}
			
			return dp;
		}
Esempio n. 12
0
        public async Task ReadOnlyProxyRefreshDetachedAsync()
        {
            DataPoint dp = null;

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    dp             = new DataPoint();
                    dp.Description = "original";
                    dp.X           = 0.1M;
                    dp.Y           = (decimal)System.Math.Cos((double)dp.X);
                    await(s.SaveAsync(dp));
                    await(t.CommitAsync());
                }
            }

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    s.DefaultReadOnly = true;

                    dp = await(s.LoadAsync <DataPoint>(dp.Id));
                    Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
                    Assert.That(s.IsReadOnly(dp), Is.True);
                    await(s.EvictAsync(dp));

                    await(s.RefreshAsync(dp));
                    Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
                    s.DefaultReadOnly = false;
                    Assert.That(s.IsReadOnly(dp), Is.True);
                    await(s.EvictAsync(dp));

                    await(s.RefreshAsync(dp));
                    Assert.That(NHibernateUtil.IsInitialized(dp), Is.False);
                    Assert.That(s.IsReadOnly(dp), Is.False);
                    Assert.That(s.IsReadOnly(await(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementationAsync(CancellationToken.None))), Is.False);
                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));
                    Assert.That(NHibernateUtil.IsInitialized(dp), Is.True);
                    await(s.EvictAsync(dp));

                    await(s.RefreshAsync(dp));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    Assert.That(s.IsReadOnly(dp), Is.False);
                    Assert.That(s.IsReadOnly(await(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementationAsync(CancellationToken.None))), Is.False);
                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));
                    s.DefaultReadOnly = true;
                    await(s.EvictAsync(dp));

                    await(s.RefreshAsync(dp));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    Assert.That(s.IsReadOnly(dp), Is.True);
                    Assert.That(s.IsReadOnly(await(((INHibernateProxy)dp).HibernateLazyInitializer.GetImplementationAsync(CancellationToken.None))), Is.True);
                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));

                    await(t.CommitAsync());
                }

                s.Clear();

                using (ITransaction t = s.BeginTransaction())
                {
                    dp = await(s.GetAsync <DataPoint>(dp.Id));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    await(s.DeleteAsync(dp));
                    await(t.CommitAsync());
                }
            }
        }
Esempio n. 13
0
        public async Task ReadOnlyRefreshDetachedAsync()
        {
            DataPoint dp = null;

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    dp             = new DataPoint();
                    dp.Description = "original";
                    dp.X           = 0.1M;
                    dp.Y           = (decimal)System.Math.Cos((double)dp.X);
                    await(s.SaveAsync(dp));
                    await(t.CommitAsync());
                }
            }

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    s.DefaultReadOnly = false;

                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));
                    await(s.RefreshAsync(dp));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    Assert.That(s.IsReadOnly(dp), Is.False);

                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));
                    await(s.EvictAsync(dp));
                    await(s.RefreshAsync(dp));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    Assert.That(s.IsReadOnly(dp), Is.False);

                    dp.Description = "changed";
                    Assert.That(dp.Description, Is.EqualTo("changed"));
                    s.DefaultReadOnly = true;
                    await(s.EvictAsync(dp));
                    await(s.RefreshAsync(dp));
                    Assert.That(dp.Description, Is.EqualTo("original"));
                    Assert.That(s.IsReadOnly(dp), Is.True);

                    dp.Description = "changed";

                    await(t.CommitAsync());
                }

                s.Clear();

                using (ITransaction t = s.BeginTransaction())
                {
                    dp = await(s.GetAsync <DataPoint>(dp.Id));
                    Assert.That(dp.Description, Is.EqualTo("original"));

                    await(s.DeleteAsync(dp));
                    await(t.CommitAsync());
                }
            }
        }
Esempio n. 14
0
        public async Task QueryModifiableIterateAsync()
        {
            long lastDataPointId  = 0;
            int  nExpectedChanges = 0;

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    DataPoint dp = null;

                    for (int i = 0; i < 100; i++)
                    {
                        dp   = new DataPoint();
                        dp.X = 0.1M * i;
                        dp.Y = (decimal)System.Math.Cos((double)dp.X);
                        await(s.SaveAsync(dp));
                    }
                    await(t.CommitAsync());

                    lastDataPointId = dp.Id;
                }
            }

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    s.DefaultReadOnly = true;

                    IQuery query = s.CreateQuery("from DataPoint dp order by dp.X asc");

                    Assert.That(query.IsReadOnly, Is.True);
                    s.DefaultReadOnly = false;
                    Assert.That(query.IsReadOnly, Is.False);
                    s.DefaultReadOnly = true;
                    Assert.That(query.IsReadOnly, Is.True);
                    query.SetReadOnly(false);
                    Assert.That(query.IsReadOnly, Is.False);
                    s.DefaultReadOnly = false;
                    Assert.That(query.IsReadOnly, Is.False);
                    s.DefaultReadOnly = true;
                    Assert.That(query.IsReadOnly, Is.False);
                    query.SetReadOnly(true);
                    Assert.That(query.IsReadOnly, Is.True);
                    s.DefaultReadOnly = false;
                    Assert.That(query.IsReadOnly, Is.True);

                    query.SetReadOnly(false);
                    Assert.That(query.IsReadOnly, Is.False);
                    s.DefaultReadOnly = true;
                    Assert.That(s.DefaultReadOnly, Is.True);

                    IEnumerator <DataPoint> it = (await(query.EnumerableAsync <DataPoint>())).GetEnumerator();
                    Assert.That(query.IsReadOnly, Is.False);
                    DataPoint dpLast = await(s.GetAsync <DataPoint>(lastDataPointId));
                    Assert.That(s.IsReadOnly(dpLast), Is.True);
                    query.SetReadOnly(true);
                    Assert.That(query.IsReadOnly, Is.True);
                    Assert.That(s.DefaultReadOnly, Is.True);

                    int i = 0;

                    while (it.MoveNext())
                    {
                        Assert.That(s.DefaultReadOnly, Is.True);
                        DataPoint dp = it.Current;
                        Assert.That(s.DefaultReadOnly, Is.True);

                        if (dp.Id == dpLast.Id)
                        {
                            //dpLast existed in the session before executing the read-only query
                            Assert.That(s.IsReadOnly(dp), Is.True);
                        }
                        else
                        {
                            Assert.That(s.IsReadOnly(dp), Is.False);
                        }

                        if (++i == 50)
                        {
                            s.SetReadOnly(dp, true);
                            nExpectedChanges = (dp == dpLast ? 99 : 98);
                        }

                        dp.Description = "done!";
                    }

                    Assert.That(s.DefaultReadOnly, Is.True);

                    await(t.CommitAsync());
                }

                s.Clear();

                using (ITransaction t = s.BeginTransaction())
                {
                    try
                    {
                        IList list = await(s.CreateQuery("from DataPoint where Description = 'done!'").ListAsync());
                        Assert.That(list.Count, Is.EqualTo(nExpectedChanges));
                    }
                    finally
                    {
                        // cleanup
                        await(s.CreateQuery("delete from DataPoint").ExecuteUpdateAsync());
                    }

                    await(t.CommitAsync());
                }
            }
        }
Esempio n. 15
0
        public async Task ModifiableSessionDefaultQueryReadOnlySessionIterateAsync()
        {
            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;
                using (ITransaction t = s.BeginTransaction())
                {
                    for (int i = 0; i < 100; i++)
                    {
                        DataPoint dp = new DataPoint();
                        dp.X = 0.1M * i;
                        dp.Y = (decimal)System.Math.Cos((double)dp.X);
                        await(s.SaveAsync(dp));
                    }
                    await(t.CommitAsync());
                }
            }

            using (ISession s = OpenSession())
            {
                s.CacheMode = CacheMode.Ignore;

                using (ITransaction t = s.BeginTransaction())
                {
                    s.DefaultReadOnly = false;

                    IQuery query = s.CreateQuery("from DataPoint dp order by dp.X asc");

                    s.DefaultReadOnly = true;
                    IEnumerable enumerable = await(query.EnumerableAsync());
                    s.DefaultReadOnly = false;

                    int i = 0;
                    foreach (DataPoint dp in enumerable)
                    {
                        if (++i == 50)
                        {
                            s.SetReadOnly(dp, false);
                        }
                        dp.Description = "done!";
                    }
                    await(t.CommitAsync());
                }

                s.Clear();

                using (ITransaction t = s.BeginTransaction())
                {
                    try
                    {
                        IList single = await(s.CreateQuery("from DataPoint where Description = 'done!'").ListAsync());
                        Assert.That(single.Count, Is.EqualTo(1));
                    }
                    finally
                    {
                        // cleanup
                        await(s.CreateQuery("delete from DataPoint").ExecuteUpdateAsync());
                    }

                    await(t.CommitAsync());
                }
            }
        }
		public void ModifiableSessionDefaultQueryReadOnlySessionIterate()
		{
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				using (ITransaction t = s.BeginTransaction())
				{
					for (int i = 0; i < 100; i++)
					{
						DataPoint dp = new DataPoint();
						dp.X = 0.1M * i;
						dp.Y = (decimal)System.Math.Cos((double)dp.X);
						s.Save(dp);
					}
					t.Commit();
				}
			}
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					s.DefaultReadOnly = false;
					
					IQuery query = s.CreateQuery("from DataPoint dp order by dp.X asc");
					
					s.DefaultReadOnly = true;
					IEnumerable enumerable = query.Enumerable();
					s.DefaultReadOnly = false;
					
					int i = 0;
					foreach (DataPoint dp in enumerable)
					{
						if (++i == 50)
						{
							s.SetReadOnly(dp, false);
						}
						dp.Description = "done!";
					}
					t.Commit();
				}
				
				s.Clear();
			
				using (ITransaction t = s.BeginTransaction())
				{
					try
					{
						IList single = s.CreateQuery("from DataPoint where Description = 'done!'").List();
						Assert.That(single.Count, Is.EqualTo(1));
					}
					finally
					{
						// cleanup
						s.CreateQuery("delete from DataPoint").ExecuteUpdate();
					}
					
					t.Commit();
				}
			}
		}
Esempio n. 17
0
		public void ReadOnlyRefreshDetached()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			DataPoint dp = new DataPoint();
			dp.Description = "original";
			dp.X = 0.1M;
			dp.Y = (decimal)System.Math.Cos((double)dp.X);
			s.Save(dp);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			dp.Description = "changed";
			Assert.That(dp.Description, Is.EqualTo("changed"));
			s.Refresh(dp);
			Assert.That(dp.Description, Is.EqualTo("original"));
			Assert.That(s.IsReadOnly(dp), Is.False);
			s.SetReadOnly(dp, true);
			dp.Description = "changed";
			Assert.That(dp.Description, Is.EqualTo("changed"));
			s.Evict(dp);
			s.Refresh(dp);
			Assert.That(dp.Description, Is.EqualTo("original"));
			Assert.That(s.IsReadOnly(dp), Is.False);
			t.Commit();
	
			AssertInsertCount(0);
			AssertUpdateCount(0);
	
			s.Clear();
			t = s.BeginTransaction();
			dp = s.Get<DataPoint>(dp.Id);
			Assert.That(dp.Description, Is.EqualTo("original"));
			s.Delete(dp);;
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(1);
		}
Esempio n. 18
0
		public void ReadOnlyGetModifyAndDelete()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			DataPoint dp = new DataPoint();
			dp.X = 0.1M;
			dp.Y = (decimal)System.Math.Cos((double)dp.X);
			s.Save(dp);
			t.Commit();
			s.Close();
	
			AssertInsertCount(1);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			dp = s.Get<DataPoint>(dp.Id);
			s.SetReadOnly(dp, true);
			dp.Description = "a DataPoint";
			s.Delete(dp);
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
			AssertDeleteCount(1);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			IList list = s.CreateQuery("from DataPoint where Description='done!'").List();
			Assert.That(list.Count, Is.EqualTo(0));
			t.Commit();
			s.Close();
		}
		public void ReadOnlyRefresh()
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.Description = "original";
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					s.Save(dp);
					t.Commit();
				}
			}
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				s.DefaultReadOnly = true;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = s.Get<DataPoint>(dp.Id);
					Assert.That(s.IsReadOnly(dp), Is.True);
					Assert.That(dp.Description, Is.EqualTo("original"));
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));
					s.Refresh(dp);
					Assert.That(s.IsReadOnly(dp), Is.True);
					Assert.That(dp.Description, Is.EqualTo("original"));
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));
					s.DefaultReadOnly = false;
					s.Refresh(dp);
					Assert.That(s.IsReadOnly(dp), Is.True);
					Assert.That(dp.Description, Is.EqualTo("original"));
					dp.Description = "changed";
					Assert.That(dp.Description, Is.EqualTo("changed"));
					
					t.Commit();
				}
				
				s.Clear();
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = s.Get<DataPoint>(dp.Id);
					Assert.That(dp.Description, Is.EqualTo("original"));
					s.Delete(dp);
					t.Commit();
				}
			}
		}
Esempio n. 20
0
		public void ModifiableModeWithExistingReadOnlyEntity()
		{
			ClearCounts();
	
			ISession s = OpenSession();
			ITransaction t = s.BeginTransaction();
			DataPoint dp = null;
			for (int i = 0; i < 100; i++)
			{
				dp = new DataPoint();
				dp.X = i * 0.1M;
				dp.Y = (decimal)System.Math.Cos((double)dp.X);
				s.Save(dp);
			}
			t.Commit();
			s.Close();
	
			AssertInsertCount(100);
			AssertUpdateCount(0);
			ClearCounts();
	
			s = OpenSession();
			t = s.BeginTransaction();
			DataPoint dpLast = s.Get<DataPoint>(dp.Id);
			Assert.That(s.IsReadOnly(dpLast), Is.False);
			s.SetReadOnly(dpLast, true);
			Assert.That(s.IsReadOnly(dpLast), Is.True);
			dpLast.Description = "oy";

			AssertUpdateCount(0);
	
			// NH-specific: Replace use of Scroll with List
			IList<DataPoint> sr = s.CreateQuery("from DataPoint dp order by dp.X asc")
					.SetReadOnly(false)
					.List<DataPoint>();

			int nExpectedChanges = 0;
			int index = 0;
			
			foreach(DataPoint nextDp in sr)
			{
				if (nextDp.Id == dpLast.Id )
				{
					//dpLast existed in the session before executing the read-only query
					Assert.That(s.IsReadOnly(nextDp), Is.True);
				}
				else
				{
					Assert.That(s.IsReadOnly(nextDp), Is.False);
				}
				if (++index == 50)
				{
					s.SetReadOnly(nextDp, true);
					nExpectedChanges = (nextDp == dpLast ? 99 : 98);
				}
				nextDp.Description = "done!";
			}
			t.Commit();
			s.Clear();
	
			AssertUpdateCount(nExpectedChanges);
			ClearCounts();
	
			t = s.BeginTransaction();
			IList list = s.CreateQuery("from DataPoint where Description='done!'").List();
			Assert.That(list.Count, Is.EqualTo(nExpectedChanges));
			Assert.That(s.CreateQuery("delete from DataPoint").ExecuteUpdate(), Is.EqualTo(100));
			t.Commit();
			s.Close();
	
			AssertUpdateCount(0);
		}
		public void ReadOnlyGetModifyAndDelete()
		{
			DataPoint dp = null;
			
			using (ISession s = OpenSession())
			{
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = new DataPoint();
					dp.X = 0.1M;
					dp.Y = (decimal)System.Math.Cos((double)dp.X);
					s.Save(dp);
					t.Commit();
				}
			}
	
			using (ISession s = OpenSession())
			{
				s.DefaultReadOnly = true;
				s.CacheMode = CacheMode.Ignore;
				
				using (ITransaction t = s.BeginTransaction())
				{
					dp = s.Get<DataPoint>(dp.Id);
					s.DefaultReadOnly = true;
					dp.Description = "a DataPoint";
					s.Delete(dp);
					t.Commit();
				}
			}
	
			using (ISession s = OpenSession())
			{
				using (ITransaction t = s.BeginTransaction())
				{
					IList list = s.CreateQuery("from DataPoint where id=" + dp.Id ).List();
					Assert.That(list.Count, Is.EqualTo(0));
					t.Commit();
				}
			}
		}
Esempio n. 22
0
        public async Task ModifiableModeWithExistingReadOnlyEntityAsync()
        {
            ClearCounts();

            ISession     s  = OpenSession();
            ITransaction t  = s.BeginTransaction();
            DataPoint    dp = null;

            for (int i = 0; i < 100; i++)
            {
                dp   = new DataPoint();
                dp.X = i * 0.1M;
                dp.Y = (decimal)System.Math.Cos((double)dp.X);
                await(s.SaveAsync(dp));
            }
            await(t.CommitAsync());
            s.Close();

            AssertInsertCount(100);
            AssertUpdateCount(0);
            ClearCounts();

            s = OpenSession();
            t = s.BeginTransaction();
            DataPoint dpLast = await(s.GetAsync <DataPoint>(dp.Id));

            Assert.That(s.IsReadOnly(dpLast), Is.False);
            s.SetReadOnly(dpLast, true);
            Assert.That(s.IsReadOnly(dpLast), Is.True);
            dpLast.Description = "oy";

            AssertUpdateCount(0);

            // NH-specific: Replace use of Scroll with List
            IList <DataPoint> sr = await(s.CreateQuery("from DataPoint dp order by dp.X asc")
                                         .SetReadOnly(false)
                                         .ListAsync <DataPoint>());

            int nExpectedChanges = 0;
            int index            = 0;

            foreach (DataPoint nextDp in sr)
            {
                if (nextDp.Id == dpLast.Id)
                {
                    //dpLast existed in the session before executing the read-only query
                    Assert.That(s.IsReadOnly(nextDp), Is.True);
                }
                else
                {
                    Assert.That(s.IsReadOnly(nextDp), Is.False);
                }
                if (++index == 50)
                {
                    s.SetReadOnly(nextDp, true);
                    nExpectedChanges = (nextDp == dpLast ? 99 : 98);
                }
                nextDp.Description = "done!";
            }
            await(t.CommitAsync());
            s.Clear();

            AssertUpdateCount(nExpectedChanges);
            ClearCounts();

            t = s.BeginTransaction();
            IList list = await(s.CreateQuery("from DataPoint where Description='done!'").ListAsync());

            Assert.That(list.Count, Is.EqualTo(nExpectedChanges));
            Assert.That(await(s.CreateQuery("delete from DataPoint").ExecuteUpdateAsync()), Is.EqualTo(100));
            await(t.CommitAsync());
            s.Close();

            AssertUpdateCount(0);
        }