Exemplo n.º 1
0
 public void Provider_Property()
 {
     using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
     {
         Assert.Equal(repository.DataContext.GetTable <TestEntity>().AsQueryable().Provider.ToString(), repository.Provider.ToString());
     }
 }
Exemplo n.º 2
0
 public void Dispose()
 {
     using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
     {
         repository.DeleteAll().Commit();
     }
 }
Exemplo n.º 3
0
 public void ElementType_Property()
 {
     using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
     {
         Assert.True(ReferenceEquals(repository.DataContext.GetTable <TestEntity>().AsQueryable().ElementType, repository.ElementType));
     }
 }
Exemplo n.º 4
0
 public void Transaction_Method()
 {
     using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
     {
         Assert.NotNull(repository.Transaction());
     }
 }
Exemplo n.º 5
0
        public void Persist_Method()
        {
            Assert.Throws <ArgumentNullException>(() => new LinqToSqlRepository <TestEntity>(this.Connection()).Persist(null));

            var entity = new TestEntity {
                Name = "first"
            };

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.False(repository.Any());

                Assert.True(ReferenceEquals(repository.Persist(entity), repository));
                repository.Commit();
                entity = repository.Single();
                Assert.NotEqual(0, entity.Id);
                Assert.Equal("first", entity.Name);

                entity.Name = "second";
                repository.Commit();
                entity = repository.Single();
                Assert.NotEqual(0, entity.Id);
                Assert.Equal("second", entity.Name);
                Assert.Equal(1, repository.Count(x => x.Name == "second"));
                Assert.Equal("second", repository.Single(x => x.Name == "second").Name);
            }
        }
Exemplo n.º 6
0
        public void Delete_Method()
        {
            Assert.Throws <ArgumentNullException>(() => new LinqToSqlRepository <TestEntity>(this.Connection()).Delete(null));

            var entity = new TestEntity();

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.Throws <InvalidOperationException>(() => repository.Delete(entity));
                Assert.False(repository.Persist(entity).Delete(entity).Commit().Any());
            }
        }
Exemplo n.º 7
0
        public void Dispose_Method()
        {
            var entity = new TestEntity();

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                repository.Persist(entity).Dispose();
                Assert.Throws <ObjectDisposedException>(() => repository.Single());

                repository.Dispose();
            }
        }
Exemplo n.º 8
0
        public void DeleteAll_Method()
        {
            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.True(ReferenceEquals(repository.DeleteAll(), repository));
                Assert.False(repository.Commit().Any());

                repository.Persist(new TestEntity()).Persist(new TestEntity());
                Assert.Equal(2, repository.Commit().Count());

                repository.DeleteAll();
                Assert.False(repository.Commit().Any());
            }
        }
Exemplo n.º 9
0
        public void GetEnumerator_Method()
        {
            var entity = new TestEntity();

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.False(repository.GetEnumerator().MoveNext());

                repository.Persist(entity).Commit();
                var enumerator = repository.GetEnumerator();
                Assert.True(enumerator.MoveNext());
                Assert.False(enumerator.MoveNext());
            }
        }
Exemplo n.º 10
0
        public void Commit_Method()
        {
            var entity = new TestEntity();

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.False(repository.Any());

                Assert.True(ReferenceEquals(repository.Persist(entity).Commit(), repository));
                Assert.True(ReferenceEquals(repository.Single(), entity));

                repository.Delete(entity);
                Assert.True(ReferenceEquals(repository.Single(), entity));

                repository.Commit();
                Assert.False(repository.Any());
            }
        }
Exemplo n.º 11
0
        public void Constructors()
        {
            Assert.Throws <ArgumentNullException>(() => new LinqToSqlRepository <TestEntity>((string)null));
            Assert.Throws <ArgumentException>(() => new LinqToSqlRepository <TestEntity>(string.Empty));
            Assert.Throws <ArgumentNullException>(() => new LinqToSqlRepository <TestEntity>((IDbConnection)null));

            using (var repository = new LinqToSqlRepository <TestEntity>(this.connectionString))
            {
                Assert.Equal(ConnectionState.Closed, repository.DataContext.Connection.State);
                Assert.True(repository.Field("ownsConnection").To <bool>());
            }

            using (var connection = this.Connection())
            {
                using (var repository = new LinqToSqlRepository <TestEntity>(connection))
                {
                    Assert.True(ReferenceEquals(repository.DataContext.Connection, connection));
                    Assert.False(repository.Field("ownsConnection").To <bool>());
                }
                Assert.Equal(ConnectionState.Open, connection.State);
            }
        }
Exemplo n.º 12
0
        public void Refresh_Method()
        {
            Assert.Throws <ArgumentNullException>(() => new LinqToSqlRepository <TestEntity>(this.Connection()).Refresh(null));

            var entity = new TestEntity {
                Name = "first"
            };

            using (var repository = new LinqToSqlRepository <TestEntity>(this.Connection()))
            {
                Assert.Throws <ArgumentException>(() => repository.Refresh(entity));

                repository.Persist(entity).Commit();
                var originalId   = entity.Id;
                var originalName = entity.Name;

                entity.Name = "second";
                Assert.True(ReferenceEquals(repository.Refresh(entity), repository));
                Assert.Equal(originalId, entity.Id);
                Assert.Equal(originalName, entity.Name);
            }
        }