Ejemplo n.º 1
0
        public void AsTypedEnumerableAsync()
        {
            SetupAndAssert(async dbCmd =>
            {
                IManagedEnumerable <CategoryAsClass> result = await dbCmd
                                                              .CommandType(CommandType.Text)
                                                              .CommandText(@"select * from main.[Category]")
                                                              .AsEnumerableAsync <CategoryAsClass>()
                                                              .ConfigureAwait(false);

                ManagedEnumerable <CategoryAsClass> enumerable = result as ManagedEnumerable <CategoryAsClass>;
                Assert.False(enumerable.Diposed);

                using (result)
                {
                    Assert.NotNull(result);
                    int count = 0;

                    foreach (CategoryAsClass item in result)
                    {
                        count++;
                        Assert.True(item.Id >= 0);
                        Assert.True(!string.IsNullOrEmpty(item.CategoryName));
                        Assert.True(!string.IsNullOrEmpty(item.Description));
                    }

                    Assert.Equal(8, count);

                    // Command and Reader should be cleaned up at this point.
                    Assert.ThrowsAny <NullReferenceException>(() => result.Count());
                    Assert.True(enumerable.Diposed);
                }
            });
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Returns the only element of a sequence, or a default value if the sequence is
 /// empty; this method throws an exception if there is more than one element in the
 /// sequence.  Cleans up all db resources.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source"></param>
 /// <returns></returns>
 public static T ExecuteSingleOrDefault <T>(
     this IManagedEnumerable <T> source)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     using (source)
     {
         return(source.SingleOrDefault());
     }
 }
Ejemplo n.º 3
0
        public void ExecuteFirst_NoResults()
        {
            SetupAndAssert(dbCmd =>
            {
                IManagedEnumerable <dynamic> result = dbCmd
                                                      .CommandType(CommandType.Text)
                                                      .CommandText(@"select * from main.[Category] where Id = 20")
                                                      .AsEnumerable();

                ManagedEnumerable enumerable = result as ManagedEnumerable;
                Assert.False(enumerable.Diposed);

                Assert.ThrowsAny <InvalidOperationException>(() => result.ExecuteFirst());

                Assert.True(enumerable.Diposed);
            });
        }
Ejemplo n.º 4
0
        public void ExecuteSingle()
        {
            SetupAndAssert(dbCmd =>
            {
                IManagedEnumerable <dynamic> result = dbCmd
                                                      .CommandType(CommandType.Text)
                                                      .CommandText(@"select * from main.[Category] where Id = 1")
                                                      .AsEnumerable();

                ManagedEnumerable enumerable = result as ManagedEnumerable;
                Assert.False(enumerable.Diposed);

                var tmp = result.ExecuteSingle();

                Assert.NotNull(tmp);
                Assert.True(enumerable.Diposed);
            });
        }
Ejemplo n.º 5
0
        public void AsDynamicEnumerableAsync_Cancel()
        {
            SetupAndAssert(async dbCmd =>
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                CancellationToken token     = cts.Token;

                IManagedEnumerable <dynamic> result = await dbCmd
                                                      .CommandType(CommandType.Text)
                                                      .CommandText(@"select * from main.[Category]")
                                                      .CancellationToken(token)
                                                      .AsEnumerableAsync()
                                                      .ConfigureAwait(false);

                ManagedEnumerable enumerable = result as ManagedEnumerable;
                Assert.False(enumerable.Diposed);

                using (result)
                {
                    Assert.NotNull(result);
                    int count = 0;

                    foreach (dynamic item in result)
                    {
                        count++;
                        Assert.IsType <long>(item.Id);
                        Assert.IsType <string>(item.CategoryName);
                        Assert.IsType <string>(item.Description);

                        Assert.True(item.Id >= 0);
                        Assert.True(!string.IsNullOrEmpty(item.CategoryName));
                        Assert.True(!string.IsNullOrEmpty(item.Description));
                        cts.Cancel();
                    }

                    Assert.Equal(1, count);

                    // Command and Reader should be cleaned up at this point.
                    Assert.ThrowsAny <NullReferenceException>(() => result.Count());
                    Assert.True(enumerable.Diposed);
                }
            });
        }
Ejemplo n.º 6
0
        public void AsTypedEnumerable_Cancel()
        {
            SetupAndAssert(dbCmd =>
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                CancellationToken token     = cts.Token;

                IManagedEnumerable <CategoryAsClass> result = dbCmd
                                                              .CommandType(CommandType.Text)
                                                              .CommandText(@"select * from main.[Category]")
                                                              .CancellationToken(token)
                                                              .AsEnumerable <CategoryAsClass>();

                ManagedEnumerable <CategoryAsClass> enumerable = result as ManagedEnumerable <CategoryAsClass>;
                Assert.False(enumerable.Diposed);

                using (result)
                {
                    Assert.NotNull(result);
                    int count = 0;

                    foreach (CategoryAsClass item in result)
                    {
                        count++;
                        Assert.True(item.Id >= 0);
                        Assert.True(!string.IsNullOrEmpty(item.CategoryName));
                        Assert.True(!string.IsNullOrEmpty(item.Description));
                        cts.Cancel();
                    }

                    Assert.Equal(1, count);;

                    // Command and Reader should be cleaned up at this point.
                    Assert.ThrowsAny <NullReferenceException>(() => result.Count());
                    Assert.True(enumerable.Diposed);
                }
            });
        }
Ejemplo n.º 7
0
        public void AsDynamicEnumerable()
        {
            SetupAndAssert(dbCmd =>
            {
                IManagedEnumerable <dynamic> result = dbCmd
                                                      .CommandType(CommandType.Text)
                                                      .CommandText(@"select * from main.[Category]")
                                                      .AsEnumerable();

                ManagedEnumerable enumerable = result as ManagedEnumerable;
                Assert.False(enumerable.Diposed);

                using (result)
                {
                    Assert.NotNull(result);
                    int count = 0;

                    foreach (dynamic item in result)
                    {
                        count++;
                        Assert.IsType <long>(item.Id);
                        Assert.IsType <string>(item.CategoryName);
                        Assert.IsType <string>(item.Description);

                        Assert.True(item.Id >= 0);
                        Assert.True(!string.IsNullOrEmpty(item.CategoryName));
                        Assert.True(!string.IsNullOrEmpty(item.Description));
                    }

                    Assert.Equal(8, count);

                    // Command and Reader should be cleaned up at this point.
                    Assert.ThrowsAny <NullReferenceException>(() => result.Count());
                    Assert.True(enumerable.Diposed);
                }
            });
        }