public void TreeMonDbContext_Context_Transaction_Succeed() { User u = TestHelper.GenerateTestUser("ALPHA"); Account a = new Account() { Name = "ALPHA_ACCOUNT", DateCreated = DateTime.Now }; TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); using (var dbContext = new DbContext(context.ConnectionKey)) { using (var contextTransaction = context.Database.BeginTransaction()) { try { Assert.IsTrue(context.Insert <User>(u), "Failed to insert user."); Assert.IsTrue(context.Insert <Account>(a), "Failed to insert account."); contextTransaction.Commit(); } catch (Exception ex) { contextTransaction.Rollback(); Assert.Fail(ex.Message); } } } Assert.AreEqual(context.Get <User>(u.Id).Name, "ALPHA"); Assert.AreEqual(context.Get <Account>(a.Id).Name, "ALPHA_ACCOUNT"); }
public void TreeMonDbContext_Context_Transaction_Fail() { User u = TestHelper.GenerateTestUser("BETA"); Account a = new Account() { Name = "BETA_ACCOUNT" }; string failTest = null; TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); using (var dbContext = new DbContext(context.ConnectionKey)) { using (var contextTransaction = context.Database.BeginTransaction()) { try { context.Insert <User>(u); failTest.ToUpper();//this should blow up. context.Insert <Account>(a); contextTransaction.Commit(); } catch (Exception) { contextTransaction.Rollback(); } } } //Should have removed the user and not inserted the account. Assert.IsNull(context.Get <User>(u.Id), "BETA"); Assert.IsNull(context.Get <Account>(a.Id), "BETA_ACCOUNT"); }
public void TreeMonDbContext_Context_Delete() { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); User u = TestHelper.GenerateTestUser("DELETE_ME"); Assert.IsTrue(context.Insert <User>(u), "failed to insert user for test"); u = context.Get <User>(u.Id); Assert.IsNotNull(u); Assert.AreEqual(u.Name, "DELETE_ME"); int res = context.Delete <User>(u); Assert.AreEqual(res, 1); Assert.IsNull(context.Get <User>(u.Id)); }
public void TreeMonDbContext_Context_Delete_Async() { Task.Run(async() => { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); User u = TestHelper.GenerateTestUser("DELETE_ME"); context.Insert <User>(u); Assert.AreEqual(context.Get <User>(u.Id).Name, "DELETE_ME"); int res = await context.DeleteAsync <User>(u); Assert.AreEqual(res, 1); Assert.IsNull(context.Get <User>(u.Id)); }).GetAwaiter().GetResult(); }
public void TreeMonDbContext_Context_TransactionScope_Fail() { User u = TestHelper.GenerateTestUser("ECHO"); Account a = new Account() { Name = "ECHO_ACCOUNT" }; string failTest = null; TreeMonDbContext contextUser = new TreeMonDbContext("MSSQL_TEST"); TreeMonDbContext contextAccount = new TreeMonDbContext("MSSQL_TEST"); using (var scope = new TransactionScope()) { try { contextUser.Insert <User>(u); failTest.ToUpper();//this should blow up. contextAccount.Insert <Account>(a); scope.Complete(); } catch (Exception) { } } //Should have removed the user and not inserted the account. Assert.IsNull(contextUser.Get <User>(u.Id), "ECHO"); Assert.IsNull(contextAccount.Get <Account>(a.Id), "ECHO_ACCOUNT"); }
public void TreeMonDbContext_Context_TransactionScope_Succeed() { User u = TestHelper.GenerateTestUser("CHARLIE"); Account a = new Account() { Name = "CHARLIE_ACCOUNT", DateCreated = DateTime.Now }; TreeMonDbContext contextUser = new TreeMonDbContext("MSSQL_TEST"); TreeMonDbContext contextAccount = new TreeMonDbContext("MSSQL_TEST"); using (var scope = new TransactionScope()) { try { contextUser.Insert <User>(u); contextAccount.Insert <Account>(a); scope.Complete(); } catch (Exception) { } } Assert.AreEqual(contextUser.Get <User>(u.Id).Name, "CHARLIE"); Assert.AreEqual(contextAccount.Get <Account>(a.Id).Name, "CHARLIE_ACCOUNT"); }
public void TreeMonDbContext_Context_Delete_Where() { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); User u = TestHelper.GenerateTestUser("DELETE_ME_WHERE"); context.Insert <User>(u); Assert.AreEqual(context.Get <User>(u.Id).Name, "DELETE_ME_WHERE"); // List<SqlParameter> parameters = new List<SqlParameter>(); DynamicParameters parameters = new DynamicParameters(); parameters.Add("@Name", "DELETE_ME_WHERE"); int res = context.Delete <User>("WHERE Name=@Name", parameters); Assert.AreEqual(res, 1); Assert.IsNull(context.Get <User>(u.Id)); }
public LogEntry Get(int id) { using (var context = new TreeMonDbContext(_dbConnectionKey)) { return(context.Get <LogEntry>(id)); } }
public void TreeMonDbContext_Context_Update() { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); _user = context.GetAll <User>().FirstOrDefault(); _user.Name = "UPDATE_TEST"; Assert.AreEqual(context.Update <User>(_user), 1); Assert.AreEqual(context.Get <User>(_user.Id).Name, "UPDATE_TEST"); }
public void TreeMonDbContext_Context_Update_Async() { Task.Run(async() => { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); _userAsync = context.GetAll <User>().FirstOrDefault(); _userAsync.Name = "UPDATE_ASYNC_TEST"; int res = await context.UpdateAsync <User>(_userAsync); Assert.AreEqual(res, 1); Assert.AreEqual(context.Get <User>(_userAsync.Id).Name, "UPDATE_ASYNC_TEST"); }).GetAwaiter().GetResult(); }
public void TreeMonDbContext_Context_ExecuteNonQuery_Async_MSSQL() { Task.Run(async() => { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); _userAsync = context.GetAll <User>().FirstOrDefault(); DynamicParameters parameters = new DynamicParameters(); parameters.Add("@PROVIDER", "UPDATED_ASYNC_PROVIDER"); parameters.Add("@UUID", _userAsync.UUID); int res = await context.ExecuteNonQueryAsync("UPDATE USERS SET ProviderName=@PROVIDER WHERE UUID=@UUID", parameters); Assert.AreEqual(res, 1); Assert.AreEqual(context.Get <User>(_userAsync.Id).ProviderName, "UPDATED_ASYNC_PROVIDER"); }).GetAwaiter().GetResult(); }
public void TreeMonDbContext_Context_ExecuteNonQuery_MSSQL() { TreeMonDbContext context = new TreeMonDbContext("MSSQL_TEST"); _user = context.GetAll <User>().FirstOrDefault(); DynamicParameters parameters = new DynamicParameters();//dapper parameters.Add("@PROVIDER", "UPDATED_PROVIDER"); parameters.Add("@UUID", _user.UUID); // List<SqlParameter> parameters = new List<SqlParameter>(); // parameters.Add(new SqlParameter("@PROVIDER", "UPDATED_PROVIDER")); // parameters.Add(new SqlParameter("@UUID", _user.UUID)); int res = context.ExecuteNonQuery("UPDATE USERS SET ProviderName=@PROVIDER WHERE UUID=@UUID", parameters); Assert.AreEqual(res, 1); Assert.AreEqual(context.Get <User>(_user.Id).ProviderName, "UPDATED_PROVIDER"); }