Skip to content

Commit 2ec5318

Browse files
authored
Run black on tests (#692)
1 parent deb8a16 commit 2ec5318

15 files changed

+153
-140
lines changed

pytest_django/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def live_server(request):
364364
warnings.warn(
365365
"Specifying multiple live server ports is not supported "
366366
"in Django 1.11. This will be an error in a future "
367-
"pytest-django release.",
367+
"pytest-django release."
368368
)
369369

370370
if not addr:

pytest_django_test/app/migrations/0001_initial.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@ class Migration(migrations.Migration):
99

1010
initial = True
1111

12-
dependencies = [
13-
]
12+
dependencies = []
1413

1514
operations = [
1615
migrations.CreateModel(
17-
name='Item',
16+
name="Item",
1817
fields=[
19-
('id', models.AutoField(auto_created=True, primary_key=True,
20-
serialize=False, verbose_name='ID')),
21-
('name', models.CharField(max_length=100)),
18+
(
19+
"id",
20+
models.AutoField(
21+
auto_created=True,
22+
primary_key=True,
23+
serialize=False,
24+
verbose_name="ID",
25+
),
26+
),
27+
("name", models.CharField(max_length=100)),
2228
],
23-
),
29+
)
2430
]

pytest_django_test/app/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
def admin_required_view(request):
99
if request.user.is_staff:
10-
return HttpResponse(Template('You are an admin').render(Context()))
11-
return HttpResponse(Template('Access denied').render(Context()))
10+
return HttpResponse(Template("You are an admin").render(Context()))
11+
return HttpResponse(Template("Access denied").render(Context()))
1212

1313

1414
def item_count(request):
15-
return HttpResponse('Item count: %d' % Item.objects.count())
15+
return HttpResponse("Item count: %d" % Item.objects.count())

pytest_django_test/compat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
try:
88
from django.conf.urls import patterns
99
except ImportError:
10+
1011
def patterns(prefix, *urls):
11-
assert prefix == ''
12+
assert prefix == ""
1213
return urls

pytest_django_test/db_helpers.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@
99

1010

1111
# Construct names for the "inner" database used in runpytest tests
12-
_settings = settings.DATABASES['default']
12+
_settings = settings.DATABASES["default"]
1313

14-
DB_NAME = _settings['NAME']
15-
TEST_DB_NAME = _settings['TEST']['NAME']
14+
DB_NAME = _settings["NAME"]
15+
TEST_DB_NAME = _settings["TEST"]["NAME"]
1616

17-
if _settings['ENGINE'] == 'django.db.backends.sqlite3' and TEST_DB_NAME is None:
18-
TEST_DB_NAME = ':memory:'
17+
if _settings["ENGINE"] == "django.db.backends.sqlite3" and TEST_DB_NAME is None:
18+
TEST_DB_NAME = ":memory:"
1919
else:
20-
DB_NAME += '_inner'
20+
DB_NAME += "_inner"
2121

2222
if TEST_DB_NAME is None:
2323
# No explicit test db name was given, construct a default one
24-
TEST_DB_NAME = 'test_{}_inner'.format(DB_NAME)
24+
TEST_DB_NAME = "test_{}_inner".format(DB_NAME)
2525
else:
2626
# An explicit test db name was given, is that as the base name
27-
TEST_DB_NAME = '{}_inner'.format(TEST_DB_NAME)
27+
TEST_DB_NAME = "{}_inner".format(TEST_DB_NAME)
2828

2929

3030
def get_db_engine():
31-
return _settings['ENGINE'].split('.')[-1]
31+
return _settings["ENGINE"].split(".")[-1]
3232

3333

3434
class CmdResult(object):
@@ -46,121 +46,125 @@ def run_cmd(*args):
4646

4747

4848
def run_mysql(*args):
49-
user = _settings.get('USER', None)
49+
user = _settings.get("USER", None)
5050
if user:
51-
args = ('-u', user) + tuple(args)
52-
args = ('mysql',) + tuple(args)
51+
args = ("-u", user) + tuple(args)
52+
args = ("mysql",) + tuple(args)
5353
return run_cmd(*args)
5454

5555

5656
def skip_if_sqlite_in_memory():
57-
if _settings['ENGINE'] == 'django.db.backends.sqlite3' and _settings['TEST']['NAME'] is None:
58-
pytest.skip('Do not test db reuse since database does not support it')
57+
if (
58+
_settings["ENGINE"] == "django.db.backends.sqlite3"
59+
and _settings["TEST"]["NAME"] is None
60+
):
61+
pytest.skip("Do not test db reuse since database does not support it")
5962

6063

6164
def drop_database(name=TEST_DB_NAME, suffix=None):
62-
assert bool(name) ^ bool(suffix), 'name and suffix cannot be used together'
65+
assert bool(name) ^ bool(suffix), "name and suffix cannot be used together"
6366

6467
if suffix:
65-
name = '%s_%s' % (name, suffix)
68+
name = "%s_%s" % (name, suffix)
6669

67-
if get_db_engine() == 'postgresql_psycopg2':
68-
r = run_cmd('psql', 'postgres', '-c', 'DROP DATABASE %s' % name)
70+
if get_db_engine() == "postgresql_psycopg2":
71+
r = run_cmd("psql", "postgres", "-c", "DROP DATABASE %s" % name)
6972
assert "DROP DATABASE" in force_text(
7073
r.std_out
7174
) or "does not exist" in force_text(r.std_err)
7275
return
7376

74-
if get_db_engine() == 'mysql':
75-
r = run_mysql('-e', 'DROP DATABASE %s' % name)
77+
if get_db_engine() == "mysql":
78+
r = run_mysql("-e", "DROP DATABASE %s" % name)
7679
assert "database doesn't exist" in force_text(r.std_err) or r.status_code == 0
7780
return
7881

79-
if get_db_engine() == 'sqlite3':
80-
if name == ':memory:':
81-
raise AssertionError(
82-
'sqlite in-memory database cannot be dropped!')
82+
if get_db_engine() == "sqlite3":
83+
if name == ":memory:":
84+
raise AssertionError("sqlite in-memory database cannot be dropped!")
8385
if os.path.exists(name):
8486
os.unlink(name)
8587
return
8688

87-
raise AssertionError('%s cannot be tested properly!' % get_db_engine())
89+
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
8890

8991

9092
def db_exists(db_suffix=None):
9193
name = TEST_DB_NAME
9294

9395
if db_suffix:
94-
name = '%s_%s' % (name, db_suffix)
96+
name = "%s_%s" % (name, db_suffix)
9597

96-
if get_db_engine() == 'postgresql_psycopg2':
97-
r = run_cmd('psql', name, '-c', 'SELECT 1')
98+
if get_db_engine() == "postgresql_psycopg2":
99+
r = run_cmd("psql", name, "-c", "SELECT 1")
98100
return r.status_code == 0
99101

100-
if get_db_engine() == 'mysql':
101-
r = run_mysql(name, '-e', 'SELECT 1')
102+
if get_db_engine() == "mysql":
103+
r = run_mysql(name, "-e", "SELECT 1")
102104
return r.status_code == 0
103105

104-
if get_db_engine() == 'sqlite3':
105-
if TEST_DB_NAME == ':memory:':
106+
if get_db_engine() == "sqlite3":
107+
if TEST_DB_NAME == ":memory:":
106108
raise AssertionError(
107-
'sqlite in-memory database cannot be checked for existence!')
109+
"sqlite in-memory database cannot be checked for existence!"
110+
)
108111
return os.path.exists(name)
109112

110-
raise AssertionError('%s cannot be tested properly!' % get_db_engine())
113+
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
111114

112115

113116
def mark_database():
114-
if get_db_engine() == 'postgresql_psycopg2':
115-
r = run_cmd('psql', TEST_DB_NAME, '-c', 'CREATE TABLE mark_table();')
117+
if get_db_engine() == "postgresql_psycopg2":
118+
r = run_cmd("psql", TEST_DB_NAME, "-c", "CREATE TABLE mark_table();")
116119
assert r.status_code == 0
117120
return
118121

119-
if get_db_engine() == 'mysql':
120-
r = run_mysql(TEST_DB_NAME, '-e', 'CREATE TABLE mark_table(kaka int);')
122+
if get_db_engine() == "mysql":
123+
r = run_mysql(TEST_DB_NAME, "-e", "CREATE TABLE mark_table(kaka int);")
121124
assert r.status_code == 0
122125
return
123126

124-
if get_db_engine() == 'sqlite3':
125-
if TEST_DB_NAME == ':memory:':
126-
raise AssertionError('sqlite in-memory database cannot be marked!')
127+
if get_db_engine() == "sqlite3":
128+
if TEST_DB_NAME == ":memory:":
129+
raise AssertionError("sqlite in-memory database cannot be marked!")
127130

128131
conn = sqlite3.connect(TEST_DB_NAME)
129132
try:
130133
with conn:
131-
conn.execute('CREATE TABLE mark_table(kaka int);')
134+
conn.execute("CREATE TABLE mark_table(kaka int);")
132135
finally: # Close the DB even if an error is raised
133136
conn.close()
134137
return
135138

136-
raise AssertionError('%s cannot be tested properly!' % get_db_engine())
139+
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
137140

138141

139142
def mark_exists():
140-
if get_db_engine() == 'postgresql_psycopg2':
141-
r = run_cmd('psql', TEST_DB_NAME, '-c', 'SELECT 1 FROM mark_table')
143+
if get_db_engine() == "postgresql_psycopg2":
144+
r = run_cmd("psql", TEST_DB_NAME, "-c", "SELECT 1 FROM mark_table")
142145

143146
# When something pops out on std_out, we are good
144147
return bool(r.std_out)
145148

146-
if get_db_engine() == 'mysql':
147-
r = run_mysql(TEST_DB_NAME, '-e', 'SELECT 1 FROM mark_table')
149+
if get_db_engine() == "mysql":
150+
r = run_mysql(TEST_DB_NAME, "-e", "SELECT 1 FROM mark_table")
148151

149152
return r.status_code == 0
150153

151-
if get_db_engine() == 'sqlite3':
152-
if TEST_DB_NAME == ':memory:':
154+
if get_db_engine() == "sqlite3":
155+
if TEST_DB_NAME == ":memory:":
153156
raise AssertionError(
154-
'sqlite in-memory database cannot be checked for mark!')
157+
"sqlite in-memory database cannot be checked for mark!"
158+
)
155159

156160
conn = sqlite3.connect(TEST_DB_NAME)
157161
try:
158162
with conn:
159-
conn.execute('SELECT 1 FROM mark_table')
163+
conn.execute("SELECT 1 FROM mark_table")
160164
return True
161165
except sqlite3.OperationalError:
162166
return False
163167
finally: # Close the DB even if an error is raised
164168
conn.close()
165169

166-
raise AssertionError('%s cannot be tested properly!' % get_db_engine())
170+
raise AssertionError("%s cannot be tested properly!" % get_db_engine())

pytest_django_test/settings_base.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22

33
import django
44

5-
ROOT_URLCONF = 'pytest_django_test.urls'
5+
ROOT_URLCONF = "pytest_django_test.urls"
66
INSTALLED_APPS = [
7-
'django.contrib.auth',
8-
'django.contrib.contenttypes',
9-
'django.contrib.sessions',
10-
'django.contrib.sites',
11-
'pytest_django_test.app',
7+
"django.contrib.auth",
8+
"django.contrib.contenttypes",
9+
"django.contrib.sessions",
10+
"django.contrib.sites",
11+
"pytest_django_test.app",
1212
]
1313

