Skip to main content

The library provides SQL calls for the Python unittest framework

Project description

pyodbc-unittest

The library provides SQL calls for the Python unittest framework

Unit Test

Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended.

Database Unit Test

The SQL unit testing approach allows us to test part of database objects such as stored procedures, functions and schema. The advantage of SQL unit testing is to develop more robust database designs, because these objects have already been checked before production deployment, so SQL unit testing process allows us to minimize the errors, which are related to database objects.

Quick Start

Install pyodbc-unittest from pypi using pip

pip install pyodbc-unittest

Import Dbtest object in your module

import unittest
from pyodbc_unittest import Dbtest

Now you need to setup ODBC data source. Select the type of database you want to set up a database for, for example, SAP/Sybase ASE, MS SQL Server, PostgreSQL, etc. Moreover, fill the login, password and server fields.

Write your first test like in the example. Please fill in CONNECTION_STRING with the name of the ODBC data source.

CONNECTION_STRING = r'DSN=mssql.local'

class TestSelect(unittest.TestCase):

    def test_data(self):
        database = Dbtest(CONNECTION_STRING)
        sql = 'SELECT 1 AS ONE'
        file_name = 'SELECTONE'
        self.assertEqual(database.from_db(sql, file_name),
                         database.from_file(file_name))
        database.close()

And run unittest

> python -m unittest
.
----------------------------------------------------------------------
Ran 1 test in 0.112s

OK

An artifact named SELECTONE.json was created that contains all the information about the result set.

{
  "rowcount": 1,
  "resultcount": 1,
  "error": 0,
  "errormessage": "",
  "names": "[[\"one\"]]",
  "types": "[[\"int\"]]",
  "sizes": "[[10]]",
  "datas": [
    "{\"columns\":[\"one\"],\"index\":[0],\"data\":[[\"1\"]]}"
  ]
}

You can directly dive into the examples at tests/.

This example uses two main functions:

  • Dbtest.db.from_db loads data from a DB and returns a string for comparison.
  • Dbtest.db.from_file loads data from a file and returns a string for comparison.

Now we can change something in select. Digit or name.

        sql = 'SELECT 2 AS ONE'

And, if we run unittest again, it will be a failed.

> python -m unittest
F
======================================================================
FAIL: test_data (test_pyodbc_unittest.TestSelect)
Create SQLVERSION.json.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\yashi\Projects\pyodbc-unittest\tests\test_pyodbc_unittest.py", line 17, in test_data
    self.assertEqual(database.from_db(sql, file_name),
AssertionError: 'ROWS[96 chars][0] = [\'int\']\nDATA[0] = {\n  "one":{\n    "0":"2"\n  }\n}\n' != 'ROWS[96 chars][0] = [\'int\']\nDATA[0] = {\n  "one":{\n    "0":"1"\n  }\n}\n'
  ROWS_OUNT = 1
  RESULT_COUNT = 1
  SQLCODE = 0
  MESSAGE =
  COLUMN_NAMES[0] = ['one']
  COLUMNN_TYPES[0] = ['int']
  DATA[0] = {
    "one":{
-     "0":"2"
?          ^
+     "0":"1"
?          ^
    }
  }


----------------------------------------------------------------------
Ran 1 test in 0.127s

FAILED (failures=1)

To be continued...

Project details


Download files

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

Source Distribution

pyodbc-unittest-0.8.7.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

pyodbc_unittest-0.8.7-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pyodbc-unittest-0.8.7.tar.gz.

File metadata

  • Download URL: pyodbc-unittest-0.8.7.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for pyodbc-unittest-0.8.7.tar.gz
Algorithm Hash digest
SHA256 24b759085990b278115d18cd7291a3ba197f0f73021cb93340083ea301467d2f
MD5 d7fa677e3bf096880eab3ffa62b58c4d
BLAKE2b-256 4591b3c23f5c2b482047ceef69559759f25fd9fe274ce703bbd32fefeb2b5f2a

See more details on using hashes here.

File details

Details for the file pyodbc_unittest-0.8.7-py3-none-any.whl.

File metadata

  • Download URL: pyodbc_unittest-0.8.7-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for pyodbc_unittest-0.8.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0b8991f10419d7861abc46f708728cd2c2b38fe14a0d735cfaefd3ee65a8b274
MD5 0353de4d93f126c5e9145b0b133bc0da
BLAKE2b-256 97be4c5deb059782f22287c29d73d5b65e9e2a8d131fe27f9c2a6bd91114846d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page