Skip to content

Commit f55c709

Browse files
authored
Cleanup/improve coverage, mainly with tests (#706)
1 parent 743ea26 commit f55c709

File tree

7 files changed

+52
-90
lines changed

7 files changed

+52
-90
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ after_success:
102102
codecov_flags=${codecov_flags//-/,}
103103
bash <(curl -s https://codecov.io/bash) -Z -X gcov -X xcode -X gcovout -F "$codecov_flags"
104104
fi
105-
set +x
105+
set +ex

pytest_django_test/app/models.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@
33

44
class Item(models.Model):
55
name = models.CharField(max_length=100)
6-
7-
def __unicode__(self):
8-
return self.name
9-
10-
def __str__(self):
11-
return self.name

pytest_django_test/app/views.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77

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

1312

1413
def item_count(request):

pytest_django_test/db_helpers.py

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def run_cmd(*args):
4747

4848
def run_mysql(*args):
4949
user = _settings.get("USER", None)
50-
if user:
50+
if user: # pragma: no branch
5151
args = ("-u", user) + tuple(args)
5252
args = ("mysql",) + tuple(args)
5353
return run_cmd(*args)
@@ -61,110 +61,97 @@ def skip_if_sqlite_in_memory():
6161
pytest.skip("Do not test db reuse since database does not support it")
6262

6363

64-
def drop_database(name=TEST_DB_NAME, suffix=None):
65-
assert bool(name) ^ bool(suffix), "name and suffix cannot be used together"
64+
def drop_database(name=TEST_DB_NAME):
65+
db_engine = get_db_engine()
6666

67-
if suffix:
68-
name = "%s_%s" % (name, suffix)
69-
70-
if get_db_engine() == "postgresql_psycopg2":
67+
if db_engine == "postgresql_psycopg2":
7168
r = run_cmd("psql", "postgres", "-c", "DROP DATABASE %s" % name)
7269
assert "DROP DATABASE" in force_text(
7370
r.std_out
7471
) or "does not exist" in force_text(r.std_err)
7572
return
7673

77-
if get_db_engine() == "mysql":
74+
if db_engine == "mysql":
7875
r = run_mysql("-e", "DROP DATABASE %s" % name)
7976
assert "database doesn't exist" in force_text(r.std_err) or r.status_code == 0
8077
return
8178

82-
if get_db_engine() == "sqlite3":
83-
if name == ":memory:":
84-
raise AssertionError("sqlite in-memory database cannot be dropped!")
85-
if os.path.exists(name):
86-
os.unlink(name)
87-
return
88-
89-
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
79+
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
80+
assert name != ":memory:", "sqlite in-memory database cannot be dropped!"
81+
if os.path.exists(name): # pragma: no branch
82+
os.unlink(name)
9083

9184

9285
def db_exists(db_suffix=None):
9386
name = TEST_DB_NAME
87+
db_engine = get_db_engine()
9488

9589
if db_suffix:
9690
name = "%s_%s" % (name, db_suffix)
9791

98-
if get_db_engine() == "postgresql_psycopg2":
92+
if db_engine == "postgresql_psycopg2":
9993
r = run_cmd("psql", name, "-c", "SELECT 1")
10094
return r.status_code == 0
10195

102-
if get_db_engine() == "mysql":
96+
if db_engine == "mysql":
10397
r = run_mysql(name, "-e", "SELECT 1")
10498
return r.status_code == 0
10599

106-
if get_db_engine() == "sqlite3":
107-
if TEST_DB_NAME == ":memory:":
108-
raise AssertionError(
109-
"sqlite in-memory database cannot be checked for existence!"
110-
)
111-
return os.path.exists(name)
112-
113-
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
100+
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
101+
assert TEST_DB_NAME != ":memory:", (
102+
"sqlite in-memory database cannot be checked for existence!")
103+
return os.path.exists(name)
114104

115105

116106
def mark_database():
117-
if get_db_engine() == "postgresql_psycopg2":
107+
db_engine = get_db_engine()
108+
109+
if db_engine == "postgresql_psycopg2":
118110
r = run_cmd("psql", TEST_DB_NAME, "-c", "CREATE TABLE mark_table();")
119111
assert r.status_code == 0
120112
return
121113

122-
if get_db_engine() == "mysql":
114+
if db_engine == "mysql":
123115
r = run_mysql(TEST_DB_NAME, "-e", "CREATE TABLE mark_table(kaka int);")
124116
assert r.status_code == 0
125117
return
126118

127-
if get_db_engine() == "sqlite3":
128-
if TEST_DB_NAME == ":memory:":
129-
raise AssertionError("sqlite in-memory database cannot be marked!")
130-
131-
conn = sqlite3.connect(TEST_DB_NAME)
132-
try:
133-
with conn:
134-
conn.execute("CREATE TABLE mark_table(kaka int);")
135-
finally: # Close the DB even if an error is raised
136-
conn.close()
137-
return
119+
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
120+
assert TEST_DB_NAME != ":memory:", (
121+
"sqlite in-memory database cannot be marked!")
138122

139-
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
123+
conn = sqlite3.connect(TEST_DB_NAME)
124+
try:
125+
with conn:
126+
conn.execute("CREATE TABLE mark_table(kaka int);")
127+
finally: # Close the DB even if an error is raised
128+
conn.close()
140129

141130

142131
def mark_exists():
143-
if get_db_engine() == "postgresql_psycopg2":
132+
db_engine = get_db_engine()
133+
134+
if db_engine == "postgresql_psycopg2":
144135
r = run_cmd("psql", TEST_DB_NAME, "-c", "SELECT 1 FROM mark_table")
145136

146137
# When something pops out on std_out, we are good
147138
return bool(r.std_out)
148139

149-
if get_db_engine() == "mysql":
140+
if db_engine == "mysql":
150141
r = run_mysql(TEST_DB_NAME, "-e", "SELECT 1 FROM mark_table")
151142

152143
return r.status_code == 0
153144

154-
if get_db_engine() == "sqlite3":
155-
if TEST_DB_NAME == ":memory:":
156-
raise AssertionError(
157-
"sqlite in-memory database cannot be checked for mark!"
158-
)
159-
160-
conn = sqlite3.connect(TEST_DB_NAME)
161-
try:
162-
with conn:
163-
conn.execute("SELECT 1 FROM mark_table")
164-
return True
165-
except sqlite3.OperationalError:
166-
return False
167-
finally: # Close the DB even if an error is raised
168-
conn.close()
169-
170-
raise AssertionError("%s cannot be tested properly!" % get_db_engine())
145+
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
146+
assert TEST_DB_NAME != ":memory:", (
147+
"sqlite in-memory database cannot be checked for mark!")
148+
149+
conn = sqlite3.connect(TEST_DB_NAME)
150+
try:
151+
with conn:
152+
conn.execute("SELECT 1 FROM mark_table")
153+
return True
154+
except sqlite3.OperationalError:
155+
return False
156+
finally: # Close the DB even if an error is raised
157+
conn.close()

pytest_django_test/settings_base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
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", "")
20-
21-
if uid:
22-
db_suffix = "_%s" % uid
23-
else:
24-
db_suffix = ""
19+
db_suffix = "_%s" % os.getuid()
2520

2621
MIDDLEWARE = [
2722
"django.contrib.sessions.middleware.SessionMiddleware",

tests/test_django_configurations.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66

77
pytest.importorskip("configurations")
88

9-
try:
10-
import configurations.importer
11-
12-
configurations
13-
except ImportError as e:
14-
if "LaxOptionParser" in e.args[0]:
15-
pytest.skip(
16-
"This version of django-configurations is incompatible with Django: " # noqa
17-
"https://github.com/jezdez/django-configurations/issues/65"
18-
) # noqa
199

2010
BARE_SETTINGS = """
2111
from configurations import Configuration

tests/test_fixtures.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,8 @@ class MyCustomUser(AbstractUser):
522522
523523
524524
def admin_required_view(request):
525-
if request.user.is_staff:
526-
return HttpResponse(
527-
Template('You are an admin').render(Context()))
528-
return HttpResponse(
529-
Template('Access denied').render(Context()))
525+
assert request.user.is_staff
526+
return HttpResponse(Template('You are an admin').render(Context()))
530527
""",
531528
"views.py",
532529
)

0 commit comments

Comments
 (0)