No project description provided
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql_testing_tools-0.3.0.tar.gz.
File metadata
- Download URL: sql_testing_tools-0.3.0.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db42ce6dee2a136e37d72a2e235513a508e686eca4d9ac0dfca63af54f02d558
|
|
| MD5 |
c4e10cb58cfed0645e3708280a0dc8d3
|
|
| BLAKE2b-256 |
64701817ec187095db4a885c84fab26d89b4a392dde615b86a64246460325aa5
|
Provenance
The following attestation bundles were made for sql_testing_tools-0.3.0.tar.gz:
Publisher:
publish.yml on ValentinHerrmann/sql_testing_tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql_testing_tools-0.3.0.tar.gz -
Subject digest:
db42ce6dee2a136e37d72a2e235513a508e686eca4d9ac0dfca63af54f02d558 - Sigstore transparency entry: 721275759
- Sigstore integration time:
-
Permalink:
ValentinHerrmann/sql_testing_tools@d627063227a5435b5f6fcae0349ac05fce396690 -
Branch / Tag:
refs/tags/V0.3 - Owner: https://github.com/ValentinHerrmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d627063227a5435b5f6fcae0349ac05fce396690 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sql_testing_tools-0.3.0-py3-none-any.whl.
File metadata
- Download URL: sql_testing_tools-0.3.0-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
420a10788208a78abe2b32117e7496be5df8d3e6fa32d9c52a666a34a874dd39
|
|
| MD5 |
49dbe9b74f0787873fd0e4ba436f3b24
|
|
| BLAKE2b-256 |
6279366e4716833d1bc564e55bbfb899346386a9f7c6f0ed6bf039d975f3735a
|
Provenance
The following attestation bundles were made for sql_testing_tools-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on ValentinHerrmann/sql_testing_tools
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql_testing_tools-0.3.0-py3-none-any.whl -
Subject digest:
420a10788208a78abe2b32117e7496be5df8d3e6fa32d9c52a666a34a874dd39 - Sigstore transparency entry: 721275767
- Sigstore integration time:
-
Permalink:
ValentinHerrmann/sql_testing_tools@d627063227a5435b5f6fcae0349ac05fce396690 -
Branch / Tag:
refs/tags/V0.3 - Owner: https://github.com/ValentinHerrmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d627063227a5435b5f6fcae0349ac05fce396690 -
Trigger Event:
release
-
Statement type: