Skip to content

Commit b2fc969

Browse files
committed
working on transactions
1 parent a598334 commit b2fc969

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

src/cs50/sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def __init__(self, url, **kwargs):
5656
if not os.path.isfile(matches.group(1)):
5757
raise RuntimeError("not a file: {}".format(matches.group(1)))
5858

59-
# Create engine, disabling SQLAlchemy's own autocommit mode, raising exception if back end's module not installed
60-
self._engine = sqlalchemy.create_engine(url, **kwargs).execution_options(autocommit=False)
59+
# Create engine, raising exception if back end's module not installed
60+
self._engine = sqlalchemy.create_engine(url, **kwargs).execution_options(autocommit=True)
6161

6262
# Listener for connections
6363
def connect(dbapi_connection, connection_record):

tests/foo.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import logging
2+
import sys
3+
4+
sys.path.insert(0, "../src")
5+
6+
import cs50
7+
8+
db = cs50.SQL("sqlite:///foo.db")
9+
10+
logging.getLogger("cs50").disabled = False
11+
12+
"""
13+
#db.execute("SELECT ? FROM ? ORDER BY ?", "a", "tbl", "c")
14+
db.execute("CREATE TABLE IF NOT EXISTS bar (firstname STRING)")
15+
16+
db.execute("INSERT INTO bar VALUES (?)", "baz")
17+
db.execute("INSERT INTO bar VALUES (?)", "qux")
18+
db.execute("SELECT * FROM bar WHERE firstname IN (?)", ("baz", "qux"))
19+
db.execute("DELETE FROM bar")
20+
"""
21+
22+
db = cs50.SQL("postgresql://postgres@localhost/test")
23+
24+
"""
25+
print(db.execute("DROP TABLE IF EXISTS cs50"))
26+
print(db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)"))
27+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
28+
print(db.execute("SELECT * FROM cs50"))
29+
30+
print(db.execute("DROP TABLE IF EXISTS cs50"))
31+
print(db.execute("CREATE TABLE cs50 (val VARCHAR(16), bin BYTEA)"))
32+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
33+
print(db.execute("SELECT * FROM cs50"))
34+
"""
35+
36+
print(db.execute("DROP TABLE IF EXISTS cs50"))
37+
print(db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)"))
38+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
39+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
40+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
41+
print(db.execute("SELECT * FROM cs50"))
42+
print(db.execute("COMMIT"))
43+
"""
44+
try:
45+
print(db.execute("INSERT INTO cs50 (id, val) VALUES(1, 'bar')"))
46+
except Exception as e:
47+
print(e)
48+
pass
49+
print(db.execute("INSERT INTO cs50 (val) VALUES('foo')"))
50+
print(db.execute("DELETE FROM cs50"))
51+
"""

tests/sql.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def setUpClass(self):
150150
self.db = SQL("mysql://root@localhost/test")
151151

152152
def setUp(self):
153-
self.db.execute("CREATE TABLE cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), bin BLOB, PRIMARY KEY (id))")
153+
self.db.execute("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), bin BLOB, PRIMARY KEY (id))")
154+
self.db.execute("DELETE FROM cs50")
154155

155156

156157
class PostgresTests(SQLTests):
@@ -159,7 +160,8 @@ def setUpClass(self):
159160
self.db = SQL("postgresql://postgres@localhost/test")
160161

161162
def setUp(self):
162-
self.db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)")
163+
self.db.execute("CREATE TABLE IF NOT EXISTS cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)")
164+
self.db.execute("DELETE FROM cs50")
163165

164166
def test_cte(self):
165167
self.assertEqual(self.db.execute("WITH foo AS ( SELECT 1 AS bar ) SELECT bar FROM foo"), [{"bar": 1}])
@@ -173,7 +175,8 @@ def setUpClass(self):
173175
self.db = SQL("sqlite:///test.db")
174176

175177
def setUp(self):
176-
self.db.execute("CREATE TABLE cs50(id INTEGER PRIMARY KEY, val TEXT, bin BLOB)")
178+
self.db.execute("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER PRIMARY KEY, val TEXT, bin BLOB)")
179+
self.db.execute("DELETE FROM cs50")
177180

178181
def test_lastrowid(self):
179182
self.db.execute("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)")

0 commit comments

Comments
 (0)