diff --git a/.travis.yml b/.travis.yml index fbe1e3f1d..a10a0d7f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/pytest_django_test/app/models.py b/pytest_django_test/app/models.py index 717c02048..381ce30aa 100644 --- a/pytest_django_test/app/models.py +++ b/pytest_django_test/app/models.py @@ -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 diff --git a/pytest_django_test/app/views.py b/pytest_django_test/app/views.py index 8e50fd81d..b400f408b 100644 --- a/pytest_django_test/app/views.py +++ b/pytest_django_test/app/views.py @@ -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): diff --git a/pytest_django_test/db_helpers.py b/pytest_django_test/db_helpers.py index bb1cc56b2..dcee3c3da 100644 --- a/pytest_django_test/db_helpers.py +++ b/pytest_django_test/db_helpers.py @@ -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) @@ -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() diff --git a/pytest_django_test/settings_base.py b/pytest_django_test/settings_base.py index 0a108b797..543ccaff5 100644 --- a/pytest_django_test/settings_base.py +++ b/pytest_django_test/settings_base.py @@ -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", diff --git a/tests/test_django_configurations.py b/tests/test_django_configurations.py index a7aa7a982..5928a6e04 100644 --- a/tests/test_django_configurations.py +++ b/tests/test_django_configurations.py @@ -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 diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 24265a716..124127247 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -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", )