Code Monkey home page Code Monkey logo

pgtap-test-constraints's Introduction

PgTap Test Constraints

Introduction

The purpose of this repo is to show a quick demonstration on how to use pgtap to validate data integrity for Postgres databases. In this simple example, we show how to utilize and test indexes to prevent a room being double booked for a hotel suite. A summary of the steps are:

  1. Create a database that has a bookings table
  2. Create a series of tests for bookings table (including failing test for conflict booking)
  3. Run tests to see failure
  4. Update database with unique index to fix failing test
  5. Re-run tests to ensure previous failing test now passes

Requirements

Steps

  1. Start Postgres database server
docker-compose up -d db
  1. Initialize Database

Create awesome_hotel_booking database and bookings table.

Review init_db.sql to see details

docker-compose run db psql -h db -U test_pg_tap -f /seeds/init_db.sql
  1. Run pgtap tests

Review pgtap/bookings.sql and pgtap documentation to understand the test cases.

The 7th test should fail which tries to insert a double booking for the same room.

docker-compose run pgtap

## OUTPUT
Running tests: /test/*.sql
/test/bookings.sql .. 1/8
# Failed test 7: "do not allow two bookings for the same room on the same date"
#     no exception thrown
# Looks like you failed 1 test of 8
/test/bookings.sql .. Failed 1/8 subtests

Test Summary Report
-------------------
/test/bookings.sql (Wstat: 0 Tests: 8 Failed: 1)
  Failed test:  7
  Files=1, Tests=8,  0 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU)
  Result: FAIL
  1. Create unique index

See create_uq_index.sql for details.

docker-compose run db psql -h db -U test_pg_tap -d awesome_hotel_booking \
  -f /seeds/create_uq_index.sql
  1. Re-run pg tap tests

All of the tests should pass now.

docker-compose run pgtap

## OUTPUT
Running tests: /test/*.sql
/test/bookings.sql .. ok
All tests successful.
Files=1, Tests=8,  0 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU)
Result: PASS
  1. Stop Docker
docker-compose down

pgtap-test-constraints's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

mailtoshrb

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.