Skip to main content

Submission correctness tests for SQL

Project description


Build Status codecov PyPI version

sqlwhat enables you to write Submission Correctness Tests (SCTs) for interactive SQL exercises on DataCamp.


pip install sqlwhat     # install from pypi
make install            # install from source


Raising issues with how SQL is parsed

Please raise an issue on the respsective parser repo:

Basic Use

from sqlwhat.State import State    # State holds info needed for tests
from sqlwhat.Reporter import Reporter
from sqlwhat.checks import *       # imports all SCTs
from sqlalchemy import create_engine

code = "SELECT * FROM artists WHERE id < 100"

state = State(
    student_code = code,
    solution_code = code,
    pre_exercise_code = "",
    student_conn = create_engine('sqlite:///'),
    solution_conn = create_engine('sqlite:///'),
    student_result = {'id': [1,2,3], 'name': ['greg', 'jon', 'martha']},
    solution_result = {'id': [1,2,3], 'name': ['toby', 'keith', 'deb']},
    reporter = Reporter()

# test below passes, since code is equal for student and solution

# test below raises a TestFail error, since 'name' col of results
# doesn't match between student and solution results
# shows error data

# can also be done using a chain
from sqlwhat.sct_syntax import Ex

Running unit tests

pytest -m "not backend"

If you also want to run the backend tests, you need psycopg2. to install psycopg2 in a virtualenv, I needed to run..

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip install --no-cache psycopg2

Rules of testing

  1. Running queries is the backend's job
  2. If a test doesn't run queries, it doesn't need the backend
  3. Very few tests should run queries

Building Docs

Install sqlwhat and run ..

cd docs
make html

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sqlwhat, version 3.0.0
Filename, size File type Python version Upload date Hashes
Filename, size sqlwhat-3.0.0.tar.gz (7.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page