SlideShare a Scribd company logo
1 of 56
Test-Driven Database Development with DataDude Cory Foy http://coryfoy.com | @cory_foy
Agenda Overview Features Demos Availability Other Tools
Agenda Overview Features Demos Availability Other Tools
Overview – TDD TDD == Test-Driven Development Developer Design Technique Red-Green-Refactor Write a failing test Write just enough code to make it pass Refactor any duplication
Overview – TDD in DBs Why is agility at the database so hard? Control / Permission issues Tightly coupled applications No good testing tools Data Migration issues If it hurts, do it more
Overview – Agile Defined Agile Manifesto Individuals and Interactions over Processes and Tools Working Software over Comprehensive Documentation Responding to Change over Following a Plan Customer Collaboration over Contract Negotiation
Overview – Agile Defined Agile Manifesto Don’t rely on tools and processes to save you Talk to your data experts, your developers Find common ground Make incremental improvements Build a safety net
Overview – Goals of DataDude Integrated Change Management for DB Apps Provide Tools to Manage DB Projects Assess effects of changes to the DB Help work in an isolated environment Help test Updated Solutions Simplify the deployment of changes Facilitate Collaborative Development
Overview - Editions DataDude (2005) (Add on to VSTS) Team Edition for Database Professionals (2008)  Visual Studio Team System Database Edition (2010) Comes for free with VSTS Developer Edition
Overview - Tasks Create and Deploy DBs under version control Put existing DBs under version control Modify Offline Representations and deploy Compare Schemas and Data between DBs Develop and run unit tests against DB Objects Generate repeatable test data Refactor database changes across the DB
Agenda Overview Features Demos Availability Other Tools
Features Version Control Database VC a new or existing database Refactor Compare DBs Generate Deployment Script Unit Test Database TDD Schema Changes TDD Business Logic Change Generate Test Data
Agenda Overview Features Demos Availability Other Tools
Demo – Version Control Database Cory Foy http://coryfoy.com | @cory_foy
Version Control Database Existing Schema Two Tables   - Msgs, Users Two Stored Procs  - PostMsg: Posts a message for a specific user to the database. Take a UserID and varchar message as a parameter  - UserInfo: Takes a UserID and returns back all of the user information, including how many posts they’ve made
Version Control Database Get Database Under Version Control
Version Control Database You now have an isolated copy you can work with that doesn’t change the source database
Version Control Database We want to change “Msg” to “Neet” everywhere it exists in the database. This will require a change to the Msg column in the Msgs table, renaming the Msgs table itself, and updating the Stored Procs. We could do it by hand, or we could use the refactoring tools built in.
Version Control Database We use the Refactoring Option to rename the column, and by selecting “Preview Changes” it shows us what it is going to do
Version Control Database We can even see the change in the context of the DDL by clicking on the change in the preview window. When we click on Apply, the change is made to the version controlled local copy.
Version Control Database We also need to change the table name, so we can use refactor for that as well. It detects the need to update the constraints as well
Version Control Database Changing the Stored Proc name can also be done through Refactoring But changes to the values returned from the UserInfo stored proc would have to be done by hand.  Also note that the refactorings only look at the database – not your code which accesses or uses the database
Version Control Database We can now compare the schema changes to see how this would deploy it to the database
Version Control Database By Default, choosing to deploy creates a script which can be run to do the actual deploy
Version Control Database We can also choose to have it automatically deploy to a database. For example, here we have it deploying to a local SQLEXPRESS Database And everything should still work (meaning no data loss)
Demo – Unit Test Database Cory Foy http://coryfoy.com | @cory_foy
Unit Test Database We have a new requirement for scalability. Instead of doing a count for user_info, we want a new table which just has a user_id and count. This field should be updated on every Neet by the user, and should be the one queried to get the current Neet count. But, we want to make sure that all of our existing scripts work. Unit Testing to the rescue! Add a new Test Project to our Solution, then delete the UnitTest1.cs file. Then right-click on the Test Project, do a New->Add Item, and select Data -> Database Unit Test. Name it NewNeetUpdatesNeetCount
Unit Test Database Database Unit Tests execute against a real database connection. So we’ll need to tell it which database to execute against. Note that in this screen we can Execute the tests using one connection, and validate using a different one. This is important when you have your production code which uses a limited access user. We’ll also have it automatically deploy the project to the database before the tests are run
Unit Test Database We now see the DB Test screen. Note that we can have Pre-test, Test and Post-test scripts. These can be used for setting the database into specific states. All tests are written in SQL, and then validated by the Test Conditions at the bottom of the screen.
Unit Test Database What we’re wanting to test is that, with an existing user who as Neet’d before, when we call the PostNeet stored procedure, that the NeetCount table is updated. So our test will look something like this (Arrange, Act, Assert) We’ll then need to remove the default test condition (by clicking the Red x) Then we’ll choose “Scalar Value” from the drop down and hit the plus sign, configuring it to expect that the last query our test runs will return the value “1” in Row 1, Column 1
Unit Test Database Next, we’ll run the test using the built-in runner. Note that the first time you run the tests, it will be slow because it has to synchronize some information. You’ll see the test failed, and if you right-click on the test and say “View Test Result Details” you can see the exact error message The error is that we don’t actually have a table called NeetCount. Well, this will never do, let’s add it.
Unit Test Database So, do we go to the database? Nope – we make the change right from our project. In our schema view, we click on the Tables node, then choose Add->Table.  We’ll name it NeetCount so we stand a chance of having our unit tests pass. We’ll also fill out the DDL to have the columns we are looking for and save it.
Unit Test Database Now we’ll rerun our tests. And they fail again. But for a different reason this time Yep. Our table is there, but nothing is being inserted. How did our table get there? Remember that when we setup the test we told it to deploy the DB project before running tests. So it added the table to the database before running the tests – just by us clicking Run.
Unit Test Database Now that our unit test is failing for logic reasons, we can make the change to the stored procedure. Again, we change it by opening it from the Schema view rather than changing the database directly. But look at the proc. We’ve found our first business decision – at what point should UserID records get inserted into NeetCount? It’s decided that upon user creation a record will get inserted in, and that we’ll need a migration effort to move existing records. With those taken care of, we can focus back on modifying this Proc. (Yes, we’re ignoring transactions, etc for now)
Unit Test Database After updating our test database with the migration of data, we can rerun our tests And everything runs succesfully! Now we know that post a Neet updates the correct count. It’s now time to make sure that UserInfo is pulling the information from the right table
Unit Test Database We create another Database Unit Test called UserInfoGetsCountFromNeetCount. We’ll update the NeetCount for our test user, then execute the UserInfo and we should see the updated count. We’ll start with choosing the Pre-test option and filling it in, then setting the actual test up And running our tests shows it fails for the “right” reason
Unit Test Database So now we’ll modify our UserInfo stored proc to make the test pass. And it does!
Unit Test Database While what we’ve done works, it is better to capture the logic of a change like this in a way that shows the business value and the coupling. Let’s create a new Database Unit Test and call it EnhanceUserInformationPerformance. We’ll add two tests in this for the two conditions we covered previously. Use the Rename button to rename the first test, then click the plus sign to add the second test. Copy over the logic for each from the previous tests, making sure to include the Test Conditions and PreTests
Unit Test Database Now let’s make sure they run. Go to Test->Run All Tests in Solution and they should all pass.  Now, let’s delete the first two tests we created in favor of this new combined test, and rerun all of our tests to make sure. All well and good so far. But we should write one more test which does everything end-to-end
Unit Test Database Add a new Database Unit Test and call it NewNeetUpdatesUserInfo. We’ll use a temp table to capture the results of the UserInfo to compare against our expected values. Note that you *can’t* do this in pre/post test since those run in different connections, therefore the variables can’t be shared. So run the tests and see it green. Right? Or not. So what happened? Remember our Unit Test which updated the NeetTable directly? It’s polluted our DB, which invalidates our results.
Unit Test Database To get around that, we need to either run things in transactions (which we have to do all in the Test Script) or back our changes out. In this case, we’ll just revert the changes at the end of the script in Post Test. So modify the UserInfoGetsCountFromNeetCount test and add the following to Post-test Now go to Test->Run-> All Tests in Solution and what do we see? That’s much better
Unit Test Database One thing to note about the Database tests – they are all just code and xml underneath. For example, right-clicking on one of our tests and saying “View Code” shows the “magic” happening underneath These are just MSTest unit tests, so you can drop to the code level if there are special things you need to do with the tests.
Demo – Generating Data Cory Foy http://coryfoy.com | @cory_foy
Generating Data In our last demo, we saw the challenges data can cause us. We can use the data generation tool to help us populate the information in our database. Right-Click on the Knitter project in Solution Explorer and choose Add-> Data Generation Plan, then name it TestData.  If you click on dbo.Neets, you’ll see that UserID shows as a Foreign Key, but it doesn’t in dbo.NeetCount (because we didn’t add it). Fix that by Right-Clicking on the NeetCount table in Schema View and choosing Add->Foreign Key. Once you’ve filled it in, deploy it, then refresh the data plan
Generating Data NeetCount.totalNeets is effectively a computed field, but it’s not. So make sure the generator is Integer, and change the properties to be Max and Min of 1 We also only want our usernames (UserID in the Users table) to only be AlphaNumeric. So change the generator for that column to be Regular Expression, and in the Properties window, enter [a-zA-Z0-9]*_[a-zA-Z0-9]* then right-click on the UserID row and choose “Preview Data Generation”. As you can see, the characters run the gamut.
Generating Data With our test plan in place, we can have our unit tests use it automatically. Go to Test->Database Test Configuration and check the “Generate Test Data” box. Now whenever the tests are run, the data is generated automatically. Let’s rerun our tests to show the difference. What a difference! They all fail because there is no UserID “1” in the database.
Generating Data To fix that, we’ll update the tests to select the top user from the users table, making sure we update all 3 tests, and the pre and post tests where necessary. And that looks much better
Generating Data If we do an actual query in our test database, we’ll see that indeed the values get inserted there The Database Edition comes with several generators – for each type, the Regular Expression generator, and Databound generators which can pull data from any connection – other databases, Excel files, CSV files, etc.
Agenda Overview Features Demos Availability Other Tools
Availability Available in VS2005, VS2008 and VS2010 Requires a Team Edition of Visual Studio Comes with the Team Edition for Software Developers
Availability Supports SQL 2000, 2005 and 2008 Plans are in place to provide data bindings for other platforms but no timeline
Agenda Overview Features Demos Availability Other Tools
Other Tools DBFit Supports Oracle, DB2, SQL Server and MySQL Free of charge Add on to the FitNesse testing framework http://www.fitnesse.info/dbfit:reference http://gojko.net/fitnesse/dbfit/
Other Tools AnyDbTest Supports Oracle, SQL Server and MySQL Uses XML as the backing http://www.anydbtest.com/
Other Tools Agile Data – Scott Ambler Agile Database Techniques Refactoring Databases http://www.agiledata.org
More Information http://msdn.microsoft.com/teamsystem http://www.infoq.com/articles/tdd-dbpro-Foy http://blog.coryfoy.com/2007/07/test-driving-stored-procedures-in-sql-server-in-vs2008/ http://www.coryfoy.com foyc at coryfoy dot com @cory_foy on Twitter Slides will be posted on CoryFoy.com

More Related Content

Similar to Test Driven Database Development With Data Dude

Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databaseselliando dias
 
performancetestingjmeter-121109061704-phpapp02
performancetestingjmeter-121109061704-phpapp02performancetestingjmeter-121109061704-phpapp02
performancetestingjmeter-121109061704-phpapp02Gopi Raghavendra
 
performancetestingjmeter-121109061704-phpapp02 (1)
performancetestingjmeter-121109061704-phpapp02 (1)performancetestingjmeter-121109061704-phpapp02 (1)
performancetestingjmeter-121109061704-phpapp02 (1)QA Programmer
 
Testing Software Solutions
Testing Software SolutionsTesting Software Solutions
Testing Software Solutionsgavhays
 
Introduction to testing.
Introduction to testing.Introduction to testing.
Introduction to testing.Jithinctzz
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769subhasis100
 
qtp 9.2 features
qtp 9.2 featuresqtp 9.2 features
qtp 9.2 featureskrishna3032
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorialsasidhar
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769subhasis100
 
Qtp 92 Tutorial Anil
Qtp 92 Tutorial AnilQtp 92 Tutorial Anil
Qtp 92 Tutorial Anilguest3373d3
 
Qtp 9.2 Tutorial
Qtp 9.2 TutorialQtp 9.2 Tutorial
Qtp 9.2 Tutorialguest37ae7f
 
