A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
[Beta in Development!]
- Submit bug reports and features requests at: https://github.com/ValentinHerrmann/sql_testing_tools
- PyPi-Package available at: https://pypi.org/project/sql-testing-tools/
Getting started
Imports
# ensure to always use the latest version of sql_testing_tools
import os
os.system('pip install -U sql_testing_tools')
# import sql_testing_tools
import sql_testing_tools.BaseAccess as Ba
import sql_testing_tools.Helper as He
# import unittest and create TestClass
import unittest
class TestClass(unittest.TestCase):
# Ba.setDBName(...)
# ...
Choosing a database
On top level of your test class, set the SQLite database you want to use.
Option 1: Use the predefined ones from dbiu_databases which are based on datenbanken-im-unterricht.de
# with DB name (preferred)
# --> only available if current dbiu_databases version has been imported to sql_testing_tools
Ba.setDBName("dbiu.bayern")
# or with dbiu_databases version number
# --> available as soon as a dbiu_databases version has been published on PyPi
Ba.setDBName(2)
Currently supported to use with DB name are:
dbiu.bahn: https://www.dbiu.de/bahndbiu.bayern: https://www.dbiu.de/bayerndbiu.bundestag: https://www.datenbanken-im-unterricht.de/downloads/bundestag.zipdbiu.bundestag_einfach: https://www.dbiu.de/bundestagsmitgliederdbiu.film_fernsehen: https://www.dbiu.de/filmundfernsehendbiu.haushaltsausstattung: https://www.dbiu.de/haushaltsausstattungdbiu.straftaten: https://www.datenbanken-im-unterricht.de/downloads/kriminalstatistik-erweitert.zipdbiu.straftaten_einfach: https://www.dbiu.de/kriminalstatistikdbiu.kunstsammlung: https://www.dbiu.de/kunstsammlungdbiu.ladepunkte: https://www.dbiu.de/ladepunktedbiu.laenderspiele: https://www.dbiu.de/laenderspieledbiu.lebensmittel: https://www.dbiu.de/lebensmitteldbiu.schulstatistik: https://www.dbiu.de/schulstatistikdbiu.studierende: https://www.dbiu.de/studierendedbiu.unfallstatistik: https://www.dbiu.de/unfallstatistikdbiu.videospiele_einfach: https://www.dbiu.de/videospieledbiu.videospiele: https://www.dbiu.de/videospiele2dbiu.wetterdaten: https://www.dbiu.de/wetterdatendbiu.terra: https://terra.sachsen.schule/
Option 2: Use your own SQLite database with relative path starting at the working dir of your test repository.
Ba.setDBName("databases/bayern.db")
Available testing features
The following methods are available for use in test methods:
Run the query to find out if syntax/database errors occur.
try:
Ba.runAndGetStringTable_fromFile("sqlfile.sql")
except Exception as e:
# the execution failed (usually due to syntax or database errors)
self.fail(e)
Set sql files to be compared (optional).
The sql strings will be normalized and used or all following methods (to improve performance) until new files are specified. All check...() functions call setup(...) as first step. If one argument is "" or None the corresponding file is not changed. Therefore calling the check...() functions without arguments does not change the sql files that are compared.
# Raises an Exception if one of the files is empty.
setup("sqlfile.sql","solution.sql")
Compared single parts of a sql query
Call without arguments to keep the last normalized sql strings (and improve performance) and with one or two sql file paths to load and normalize new queries.
Each check was successfull if "" is returned. Returns a German error message if not.
Each method compares the normalized string between the start keyword and the next keyword or ;
res = He.checkColumns() # starts at "SELECT"
res = He.checkTables() # starts at "FROM"
res = He.checkCondition() # starts at "WHERE"
res = He.checkOrder() # starts at "ORDER BY"
res = He.checkGroup() # starts at "GROUP BY"
In special cases where you'd like to start and stop at individual keywords for comparison use this directly:
# Can not be called with new sql files. If needed call setup(...) before.
res = checkKeywords("startKeyword",["end","keywords"])
Compares equality of the full normalized sql strings and if not equal uses the Cosette API (cosette.cs.washington.edu) for comparison. A file cosette_apikey.txt with only the apikey in it on root level of the test repository is required to use this feature. If not existant, only the string comparison is performed. Warning: Cosette is currently not maintained!
res = checkEquality()
Changelog
Version 0.3 (since 0.2)
SQL Features
- Support
LIMITclause - Support
LIKEoperator - Support
<=and>=(leq and geq) - Support
ASC/DESCinORDER BY(also with multiple columns and order directions), no direction treated asASC - Support
NOT - Accept
COUNT(colname)in addition toCOUNT(*) - Normalize string inequality:
<>,!=,NOT LIKE→NOT...LIKE... - Ignore case in
=string comparison and thereforeconvertto LIKE (if no%,_in string)
Test Features
- Added support for dbiu.de databases 1-18 (loaded via dbiu_databases) with option to still load DBs from local repo
- Added more check methods for single parts of queries:
checkColumns,checkTables,checkCondition,checkOrder,checkGroup,checkKeywords - Improved documentation
- Verified that
;, whitespaces (at the ende of the query), and linebreaks are ignored
Bug Fixes
- Fixed bug when having only one column selected
- Fixed handling of nested conditions with parantheses
- Fixed memory problem with huge cross products (used to lead to build failed)
- Fixed bug that forced irrelevant equality order if both sides aren't literals
- Fixed part checks if keywords are in wrong order
- Fixed bug in checkKeywords: if keyword was present in one query and not in the other, the comparison was not performed correctly
Other
- added dbiu.terra database support
Detailed Changelogs of Versions 0.2.x
V 0.2.13
- Acccept
COUNT(colname), too (instead of justCOUNT(*))
V 0.2.12
- fixed bug when having only one colum selected
- fixed bug with nested conditions
- fixed memory problem with huge cross products (used to lead to build failed)
V 0.2.11
- fixed irrelevant equality order if both sides aren't literals
V 0.2.10
- fixed part checks if keywords are in wrong order
V 0.2.9
- added NOT support
- improved Parentheses handling
V 0.2.8
- ignore case in '=' string comparison and convert to LIKE (if no '%', '_' in String)
V 0.2.7
- Normalize String unequality: ...<>..., ...!=..., ...NOT LIKE... --> NOT...LIKE...
V 0.2.6
- Fixed bug in checkKeywords: if keyword was present in one query and not in the other, the comparison was not performed correctly.
V 0.2.5
- Added support for dbiu.de databases 1-18 (loaded via dbiu_databases) with option to still load DBs from local repo.
- Improved docs
V 0.2.4
- Added more check methods for single parts of queries: checkColumns, checkTables, checkCondition, checkOrder, checkGroup, checkKeywords
V 0.2.3
- fix: ASC/DESC in ORDER BY (also with multiple columns and order directions), no direction treated as ASC
- Verified that ; and whitespaces, linebreaks at end of query are ignored
V 0.2.2
- Support LIKE
- Support '<=' and '>=' (geq and leq)
V 0.2.1
- Support LIMIT
Version 0.2
SQL Features
- Support SELECT: columns with our without table prefix
- Support FROM: one or more table from DB; no queries as tables!
- Support WHERE: single conditions; no Paranthesises!
- Support GROUP BY one or more columns
- Support Support ORDER BY
- Support chained conditions (with AND,OR and Paranthesises) in WHERE statement
- Support aggregate functions
Bug Fixes
- Fixed import error to ensure imports working in different environments
- Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
Detailed Changelogs of Versions 0.1.x
V 0.1.9 + 0.2.0
- Support ORDER BY
V 0.1.8
- Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
V 0.1.6 + V 0.1.7
- Fixed import error to ensure imports working in different environments
V 0.1.4 + V 0.1.5
- Chained conditions (with AND,OR and Paranthesises) in WHERE statement
- Aggregate Functions
V 0.1.3
- SELECT: columns with our without table prefix
- FROM: one or more table from DB; no queries as tables!
- WHERE: single conditions; no Paranthesises!
- GROUP BY one or more columns
Metadata
Release files for sql-testing-tools 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sql_testing_tools-0.3.0.tar.gz | 26.7 kB | With provenance | Details for sql_testing_tools-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sql_testing_tools-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for sql_testing_tools-0.3.0-py3-none-any.whl |
Total release size: 51.7 kB
Release files / sql_testing_tools-0.3.0.tar.gz
| Download URL | sql_testing_tools-0.3.0.tar.gz |
|---|---|
| Size | 26.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db42ce6dee2a136e37d72a2e235513a508e686eca4d9ac0dfca63af54f02d558
|
|
BLAKE2b-256 checksum How to use checksums |
64701817ec187095db4a885c84fab26d89b4a392dde615b86a64246460325aa5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2025.
Transparency logRelease files / sql_testing_tools-0.3.0-py3-none-any.whl
| Download URL | sql_testing_tools-0.3.0-py3-none-any.whl |
|---|---|
| Size | 25.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
420a10788208a78abe2b32117e7496be5df8d3e6fa32d9c52a666a34a874dd39
|
|
BLAKE2b-256 checksum How to use checksums |
6279366e4716833d1bc564e55bbfb899346386a9f7c6f0ed6bf039d975f3735a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2025.
Transparency logSecurity for sql-testing-tools 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for sql_testing_tools-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 24, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.