Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqltests #25

Merged
merged 11 commits into from
May 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,4 +2,6 @@
!.gitignore
!.travis.yml
dist/
*.db
*.egg-info/
*.pyc
17 changes: 14 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
language: python
python: '3.4'
python:
- '2.7'
- '3.4'
branches:
except: "/^v\\d/"
install: true
script: true
services:
- mysql
- postgresql
install:
- python setup.py install
- pip install mysqlclient
- pip install psycopg2
before_script:
- mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
- psql -c 'create database test;' -U postgres
script: python tests/sqltests.py
deploy:
- provider: script
script: 'curl --fail --data "{ \"tag_name\": \"v$(python setup.py --version)\",
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
install_requires=["SQLAlchemy"],
keywords="cs50",
name="cs50",
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="2.2.0"
Binary file removed src/cs50/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file removed src/cs50/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file removed src/cs50/__pycache__/cs50.cpython-34.pyc
Binary file not shown.
Binary file removed src/cs50/__pycache__/cs50.cpython-36.pyc
Binary file not shown.
Binary file removed src/cs50/__pycache__/sql.cpython-34.pyc
Binary file not shown.
55 changes: 26 additions & 29 deletions tests/sqltests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import unittest
from cs50.sql import SQL
import sys
import unittest
import warnings

class SQLTests(unittest.TestCase):
def multi_inserts_enabled(self):
return True

def test_delete_returns_affected_rows(self):
rows = [
{"id": 1, "val": "foo"},
@@ -22,6 +27,8 @@ def test_delete_returns_affected_rows(self):
def test_insert_returns_last_row_id(self):
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('foo')"), 1)
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('bar')"), 2)
if self.multi_inserts_enabled():
self.assertEqual(self.db.execute("INSERT INTO cs50(val) VALUES('baz'); INSERT INTO cs50(val) VALUES('qux')"), 4)

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

class MySQLTests(SQLTests):
@classmethod
def setUpClass(self):
self.db = SQL("mysql://root@localhost/cs50_sql_tests")

def setUp(self):
self.db.execute("CREATE TABLE cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), PRIMARY KEY (id))")

def tearDown(self):
self.db.execute("DROP TABLE cs50")

@classmethod
def tearDownClass(self):
self.db.execute("DROP TABLE IF EXISTS cs50")
try:
self.db.execute("DROP TABLE IF EXISTS cs50")
except Warning as e:
# suppress "unknown table"
if not str(e).startswith("(1051"):
raise e

class PostgresTests(SQLTests):
class MySQLTests(SQLTests):
@classmethod
def setUpClass(self):
self.db = SQL("postgresql://postgres:postgres@localhost/cs50_sql_tests")
self.db = SQL("mysql://root@localhost/test")

def setUp(self):
self.db.execute("CREATE TABLE cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16))")

def tearDown(self):
self.db.execute("DROP TABLE cs50")
self.db.execute("CREATE TABLE cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), PRIMARY KEY (id))")

class PostgresTests(SQLTests):
@classmethod
def tearDownClass(self):
self.db.execute("DROP TABLE IF EXISTS cs50")
def setUpClass(self):
self.db = SQL("postgresql://postgres@localhost/test")

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

class SQLiteTests(SQLTests):
@classmethod
def setUpClass(self):
self.db = SQL("sqlite:///cs50_sql_tests.db")
self.db = SQL("sqlite:///test.db")

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

def tearDown(self):
self.db.execute("DROP TABLE cs50")

@classmethod
def tearDownClass(self):
self.db.execute("DROP TABLE IF EXISTS cs50")
def multi_inserts_enabled(self):
return False

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

unittest.TextTestRunner(verbosity=2).run(suite)
sys.exit(not unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful())