public async Task Test_4_Add_PublisherEntityAsync() { IDatabase database = _sqlite; ITransaction transaction = _sqlIteTransaction; TransactionContext tContext = await transaction.BeginTransactionAsync <PublisherEntity_Client>().ConfigureAwait(false); try { IList <PublisherEntity_Client> lst = new List <PublisherEntity_Client>(); for (int i = 0; i < 10; ++i) { PublisherEntity_Client entity = Mocker.MockOnePublisherEntity_Client(); await database.AddAsync(entity, "lastUsre", tContext).ConfigureAwait(false); lst.Add(entity); } await transaction.CommitAsync(tContext).ConfigureAwait(false); Assert.True(lst.All(p => p.Version == 0)); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(tContext).ConfigureAwait(false); throw; } }
public async Task Test_2_Batch_Update_PublisherEntityAsync() { IDatabase database = _sqlite; ITransaction transaction = _sqlIteTransaction; TransactionContext transContext = await transaction.BeginTransactionAsync <PublisherEntity_Client>().ConfigureAwait(false); try { IEnumerable <PublisherEntity_Client> lst = await database.RetrieveAllAsync <PublisherEntity_Client>(transContext).ConfigureAwait(false); for (int i = 0; i < lst.Count(); i += 2) { PublisherEntity_Client entity = lst.ElementAt(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" } } }; } await database.BatchUpdateAsync <PublisherEntity_Client>(lst, "lastUsre", transContext).ConfigureAwait(false); await transaction.CommitAsync(transContext).ConfigureAwait(false); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(transContext).ConfigureAwait(false); throw; } }
public async Task Test_5_Update_PublisherEntityAsync() { IDatabase database = _sqlite; ITransaction transaction = _sqlIteTransaction; TransactionContext tContext = await transaction.BeginTransactionAsync <PublisherEntity_Client>().ConfigureAwait(false); try { IList <PublisherEntity_Client> testEntities = (await database.PageAsync <PublisherEntity_Client>(1, 1, tContext).ConfigureAwait(false)).ToList(); if (testEntities.Count == 0) { throw new Exception("No Entity to update"); } PublisherEntity_Client entity = testEntities[0]; entity.Books.Add("New Book2"); //entity.BookAuthors.Add("New Book2", new Author() { Mobile = "15190208956", Name = "Yuzhaobai" }); await database.UpdateAsync(entity, "lastUsre", tContext).ConfigureAwait(false); PublisherEntity_Client?stored = await database.ScalarAsync <PublisherEntity_Client>(entity.Id, tContext).ConfigureAwait(false); await transaction.CommitAsync(tContext).ConfigureAwait(false); Assert.True(stored?.Books.Contains("New Book2")); //Assert.True(stored?.BookAuthors["New Book2"].Mobile == "15190208956"); } catch (Exception ex) { _output.WriteLine(ex.Message); await transaction.RollbackAsync(tContext).ConfigureAwait(false); throw; } }
public async Task Test_9_UpdateLastTimeTestAsync() { IDatabase database = _sqlite; ITransaction transaction = _sqlIteTransaction; TransactionContext transactionContext = await transaction.BeginTransactionAsync <PublisherEntity_Client>().ConfigureAwait(false); //TransactionContext? transactionContext = null; try { PublisherEntity_Client item = Mocker.MockOnePublisherEntity_Client(); await database.AddAsync(item, "xx", transactionContext).ConfigureAwait(false); //await database.AddOrUpdateAsync(item, "sfas", transactionContext).ConfigureAwait(false); await database.DeleteAsync(item, "xxx", transactionContext).ConfigureAwait(false); IList <PublisherEntity_Client> testEntities = (await database.PageAsync <PublisherEntity_Client>(1, 1, transactionContext).ConfigureAwait(false)).ToList(); if (testEntities.Count == 0) { throw new Exception("No Entity to update"); } PublisherEntity_Client entity = testEntities[0]; entity.Books.Add("New Book2"); //entity.BookAuthors.Add("New Book2", new Author() { Mobile = "15190208956", Name = "Yuzhaobai" }); await database.UpdateAsync(entity, "lastUsre", transactionContext).ConfigureAwait(false); PublisherEntity_Client?stored = await database.ScalarAsync <PublisherEntity_Client>(entity.Id, transactionContext).ConfigureAwait(false); item = Mocker.MockOnePublisherEntity_Client(); await database.AddAsync(item, "xx", transactionContext).ConfigureAwait(false); PublisherEntity_Client?fetched = await database.ScalarAsync <PublisherEntity_Client>(item.Id, transactionContext).ConfigureAwait(false); Assert.Equal(item.LastTime, fetched !.LastTime); fetched.Name = "ssssss"; await database.UpdateAsync(fetched, "xxx", transactionContext).ConfigureAwait(false); await transaction.CommitAsync(transactionContext).ConfigureAwait(false); } catch { await transaction.RollbackAsync(transactionContext).ConfigureAwait(false); throw; } }
public async Task Test_8_LastTimeTestAsync() { IDatabase database = _sqlite; ITransaction transaction = _sqlIteTransaction; PublisherEntity_Client item = Mocker.MockOnePublisherEntity_Client(); await database.AddAsync(item, "xx", null).ConfigureAwait(false); PublisherEntity_Client?fetched = await database.ScalarAsync <PublisherEntity_Client>(item.Id, null).ConfigureAwait(false); Assert.Equal(item.LastTime, fetched !.LastTime); fetched.Name = "ssssss"; await database.UpdateAsync(fetched, "xxx", null).ConfigureAwait(false); fetched = await database.ScalarAsync <PublisherEntity_Client>(item.Id, null).ConfigureAwait(false); //await database.AddOrUpdateAsync(item, "ss", null); fetched = await database.ScalarAsync <PublisherEntity_Client>(item.Id, null).ConfigureAwait(false); //Batch List <PublisherEntity_Client>?items = Mocker.GetPublishers_Client(); TransactionContext trans = await transaction.BeginTransactionAsync <PublisherEntity_Client>().ConfigureAwait(false); try { await database.BatchAddAsync <PublisherEntity_Client>(items, "xx", trans).ConfigureAwait(false); IEnumerable <PublisherEntity_Client>?results = await database.RetrieveAsync <PublisherEntity_Client>(item => SqlStatement.In(item.Id, true, items.Select(item => (object)item.Id).ToArray()), trans).ConfigureAwait(false); await database.BatchUpdateAsync <PublisherEntity_Client>(items, "xx", trans).ConfigureAwait(false); List <PublisherEntity_Client>?items2 = Mocker.GetPublishers_Client(); await database.BatchAddAsync <PublisherEntity_Client>(items2, "xx", trans).ConfigureAwait(false); results = await database.RetrieveAsync <PublisherEntity_Client>(item => SqlStatement.In(item.Id, true, items2.Select(item => (object)item.Id).ToArray()), trans).ConfigureAwait(false); await database.BatchUpdateAsync <PublisherEntity_Client>(items2, "xx", trans).ConfigureAwait(false); await transaction.CommitAsync(trans).ConfigureAwait(false); } catch { await transaction.RollbackAsync(trans).ConfigureAwait(false); throw; } finally { } }