GithubHelp home page GithubHelp logo

sql-tasks's Introduction

PostgreSQL scaffolding for RealSkill

You can quickly create PostgreSQL tasks by cloning this repository. The scaffolding provides PostgreSQL driver configuration, mocha tests setup and simple SQL test scenarios runner.

Setup

To install dependencies

npm install

PgSQL test runner

Scenarios format

Provided runner parses plain text file as below (for syntax highlighting the sql format is recommended).

--statement="../solution/schema.sql" Seed schema
--statement insert valid row
INSERT INTO users(email) VALUES ('[email protected]'),('[email protected]');
--statement
SELECT * FROM users;
--expect 2 users
id,email
1,[email protected]
2,[email protected]
--statement="select_all_users.sql" Select all users
--expect="expected_users.csv" 2 users
--statement insert incorrect row
INSERT INTO users(email) VALUES (1,2,3,4)
--expect syntax error
name,code
error,SQL-42601

That would be translated into following scenarios:

Evaluate scenario
    Statement Seed schema
      ✓ should be successfull
    Statement insert valid row
      ✓ should be successfull
    Statement SELECT * FROM users;
      ✓ should return 2 users
    Statement Select all users
      ✓ should return 2 users
    Statement insert incorrect row
      ! error: INSERT has more expressions than target columns
        code: 42601
        routine: transformInsertRow 
      ✓ should return syntax error

Scenarios are composed of 2 types of instructions: statement and expect. Each instruction symbol must be prepended with --. Last executed statement result is stored and compared with subsequent expect. Statement and expected content can be provided inline (in subsequent lines, before next instruction symbol) as well as extracted into separate file (see example above). You can put some inline comment after --statement or --expect statement if instruction doesn't point to external file. Those comments will be displayed with tests results. Expect must be a valid CSV data set, with column names in first row. You can expect data set response as well as SQL error. To test error you need to specify 2x2 csv table as follows:

name code
error SQL-errorCode

Code value must be valid PostgreSQL error code prefixed with SQL- string. Please place your test scenario in test/scenario.sql (you can find example scenario file there).

Database connection

You are required to provide valid connection to working PostgreSQL instance. This scaffolding is tested on PostgreSQL 9.4, however it should work on other database versions. If you just installed fresh version of PostgreSQL server don't forget to enable listening, setting listen_address = 'localhost' in PostgreSQL configuration file (on most *nix system it's located at /etc/postgresql/9.4/main/postgresql.conf). You may also have to adjust Host Based Authentication Policy that is described in pg_hba.conf file (recommended authentication method is MD5).

User, database and schema

Configuration on *nix systems

You can manually prepare database connection or use command below that will create user, database, and set appropriate ownerships. Command below must be run from postgres system user (switch to root user then switch to postgres by su postgres). When prompted for password, enter password realskill.

createuser realskill -P && createdb realskill -O realskill && psql -d realskill -c 'ALTER SCHEMA public OWNER TO realskill;'

Configuration on Windows systems

Using GUI tools

Use pgadmin to set following configuration:

user: realskill
password: realskill
database: realskill
schema: public

Database and schema owner must be set to realskill user.

Using command line

Open Windows Command Prompt as administrator (see help).

Create user realskill with realskill password.

createuser -P -U postgres -W realskill

You will be prompted for new user password twice, then postgres superuser password (default is postgres).

Create database realskill and set ownership to user realskill (you will be prompted for postgres password).

createdb -O realskill -U postgres -W realskill

Change schema public (of realskill database) ownership to user realskill (you will be prompted for postgres password).

psql -d realskill -U postgres -W -c "ALTER SCHEMA public OWNER TO realskill;"

Run tests

npm test

sql-tasks's People

Contributors

jbeynar avatar

Watchers

 avatar

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.