Skip to content

Commit

Permalink
[cleanup] Cleaned all Python files using black
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaist authored and jugmac00 committed Oct 9, 2020
1 parent e8e682a commit e551360
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion more/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
__import__("pkg_resources").declare_namespace(__name__)
1 change: 1 addition & 0 deletions more/transaction/__init__.py
@@ -1,4 +1,5 @@
# flake8: noqa
from .main import TransactionApp, default_commit_veto

# old-style naming for backwards compatibility
from .main import TransactionApp as transaction_app
16 changes: 7 additions & 9 deletions more/transaction/main.py
Expand Up @@ -9,6 +9,7 @@ class TransactionApp(morepath.App):

# code taken and adjusted from pyramid_tm


def default_commit_veto(request, response):
"""
When used as a commit veto, the logic in this function will cause the
Expand All @@ -21,23 +22,20 @@ def default_commit_veto(request, response):
Otherwise the transaction will be allowed to commit.
"""
xtm = response.headers.get('x-tm')
xtm = response.headers.get("x-tm")
if xtm is not None:
return xtm != 'commit'
return response.status.startswith(('4', '5'))
return xtm != "commit"
return response.status.startswith(("4", "5"))


class AbortResponse(Exception):
def __init__(self, response):
self.response = response


@TransactionApp.setting_section(section='transaction')
@TransactionApp.setting_section(section="transaction")
def get_transaction_settings():
return {
'attempts': 1,
'commit_veto': default_commit_veto
}
return {"attempts": 1, "commit_veto": default_commit_veto}


@TransactionApp.tween_factory(over=morepath.EXCVIEW)
Expand All @@ -60,7 +58,7 @@ def transaction_tween(request):
request.reset()
t = manager.get()
if userid is not None:
t.setUser(userid, '')
t.setUser(userid, "")
t.note(str(request.path))
response = handler(request)
if manager.isDoomed():
Expand Down
26 changes: 13 additions & 13 deletions more/transaction/tests/test_default_commit_veto.py
Expand Up @@ -6,62 +6,62 @@ def callFUT(response, request=None):


def test_it_true_500():
response = DummyResponse('500 Server Error')
response = DummyResponse("500 Server Error")
assert callFUT(response)


def test_it_true_503():
response = DummyResponse('503 Service Unavailable')
response = DummyResponse("503 Service Unavailable")
assert callFUT(response)


def test_it_true_400():
response = DummyResponse('400 Bad Request')
response = DummyResponse("400 Bad Request")
assert callFUT(response)


def test_it_true_411():
response = DummyResponse('411 Length Required')
response = DummyResponse("411 Length Required")
assert callFUT(response)


def test_it_false_200():
response = DummyResponse('200 OK')
response = DummyResponse("200 OK")
assert not callFUT(response)


def test_it_false_201():
response = DummyResponse('201 Created')
response = DummyResponse("201 Created")
assert not callFUT(response)


def test_it_false_301():
response = DummyResponse('301 Moved Permanently')
response = DummyResponse("301 Moved Permanently")
assert not callFUT(response)


def test_it_false_302():
response = DummyResponse('302 Found')
response = DummyResponse("302 Found")
assert not callFUT(response)


def test_it_false_x_tm_commit():
response = DummyResponse('200 OK', {'x-tm': 'commit'})
response = DummyResponse("200 OK", {"x-tm": "commit"})
assert not callFUT(response)


def test_it_true_x_tm_abort():
response = DummyResponse('200 OK', {'x-tm': 'abort'})
response = DummyResponse("200 OK", {"x-tm": "abort"})
assert callFUT(response)


def test_it_true_x_tm_anythingelse():
response = DummyResponse('200 OK', {'x-tm': ''})
response = DummyResponse("200 OK", {"x-tm": ""})
assert callFUT(response)


class DummyRequest(object):
path_info = '/'
path_info = "/"

def __init__(self):
self.environ = {}
Expand All @@ -72,7 +72,7 @@ def make_body_seekable(self):


class DummyResponse(object):
def __init__(self, status='200 OK', headers=None):
def __init__(self, status="200 OK", headers=None):
self.status = status
if headers is None:
headers = {}
Expand Down
63 changes: 29 additions & 34 deletions more/transaction/tests/test_transaction.py
Expand Up @@ -3,18 +3,16 @@
from transaction import TransactionManager
from transaction.interfaces import TransientError
from more.transaction import TransactionApp
from more.transaction.main import (transaction_tween_factory,
default_commit_veto)
from more.transaction.main import transaction_tween_factory, default_commit_veto
from webtest import TestApp as Client
import pytest


def test_multiple_path_variables():

class TestApp(TransactionApp):
attempts = 0

@TestApp.path('/{type}/{id}')
@TestApp.path("/{type}/{id}")
class Document(object):
def __init__(self, type, id):
self.type = type
Expand All @@ -28,24 +26,23 @@ def view_document(self, request):
if TestApp.attempts == 1:
raise Conflict

return 'ok'
return "ok"

@TestApp.setting(section='transaction', name='attempts')
@TestApp.setting(section="transaction", name="attempts")
def get_retry_attempts():
return 2

client = Client(TestApp())
response = client.get('/document/1')
assert response.text == 'ok'
response = client.get("/document/1")
assert response.text == "ok"
assert TestApp.attempts == 2


def test_reset_unconsumed_path():

class TestApp(TransactionApp):
attempts = 0

@TestApp.path('/foo/bar')
@TestApp.path("/foo/bar")
class Foo(object):
pass

Expand All @@ -57,25 +54,25 @@ def view_foo(self, request):
if TestApp.attempts == 1:
raise Conflict

return 'ok'
return "ok"

# if the unconsumed path is reset wrongly, it'll accidentally pick
# up this model instead of Foo
@TestApp.path('/bar/foo')
@TestApp.path("/bar/foo")
class Bar(object):
pass

@TestApp.view(model=Bar)
def view_bar(self, request):
return 'error'
return "error"

@TestApp.setting(section='transaction', name='attempts')
@TestApp.setting(section="transaction", name="attempts")
def get_retry_attempts():
return 2

client = Client(TestApp())
response = client.get('/foo/bar')
assert response.text == 'ok'
response = client.get("/foo/bar")
assert response.text == "ok"
assert TestApp.attempts == 2


Expand All @@ -86,11 +83,11 @@ class RootApp(TransactionApp):
class TestApp(morepath.App):
pass

@RootApp.mount(app=TestApp, path='/mount')
@RootApp.mount(app=TestApp, path="/mount")
def mount_testapp():
return TestApp()

@TestApp.path('/sub')
@TestApp.path("/sub")
class Foo(object):
pass

Expand All @@ -102,20 +99,19 @@ def view_foo(self, request):
if RootApp.attempts == 1:
raise Conflict

return 'ok'
return "ok"

@RootApp.setting(section='transaction', name='attempts')
@RootApp.setting(section="transaction", name="attempts")
def get_retry_attempts():
return 2

client = Client(RootApp())
response = client.get('/mount/sub')
assert response.text == 'ok'
response = client.get("/mount/sub")
assert response.text == "ok"
assert RootApp.attempts == 2


def test_handler_exception():

def handler(request):
raise NotImplementedError

Expand Down Expand Up @@ -168,8 +164,7 @@ def test_handler_retryable_exception_defaults_to_1():
def handler(request, count=count):
raise Conflict

publish = transaction_tween_factory(DummyApp(),
handler, DummyTransaction())
publish = transaction_tween_factory(DummyApp(), handler, DummyTransaction())

with pytest.raises(Conflict):
publish(DummyRequest())
Expand Down Expand Up @@ -199,27 +194,27 @@ def handler(request):
publish = transaction_tween_factory(DummyApp(), handler, txn)

publish(DummyRequest())
assert txn._note == '/'
assert txn._note == "/"
assert txn.username is None


def test_identity():
txn = DummyTransaction()
request = DummyRequest()
request.identity = morepath.Identity('foo')
request.identity = morepath.Identity("foo")

def handler(request):
return DummyResponse()

publish = transaction_tween_factory(DummyApp(), handler, txn)

publish(request)
assert txn.username == ':foo'
assert txn.username == ":foo"


def test_500_without_commit_veto():
response = DummyResponse()
response.status = '500 Bad Request'
response.status = "500 Bad Request"

def handler(request):
return response
Expand All @@ -238,7 +233,7 @@ def test_500_with_default_commit_veto():
app.settings.transaction.commit_veto = default_commit_veto

response = DummyResponse()
response.status = '500 Bad Request'
response.status = "500 Bad Request"

def handler(request):
return response
Expand All @@ -254,7 +249,7 @@ def handler(request):

def test_null_commit_veto():
response = DummyResponse()
response.status = '500 Bad Request'
response.status = "500 Bad Request"

def handler(request):
return response
Expand Down Expand Up @@ -376,7 +371,7 @@ def _retryable(self, t, v):
def get(self):
return self

def setUser(self, name, path='/'):
def setUser(self, name, path="/"):
self.username = "%s:%s" % (path, name)

def isDoomed(self):
Expand All @@ -399,7 +394,7 @@ def note(self, value):


class DummyRequest(object):
path = '/'
path = "/"
identity = morepath.NO_IDENTITY

def __init__(self):
Expand All @@ -418,7 +413,7 @@ def path_info(self):


class DummyResponse(object):
def __init__(self, status='200 OK', headers=None):
def __init__(self, status="200 OK", headers=None):
self.status = status
if headers is None:
headers = {}
Expand Down

0 comments on commit e551360

Please sign in to comment.