-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sql_helper.py
More file actions
148 lines (109 loc) · 4.52 KB
/
Copy pathtest_sql_helper.py
File metadata and controls
148 lines (109 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
#
# Copyright (C) 2021 Steve Campbell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__author__ = "Steve Campbell"
# This test suite runs for both Sqlite and MySQL.
# Run it with 'pytest test_sql_helper.py'
# To run it for MySQL:
# * Start up a MySQl/MariaDB database with an empty database schema
# * create database TestDb;
# * create user 'test'@'localhost' identified by 'test';
# * grant select,insert,update,delete,create,drop on TestDb.* to 'test'@'localhost';
# * Uncomment the MysqlHelper import line
# * Update the db() fixture with an appropriate connection URL
# * Run pytest as above
import pytest
import re
import sqlite3
from sql_helper import SqlHelper, SqliteHelper
# from sql_helper_mysql import MysqlHelper
@pytest.fixture
def db() -> SqlHelper:
# return MysqlHelper(url="mysql://test:test@127.0.0.1/TestDb")
return SqliteHelper(url="sqlite://:memory:/")
@pytest.fixture
def db2(db) -> SqlHelper:
db.execute("DROP TABLE IF EXISTS Test ")
db.execute("CREATE TABLE Test(Id INTEGER, Value TEXT)")
db.execute("INSERT INTO Test(Id, Value) VALUES (1, 'a'), (2, 'b')")
return db
def test_connect(db):
# Matches both pymysql.connections.Connection and sqlite3.Connection
assert re.search("Connection", str(type(db.connect())))
def test_execute_returns_cursor(db):
type_str = str(type(db.execute("SELECT 1")))
assert re.search(r"(Cursor|MysqlHelper)", type_str)
def test_execute_statement_was_executed(db):
cur: sqlite3.Cursor = db.execute("SELECT 1")
assert list(cur.fetchone())[0] == 1
def test_execute_returns_addressable_by_name(db):
row = db.execute("SELECT 1 AS A", fetch_dicts=True).fetchone()
# noinspection PyTypeChecker
assert row["A"] == 1
def test_execute_bind_with_question_mark(db):
row = db.execute("SELECT ?", [2]).fetchone()
assert row[0] == 2
def test_execute_bind_with_percent(db):
row = db.execute("SELECT %s", [2]).fetchone()
assert row[0] == 2
def test_t_row_returns_none(db2):
row = db2.t_row("SELECT Id,Value FROM Test WHERE Id=99")
assert row is None
def test_t_row_returns_tuple(db2):
row = db2.t_row("SELECT Id,Value FROM Test WHERE Id=?", [1])
assert row == (1, 'a')
def test_t_row_returns_tuple2(db2):
row_id, value = db2.t_row("SELECT Id,Value FROM Test WHERE Id=?", [1])
assert row_id == 1
assert value == "a"
def test_t_row_returns_exception(db2):
with pytest.raises(RuntimeError, match=r"Multiple rows"):
db2.t_row("SELECT * FROM Test")
def test_value(db2):
assert db2.value("SELECT Value from Test where Id=?", [1]) == "a"
def test_rows(db2):
assert db2.rows("SELECT * FROM Test") == (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "b"}
)
def test_column(db2):
assert db2.column("SELECT Value FROM Test") == ["a", "b"]
def test_insert(db2):
db2.insert("Test", {"Id": 3, "Value": "c"})
assert db2.value("SELECT COUNT(*) FROM Test") == 3
@pytest.mark.parametrize(
"attributes, filters, exp_state, description", [
({"Value": "d"}, {}, (
{"Id": 1, "Value": "d"}, {"Id": 2, "Value": "d"}
), "Update every row, no filter"),
({"Value": "a"}, {}, (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "a"}
), "Update where one row is unaffected"),
({"Value": "a"}, {"Id": 1}, (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "b"}
), "Update filtering on an unaffected row"),
({"Value": "a"}, {"Id": 2}, (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "a"}
), "Update filtering on an affected row"),
({"Value": "d"}, {"Id": 99}, (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "b"}
), "Update filtering out all rows"),
({}, {}, (
{"Id": 1, "Value": "a"}, {"Id": 2, "Value": "b"}
), "Make an empty update"),
]
)
def test_update(db2, attributes, filters, exp_state, description):
db2.update("Test", attributes=attributes, filters=filters)
assert db2.rows("SELECT * FROM Test") == exp_state