SlideShare una empresa de Scribd logo
1 de 53
Database Continuous Integration, 
Unit Test and Functional Test
About myself: 
• Data architect/DBA with 16 years of SQL Server experience 
• Independent consultant, currently Architecture Lead and Technical 
Delivery Manager at Government of Alberta 
• Microsoft Certified System Engineer: MCSE 
• Oracle Certified Professional: Oracle DBA 
• IBM Certified Solution Expert: DB2 UDB 
http://netdbsolutions.com 
https://twitter.com/HarryZheng 
http://ca.linkedin.com/in/harryzheng 
https://www.facebook.com/Harry.H.Zheng 
2
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
3
What is Continuous Integration 
• Continuous integration (CI) – the process of 
continuously integrating developers code in 
order to find problems quickly 
• Wikipedia: CI is the practice of merging all 
developer working copies with a shared 
mainline several times a day. 
• CI is often related to Test Driven Development 
(TDD) and Agile practice 
4
Traditional work flow 
1. Dev A works on his computer and checks in 
code when ready 
2. Dev B works on his computer and checks in 
code when his task is done 
3. Dev C… 
4. When time to release, we start up build process 
and see if the combined code works 
5. The version of code doesn’t build 
6. Who is responsible? 
5
Continuous Integration Work Flow 
6
What does CI check? 
• Does the solution compile? 
• Does the code pass our unit tests? 
• Does the code meet functional requirements? 
• Does the version meet code quality 
requirements? 
7
What does CI produce? 
• Automatically produce build artifacts 
• Ensure same version can be staged to various 
environments 
• Setup for automated deployment 
8
Benefits of CI 
• Ensure code in source control works 
• Less time on integration 
• Reduce friction within project team 
• Save time on build and deploy 
• Increased visibility of progress 
• Increased overall confidence on code 
9
Do you need CI: 
scenario 1 
• Harry works with 3 other Devs on a side project 
• Project is a web based app with SQL backend 
• Harry is the only dba on the team 
• SQL database is relatively simple with 30 tables and a few SPs 
• Do we need setup CI? 
10
Do you need CI: 
scenario 2 
• Harry works in a team with 20 developers, 4 DBAs 
• Main SQL Server has 200+ tables and many SPs, functions 
• Some developers have good knowledge of SQL skills, others not so 
good but can code T-SQL 
• We used to have a rule that only DBAs can check in SQL code, but 
DBAs became a bottom neck quickly 
• Now we have opened database projects to developers to check in 
code changes, bug fixes, etc. 
• Do we need setup CI? 
11
Software for Running CI 
• Team City 
• Cruise Control 
• Final Builder 
12
TeamCity Web Interface 
13
Final Builder Server Web Interface 
14
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
15
Prerequisite for Continuous Integration 
• Source Control System 
• Build Server 
• Manage Database project with SSDT 
• Database Tests 
16
Database CI Work flow 
1. Developer check in code to source control 
2. Check in triggers a build of the project 
3. On successful build, the version of the code is 
deployed to testing databases 
4. Unit tests are executed against test database 
5. Functional tests are executed against functional 
test database 
6. If all steps succeed, the version is ready for 
deployment 
7. If any step fails, alert is sent to the team and the 
cycle starts from step1 again 
17
Database CI 
18
Database deploy script 
19
Substitution/Build configuration file 
<Environment value="BuildDB_CI" machine="VM-DBServer-1" substitute="false"> 
<Database environmentsuffix="_CI" BackupFileFolder="VM-DBServer-1Deploy"> 
<SQLInstances> 
<SQLInstance name="SQLInst1" host="VM-DBServer-1" sqlinstance="Dev" 
databaseFolder="d:SQLDataDev" databaseLogFolder="d:SQLLogDev" dbdeployfolder="D:Deploy"> 
<Projects> 
<Project name="DBProj1" seedserver="VM-DBServer-1Seed" seeddatabase="DB1_Seed" fullbuild="No"> 
<Databases> 
<Database name="DBProj1" targetdatabase="DB1_CI" /> 
</Databases> 
</Project> 
<Project name="DBProj2" seedserver="VM-DBServer-1Seed" seeddatabase="DB2_Seed" fullbuild="No"> 
<Databases> 
<Database name="DBProj2" targetdatabase="DB2_CI" /> 
</Databases> 
</Project> 
</Projects> 
</SQLInstance> 
</SQLInstances> 
</Database> 
<SSIS ServerName="VM-DBServer-1" /> 
<SSRS ServerName="VM-DBServer-1" ReportServerPath="ReportServer_Dev"/> 
</Environment> 
20
Deploy DB schema with SQLPackage 
"C:Program Files (x86)Microsoft SQL Server110DACbinSqlPackage.exe" 
/Action:Publish 
/SourceFile:"%1" 
/TargetConnectionString:"Server=%2;Initial Catalog=%3;Integrated 
Security=True" 
/p:BlockOnPossibleDataLoss=False 
/p:CreateNewDatabase=False 
/p:DisableAndReenableDdlTriggers=True 
/p:DropConstraintsNotInSource=True 
/p:DropDmlTriggersNotInSource=True 
21
Deploy database and preserve data 
1. Run Pre-deployment scripts 
2. Execute sqlpackager.exe to deploy database 
schema changes 
3. Run Post-deployment scripts 
22
Deploy database and preserve data 
When to use Pre and Post scripts? 
•If you change table definition that may cause data 
loss when database project is deployed, you need 
to write a pre-deployment script to preserve the 
data 
•All other data change scripts will go to Post-deployment 
script 
•All Pre and Post scripts should be created so that 
the scripts can be executed repeatedly 
23
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
24
What is unit test? 
• Test on a discrete unit of code 
• Unit test should not be affected by other units 
• Isolates the unit under test from the rest of the 
code 
• Repeatable 
25
My definition of database unit test 
• Test that runs quickly, and does not rely on 
existing data with the exception of static data 
(e.g.., reference data) 
26
Unit Testing - AAA 
•Arrange 
• Setup data (with TestHelper) 
•Act 
• Call Precedure/Function 
•Assert 
• Compare results with expectation 
27
Create Unit Test From SSOE from VS 
28
Unit Test Example for SP 
CREATE PROCEDURE [dbo].[uspShowMeetingAttendee] 
@MeetingID int = 0 
AS 
SELECT MT.Name 
,M.FirstName 
,M.[LastName] 
From Meeting MT 
inner join MeetingAttendee MA 
on MT.MeetingID = MA.MeetingID 
inner join Member M 
on M.MemberID = MA.MemberID 
RETURN 0 
29
Unit Test Example 
-- database unit test for dbo.uspShowMeetingAttendee 
BEGIN TRANSACTION 
--Arrange 
Declare @MeetingID int 
INSERT INTO [dbo].[Meeting] ([Name]) VALUES ('XXTestMeeting') 
select @MeetingID = MeetingID from Meeting where Name = 'XXTestMeeting' 
INSERT INTO [dbo].[Member]([FirstName],[LastName],[BirthDate]) VALUES ('XXFirstName','XXLastName','1970-01-01') 
INSERT INTO [dbo].[MeetingAttendee] ([MeetingID],[MemberID]) 
Select MeetingID, MemberID From Meeting M1 inner Join Member M2 on 1=1 
where M1.Name = 'XXTestMeeting' and M2.LastName = 'XXLastName' 
--code to create a dynamic temporary table ##xx{Schema}{SPName}xx 
-- based of the output of a stored procedure 
exec [TestHelper].[uspCreateTableForStoredProcedureOutput] 
@storedProcedure = '[dbo].[uspShowMeetingAttendee]' 
--ACT 
--insert into the temporary table created by the SP 
insert into ##xxdbouspShowMeetingAttendeexx 
EXECUTE [dbo].[uspShowMeetingAttendee] @MeetingID = @MeetingID; 
--ASSERT 
--now you can select specific column without knowledge of the oridinal value 
select LastName 
from ##xxdbouspShowMeetingAttendeexx 
where FirstName = 'XXFirstName' 
ROLLBACK TRANSACTION 
30
What is functional test? 
• Functional testing is a type of black box testing 
that bases its test cases on the specifications of 
the software component under test 
• Functions are tested by feeding them input and 
examining the output, and internal program 
structure is rarely considered 
31
My definition of database functional 
test 
• Tests with long execution times and they rely 
on existing non-static data 
• Functional tests will run on a server where the 
complete dataset is available. 
32
Database Test Tools and Frameworks 
• Microsoft SSDT 
• http://msdn.microsoft.com/en-ca/data/tools.aspx 
• TSQLT 
• http://tsqlt.org/ 
• Red-Gate SQL Test 
• http://www.red-gate.com/products/sql-development/sql-test/ 
33
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
34
Database test standard and best 
practice 
• The folder structure within the database test 
project will have a structure that closely 
resembles the structure of the database project. 
35
Naming Conventions 
Database tests must be discoverable. All database 
tests will follow one of these naming conventions: 
<Database Object Name>_Main_<Description>.cs 
<Database Object Name>_State_<Description>.cs 
<Database Object Name>_Defect_<Defect #>.cs 
36
Rules 
• All database objects must have at least one main 
test. 
• Static tables (e.g., reference data) must have one or 
more state tests. 
• A defect that results in a change to a database 
object must have at least one defect test. 
37
Rules 
• All test scripts must not change the state of the 
database. 
• This is done by adding a Begin Tran/ Rollback 
Tran code block to the test script. 
38
Test Helpers 
• In order to assist in creating database tests, the database 
has a number of stored procedures to create test data. 
• All of these stored procedures exist in the [TestHelper] 
schema. 
• Developers are free to modify existing [TestHelper] 
stored procedures provided the changes do not alter the 
original purpose of the stored procedure and the 
changes do not impact any existing database tests. 
39
Documenting Tests 
All test scripts should be well documented. 
1.The comment will indicate which test condition(s) 
are associated with a particular result set. 
2.The comments will indicate what part of the SQL 
in the database object is being exercised (e.g., 
which case statements within a where clause are 
exercised). 
40
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
41
Writing tests for SP or Function 
Problem: 
•A test that calls to the stored procedure returns a 
result set with many rows and the test involves 
retrieving one row only 
•A test is only concerned about testing a single 
value, but the stored procedure returns a result set 
with many columns 
42
Writing tests for SP or Function 
There are a couple of ways of making the tests less 
susceptible to changes: 
1.Using a dynamic temporary table 
2.Using a declared temporary table 
The first is fair more desirable but cannot be used 
for stored procedure or functions that have 
dynamic SQL. 
43
Insert into a dynamic temporary table 
This approach uses 
sys.dm_exec_decribe_first_result_set_for_object to 
dynamically create a temporary table that has the 
same columns as the first result set of the stored 
procedure. 
[TestHelper]. 
[uspCreateTableForStoredProcedureColumns] 
creates a global temporary table that can be used 
to store the results of executing the stored 
procedure under test. 
44
Insert into a dynamic temporary table 
Begin Tran 
--code to create a dynamic temporary table ##xx{Schema}{SPName}xx 
-- based of the output of a stored procedure 
exec [TestHelper].[uspCreateTableForStoredProcedureColumns] 
@storedProcedure = '[dbo].[uspRetrieveUserInfo]' 
--insert into the temporary table created by the SP 
insert into ##xxdbouspRetrieveUserInfoxx 
exec [dbo].[uspRetrieveUserInfo] @UserID = 51 
--now you can select specific column without knowledge of the oridinal value 
select FirstName 
from ##xxdbouspRetrieveUserInfoxx 
where [BirthDate] > '1995-01-01' 
Rollback Tran 
45
Insert into a dynamic temporary table 
Create procedure [TestHelper].[uspCreateTableForStoredProcedureOutput] ( 
@storedProcedure varchar(128) 
) 
as 
begin 
declare @sql nvarchar(max) = '', 
@tablename varchar(100) = '##xx' + replace(replace(replace(@storedProcedure,'[',''),']',''),'.','') + 'xx' 
set @sql = @sql + 'IF OBJECT_ID(''tempdb..' + @tablename + '''' + ') IS NOT NULL DROP TABLE ' + @tablename + ';' 
set @sql = @sql + 'create table ' + @tablename + '(' 
select @sql = @sql + '[' + name + '] ' + coalesce(system_type_name, (D.Data_Type + 
case when Character_Maximum_Length is not null then '(' + cast(Character_Maximum_Length as varchar) + ')' 
else '' End)) + ',' 
from sys.dm_exec_describe_first_result_set_for_object (object_id(@storedProcedure), null) FR 
Left Outer Join INFORMATION_SCHEMA.DOMAINS D 
on FR.User_Type_Name = D.Domain_Name 
Order by FR.Column_Ordinal 
set @sql = substring(@sql, 0, len(@sql)) + ');' 
EXEC sp_ExecuteSQL @sql; 
end 
46
Insert into a dynamic temporary table 
Pros: 
• Test is not dependent on the order of the 
columns in the stored procedure, i.e., the order 
can change 
• Result set can be further filtered 
Cons: 
• Will not work for stored procedures that use 
dynamic SQL 
• Will not work for stored procedures that return 
more that one result set 
47
Insert into a declared temporary table 
Declare a temporary table that has the same 
columns as the result set of the stored procedure. 
This is the less desirable of the two options but is 
the only choice if the stored procedure or function 
returns the result set using dynamic SQL. 
48
Insert into a declared temporary table 
Begin Tran 
-- TestHelper SP creates a simple user with a first name of xxFoobarxx 
declare @UserID Int 
exec [TestHelper].[uspCreateUser] @id = @UserID output 
declare @User table ( 
ID Int, 
FirstName varchar(50), 
LastName varchar(50), 
BirthDate date 
) 
--Insert the SP output to the temp table 
insert into @User 
exec dbo.uspSearchUserByName @FirstName = 'xxFoobarxx‘ 
--this should return a single user 
--Verify_One_User_Exists 
select * from @User where FirstName = 'xxFoobarxx‘ 
Rollback Tran 
49
Insert into a declared temporary table 
Pros: 
• Further filtering of the result set is possible. 
Cons: 
• The order of the columns in the declared 
temporary table must be the same as the order 
of the result set returned by the stored 
procedure or function. 
50
Session agenda 
1. What is Continuous Integration? 
2. Database Continuous Integration Setup 
3. Database unit test and functional test 
4. Database test standard and best practice 
5. Tips & Tricks on writing database tests 
6. Q&A 
51
52
Reference and further reading: 
1. http://www.pluralsight.com/courses/continuous-integration 
2. http://en.wikipedia.org/wiki/Continuous_integration 
3. http://www.rackspace.com/blog/the-business-advantages-of-continuous- 
integration/ 
4. http://www.youtube.com/watch?v=PbVKgoAVZjY (Continuous 
Integration with the Database by Ike Ellis) 
5. http://indragunawan.com/2013/09/teamcity-continuous-integration- 
for-everybody.html 
6. http://www.youtube.com/watch?v=4sANX9AhM8c (Introduction to 
Continuous Integration) 
53

Más contenido relacionado

La actualidad más candente

Continuous integration
Continuous integrationContinuous integration
Continuous integration
amscanne
 

La actualidad más candente (19)

Automated Database Deployment at SQL Rally
Automated Database Deployment at SQL RallyAutomated Database Deployment at SQL Rally
Automated Database Deployment at SQL Rally
 
Microsoft SQL Server Continuous Integration
Microsoft SQL Server Continuous IntegrationMicrosoft SQL Server Continuous Integration
Microsoft SQL Server Continuous Integration
 
Database CI/CD Pipeline
Database CI/CD PipelineDatabase CI/CD Pipeline
Database CI/CD Pipeline
 
Security Implications for a DevOps Transformation
Security Implications for a DevOps TransformationSecurity Implications for a DevOps Transformation
Security Implications for a DevOps Transformation
 
Iltam database version control
Iltam database version controlIltam database version control
Iltam database version control
 
Continuous Delivery Distilled
Continuous Delivery DistilledContinuous Delivery Distilled
Continuous Delivery Distilled
 
Jenkins - From Continuous Integration to Continuous Delivery
Jenkins - From Continuous Integration to Continuous DeliveryJenkins - From Continuous Integration to Continuous Delivery
Jenkins - From Continuous Integration to Continuous Delivery
 
Transforming Organizations with CI/CD
Transforming Organizations with CI/CDTransforming Organizations with CI/CD
Transforming Organizations with CI/CD
 
How to Build a DevOps Toolchain
How to Build a DevOps ToolchainHow to Build a DevOps Toolchain
How to Build a DevOps Toolchain
 
What is Jenkins | Jenkins Tutorial for Beginners | Edureka
What is Jenkins | Jenkins Tutorial for Beginners | EdurekaWhat is Jenkins | Jenkins Tutorial for Beginners | Edureka
What is Jenkins | Jenkins Tutorial for Beginners | Edureka
 
Standardizing Jenkins with CloudBees Jenkins Team
Standardizing Jenkins with CloudBees Jenkins TeamStandardizing Jenkins with CloudBees Jenkins Team
Standardizing Jenkins with CloudBees Jenkins Team
 
DevOps and Build Automation
DevOps and Build AutomationDevOps and Build Automation
DevOps and Build Automation
 
Continuous integration with Jenkins
Continuous integration with JenkinsContinuous integration with Jenkins
Continuous integration with Jenkins
 
Continuous Integration, Continuous Quality, Continuous Delivery
Continuous Integration, Continuous Quality, Continuous DeliveryContinuous Integration, Continuous Quality, Continuous Delivery
Continuous Integration, Continuous Quality, Continuous Delivery
 
Continuous Delivery Applied (Agile Richmond)
Continuous Delivery Applied (Agile Richmond)Continuous Delivery Applied (Agile Richmond)
Continuous Delivery Applied (Agile Richmond)
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
Flusso Continuous Integration & Continuous Delivery
Flusso Continuous Integration & Continuous DeliveryFlusso Continuous Integration & Continuous Delivery
Flusso Continuous Integration & Continuous Delivery
 
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
CI CD Pipeline Using Jenkins | Continuous Integration and Deployment | DevOps...
 
DevOps introduction
DevOps introductionDevOps introduction
DevOps introduction
 

Similar a Database continuous integration, unit test and functional test

Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DB
UniFabric
 

Similar a Database continuous integration, unit test and functional test (20)

Bringing DevOps to the Database
Bringing DevOps to the DatabaseBringing DevOps to the Database
Bringing DevOps to the Database
 
SharePoint 2013 Performance Analysis - Robi Vončina
SharePoint 2013 Performance Analysis - Robi VončinaSharePoint 2013 Performance Analysis - Robi Vončina
SharePoint 2013 Performance Analysis - Robi Vončina
 
Database Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance AnalysisDatabase Fundamental Concepts- Series 1 - Performance Analysis
Database Fundamental Concepts- Series 1 - Performance Analysis
 
SSDT unleashed
SSDT unleashedSSDT unleashed
SSDT unleashed
 
Technical Without Code
Technical Without CodeTechnical Without Code
Technical Without Code
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
ow.ppt
ow.pptow.ppt
ow.ppt
 
Ow
OwOw
Ow
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
 
How to generate customized java 8 code from your database
How to generate customized java 8 code from your databaseHow to generate customized java 8 code from your database
How to generate customized java 8 code from your database
 
Silicon Valley JUG - How to generate customized java 8 code from your database
Silicon Valley JUG - How to generate customized java 8 code from your databaseSilicon Valley JUG - How to generate customized java 8 code from your database
Silicon Valley JUG - How to generate customized java 8 code from your database
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
 
SOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DBSOUG_Deployment__Automation_DB
SOUG_Deployment__Automation_DB
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014
 
In Memory Unit Testing with Apache DbUnit
In Memory Unit Testing with Apache DbUnitIn Memory Unit Testing with Apache DbUnit
In Memory Unit Testing with Apache DbUnit
 
Why retail companies can't afford database downtime
Why retail companies can't afford database downtimeWhy retail companies can't afford database downtime
Why retail companies can't afford database downtime
 
Plantilla oracle
Plantilla oraclePlantilla oracle
Plantilla oracle
 
ECMDay2015 - Kent Agerlund – Configuration Manager 2012 – A Site Review
ECMDay2015 - Kent Agerlund – Configuration Manager 2012 – A Site ReviewECMDay2015 - Kent Agerlund – Configuration Manager 2012 – A Site Review
ECMDay2015 - Kent Agerlund – Configuration Manager 2012 – A Site Review
 
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
How to JavaOne 2016 - Generate Customized Java 8 Code from Your Database [TUT...
 
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
JavaOne2016 - How to Generate Customized Java 8 Code from Your Database [TUT4...
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

Database continuous integration, unit test and functional test

  • 1. Database Continuous Integration, Unit Test and Functional Test
  • 2. About myself: • Data architect/DBA with 16 years of SQL Server experience • Independent consultant, currently Architecture Lead and Technical Delivery Manager at Government of Alberta • Microsoft Certified System Engineer: MCSE • Oracle Certified Professional: Oracle DBA • IBM Certified Solution Expert: DB2 UDB http://netdbsolutions.com https://twitter.com/HarryZheng http://ca.linkedin.com/in/harryzheng https://www.facebook.com/Harry.H.Zheng 2
  • 3. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 3
  • 4. What is Continuous Integration • Continuous integration (CI) – the process of continuously integrating developers code in order to find problems quickly • Wikipedia: CI is the practice of merging all developer working copies with a shared mainline several times a day. • CI is often related to Test Driven Development (TDD) and Agile practice 4
  • 5. Traditional work flow 1. Dev A works on his computer and checks in code when ready 2. Dev B works on his computer and checks in code when his task is done 3. Dev C… 4. When time to release, we start up build process and see if the combined code works 5. The version of code doesn’t build 6. Who is responsible? 5
  • 7. What does CI check? • Does the solution compile? • Does the code pass our unit tests? • Does the code meet functional requirements? • Does the version meet code quality requirements? 7
  • 8. What does CI produce? • Automatically produce build artifacts • Ensure same version can be staged to various environments • Setup for automated deployment 8
  • 9. Benefits of CI • Ensure code in source control works • Less time on integration • Reduce friction within project team • Save time on build and deploy • Increased visibility of progress • Increased overall confidence on code 9
  • 10. Do you need CI: scenario 1 • Harry works with 3 other Devs on a side project • Project is a web based app with SQL backend • Harry is the only dba on the team • SQL database is relatively simple with 30 tables and a few SPs • Do we need setup CI? 10
  • 11. Do you need CI: scenario 2 • Harry works in a team with 20 developers, 4 DBAs • Main SQL Server has 200+ tables and many SPs, functions • Some developers have good knowledge of SQL skills, others not so good but can code T-SQL • We used to have a rule that only DBAs can check in SQL code, but DBAs became a bottom neck quickly • Now we have opened database projects to developers to check in code changes, bug fixes, etc. • Do we need setup CI? 11
  • 12. Software for Running CI • Team City • Cruise Control • Final Builder 12
  • 14. Final Builder Server Web Interface 14
  • 15. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 15
  • 16. Prerequisite for Continuous Integration • Source Control System • Build Server • Manage Database project with SSDT • Database Tests 16
  • 17. Database CI Work flow 1. Developer check in code to source control 2. Check in triggers a build of the project 3. On successful build, the version of the code is deployed to testing databases 4. Unit tests are executed against test database 5. Functional tests are executed against functional test database 6. If all steps succeed, the version is ready for deployment 7. If any step fails, alert is sent to the team and the cycle starts from step1 again 17
  • 20. Substitution/Build configuration file <Environment value="BuildDB_CI" machine="VM-DBServer-1" substitute="false"> <Database environmentsuffix="_CI" BackupFileFolder="VM-DBServer-1Deploy"> <SQLInstances> <SQLInstance name="SQLInst1" host="VM-DBServer-1" sqlinstance="Dev" databaseFolder="d:SQLDataDev" databaseLogFolder="d:SQLLogDev" dbdeployfolder="D:Deploy"> <Projects> <Project name="DBProj1" seedserver="VM-DBServer-1Seed" seeddatabase="DB1_Seed" fullbuild="No"> <Databases> <Database name="DBProj1" targetdatabase="DB1_CI" /> </Databases> </Project> <Project name="DBProj2" seedserver="VM-DBServer-1Seed" seeddatabase="DB2_Seed" fullbuild="No"> <Databases> <Database name="DBProj2" targetdatabase="DB2_CI" /> </Databases> </Project> </Projects> </SQLInstance> </SQLInstances> </Database> <SSIS ServerName="VM-DBServer-1" /> <SSRS ServerName="VM-DBServer-1" ReportServerPath="ReportServer_Dev"/> </Environment> 20
  • 21. Deploy DB schema with SQLPackage "C:Program Files (x86)Microsoft SQL Server110DACbinSqlPackage.exe" /Action:Publish /SourceFile:"%1" /TargetConnectionString:"Server=%2;Initial Catalog=%3;Integrated Security=True" /p:BlockOnPossibleDataLoss=False /p:CreateNewDatabase=False /p:DisableAndReenableDdlTriggers=True /p:DropConstraintsNotInSource=True /p:DropDmlTriggersNotInSource=True 21
  • 22. Deploy database and preserve data 1. Run Pre-deployment scripts 2. Execute sqlpackager.exe to deploy database schema changes 3. Run Post-deployment scripts 22
  • 23. Deploy database and preserve data When to use Pre and Post scripts? •If you change table definition that may cause data loss when database project is deployed, you need to write a pre-deployment script to preserve the data •All other data change scripts will go to Post-deployment script •All Pre and Post scripts should be created so that the scripts can be executed repeatedly 23
  • 24. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 24
  • 25. What is unit test? • Test on a discrete unit of code • Unit test should not be affected by other units • Isolates the unit under test from the rest of the code • Repeatable 25
  • 26. My definition of database unit test • Test that runs quickly, and does not rely on existing data with the exception of static data (e.g.., reference data) 26
  • 27. Unit Testing - AAA •Arrange • Setup data (with TestHelper) •Act • Call Precedure/Function •Assert • Compare results with expectation 27
  • 28. Create Unit Test From SSOE from VS 28
  • 29. Unit Test Example for SP CREATE PROCEDURE [dbo].[uspShowMeetingAttendee] @MeetingID int = 0 AS SELECT MT.Name ,M.FirstName ,M.[LastName] From Meeting MT inner join MeetingAttendee MA on MT.MeetingID = MA.MeetingID inner join Member M on M.MemberID = MA.MemberID RETURN 0 29
  • 30. Unit Test Example -- database unit test for dbo.uspShowMeetingAttendee BEGIN TRANSACTION --Arrange Declare @MeetingID int INSERT INTO [dbo].[Meeting] ([Name]) VALUES ('XXTestMeeting') select @MeetingID = MeetingID from Meeting where Name = 'XXTestMeeting' INSERT INTO [dbo].[Member]([FirstName],[LastName],[BirthDate]) VALUES ('XXFirstName','XXLastName','1970-01-01') INSERT INTO [dbo].[MeetingAttendee] ([MeetingID],[MemberID]) Select MeetingID, MemberID From Meeting M1 inner Join Member M2 on 1=1 where M1.Name = 'XXTestMeeting' and M2.LastName = 'XXLastName' --code to create a dynamic temporary table ##xx{Schema}{SPName}xx -- based of the output of a stored procedure exec [TestHelper].[uspCreateTableForStoredProcedureOutput] @storedProcedure = '[dbo].[uspShowMeetingAttendee]' --ACT --insert into the temporary table created by the SP insert into ##xxdbouspShowMeetingAttendeexx EXECUTE [dbo].[uspShowMeetingAttendee] @MeetingID = @MeetingID; --ASSERT --now you can select specific column without knowledge of the oridinal value select LastName from ##xxdbouspShowMeetingAttendeexx where FirstName = 'XXFirstName' ROLLBACK TRANSACTION 30
  • 31. What is functional test? • Functional testing is a type of black box testing that bases its test cases on the specifications of the software component under test • Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered 31
  • 32. My definition of database functional test • Tests with long execution times and they rely on existing non-static data • Functional tests will run on a server where the complete dataset is available. 32
  • 33. Database Test Tools and Frameworks • Microsoft SSDT • http://msdn.microsoft.com/en-ca/data/tools.aspx • TSQLT • http://tsqlt.org/ • Red-Gate SQL Test • http://www.red-gate.com/products/sql-development/sql-test/ 33
  • 34. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 34
  • 35. Database test standard and best practice • The folder structure within the database test project will have a structure that closely resembles the structure of the database project. 35
  • 36. Naming Conventions Database tests must be discoverable. All database tests will follow one of these naming conventions: <Database Object Name>_Main_<Description>.cs <Database Object Name>_State_<Description>.cs <Database Object Name>_Defect_<Defect #>.cs 36
  • 37. Rules • All database objects must have at least one main test. • Static tables (e.g., reference data) must have one or more state tests. • A defect that results in a change to a database object must have at least one defect test. 37
  • 38. Rules • All test scripts must not change the state of the database. • This is done by adding a Begin Tran/ Rollback Tran code block to the test script. 38
  • 39. Test Helpers • In order to assist in creating database tests, the database has a number of stored procedures to create test data. • All of these stored procedures exist in the [TestHelper] schema. • Developers are free to modify existing [TestHelper] stored procedures provided the changes do not alter the original purpose of the stored procedure and the changes do not impact any existing database tests. 39
  • 40. Documenting Tests All test scripts should be well documented. 1.The comment will indicate which test condition(s) are associated with a particular result set. 2.The comments will indicate what part of the SQL in the database object is being exercised (e.g., which case statements within a where clause are exercised). 40
  • 41. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 41
  • 42. Writing tests for SP or Function Problem: •A test that calls to the stored procedure returns a result set with many rows and the test involves retrieving one row only •A test is only concerned about testing a single value, but the stored procedure returns a result set with many columns 42
  • 43. Writing tests for SP or Function There are a couple of ways of making the tests less susceptible to changes: 1.Using a dynamic temporary table 2.Using a declared temporary table The first is fair more desirable but cannot be used for stored procedure or functions that have dynamic SQL. 43
  • 44. Insert into a dynamic temporary table This approach uses sys.dm_exec_decribe_first_result_set_for_object to dynamically create a temporary table that has the same columns as the first result set of the stored procedure. [TestHelper]. [uspCreateTableForStoredProcedureColumns] creates a global temporary table that can be used to store the results of executing the stored procedure under test. 44
  • 45. Insert into a dynamic temporary table Begin Tran --code to create a dynamic temporary table ##xx{Schema}{SPName}xx -- based of the output of a stored procedure exec [TestHelper].[uspCreateTableForStoredProcedureColumns] @storedProcedure = '[dbo].[uspRetrieveUserInfo]' --insert into the temporary table created by the SP insert into ##xxdbouspRetrieveUserInfoxx exec [dbo].[uspRetrieveUserInfo] @UserID = 51 --now you can select specific column without knowledge of the oridinal value select FirstName from ##xxdbouspRetrieveUserInfoxx where [BirthDate] > '1995-01-01' Rollback Tran 45
  • 46. Insert into a dynamic temporary table Create procedure [TestHelper].[uspCreateTableForStoredProcedureOutput] ( @storedProcedure varchar(128) ) as begin declare @sql nvarchar(max) = '', @tablename varchar(100) = '##xx' + replace(replace(replace(@storedProcedure,'[',''),']',''),'.','') + 'xx' set @sql = @sql + 'IF OBJECT_ID(''tempdb..' + @tablename + '''' + ') IS NOT NULL DROP TABLE ' + @tablename + ';' set @sql = @sql + 'create table ' + @tablename + '(' select @sql = @sql + '[' + name + '] ' + coalesce(system_type_name, (D.Data_Type + case when Character_Maximum_Length is not null then '(' + cast(Character_Maximum_Length as varchar) + ')' else '' End)) + ',' from sys.dm_exec_describe_first_result_set_for_object (object_id(@storedProcedure), null) FR Left Outer Join INFORMATION_SCHEMA.DOMAINS D on FR.User_Type_Name = D.Domain_Name Order by FR.Column_Ordinal set @sql = substring(@sql, 0, len(@sql)) + ');' EXEC sp_ExecuteSQL @sql; end 46
  • 47. Insert into a dynamic temporary table Pros: • Test is not dependent on the order of the columns in the stored procedure, i.e., the order can change • Result set can be further filtered Cons: • Will not work for stored procedures that use dynamic SQL • Will not work for stored procedures that return more that one result set 47
  • 48. Insert into a declared temporary table Declare a temporary table that has the same columns as the result set of the stored procedure. This is the less desirable of the two options but is the only choice if the stored procedure or function returns the result set using dynamic SQL. 48
  • 49. Insert into a declared temporary table Begin Tran -- TestHelper SP creates a simple user with a first name of xxFoobarxx declare @UserID Int exec [TestHelper].[uspCreateUser] @id = @UserID output declare @User table ( ID Int, FirstName varchar(50), LastName varchar(50), BirthDate date ) --Insert the SP output to the temp table insert into @User exec dbo.uspSearchUserByName @FirstName = 'xxFoobarxx‘ --this should return a single user --Verify_One_User_Exists select * from @User where FirstName = 'xxFoobarxx‘ Rollback Tran 49
  • 50. Insert into a declared temporary table Pros: • Further filtering of the result set is possible. Cons: • The order of the columns in the declared temporary table must be the same as the order of the result set returned by the stored procedure or function. 50
  • 51. Session agenda 1. What is Continuous Integration? 2. Database Continuous Integration Setup 3. Database unit test and functional test 4. Database test standard and best practice 5. Tips & Tricks on writing database tests 6. Q&A 51
  • 52. 52
  • 53. Reference and further reading: 1. http://www.pluralsight.com/courses/continuous-integration 2. http://en.wikipedia.org/wiki/Continuous_integration 3. http://www.rackspace.com/blog/the-business-advantages-of-continuous- integration/ 4. http://www.youtube.com/watch?v=PbVKgoAVZjY (Continuous Integration with the Database by Ike Ellis) 5. http://indragunawan.com/2013/09/teamcity-continuous-integration- for-everybody.html 6. http://www.youtube.com/watch?v=4sANX9AhM8c (Introduction to Continuous Integration) 53