Performance testing and j meter
Performance testing and j meterPerformance testing and j meter
Performance testing and j meterPurna Chandar
 
Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Shivakumara .
 

Similar to Test Driven Database Development With Data Dude (20)

Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databases
 
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
 
performancetestingjmeter-121109061704-phpapp02
performancetestingjmeter-121109061704-phpapp02performancetestingjmeter-121109061704-phpapp02
performancetestingjmeter-121109061704-phpapp02
 
performancetestingjmeter-121109061704-phpapp02 (1)
performancetestingjmeter-121109061704-phpapp02 (1)performancetestingjmeter-121109061704-phpapp02 (1)
performancetestingjmeter-121109061704-phpapp02 (1)
 
Testing Software Solutions
Testing Software SolutionsTesting Software Solutions
Testing Software Solutions
 
Introduction to testing.
Introduction to testing.Introduction to testing.
Introduction to testing.
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
qtp 9.2 features
qtp 9.2 featuresqtp 9.2 features
qtp 9.2 features
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorial
 
Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
Qtp 92 Tutorial Anil
Qtp 92 Tutorial AnilQtp 92 Tutorial Anil
Qtp 92 Tutorial Anil
 
Qtp 9.2 Tutorial
Qtp 9.2 TutorialQtp 9.2 Tutorial
Qtp 9.2 Tutorial
 
Qtp 92 Tutorial769
Qtp 92 Tutorial769Qtp 92 Tutorial769
Qtp 92 Tutorial769
 
QTP_PRESENTATION_Andy
QTP_PRESENTATION_AndyQTP_PRESENTATION_Andy
QTP_PRESENTATION_Andy
 
Test automation
Test automationTest automation
Test automation
 
Performance testing and j meter
Performance testing and j meterPerformance testing and j meter
Performance testing and j meter
 
Database Testing
Database TestingDatabase Testing
Database Testing
 
Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02Performancetestingjmeter 121109061704-phpapp02
Performancetestingjmeter 121109061704-phpapp02
 
MSSQL Queries.pdf
MSSQL Queries.pdfMSSQL Queries.pdf
MSSQL Queries.pdf
 

More from Cory Foy

Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...
Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...
Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...Cory Foy
 
Stratgic Play - Doing the Right Thing at the Right Time
Stratgic Play - Doing the Right Thing at the Right TimeStratgic Play - Doing the Right Thing at the Right Time
Stratgic Play - Doing the Right Thing at the Right TimeCory Foy
 
Continuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software WestContinuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software WestCory Foy
 
Choosing Between Scrum and Kanban - TriAgile 2015
Choosing Between Scrum and Kanban - TriAgile 2015Choosing Between Scrum and Kanban - TriAgile 2015
Choosing Between Scrum and Kanban - TriAgile 2015Cory Foy
 
Code Katas
Code KatasCode Katas
Code KatasCory Foy
 
Distributed Agility
Distributed AgilityDistributed Agility
Distributed AgilityCory Foy
 
Scaling Agility
Scaling AgilityScaling Agility
Scaling AgilityCory Foy
 
Kanban for DevOps
Kanban for DevOpsKanban for DevOps
Kanban for DevOpsCory Foy
 
Ruby and OO for Beginners
Ruby and OO for BeginnersRuby and OO for Beginners
Ruby and OO for BeginnersCory Foy
 
Agile Roots: The Agile Mindset - Agility Across the Organization
Agile Roots: The Agile Mindset - Agility Across the OrganizationAgile Roots: The Agile Mindset - Agility Across the Organization
Agile Roots: The Agile Mindset - Agility Across the OrganizationCory Foy
 
