public async Task Debe_Ejecutar_Consulta_SQL() { var totalRegistros = 5; //Preparar datos using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); //1. Eliminar todos los datos existentes var items = await context.Parametros.ToListAsync(); context.Parametros.RemoveRange(items); //2. Agregar Datos. var list = FakeParametros(totalRegistros); foreach (var item in list) { context.Parametros.Add(item); } context.SaveChanges(); } //Ejecutar Test using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); //3. Consultar datos var valorCambiar = "new.value.foo"; var registrosModificados = sqlExecuter.Execute("UPDATE ParametroSistemas SET valor = @value", new SqlParameter("@value", valorCambiar)); registrosModificados.ShouldBe(totalRegistros); //4. Verificar foreach (var parametro in context.Parametros) { parametro.Valor.ShouldBe(valorCambiar); } } }
public async Task Debe_Ejecutar_Consulta_SQL_Con_Bloque_Transaccion() { var totalRegistros = 5; //Preparar datos using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); //1. Eliminar todos los datos existentes var items = await context.Parametros.ToListAsync(); context.Parametros.RemoveRange(items); //2. Agregar Datos. var list = FakeParametros(totalRegistros); foreach (var item in list) { context.Parametros.Add(item); } context.SaveChanges(); } var valorTransaccionComplementa = "new.value.bar"; var valorTransaccionRollback = "foo.rollback"; var valorCambiar = valorTransaccionComplementa; using (var scope = new TransactionScope()) { using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); var registrosModificados = sqlExecuter.Execute("UPDATE ParametroSistemas SET valor = @value", new SqlParameter("@value", valorCambiar)); registrosModificados.ShouldBe(totalRegistros); } //transctional code… scope.Complete(); } //Realizar otra actualizacion. Rollback valorCambiar = valorTransaccionRollback; try { using (var scope = new TransactionScope()) { using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); var registrosModificados = sqlExecuter.Execute("UPDATE ParametroSistemas SET valor = @value", new SqlParameter("@value", valorCambiar)); registrosModificados.ShouldBe(totalRegistros); } //transctional rollback… throw new Exception("RollBack.."); } } catch (Exception ex) { output.WriteLine(ex.Message); } using (var context = new SqlExecuterTestDbContext()) { var simpleDbContextProvider = new SimpleDbContextProvider <SqlExecuterTestDbContext>(context); var sqlExecuter = new SqlExecuter(simpleDbContextProvider); //4. Verificar foreach (var parametro in context.Parametros) { parametro.Valor.ShouldBe(valorTransaccionComplementa); } } }