Skip to main content

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!]

Build and Test Build and Test

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:

  1. dbiu.bahn: https://www.dbiu.de/bahn
  2. dbiu.bayern: https://www.dbiu.de/bayern
  3. dbiu.bundestag: https://www.datenbanken-im-unterricht.de/downloads/bundestag.zip
  4. dbiu.bundestag_einfach: https://www.dbiu.de/bundestagsmitglieder
  5. dbiu.film_fernsehen: https://www.dbiu.de/filmundfernsehen
  6. dbiu.haushaltsausstattung: https://www.dbiu.de/haushaltsausstattung
  7. dbiu.straftaten: https://www.datenbanken-im-unterricht.de/downloads/kriminalstatistik-erweitert.zip
  8. dbiu.straftaten_einfach: https://www.dbiu.de/kriminalstatistik
  9. dbiu.kunstsammlung: https://www.dbiu.de/kunstsammlung
  10. dbiu.ladepunkte: https://www.dbiu.de/ladepunkte
  11. dbiu.laenderspiele: https://www.dbiu.de/laenderspiele
  12. dbiu.lebensmittel: https://www.dbiu.de/lebensmittel
  13. dbiu.schulstatistik: https://www.dbiu.de/schulstatistik
  14. dbiu.studierende: https://www.dbiu.de/studierende
  15. dbiu.unfallstatistik: https://www.dbiu.de/unfallstatistik
  16. dbiu.videospiele_einfach: https://www.dbiu.de/videospiele
  17. dbiu.videospiele: https://www.dbiu.de/videospiele2
  18. dbiu.wetterdaten: https://www.dbiu.de/wetterdaten

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

V 0.2.13
  • Acccept COUNT(colname), too (instead of just COUNT(*))
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
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


Download files

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

Source Distribution

sql_testing_tools-0.2.13.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql_testing_tools-0.2.13-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file sql_testing_tools-0.2.13.tar.gz.

File metadata

  • Download URL: sql_testing_tools-0.2.13.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sql_testing_tools-0.2.13.tar.gz
Algorithm Hash digest
SHA256 111e348f3460070b036fccbe8fcfcd7c9f05e97c61f67918a80a2c33eaee2f8a
MD5 8f5e9e9d2857f688251cc91d39222891
BLAKE2b-256 e05f9ec010cedd754f81c4edbc5952491dbe054f614c73ee059a464c1b65c441

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql_testing_tools-0.2.13.tar.gz:

Publisher: publish.yml on ValentinHerrmann/sql_testing_tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sql_testing_tools-0.2.13-py3-none-any.whl.

File metadata

File hashes

Hashes for sql_testing_tools-0.2.13-py3-none-any.whl
Algorithm Hash digest
SHA256 0fa46b55392ceeb9d19482c03cdc7dc353263e237abf1b2277223bcd6a4ed3e5
MD5 840909e33e6df910b69b538651ef0767
BLAKE2b-256 59c995a60e662f8b548a05e6fccb6a90ed4a2f23209308650f199f0f2525270a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sql_testing_tools-0.2.13-py3-none-any.whl:

Publisher: publish.yml on ValentinHerrmann/sql_testing_tools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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