Triangle.rb - How Secure is Your Rails Site, Anyway?
Triangle.rb - How Secure is Your Rails Site, Anyway?Triangle.rb - How Secure is Your Rails Site, Anyway?
Triangle.rb - How Secure is Your Rails Site, Anyway?Cory Foy
 
Scrum vs Kanban - Implementing Agility at Scale
Scrum vs Kanban - Implementing Agility at ScaleScrum vs Kanban - Implementing Agility at Scale
Scrum vs Kanban - Implementing Agility at ScaleCory Foy
 
SQE Boston - When Code Cries
SQE Boston - When Code CriesSQE Boston - When Code Cries
SQE Boston - When Code CriesCory Foy
 
GOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesGOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesCory Foy
 
Rails as a Pattern Language
Rails as a Pattern LanguageRails as a Pattern Language
Rails as a Pattern LanguageCory Foy
 
Patterns in Rails
Patterns in RailsPatterns in Rails
Patterns in RailsCory Foy
 
Agile Demystified
Agile DemystifiedAgile Demystified
Agile DemystifiedCory Foy
 
When Code Cries
When Code CriesWhen Code Cries
When Code CriesCory Foy
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# DevelopersCory Foy
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataCory Foy
 

More from Cory Foy (20)

Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...
Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...
Defending Commoditization: Mapping Gameplays and Strategies to Stay Ahead in ...
 
Stratgic Play - Doing the Right Thing at the Right Time
Stratgic Play - Doing the Right Thing at the Right TimeStratgic Play - Doing the Right Thing at the Right Time
Stratgic Play - Doing the Right Thing at the Right Time
 
Continuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software WestContinuous Deployment and Testing Workshop from Better Software West
Continuous Deployment and Testing Workshop from Better Software West
 
Choosing Between Scrum and Kanban - TriAgile 2015
Choosing Between Scrum and Kanban - TriAgile 2015Choosing Between Scrum and Kanban - TriAgile 2015
Choosing Between Scrum and Kanban - TriAgile 2015
 
Code Katas
Code KatasCode Katas
Code Katas
 
Distributed Agility
Distributed AgilityDistributed Agility
Distributed Agility
 
Scaling Agility
Scaling AgilityScaling Agility
Scaling Agility
 
Kanban for DevOps
Kanban for DevOpsKanban for DevOps
Kanban for DevOps
 
Ruby and OO for Beginners
Ruby and OO for BeginnersRuby and OO for Beginners
Ruby and OO for Beginners
 
Agile Roots: The Agile Mindset - Agility Across the Organization
Agile Roots: The Agile Mindset - Agility Across the OrganizationAgile Roots: The Agile Mindset - Agility Across the Organization
Agile Roots: The Agile Mindset - Agility Across the Organization
 
Triangle.rb - How Secure is Your Rails Site, Anyway?
Triangle.rb - How Secure is Your Rails Site, Anyway?Triangle.rb - How Secure is Your Rails Site, Anyway?
Triangle.rb - How Secure is Your Rails Site, Anyway?
 
Scrum vs Kanban - Implementing Agility at Scale
Scrum vs Kanban - Implementing Agility at ScaleScrum vs Kanban - Implementing Agility at Scale
Scrum vs Kanban - Implementing Agility at Scale
 
SQE Boston - When Code Cries
SQE Boston - When Code CriesSQE Boston - When Code Cries
SQE Boston - When Code Cries
 
GOTO Berlin - When Code Cries
GOTO Berlin - When Code CriesGOTO Berlin - When Code Cries
GOTO Berlin - When Code Cries
 
Rails as a Pattern Language
Rails as a Pattern LanguageRails as a Pattern Language
Rails as a Pattern Language
 
Patterns in Rails
Patterns in RailsPatterns in Rails
Patterns in Rails
 
Agile Demystified
Agile DemystifiedAgile Demystified
Agile Demystified
 
When Code Cries
When Code CriesWhen Code Cries
When Code Cries
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 

Recently uploaded

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 

Recently uploaded (20)

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 

Test Driven Database Development With Data Dude

  • 1. Test-Driven Database Development with DataDude Cory Foy http://coryfoy.com | @cory_foy
  • 2. Agenda Overview Features Demos Availability Other Tools
  • 3. Agenda Overview Features Demos Availability Other Tools
  • 4. Overview – TDD TDD == Test-Driven Development Developer Design Technique Red-Green-Refactor Write a failing test Write just enough code to make it pass Refactor any duplication
  • 5. Overview – TDD in DBs Why is agility at the database so hard? Control / Permission issues Tightly coupled applications No good testing tools Data Migration issues If it hurts, do it more
  • 6. Overview – Agile Defined Agile Manifesto Individuals and Interactions over Processes and Tools Working Software over Comprehensive Documentation Responding to Change over Following a Plan Customer Collaboration over Contract Negotiation
  • 7. Overview – Agile Defined Agile Manifesto Don’t rely on tools and processes to save you Talk to your data experts, your developers Find common ground Make incremental improvements Build a safety net
  • 8. Overview – Goals of DataDude Integrated Change Management for DB Apps Provide Tools to Manage DB Projects Assess effects of changes to the DB Help work in an isolated environment Help test Updated Solutions Simplify the deployment of changes Facilitate Collaborative Development
  • 9. Overview - Editions DataDude (2005) (Add on to VSTS) Team Edition for Database Professionals (2008) Visual Studio Team System Database Edition (2010) Comes for free with VSTS Developer Edition
  • 10. Overview - Tasks Create and Deploy DBs under version control Put existing DBs under version control Modify Offline Representations and deploy Compare Schemas and Data between DBs Develop and run unit tests against DB Objects Generate repeatable test data Refactor database changes across the DB
  • 11. Agenda Overview Features Demos Availability Other Tools
  • 12. Features Version Control Database VC a new or existing database Refactor Compare DBs Generate Deployment Script Unit Test Database TDD Schema Changes TDD Business Logic Change Generate Test Data
  • 13. Agenda Overview Features Demos Availability Other Tools
  • 14. Demo – Version Control Database Cory Foy http://coryfoy.com | @cory_foy
  • 15. Version Control Database Existing Schema Two Tables - Msgs, Users Two Stored Procs - PostMsg: Posts a message for a specific user to the database. Take a UserID and varchar message as a parameter - UserInfo: Takes a UserID and returns back all of the user information, including how many posts they’ve made
  • 16. Version Control Database Get Database Under Version Control
  • 17. Version Control Database You now have an isolated copy you can work with that doesn’t change the source database
  • 18. Version Control Database We want to change “Msg” to “Neet” everywhere it exists in the database. This will require a change to the Msg column in the Msgs table, renaming the Msgs table itself, and updating the Stored Procs. We could do it by hand, or we could use the refactoring tools built in.
  • 19. Version Control Database We use the Refactoring Option to rename the column, and by selecting “Preview Changes” it shows us what it is going to do
  • 20. Version Control Database We can even see the change in the context of the DDL by clicking on the change in the preview window. When we click on Apply, the change is made to the version controlled local copy.
  • 21. Version Control Database We also need to change the table name, so we can use refactor for that as well. It detects the need to update the constraints as well
  • 22. Version Control Database Changing the Stored Proc name can also be done through Refactoring But changes to the values returned from the UserInfo stored proc would have to be done by hand. Also note that the refactorings only look at the database – not your code which accesses or uses the database
  • 23. Version Control Database We can now compare the schema changes to see how this would deploy it to the database
  • 24. Version Control Database By Default, choosing to deploy creates a script which can be run to do the actual deploy
  • 25. Version Control Database We can also choose to have it automatically deploy to a database. For example, here we have it deploying to a local SQLEXPRESS Database And everything should still work (meaning no data loss)
  • 26. Demo – Unit Test Database Cory Foy http://coryfoy.com | @cory_foy
  • 27. Unit Test Database We have a new requirement for scalability. Instead of doing a count for user_info, we want a new table which just has a user_id and count. This field should be updated on every Neet by the user, and should be the one queried to get the current Neet count. But, we want to make sure that all of our existing scripts work. Unit Testing to the rescue! Add a new Test Project to our Solution, then delete the UnitTest1.cs file. Then right-click on the Test Project, do a New->Add Item, and select Data -> Database Unit Test. Name it NewNeetUpdatesNeetCount
  • 28. Unit Test Database Database Unit Tests execute against a real database connection. So we’ll need to tell it which database to execute against. Note that in this screen we can Execute the tests using one connection, and validate using a different one. This is important when you have your production code which uses a limited access user. We’ll also have it automatically deploy the project to the database before the tests are run
  • 29. Unit Test Database We now see the DB Test screen. Note that we can have Pre-test, Test and Post-test scripts. These can be used for setting the database into specific states. All tests are written in SQL, and then validated by the Test Conditions at the bottom of the screen.
  • 30. Unit Test Database What we’re wanting to test is that, with an existing user who as Neet’d before, when we call the PostNeet stored procedure, that the NeetCount table is updated. So our test will look something like this (Arrange, Act, Assert) We’ll then need to remove the default test condition (by clicking the Red x) Then we’ll choose “Scalar Value” from the drop down and hit the plus sign, configuring it to expect that the last query our test runs will return the value “1” in Row 1, Column 1
  • 31. Unit Test Database Next, we’ll run the test using the built-in runner. Note that the first time you run the tests, it will be slow because it has to synchronize some information. You’ll see the test failed, and if you right-click on the test and say “View Test Result Details” you can see the exact error message The error is that we don’t actually have a table called NeetCount. Well, this will never do, let’s add it.
  • 32. Unit Test Database So, do we go to the database? Nope – we make the change right from our project. In our schema view, we click on the Tables node, then choose Add->Table. We’ll name it NeetCount so we stand a chance of having our unit tests pass. We’ll also fill out the DDL to have the columns we are looking for and save it.
  • 33. Unit Test Database Now we’ll rerun our tests. And they fail again. But for a different reason this time Yep. Our table is there, but nothing is being inserted. How did our table get there? Remember that when we setup the test we told it to deploy the DB project before running tests. So it added the table to the database before running the tests – just by us clicking Run.
  • 34. Unit Test Database Now that our unit test is failing for logic reasons, we can make the change to the stored procedure. Again, we change it by opening it from the Schema view rather than changing the database directly. But look at the proc. We’ve found our first business decision – at what point should UserID records get inserted into NeetCount? It’s decided that upon user creation a record will get inserted in, and that we’ll need a migration effort to move existing records. With those taken care of, we can focus back on modifying this Proc. (Yes, we’re ignoring transactions, etc for now)
  • 35. Unit Test Database After updating our test database with the migration of data, we can rerun our tests And everything runs succesfully! Now we know that post a Neet updates the correct count. It’s now time to make sure that UserInfo is pulling the information from the right table
  • 36. Unit Test Database We create another Database Unit Test called UserInfoGetsCountFromNeetCount. We’ll update the NeetCount for our test user, then execute the UserInfo and we should see the updated count. We’ll start with choosing the Pre-test option and filling it in, then setting the actual test up And running our tests shows it fails for the “right” reason
  • 37. Unit Test Database So now we’ll modify our UserInfo stored proc to make the test pass. And it does!
  • 38. Unit Test Database While what we’ve done works, it is better to capture the logic of a change like this in a way that shows the business value and the coupling. Let’s create a new Database Unit Test and call it EnhanceUserInformationPerformance. We’ll add two tests in this for the two conditions we covered previously. Use the Rename button to rename the first test, then click the plus sign to add the second test. Copy over the logic for each from the previous tests, making sure to include the Test Conditions and PreTests
  • 39. Unit Test Database Now let’s make sure they run. Go to Test->Run All Tests in Solution and they should all pass. Now, let’s delete the first two tests we created in favor of this new combined test, and rerun all of our tests to make sure. All well and good so far. But we should write one more test which does everything end-to-end
  • 40. Unit Test Database Add a new Database Unit Test and call it NewNeetUpdatesUserInfo. We’ll use a temp table to capture the results of the UserInfo to compare against our expected values. Note that you *can’t* do this in pre/post test since those run in different connections, therefore the variables can’t be shared. So run the tests and see it green. Right? Or not. So what happened? Remember our Unit Test which updated the NeetTable directly? It’s polluted our DB, which invalidates our results.
  • 41. Unit Test Database To get around that, we need to either run things in transactions (which we have to do all in the Test Script) or back our changes out. In this case, we’ll just revert the changes at the end of the script in Post Test. So modify the UserInfoGetsCountFromNeetCount test and add the following to Post-test Now go to Test->Run-> All Tests in Solution and what do we see? That’s much better
  • 42. Unit Test Database One thing to note about the Database tests – they are all just code and xml underneath. For example, right-clicking on one of our tests and saying “View Code” shows the “magic” happening underneath These are just MSTest unit tests, so you can drop to the code level if there are special things you need to do with the tests.
  • 43. Demo – Generating Data Cory Foy http://coryfoy.com | @cory_foy
  • 44. Generating Data In our last demo, we saw the challenges data can cause us. We can use the data generation tool to help us populate the information in our database. Right-Click on the Knitter project in Solution Explorer and choose Add-> Data Generation Plan, then name it TestData. If you click on dbo.Neets, you’ll see that UserID shows as a Foreign Key, but it doesn’t in dbo.NeetCount (because we didn’t add it). Fix that by Right-Clicking on the NeetCount table in Schema View and choosing Add->Foreign Key. Once you’ve filled it in, deploy it, then refresh the data plan
  • 45. Generating Data NeetCount.totalNeets is effectively a computed field, but it’s not. So make sure the generator is Integer, and change the properties to be Max and Min of 1 We also only want our usernames (UserID in the Users table) to only be AlphaNumeric. So change the generator for that column to be Regular Expression, and in the Properties window, enter [a-zA-Z0-9]*_[a-zA-Z0-9]* then right-click on the UserID row and choose “Preview Data Generation”. As you can see, the characters run the gamut.
  • 46. Generating Data With our test plan in place, we can have our unit tests use it automatically. Go to Test->Database Test Configuration and check the “Generate Test Data” box. Now whenever the tests are run, the data is generated automatically. Let’s rerun our tests to show the difference. What a difference! They all fail because there is no UserID “1” in the database.
  • 47. Generating Data To fix that, we’ll update the tests to select the top user from the users table, making sure we update all 3 tests, and the pre and post tests where necessary. And that looks much better
  • 48. Generating Data If we do an actual query in our test database, we’ll see that indeed the values get inserted there The Database Edition comes with several generators – for each type, the Regular Expression generator, and Databound generators which can pull data from any connection – other databases, Excel files, CSV files, etc.
  • 49. Agenda Overview Features Demos Availability Other Tools
  • 50. Availability Available in VS2005, VS2008 and VS2010 Requires a Team Edition of Visual Studio Comes with the Team Edition for Software Developers
  • 51. Availability Supports SQL 2000, 2005 and 2008 Plans are in place to provide data bindings for other platforms but no timeline
  • 52. Agenda Overview Features Demos Availability Other Tools
  • 53. Other Tools DBFit Supports Oracle, DB2, SQL Server and MySQL Free of charge Add on to the FitNesse testing framework http://www.fitnesse.info/dbfit:reference http://gojko.net/fitnesse/dbfit/
  • 54. Other Tools AnyDbTest Supports Oracle, SQL Server and MySQL Uses XML as the backing http://www.anydbtest.com/
  • 55. Other Tools Agile Data – Scott Ambler Agile Database Techniques Refactoring Databases http://www.agiledata.org
  • 56. More Information http://msdn.microsoft.com/teamsystem http://www.infoq.com/articles/tdd-dbpro-Foy http://blog.coryfoy.com/2007/07/test-driving-stored-procedures-in-sql-server-in-vs2008/ http://www.coryfoy.com foyc at coryfoy dot com @cory_foy on Twitter Slides will be posted on CoryFoy.com