Ejemplo n.º 1
0
        public static void ClassInitialize(TestContext testContext)
        {
            Debug.WriteLine("Class Initialize");
            //TODO: pull all const strings from files
            int tryCounter = 0;

            using (ApiSampleDbContext context = new ApiSampleDbContext())
            {
                try
                {
                    tryCounter++;
                    context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, Script_CreateSnapshot);
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    DropDatabase();
                    if (tryCounter <= 2)
                    {
                        context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, Script_CreateSnapshot);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        public PagedSearchResponseDto <List <PersonSearchResultDto> > SearchPeople(PagedSearchDto dto)
        {
            using (ApiSampleDbContext context = new ApiSampleDbContext())
            {
                SqlParameter pageSize = new SqlParameter("@PageSize", dto.PageSize ?? (object)DBNull.Value)
                {
                    DbType = System.Data.DbType.Int32
                };
                SqlParameter pageNumber = new SqlParameter("@PageNumber", dto.PageNumber ?? (object)DBNull.Value)
                {
                    DbType = System.Data.DbType.Int32
                };
                SqlParameter orderBy   = new SqlParameter("@OrderBy", string.IsNullOrEmpty(dto.OrderByColumn) ? (object)DBNull.Value : dto.OrderByColumn);
                SqlParameter orderAsc  = new SqlParameter("@OrderAsc", dto.OrderAscending ?? (object)DBNull.Value);
                SqlParameter totalRows = new SqlParameter("@TotalRows", 0)
                {
                    DbType    = System.Data.DbType.Int32,
                    Direction = System.Data.ParameterDirection.Output
                };

                List <PersonSearchResultDto> results = context.Database.SqlQuery <PersonSearchResultDto>("EXEC dbo.GetPeople @PageSize, @PageNumber, @OrderBy, @OrderAsc, @TotalRows OUTPUT",
                                                                                                         pageSize, pageNumber, orderBy, orderAsc, totalRows).ToList();

                PagedSearchResponseDto <List <PersonSearchResultDto> > response = new PagedSearchResponseDto <List <PersonSearchResultDto> >
                {
                    PageSize       = dto.PageSize,
                    PageNumber     = dto.PageNumber,
                    OrderByColumn  = dto.OrderByColumn,
                    OrderAscending = dto.OrderAscending,
                    TotalRows      = (int?)totalRows.Value,
                    Result         = results
                };
                return(response);
            }
        }
Ejemplo n.º 3
0
 public void TestGetPeople()
 {
     using (ApiSampleDbContext context = new ApiSampleDbContext())
     {
         var people = context.Persons.Take(25);
         Assert.IsTrue(people.Count() == 25);
     }
 }
Ejemplo n.º 4
0
 private static void DropDatabase()
 {
     using (ApiSampleDbContext context = new ApiSampleDbContext())
     {
         try
         {
             context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, Script_DropSnapshot);
         }
         catch (Exception)
         {
             //log exception
         }
     }
 }
Ejemplo n.º 5
0
 public void DeleteAppPeopleTest()
 {
     using (ApiSampleDbContext context = new ApiSampleDbContext())
     {
         var people = context.Persons;
         context.Persons.RemoveRange(people);
         context.SaveChanges();
     }
     //totally new connection
     using (ApiSampleDbContext context = new ApiSampleDbContext())
     {
         var people = context.Persons.ToList();
         Assert.AreEqual(people.Count, 0, "There shouldn't be any people.");
     }
 }
Ejemplo n.º 6
0
        public void TestCleanup()
        {
            Debug.WriteLine("Test Cleanup");

            using (ApiSampleDbContext context = new ApiSampleDbContext("MasterConnection"))
            {
                try
                {
                    context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, Script_RestoreFromSnapshot);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }