Skip to main content

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...

Metadata

Release files for pyodbc-unittest 0.8.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyodbc-unittest 0.8.7
File Size Uploaded
pyodbc-unittest-0.8.7.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyodbc-unittest 0.8.7
File Interpreter ABI Platform
pyodbc_unittest-0.8.7-py3-none-any.whl Python 3 none any Details

Total release size: 10.8 kB

Release files / pyodbc-unittest-0.8.7.tar.gz

Download URL pyodbc-unittest-0.8.7.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
24b759085990b278115d18cd7291a3ba197f0f73021cb93340083ea301467d2f
BLAKE2b-256 checksum
How to use checksums
4591b3c23f5c2b482047ceef69559759f25fd9fe274ce703bbd32fefeb2b5f2a
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pyodbc_unittest-0.8.7-py3-none-any.whl

Download URL pyodbc_unittest-0.8.7-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b8991f10419d7861abc46f708728cd2c2b38fe14a0d735cfaefd3ee65a8b274
BLAKE2b-256 checksum
How to use checksums
97be4c5deb059782f22287c29d73d5b65e9e2a8d131fe27f9c2a6bd91114846d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.8.7 This release

2 release files

0.8.6

2 release files

0.8.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page