Skip to content

Commit 1586f70

Browse files
author
Kareem Zidane
authoredMay 27, 2017
Merge pull request #25 from cs50/sqltests
sqltests
2 parents 2955b7b + 5e2b03e commit 1586f70

9 files changed

+43
-32
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
!.gitignore
33
!.travis.yml
44
dist/
5+
*.db
56
*.egg-info/
7+
*.pyc

‎.travis.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
language: python
2-
python: '3.4'
2+
python:
3+
- '2.7'
4+
- '3.4'
35
branches:
46
except: "/^v\\d/"
5-
install: true
6-
script: true
7+
services:
8+
- mysql
9+
- postgresql
10+
install:
11+
- python setup.py install
12+
- pip install mysqlclient
13+
- pip install psycopg2
14+
before_script:
15+
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
16+
- psql -c 'create database test;' -U postgres
17+
script: python tests/sqltests.py
718
deploy:
819
- provider: script
920
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",

‎setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
install_requires=["SQLAlchemy"],
1414
keywords="cs50",
1515
name="cs50",
16+
package_dir={"": "src"},
1617
packages=["cs50"],
1718
url="https://github.com/cs50/python-cs50",
1819
version="2.2.0"
-1.09 KB
Binary file not shown.
-1.06 KB
Binary file not shown.
-3.66 KB
Binary file not shown.
-3.92 KB
Binary file not shown.
-3.85 KB
Binary file not shown.

‎tests/sqltests.py

+26-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import unittest
21
from cs50.sql import SQL
2+
import sys
3+
import unittest
4+
import warnings
35

46
class SQLTests(unittest.TestCase):
7+
def multi_inserts_enabled(self):
8+
return True
9+
510
def test_delete_returns_affected_rows(self):
611
rows = [
712
{"id": 1, "val": "foo"},
@@ -22,6 +27,8 @@ def test_delete_returns_affected_rows(self):
2227
def test_insert_returns_last_row_id(self):
2328
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('foo')"), 1)
2429
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('bar')"), 2)
30+
if self.multi_inserts_enabled():
31+
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('baz'); INSERT INTO cs50(val) VALUES('qux')"), 4)
2532

2633
def test_select_all(self):
2734
self.assertEqual(self.db.execute("SELECT * FROM cs50"), [])
@@ -70,54 +77,44 @@ def test_update_returns_affected_rows(self):
7077
self.assertEqual(self.db.execute("UPDATE cs50 SET val = 'foo' WHERE id > 1"), 2)
7178
self.assertEqual(self.db.execute("UPDATE cs50 SET val = 'foo' WHERE id = -50"), 0)
7279

73-
class MySQLTests(SQLTests):
74-
@classmethod
75-
def setUpClass(self):
76-
self.db = SQL("mysql://root@localhost/cs50_sql_tests")
77-
78-
def setUp(self):
79-
self.db.execute("CREATE TABLE cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), PRIMARY KEY (id))")
80-
8180
def tearDown(self):
8281
self.db.execute("DROP TABLE cs50")
8382

8483
@classmethod
8584
def tearDownClass(self):
86-
self.db.execute("DROP TABLE IF EXISTS cs50")
85+
try:
86+
self.db.execute("DROP TABLE IF EXISTS cs50")
87+
except Warning as e:
88+
# suppress "unknown table"
89+
if not str(e).startswith("(1051"):
90+
raise e
8791

88-
class PostgresTests(SQLTests):
92+
class MySQLTests(SQLTests):
8993
@classmethod
9094
def setUpClass(self):
91-
self.db = SQL("postgresql://postgres:postgres@localhost/cs50_sql_tests")
95+
self.db = SQL("mysql://root@localhost/test")
9296

9397
def setUp(self):
94-
self.db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16))")
95-
96-
def tearDown(self):
97-
self.db.execute("DROP TABLE cs50")
98+
self.db.execute("CREATE TABLE cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), PRIMARY KEY (id))")
9899

100+
class PostgresTests(SQLTests):
99101
@classmethod
100-
def tearDownClass(self):
101-
self.db.execute("DROP TABLE IF EXISTS cs50")
102+
def setUpClass(self):
103+
self.db = SQL("postgresql://postgres@localhost/test")
102104

103-
def test_insert_returns_last_row_id(self):
104-
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('foo')"), 1)
105-
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('bar')"), 2)
105+
def setUp(self):
106+
self.db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16))")
106107

107108
class SQLiteTests(SQLTests):
108109
@classmethod
109110
def setUpClass(self):
110-
self.db = SQL("sqlite:///cs50_sql_tests.db")
111+
self.db = SQL("sqlite:///test.db")
111112

112113
def setUp(self):
113114
self.db.execute("CREATE TABLE cs50(id INTEGER PRIMARY KEY, val TEXT)")
114115

115-
def tearDown(self):
116-
self.db.execute("DROP TABLE cs50")
117-
118-
@classmethod
119-
def tearDownClass(self):
120-
self.db.execute("DROP TABLE IF EXISTS cs50")
116+
def multi_inserts_enabled(self):
117+
return False
121118

122119
if __name__ == "__main__":
123120
suite = unittest.TestSuite([
@@ -126,4 +123,4 @@ def tearDownClass(self):
126123
unittest.TestLoader().loadTestsFromTestCase(PostgresTests)
127124
])
128125

129-
unittest.TextTestRunner(verbosity=2).run(suite)
126+
sys.exit(not unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful())

0 commit comments

Comments
 (0)
Please sign in to comment.