14-
STATIC_URL = '/static/'
15-
SECRET_KEY = 'foobar'
14+
STATIC_URL = "/static/"
15+
SECRET_KEY = "foobar"
1616

1717
# Used to construct unique test database names to allow detox to run multiple
1818
# versions at the same time
19-
uid = os.getenv('UID', '')
19+
uid = os.getenv("UID", "")
2020

2121
if uid:
22-
db_suffix = '_%s' % uid
22+
db_suffix = "_%s" % uid
2323
else:
24-
db_suffix = ''
24+
db_suffix = ""
2525

2626
MIDDLEWARE = [
27-
'django.contrib.sessions.middleware.SessionMiddleware',
28-
'django.middleware.common.CommonMiddleware',
29-
'django.middleware.csrf.CsrfViewMiddleware',
30-
'django.contrib.auth.middleware.AuthenticationMiddleware',
31-
'django.contrib.messages.middleware.MessageMiddleware',
27+
"django.contrib.sessions.middleware.SessionMiddleware",
28+
"django.middleware.common.CommonMiddleware",
29+
"django.middleware.csrf.CsrfViewMiddleware",
30+
"django.contrib.auth.middleware.AuthenticationMiddleware",
31+
"django.contrib.messages.middleware.MessageMiddleware",
3232
]
3333

3434
if django.VERSION < (1, 10):
@@ -37,9 +37,9 @@
3737

3838
TEMPLATES = [
3939
{
40-
'BACKEND': 'django.template.backends.django.DjangoTemplates',
41-
'DIRS': [],
42-
'APP_DIRS': True,
43-
'OPTIONS': {},
44-
},
40+
"BACKEND": "django.template.backends.django.DjangoTemplates",
41+
"DIRS": [],
42+
"APP_DIRS": True,
43+
"OPTIONS": {},
44+
}
4545
]
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from .settings_base import * # noqa
22

33
DATABASES = {
4-
'default': {
5-
'ENGINE': 'django.db.backends.mysql',
6-
'NAME': 'pytest_django' + db_suffix, # noqa
7-
'HOST': 'localhost',
8-
'USER': 'root',
9-
'OPTIONS': {
10-
'init_command': 'SET storage_engine=InnoDB'
11-
}
12-
},
4+
"default": {
5+
"ENGINE": "django.db.backends.mysql",
6+
"NAME": "pytest_django" + db_suffix, # noqa
7+
"HOST": "localhost",
8+
"USER": "root",
9+
"OPTIONS": {"init_command": "SET storage_engine=InnoDB"},
10+
}
1311
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from pytest_django_test.settings_base import * # noqa
22

33
DATABASES = {
4-
'default': {
5-
'ENGINE': 'django.db.backends.mysql',
6-
'NAME': 'pytest_django' + db_suffix, # noqa
7-
'HOST': 'localhost',
8-
'USER': 'root',
9-
'OPTIONS': {
10-
'init_command': 'SET storage_engine=MyISAM'
11-
}
12-
},
4+
"default": {
5+
"ENGINE": "django.db.backends.mysql",
6+
"NAME": "pytest_django" + db_suffix, # noqa
7+
"HOST": "localhost",
8+
"USER": "root",
9+
"OPTIONS": {"init_command": "SET storage_engine=MyISAM"},
10+
}
1311
}

pytest_django_test/settings_postgres.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
# PyPy compatibility
44
try:
55
from psycopg2ct import compat
6+
67
compat.register()
78
except ImportError:
89
pass
910

1011

1112
DATABASES = {
12-
'default': {
13-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
14-
'NAME': 'pytest_django' + db_suffix, # noqa
15-
'HOST': 'localhost',
16-
'USER': '',
17-
},
13+
"default": {
14+
"ENGINE": "django.db.backends.postgresql_psycopg2",
15+
"NAME": "pytest_django" + db_suffix, # noqa
16+
"HOST": "localhost",
17+
"USER": "",
18+
}
1819
}

0 commit comments

Comments
 (0)