-
Notifications
You must be signed in to change notification settings - Fork 280
Transaction sessions; appcontext teardown fix; multiple databases in request fix. #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e17982
Use sessions to handle transactions, allowing for both auto and manual
jsarchibald 0dff716
Style fixes. Minor design improvements, including removing SQL class …
jsarchibald c227d18
Fix sql.py in tests for Travis CI.
jsarchibald ee41283
Requested changes to code design, including some renaming, a new inst…
jsarchibald c60d67d
Messed up the tests for Travis CI again. Fixed.
jsarchibald dac2ae8
Stylistic changes.
jsarchibald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,76 @@ | ||
import logging | ||
import os | ||
import requests | ||
import sys | ||
from flask import Flask, render_template | ||
|
||
sys.path.insert(0, "../../src") | ||
|
||
import cs50 | ||
import cs50.flask | ||
|
||
from flask import Flask, render_template | ||
|
||
app = Flask(__name__) | ||
|
||
db = cs50.SQL("sqlite:///../sqlite.db") | ||
logging.disable(logging.CRITICAL) | ||
os.environ["WERKZEUG_RUN_MAIN"] = "true" | ||
|
||
db_url = "sqlite:///../test.db" | ||
db = cs50.SQL(db_url) | ||
|
||
@app.route("/") | ||
def index(): | ||
db.execute("SELECT 1") | ||
""" | ||
def f(): | ||
res = requests.get("cs50.harvard.edu") | ||
f() | ||
""" | ||
return render_template("index.html") | ||
|
||
@app.route("/autocommit") | ||
def autocommit(): | ||
db.execute("INSERT INTO test (val) VALUES (?)", "def") | ||
db2 = cs50.SQL(db_url) | ||
ret = db2.execute("SELECT val FROM test WHERE val=?", "def") | ||
return str(ret == [{"val": "def"}]) | ||
|
||
@app.route("/create") | ||
def create(): | ||
ret = db.execute("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, val VARCHAR(16))") | ||
return str(ret) | ||
|
||
@app.route("/delete") | ||
def delete(): | ||
ret = db.execute("DELETE FROM test") | ||
return str(ret > 0) | ||
|
||
@app.route("/drop") | ||
def drop(): | ||
ret = db.execute("DROP TABLE test") | ||
return str(ret) | ||
|
||
@app.route("/insert") | ||
def insert(): | ||
ret = db.execute("INSERT INTO test (val) VALUES (?)", "abc") | ||
return str(ret > 0) | ||
|
||
@app.route("/multiple_connections") | ||
def multiple_connections(): | ||
ctx = len(app.teardown_appcontext_funcs) | ||
db1 = cs50.SQL(db_url) | ||
td1 = (len(app.teardown_appcontext_funcs) == ctx + 1) | ||
db2 = cs50.SQL(db_url) | ||
td2 = (len(app.teardown_appcontext_funcs) == ctx + 2) | ||
return str(td1 and td2) | ||
|
||
@app.route("/select") | ||
def select(): | ||
ret = db.execute("SELECT val FROM test") | ||
return str(ret == [{"val": "abc"}]) | ||
|
||
@app.route("/single_teardown") | ||
def single_teardown(): | ||
db.execute("SELECT * FROM test") | ||
ctx = len(app.teardown_appcontext_funcs) | ||
db.execute("SELECT COUNT(id) FROM test") | ||
return str(ctx == len(app.teardown_appcontext_funcs)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
import requests | ||
import sys | ||
import threading | ||
import time | ||
import unittest | ||
|
||
from application import app | ||
|
||
def request(route): | ||
r = requests.get("http://localhost:5000/{}".format(route)) | ||
return r.text == "True" | ||
|
||
class FlaskTests(unittest.TestCase): | ||
|
||
def test__create(self): | ||
self.assertTrue(request("create")) | ||
|
||
def test_autocommit(self): | ||
self.assertTrue(request("autocommit")) | ||
|
||
def test_delete(self): | ||
self.assertTrue(request("delete")) | ||
|
||
def test_insert(self): | ||
self.assertTrue(request("insert")) | ||
|
||
def test_multiple_connections(self): | ||
self.assertTrue(request("multiple_connections")) | ||
|
||
def test_select(self): | ||
self.assertTrue(request("select")) | ||
|
||
def test_single_teardown(self): | ||
self.assertTrue(request("single_teardown")) | ||
|
||
def test_zdrop(self): | ||
self.assertTrue(request("drop")) | ||
|
||
|
||
if __name__ == "__main__": | ||
t = threading.Thread(target=app.run, daemon=True) | ||
t.start() | ||
|
||
suite = unittest.TestSuite([ | ||
unittest.TestLoader().loadTestsFromTestCase(FlaskTests) | ||
]) | ||
|
||
sys.exit(not unittest.TextTestRunner(verbosity=2).run(suite).wasSuccessful()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.