sql-testing-tools 0.2.4__py3-none-any.whl → 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,27 +1,70 @@
1
1
  import sqlite3
2
2
  import os
3
- #from importlib import resources
4
-
3
+ import importlib
4
+
5
+
6
+
7
+ dbMap = {
8
+ "dbiu.bahn":1,
9
+ "dbiu.bayern":2,
10
+ "dbiu.bundestag": 3,
11
+ "dbiu.bundestag_einfach": 4,
12
+ "dbiu.film_fernsehen": 5,
13
+ "dbiu.haushaltsausstattung": 6,
14
+ "dbiu.straftaten": 7,
15
+ "dbiu.straftaten_einfach": 8,
16
+ "dbiu.kunstsammlung": 9,
17
+ "dbiu.ladepunkte": 10,
18
+ "dbiu.laenderspiele": 11,
19
+ "dbiu.lebensmittel": 12,
20
+ "dbiu.schulstatistik": 13,
21
+ "dbiu.studierende": 14,
22
+ "dbiu.unfallstatistik": 15,
23
+ "dbiu.videospiele_einfach": 16,
24
+ "dbiu.videospiele": 17,
25
+ "dbiu.wetterdaten": 18
26
+ }
5
27
 
6
28
  __db=""
7
29
 
8
- def setDBName(path: str):
30
+ def setDBName(database):
9
31
  global __db
10
- print("Setting DB to: " + path)
11
- __db = path
12
- #with resources.path('sql_testing_tools', path) as db_path:
13
- # __db = str(db_path)
14
- # print(__db)
15
-
16
-
17
- #setDBName("databases/bayern.db")
18
32
 
33
+ if isinstance(database, str) and database.startswith("dbiu."):
34
+ dbVersion = dbMap.get(database)
35
+ elif isinstance(database, int):
36
+ dbVersion = database
37
+ database = "dbiu."+str(database)
38
+ elif isinstance(database, str):
39
+ dbVersion = None
40
+ else:
41
+ raise Exception("Invalid database selected")
42
+
43
+ if(dbVersion is not None):
44
+ os.system('pip install dbiu_databases=='+str(dbVersion))
45
+ try:
46
+ import dbiu_databases
47
+ except ImportError:
48
+ raise Exception("Invalid database selected")
49
+ print("Setting DB to: " + database)
50
+ __db = database
51
+
52
+
19
53
  def run(sql: str):
20
- con = sqlite3.connect(__db)
21
- cur = con.cursor()
22
- cur.execute(sql)
23
- con.commit()
24
- return cur
54
+ global __db
55
+
56
+ if __db.startswith("dbiu.") and not __db.endswith(".db"):
57
+ with importlib.resources.path('dbiu_databases', "base.db") as db_path:
58
+ return __run(sql, db_path)
59
+ else:
60
+ return __run(sql, __db)
61
+
62
+ def __run(sql: str, db: str):
63
+ with sqlite3.connect(db) as con:
64
+ cur = con.cursor()
65
+ cur.execute(sql)
66
+ con.commit()
67
+ return cur
25
68
 
26
69
  def runFromFile(path: str):
27
70
  sql = getSQLFromFile(path)
@@ -0,0 +1,175 @@
1
+ Metadata-Version: 2.2
2
+ Name: sql_testing_tools
3
+ Version: 0.2.5
4
+ Description-Content-Type: text/markdown
5
+ Requires-Dist: sqlparse>=0.5.1
6
+ Requires-Dist: requests>=2.32.3
7
+ Dynamic: description
8
+ Dynamic: description-content-type
9
+ Dynamic: requires-dist
10
+
11
+ A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
12
+
13
+ #### [Beta in Development!]
14
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
15
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
16
+
17
+ - Submit bug reports and features requests at: https://github.com/ValentinHerrmann/sql_testing_tools
18
+ - PyPi-Package available at: https://pypi.org/project/sql-testing-tools/
19
+
20
+
21
+ ## Getting started
22
+
23
+ ### Imports
24
+ ``` python
25
+ # ensure to always use the latest version of sql_testing_tools
26
+ import os
27
+ os.system('pip install -U sql_testing_tools')
28
+
29
+ # import sql_testing_tools
30
+ import sql_testing_tools.BaseAccess as Ba
31
+ import sql_testing_tools.Helper as He
32
+
33
+ # import unittest and create TestClass
34
+ import unittest
35
+ class TestClass(unittest.TestCase):
36
+ # Ba.setDBName(...)
37
+ # ...
38
+ ```
39
+
40
+ ### Choosing a database
41
+
42
+ On top level of your test class, set the SQLite database you want to use.
43
+
44
+ **Option 1:** Use the predefined ones from [dbiu_databases](https://github.com/ValentinHerrmann/dbiu_databases) which are based on [datenbanken-im-unterricht.de](https://www.datenbanken-im-unterricht.de/catalog.php)
45
+
46
+ ``` python
47
+ # with DB name (preferred)
48
+ # --> only available if current dbiu_databases version has been imported to sql_testing_tools
49
+ Ba.setDBName("dbiu.bayern")
50
+
51
+ # or with dbiu_databases version number
52
+ # --> available as soon as a dbiu_databases version has been published on PyPi
53
+ Ba.setDBName(2)
54
+ ```
55
+
56
+ Currently supported to use with DB name are:
57
+ 1. `dbiu.bahn`: https://www.dbiu.de/bahn
58
+ 2. `dbiu.bayern`: https://www.dbiu.de/bayern
59
+ 3. `dbiu.bundestag`: https://www.datenbanken-im-unterricht.de/downloads/bundestag.zip
60
+ 4. `dbiu.bundestag_einfach`: https://www.dbiu.de/bundestagsmitglieder
61
+ 5. `dbiu.film_fernsehen`: https://www.dbiu.de/filmundfernsehen
62
+ 6. `dbiu.haushaltsausstattung`: https://www.dbiu.de/haushaltsausstattung
63
+ 7. `dbiu.straftaten`: https://www.datenbanken-im-unterricht.de/downloads/kriminalstatistik-erweitert.zip
64
+ 8. `dbiu.straftaten_einfach`: https://www.dbiu.de/kriminalstatistik
65
+ 9. `dbiu.kunstsammlung`: https://www.dbiu.de/kunstsammlung
66
+ 10. `dbiu.ladepunkte`: https://www.dbiu.de/ladepunkte
67
+ 11. `dbiu.laenderspiele`: https://www.dbiu.de/laenderspiele
68
+ 12. `dbiu.lebensmittel`: https://www.dbiu.de/lebensmittel
69
+ 13. `dbiu.schulstatistik`: https://www.dbiu.de/schulstatistik
70
+ 14. `dbiu.studierende`: https://www.dbiu.de/studierende
71
+ 15. `dbiu.unfallstatistik`: https://www.dbiu.de/unfallstatistik
72
+ 16. `dbiu.videospiele_einfach`: https://www.dbiu.de/videospiele
73
+ 17. `dbiu.videospiele`: https://www.dbiu.de/videospiele2
74
+ 18. `dbiu.wetterdaten`: https://www.dbiu.de/wetterdaten
75
+
76
+ **Option 2:** Use your own SQLite database with relative path starting at the working dir of your test repository.
77
+ ``` python
78
+ Ba.setDBName("databases/bayern.db")
79
+ ```
80
+
81
+ ### Available testing features
82
+
83
+ The following methods are available for use in test methods:
84
+
85
+
86
+ **Run the query to find out if syntax/database errors occur.**
87
+ ``` python
88
+ try:
89
+ Ba.runAndGetStringTable_fromFile("sqlfile.sql")
90
+ except Exception as e:
91
+ # the execution failed (usually due to syntax or database errors)
92
+ self.fail(e)
93
+ ```
94
+
95
+
96
+
97
+ **Set sql files to be compared (optional).**
98
+ 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.
99
+ ``` python
100
+ # Raises an Exception if one of the files is empty.
101
+ setup("sqlfile.sql","solution.sql")
102
+ ```
103
+
104
+ **Compared single parts of a sql query**
105
+ 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.
106
+ Each check was successfull if `""` is returned. Returns a German error message if not.
107
+ Each method compares the normalized string between the start keyword and the next keyword or ;
108
+ ``` python
109
+
110
+ res = He.checkColumns() # starts at "SELECT"
111
+ res = He.checkTables() # starts at "FROM"
112
+ res = He.checkCondition() # starts at "WHERE"
113
+ res = He.checkOrder() # starts at "ORDER BY"
114
+ res = He.checkGroup() # starts at "GROUP BY"
115
+ ```
116
+
117
+ In special cases where you'd like to start and stop at individual keywords for comparison use this directly:
118
+ ``` python
119
+ # Can not be called with new sql files. If needed call setup(...) before.
120
+ res = checkKeywords("startKeyword",["end","keywords"])
121
+ ```
122
+
123
+ 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!*
124
+
125
+ ``` python
126
+ res = checkEquality()
127
+ ```
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+ ### Changelog
138
+
139
+ ##### V 0.2.5
140
+ - Added support for [dbiu.de](https://www.datenbanken-im-unterricht.de/catalog.php) databases 1-18 (loaded via [dbiu_databases](https://github.com/ValentinHerrmann/dbiu_databases)) with option to still load DBs from local repo.
141
+ - Improved docs
142
+
143
+ ##### V 0.2.4
144
+ - Added more check methods for single parts of queries: checkColumns, checkTables, checkCondition, checkOrder, checkGroup, checkKeywords
145
+
146
+ ##### V 0.2.3
147
+ - fix: ASC/DESC in ORDER BY (also with multiple columns and order directions), no direction treated as ASC
148
+ - Verified that ; and whitespaces, linebreaks at end of query are ignored
149
+
150
+ ##### V 0.2.2
151
+ - Support LIKE
152
+ - Support '<=' and '>=' (geq and leq)
153
+
154
+ ##### V 0.2.1
155
+ - Support LIMIT
156
+
157
+ ##### V 0.1.9 + 0.2.0
158
+ - Support ORDER BY
159
+
160
+ ##### V 0.1.8
161
+ - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
162
+
163
+ ##### V 0.1.6 + V 0.1.7
164
+ - Fixed import error to ensure imports working in different environments
165
+
166
+ ##### V 0.1.4 + V 0.1.5
167
+ - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
168
+ - Aggregate Functions
169
+
170
+ ##### V 0.1.3
171
+ - SELECT: columns with our without table prefix
172
+ - FROM: one or more table from DB; no queries as tables!
173
+ - WHERE: single conditions; no Paranthesises!
174
+ - GROUP BY one or more columns
175
+
@@ -0,0 +1,8 @@
1
+ sql_testing_tools/BaseAccess.py,sha256=VcudaUKwhIF00UxZSYTQAN-6bK6K4Q1zrsSM6VoPF6o,6297
2
+ sql_testing_tools/Helper.py,sha256=gduUhpskF7TITInVTf8s33sWg7wsHNi1lYHxOZy5Fj0,10667
3
+ sql_testing_tools/TokenProcessing.py,sha256=S3TCdE6XVuR1G14UeQJ4vVtWFt-xahQlHqu-utEhkro,13598
4
+ sql_testing_tools/__init__.py,sha256=eihMp9aizQd5Lnegu0y57WcyCinmy4Jvp66e0fiMPU4,49
5
+ sql_testing_tools-0.2.5.dist-info/METADATA,sha256=jo8T4wdtjQWkH-a6B98jZrPypHZLJvqIh0vD9YjO1L0,6871
6
+ sql_testing_tools-0.2.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
+ sql_testing_tools-0.2.5.dist-info/top_level.txt,sha256=qwEkmfJ_lYhIwcLXgfU75m0_JavVbOg96fN2zSatN3E,18
8
+ sql_testing_tools-0.2.5.dist-info/RECORD,,
@@ -1,127 +0,0 @@
1
- Metadata-Version: 2.2
2
- Name: sql_testing_tools
3
- Version: 0.2.4
4
- Description-Content-Type: text/markdown
5
- Requires-Dist: sqlparse>=0.5.1
6
- Requires-Dist: requests>=2.32.3
7
- Dynamic: description
8
- Dynamic: description-content-type
9
- Dynamic: requires-dist
10
-
11
- A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
12
-
13
- ### [Beta in Development!]
14
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
15
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
16
-
17
- - Submit bug reports and features requests at: https://github.com/ValentinHerrmann/sql_testing_tools
18
- - PyPi-Package available at: https://pypi.org/project/sql-testing-tools/
19
-
20
-
21
-
22
- ### Getting started
23
-
24
- Use the following code to import the SQL Testing Tools to your project:
25
- ``` python
26
- # ensure to always use the latest version of sql_testing_tools
27
- import os
28
- os.system('pip install -U sql_testing_tools')
29
-
30
- # import sql_testing_tools
31
- import sql_testing_tools.BaseAccess as Ba
32
- import sql_testing_tools.Helper as He
33
- ```
34
-
35
- On global level of your test class, set the SQLite database you want to use (bases must be located in the repository of the test class, no DBs contained in sql_testing_tools).
36
- ``` python
37
- import unittest
38
- class TestClass(unittest.TestCase):
39
- Ba.setDBName("databases/ladepunkte.db")
40
- ```
41
-
42
- The following methods are available for use in test methods:
43
- ``` python
44
- try:
45
- Ba.runAndGetStringTable_fromFile("sqlfile.sql")
46
- except Exception as e:
47
- # the execution failed
48
- # (usually due to syntax or database errors)
49
- self.fail(e)
50
-
51
- # (optional) set the files to be compared. The sql strings will
52
- # be normalized and used for all following methods (to improve
53
- # performance), until new files are set. Arguments that are
54
- # None or "" are ignored and the sql string remains the same.
55
- # Raises an Exception if one of the files is empty.
56
- setup("sqlfile.sql","solution.sql")
57
-
58
- # All following methods call setup(sql,sol) before executing
59
- # anything else. Call without arguments to keep the last
60
- # normalized sql strings (and improve performance).
61
- # Each check was successfull if "" is returned. Returns a
62
- # German error message if not.
63
- # Each method compares the normalized string between the start
64
- # keyword and the next keyword or ;
65
-
66
- res = He.checkColumns() # starts to search at keyword "SELECT"
67
- res = He.checkTables() # starts to search at keyword "FROM"
68
- res = He.checkCondition() # starts to search at keyword "WHERE"
69
- res = He.checkOrder() # starts to search at keyword "ORDER BY"
70
- res = He.checkGroup() # starts to search at keyword "GROUP BY"
71
-
72
- # can not be called with new sql files
73
- res = checkKeywords("startKeyword",["end","keywords"])
74
-
75
- # compares equality of the full normalized sql strings and if
76
- # not equal uses the Cosette API (cosette.cs.washington.edu)
77
- # for comparison. A file 'cosette_apikey.txt' with only the
78
- # apikey in it on root level of the test repository is required
79
- # to use this feature. If not existant, only the string comparison
80
- # is performed.
81
- # Warning: Cosette is not maintained!
82
- res = checkEquality()
83
- ```
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
- ### Changelog
94
-
95
- ##### V0.2.4
96
- - Added more check methods for single parts of queries: checkColumns, checkTables, checkCondition, checkOrder, checkGroup, checkKeywords
97
-
98
- ##### V 0.2.3
99
- - fix: ASC/DESC in ORDER BY (also with multiple columns and order directions), no direction treated as ASC
100
- - Verified that ; and whitespaces, linebreaks at end of query are ignored
101
-
102
- ##### V 0.2.2
103
- - Support LIKE
104
- - Support '<=' and '>=' (geq and leq)
105
-
106
- ##### V 0.2.1
107
- - Support LIMIT
108
-
109
- ##### V 0.1.9 + 0.2.0
110
- - Support ORDER BY
111
-
112
- ##### V 0.1.8
113
- - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
114
-
115
- ##### V 0.1.6 + V 0.1.7
116
- - Fixed import error to ensure imports working in different environments
117
-
118
- ##### V 0.1.4 + V 0.1.5
119
- - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
120
- - Aggregate Functions
121
-
122
- ##### V 0.1.3
123
- - SELECT: columns with our without table prefix
124
- - FROM: one or more table from DB; no queries as tables!
125
- - WHERE: single conditions; no Paranthesises!
126
- - GROUP BY one or more columns
127
-
@@ -1,8 +0,0 @@
1
- sql_testing_tools/BaseAccess.py,sha256=NRbycUOSqYt2qKvggFurHMEV2-gkiJlf3Cr9xLjypIM,5050
2
- sql_testing_tools/Helper.py,sha256=gduUhpskF7TITInVTf8s33sWg7wsHNi1lYHxOZy5Fj0,10667
3
- sql_testing_tools/TokenProcessing.py,sha256=S3TCdE6XVuR1G14UeQJ4vVtWFt-xahQlHqu-utEhkro,13598
4
- sql_testing_tools/__init__.py,sha256=eihMp9aizQd5Lnegu0y57WcyCinmy4Jvp66e0fiMPU4,49
5
- sql_testing_tools-0.2.4.dist-info/METADATA,sha256=BFn8sD2wRroPiZnU-cukGp3GZx9p6JZ-WyCv-nUn_rY,4448
6
- sql_testing_tools-0.2.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
7
- sql_testing_tools-0.2.4.dist-info/top_level.txt,sha256=qwEkmfJ_lYhIwcLXgfU75m0_JavVbOg96fN2zSatN3E,18
8
- sql_testing_tools-0.2.4.dist-info/RECORD,,