Skip to content

Cleanup/improve coverage, mainly with tests #706

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 3 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
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: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ after_success:
codecov_flags=${codecov_flags//-/,}
bash <(curl -s https://codecov.io/bash) -Z -X gcov -X xcode -X gcovout -F "$codecov_flags"
fi
set +x
set +ex
6 changes: 0 additions & 6 deletions pytest_django_test/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,3 @@

class Item(models.Model):
name = models.CharField(max_length=100)

def __unicode__(self):
return self.name

def __str__(self):
return self.name
5 changes: 2 additions & 3 deletions pytest_django_test/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@


def admin_required_view(request):
if request.user.is_staff:
return HttpResponse(Template("You are an admin").render(Context()))
return HttpResponse(Template("Access denied").render(Context()))
assert request.user.is_staff
return HttpResponse(Template("You are an admin").render(Context()))


def item_count(request):
Expand Down
105 changes: 46 additions & 59 deletions pytest_django_test/db_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run_cmd(*args):

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


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

if suffix:
name = "%s_%s" % (name, suffix)

if get_db_engine() == "postgresql_psycopg2":
if db_engine == "postgresql_psycopg2":
r = run_cmd("psql", "postgres", "-c", "DROP DATABASE %s" % name)
assert "DROP DATABASE" in force_text(
r.std_out
) or "does not exist" in force_text(r.std_err)
return

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

if get_db_engine() == "sqlite3":
if name == ":memory:":
raise AssertionError("sqlite in-memory database cannot be dropped!")
if os.path.exists(name):
os.unlink(name)
return

raise AssertionError("%s cannot be tested properly!" % get_db_engine())
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
assert name != ":memory:", "sqlite in-memory database cannot be dropped!"
if os.path.exists(name): # pragma: no branch
os.unlink(name)


def db_exists(db_suffix=None):
name = TEST_DB_NAME
db_engine = get_db_engine()

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

if get_db_engine() == "postgresql_psycopg2":
if db_engine == "postgresql_psycopg2":
r = run_cmd("psql", name, "-c", "SELECT 1")
return r.status_code == 0

if get_db_engine() == "mysql":
if db_engine == "mysql":
r = run_mysql(name, "-e", "SELECT 1")
return r.status_code == 0

if get_db_engine() == "sqlite3":
if TEST_DB_NAME == ":memory:":
raise AssertionError(
"sqlite in-memory database cannot be checked for existence!"
)
return os.path.exists(name)

raise AssertionError("%s cannot be tested properly!" % get_db_engine())
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
assert TEST_DB_NAME != ":memory:", (
"sqlite in-memory database cannot be checked for existence!")
return os.path.exists(name)


def mark_database():
if get_db_engine() == "postgresql_psycopg2":
db_engine = get_db_engine()

if db_engine == "postgresql_psycopg2":
r = run_cmd("psql", TEST_DB_NAME, "-c", "CREATE TABLE mark_table();")
assert r.status_code == 0
return

if get_db_engine() == "mysql":
if db_engine == "mysql":
r = run_mysql(TEST_DB_NAME, "-e", "CREATE TABLE mark_table(kaka int);")
assert r.status_code == 0
return

if get_db_engine() == "sqlite3":
if TEST_DB_NAME == ":memory:":
raise AssertionError("sqlite in-memory database cannot be marked!")

conn = sqlite3.connect(TEST_DB_NAME)
try:
with conn:
conn.execute("CREATE TABLE mark_table(kaka int);")
finally: # Close the DB even if an error is raised
conn.close()
return
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
assert TEST_DB_NAME != ":memory:", (
"sqlite in-memory database cannot be marked!")

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


def mark_exists():
if get_db_engine() == "postgresql_psycopg2":
db_engine = get_db_engine()

if db_engine == "postgresql_psycopg2":
r = run_cmd("psql", TEST_DB_NAME, "-c", "SELECT 1 FROM mark_table")

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

if get_db_engine() == "mysql":
if db_engine == "mysql":
r = run_mysql(TEST_DB_NAME, "-e", "SELECT 1 FROM mark_table")

return r.status_code == 0

if get_db_engine() == "sqlite3":
if TEST_DB_NAME == ":memory:":
raise AssertionError(
"sqlite in-memory database cannot be checked for mark!"
)

conn = sqlite3.connect(TEST_DB_NAME)
try:
with conn:
conn.execute("SELECT 1 FROM mark_table")
return True
except sqlite3.OperationalError:
return False
finally: # Close the DB even if an error is raised
conn.close()

raise AssertionError("%s cannot be tested properly!" % get_db_engine())
assert db_engine == "sqlite3", "%s cannot be tested properly!" % db_engine
assert TEST_DB_NAME != ":memory:", (
"sqlite in-memory database cannot be checked for mark!")

conn = sqlite3.connect(TEST_DB_NAME)
try:
with conn:
conn.execute("SELECT 1 FROM mark_table")
return True
except sqlite3.OperationalError:
return False
finally: # Close the DB even if an error is raised
conn.close()
7 changes: 1 addition & 6 deletions pytest_django_test/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

# Used to construct unique test database names to allow detox to run multiple
# versions at the same time
uid = os.getenv("UID", "")

if uid:
db_suffix = "_%s" % uid
else:
db_suffix = ""
db_suffix = "_%s" % os.getuid()

MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
Expand Down
10 changes: 0 additions & 10 deletions tests/test_django_configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@

pytest.importorskip("configurations")

try:
import configurations.importer

configurations
except ImportError as e:
if "LaxOptionParser" in e.args[0]:
pytest.skip(
"This version of django-configurations is incompatible with Django: " # noqa
"https://github.com/jezdez/django-configurations/issues/65"
) # noqa

BARE_SETTINGS = """
from configurations import Configuration
Expand Down
7 changes: 2 additions & 5 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,8 @@ class MyCustomUser(AbstractUser):


def admin_required_view(request):
if request.user.is_staff:
return HttpResponse(
Template('You are an admin').render(Context()))
return HttpResponse(
Template('Access denied').render(Context()))
assert request.user.is_staff
return HttpResponse(Template('You are an admin').render(Context()))
""",
"views.py",
)
Expand Down