Proper way to maintain database known state by rolling back transactions in NUnit, Sql Server and UI Testing

Saifur

I am trying to do the following for UI test automation:

[SetUp]
public void TestSetUp()
{
    _scope = new TransactionScope();
}

[TearDown]
public void TearDown()
{
    _scope.Dispose();
}

[Test]
public void SomeTest()
{
    Utilities.SomeDeleteTransaction(companyCode);    
}

I am trying to execute one Update query and in [Test] and do some stuff with UI and rollback that transaction in [TearDown] which runs after the test. I am not sure if I am doing it right. But, I am trying to (probably commit) that transaction so that I can see it's effect on UI and rollback same transaction so my DB stays the same. Can I accomplish that with TransactionScope or some other class?

Edit

This question is mostly to handle the database known state for selenium testing. Since, my database is brought down from production monthly, I want to be able to execute some insert/update/delete sql script to modify db before tests and then do some UI testing with Selenium and then rollback in Teardown (tests are written using NUnit) to make sure db does not have any influence on tests and stays same after tests.

mycargus

Database snapshots!

Save this script and name it "create_db_snapshot.sql"

/* Create a database snapshot */

USE master;
CREATE DATABASE Your_Database_Snapshot ON
( 
    NAME = Your_Database, 
    FILENAME = 'C:\Snapshots\Your_Database_Snapshot.ss' 
)
AS SNAPSHOT OF Your_Database;
GO

Also, save this script and name it "restore_db_from_snapshot.sql"

USE master;
RESTORE DATABASE Your_Database from 
DATABASE_SNAPSHOT = 'Your_Database_Snapshot';
GO

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using System.Data.SqlClient;

[SetUp]
public void TestSetUp()
{
    string sqlConnectionString = @"server=test.database.com;uid=your_db_username;pwd=your_db_password;database=Your_Database;";

    string script = File.ReadAllText(@"~/create_db_snapshot.sql");
    SqlConnection conn = new SqlConnection(sqlConnectionString);

    Server server = new Server(new ServerConnection(conn));
    server.ConnectionContext.ExecuteNonQuery(script);
}


[TearDown]
public void TearDown()
{
    string sqlConnectionString = @"server=test.database.com;uid=your_db_username;pwd=your_db_password;database=Your_Database;";

    string script = File.ReadAllText(@"~/restore_db_from_snapshot.sql");
    SqlConnection conn = new SqlConnection(sqlConnectionString);

    Server server = new Server(new ServerConnection(conn));
    server.ConnectionContext.ExecuteNonQuery(script);
}

Snapshot documentation: https://msdn.microsoft.com/en-us/library/ms175158.aspx

Code credit for executing .sql file: https://stackoverflow.com/a/1728859/3038677

You might also need to run this script prior to executing restore_db_from_snapshot.sql...

/* Kill all current connections to Your_Database */

use master;
DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses 
WHERE dbid = db_id('Your_Database')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL Server: Database stuck in "Restoring" state

From Dev

Proper way to maintain many connections with Celluloid?

From Dev

Sequelize.js transactions not rolling back

From Dev

Best/fastest way to bulk insert data into a sql server database for load testing?

From Dev

Rolling back transactions in cucumber-jvm tests

From Dev

Spring data @transactional not rolling back with SQL Server and after runtimeexception

From Dev

Good way to maintain Qt labels and text on UI

From Dev

Rolling back database transactions in SQLAlchemy tests with PostgreSQL

From Dev

Rolling back transactions after exception is thrown

From Dev

Proper Way to CAST a Numeric as an INT in SQL Server

From Dev

Proper way to migrate a postgres database?

From Dev

SQL Server Application - Resetting database to original state

From Dev

Proper Angularjs way to use JSON maintain RESTful urls

From Dev

Is this the proper way to get a string back from an sql query in PHP?

From Dev

SQL Server - Rolling back particular transaction only at a later date

From Dev

Proper way to handle SQL database connections in java OOP style

From Dev

Connect to a SQL Server database if java connection settings are known

From Dev

What is the proper way of testing throttling in DRF?

From Dev

Proper way of using and testing generated mapper

From Dev

Proper way to set the umask for SFTP transactions?

From Dev

WSO2 data services server distributed transactions can not roll back for different types of database

From Dev

Proper way to maintain many connections with Celluloid?

From Dev

Proper way to store json in database

From Dev

Proper way to manipulate database

From Dev

Is this the proper way to get a string back from an sql query in PHP?

From Dev

Is there a way to speed up database transactions?

From Dev

Connect to a SQL Server database if java connection settings are known

From Dev

What is a good way of rolling back a transaction in Postgres

From Dev

Case statement in SQL Server with rolling weeks - easier way?

Related Related

  1. 1

    SQL Server: Database stuck in "Restoring" state

  2. 2

    Proper way to maintain many connections with Celluloid?

  3. 3

    Sequelize.js transactions not rolling back

  4. 4

    Best/fastest way to bulk insert data into a sql server database for load testing?

  5. 5

    Rolling back transactions in cucumber-jvm tests

  6. 6

    Spring data @transactional not rolling back with SQL Server and after runtimeexception

  7. 7

    Good way to maintain Qt labels and text on UI

  8. 8

    Rolling back database transactions in SQLAlchemy tests with PostgreSQL

  9. 9

    Rolling back transactions after exception is thrown

  10. 10

    Proper Way to CAST a Numeric as an INT in SQL Server

  11. 11

    Proper way to migrate a postgres database?

  12. 12

    SQL Server Application - Resetting database to original state

  13. 13

    Proper Angularjs way to use JSON maintain RESTful urls

  14. 14

    Is this the proper way to get a string back from an sql query in PHP?

  15. 15

    SQL Server - Rolling back particular transaction only at a later date

  16. 16

    Proper way to handle SQL database connections in java OOP style

  17. 17

    Connect to a SQL Server database if java connection settings are known

  18. 18

    What is the proper way of testing throttling in DRF?

  19. 19

    Proper way of using and testing generated mapper

  20. 20

    Proper way to set the umask for SFTP transactions?

  21. 21

    WSO2 data services server distributed transactions can not roll back for different types of database

  22. 22

    Proper way to maintain many connections with Celluloid?

  23. 23

    Proper way to store json in database

  24. 24

    Proper way to manipulate database

  25. 25

    Is this the proper way to get a string back from an sql query in PHP?

  26. 26

    Is there a way to speed up database transactions?

  27. 27

    Connect to a SQL Server database if java connection settings are known

  28. 28

    What is a good way of rolling back a transaction in Postgres

  29. 29

    Case statement in SQL Server with rolling weeks - easier way?

HotTag

Archive