Ejemplo n.º 1
0
        public static PublisherEntity MockOne()
        {
            PublisherEntity entity = new PublisherEntity();

            entity.Guid  = SecurityUtil.CreateUniqueToken();
            entity.Type  = PublisherType.Online;
            entity.Name  = "中文名字";
            entity.Books = new List <string>()
            {
                "Cat", "Dog"
            };
            entity.BookAuthors = new Dictionary <string, Author>()
            {
                { "Cat", new Author()
                  {
                      Mobile = "111", Name = "BB"
                  } },
                { "Dog", new Author()
                  {
                      Mobile = "222", Name = "sx"
                  } }
            };

            return(entity);
        }
Ejemplo n.º 2
0
        public void Test_4_Add_PublisherEntity()
        {
            TransactionContext tContext = database.BeginTransaction <PublisherEntity>(isolationLevel);

            try
            {
                for (int i = 0; i < 10; ++i)
                {
                    PublisherEntity entity = Mocker.MockOne();

                    DatabaseResult result = database.Add(entity, tContext);

                    if (!result.IsSucceeded())
                    {
                        output.WriteLine(result.Exception?.Message);
                        throw new Exception();
                    }

                    Assert.True(result.IsSucceeded());
                }

                database.Commit(tContext);
            }
            catch (Exception ex)
            {
                output.WriteLine(ex.Message);
                database.Rollback(tContext);
                throw ex;
            }
        }
Ejemplo n.º 3
0
        public async Task Test_4_Add_PublisherEntityAsync()
        {
            var tContext = await database.BeginTransactionAsync <PublisherEntity>(isolationLevel);

            try
            {
                for (int i = 0; i < 10; ++i)
                {
                    PublisherEntity entity = Mocker.MockOne();

                    DatabaseResult result = await database.AddAsync(entity, tContext);

                    if (!result.IsSucceeded())
                    {
                        output.WriteLine(result.Exception?.Message);
                        throw new Exception();
                    }

                    Assert.True(result.IsSucceeded());
                }

                await database.CommitAsync(tContext);
            }
            catch (Exception ex)
            {
                output.WriteLine(ex.Message);
                await database.RollbackAsync(tContext);

                throw ex;
            }
        }
Ejemplo n.º 4
0
        public async Task Test_2_Batch_Update_PublisherEntityAsync()
        {
            TransactionContext transContext = await database.BeginTransactionAsync <PublisherEntity>(isolationLevel);

            try
            {
                IList <PublisherEntity> lst = await database.RetrieveAllAsync <PublisherEntity>(transContext);

                for (int i = 0; i < lst.Count; i += 2)
                {
                    PublisherEntity entity = lst[i];
                    //entity.Guid = Guid.NewGuid().ToString();
                    entity.Type  = PublisherType.Online;
                    entity.Name  = "ÖÐsfasfafÎÄÃû×Ö";
                    entity.Books = new List <string>()
                    {
                        "xxx", "tttt"
                    };
                    entity.BookAuthors = new Dictionary <string, Author>()
                    {
                        { "Cat", new Author()
                          {
                              Mobile = "111", Name = "BB"
                          } },
                        { "Dog", new Author()
                          {
                              Mobile = "222", Name = "sx"
                          } }
                    };
                }

                DatabaseResult result = await database.BatchUpdateAsync <PublisherEntity>(lst, "tester", transContext);

                Assert.True(result.IsSucceeded());

                if (!result.IsSucceeded())
                {
                    output.WriteLine(result.Exception?.Message);
                    await database.RollbackAsync(transContext);

                    throw new Exception();
                }

                await database.CommitAsync(transContext);
            }
            catch (Exception ex)
            {
                output.WriteLine(ex.Message);
                await database.RollbackAsync(transContext);

                throw ex;
            }
        }
Ejemplo n.º 5
0
        public async Task Test_5_Update_PublisherEntityAsync()
        {
            var tContext = await database.BeginTransactionAsync <PublisherEntity>(isolationLevel);

            try
            {
                IList <PublisherEntity> testEntities = await database.PageAsync <PublisherEntity>(1, 1, tContext);

                if (testEntities.Count == 0)
                {
                    return;
                }

                PublisherEntity entity = testEntities[0];

                entity.Books.Add("New Book2");
                entity.BookAuthors.Add("New Book2", new Author()
                {
                    Mobile = "15190208956", Name = "Yuzhaobai"
                });

                DatabaseResult result = await database.UpdateAsync(entity, tContext);

                if (!result.IsSucceeded())
                {
                    output.WriteLine(result.Exception?.Message);
                    throw new Exception();
                }

                Assert.True(result.IsSucceeded());

                PublisherEntity stored = await database.ScalarAsync <PublisherEntity>(entity.Id, tContext);

                Assert.True(stored.Books.Contains("New Book2"));
                Assert.True(stored.BookAuthors["New Book2"].Mobile == "15190208956");

                await database.CommitAsync(tContext);
            }
            catch (Exception ex)
            {
                output.WriteLine(ex.Message);
                await database.RollbackAsync(tContext);

                throw ex;
            }
        }