Rework conftests + sql tables are cleared on success

- Moved common parts to main conftest.py
- Added one hacky fixture that reports test result
- SQL tables are now cleared after test method succeeds

Closes-bug: #1545779
Closes-bug: #1546983

Change-Id: If2d97c58d838062d3a579dc83014bc0645786da7
This commit is contained in:
Jedrzej Nowak 2016-02-18 12:27:31 +01:00
parent 302affe6aa
commit ab96973ea9
4 changed files with 72 additions and 69 deletions

View File

@ -13,12 +13,37 @@
# under the License.
import os
import pytest
import time
from solar.config import C
C.solar_db = C.solar_db.format(PID=os.getpid())
from solar.dblayer.model import get_bucket
from solar.dblayer.model import Model
from solar.dblayer.model import ModelMeta
from solar import utils
C.solar_db = C.solar_db.format(PID=os.getpid())
# workaround to provide test result in other fixtures
# https://github.com/pytest-dev/pytest/issues/288
@pytest.fixture
def solar_testresult():
class TestResult(object):
rep = None
return TestResult()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
result = yield
rep = result.get_result()
if 'solar_testresult' in item.fixturenames:
if 'solar_testresult' not in item.funcargs:
return
item.funcargs['solar_testresult'].rep = rep
# end of workaround
def pytest_addoption(parser):
@ -32,3 +57,46 @@ def pytest_unconfigure(config):
db, opts = utils.parse_database_conn(C.solar_db)
if db.mode == 'sqlite' and os.path.isfile(db.database):
os.unlink(db.database)
def patched_get_bucket_name(cls):
return cls.__name__ + str(os.getpid()) + str(time.time())
Model.get_bucket_name = classmethod(patched_get_bucket_name)
def pytest_runtest_teardown(item, nextitem):
ModelMeta.session_end(result=True)
return nextitem
# It will run before all fixtures
def pytest_runtest_setup(item):
ModelMeta.session_start()
# it will run after fixtures but before test
def pytest_runtest_call(item):
ModelMeta.session_end()
ModelMeta.session_start()
@pytest.fixture(autouse=True)
def setup(request, solar_testresult):
for model in ModelMeta._defined_models:
model.bucket = get_bucket(None, model, ModelMeta)
_connection, _ = utils.parse_database_conn(C.solar_db)
if _connection.type == 'sql':
def drop_tables_on_sql():
# clean only when tests not crashed
if solar_testresult.rep.failed:
return
for model in ModelMeta._defined_models:
model.bucket._sql_idx.drop_table(fail_silently=False)
model.bucket._sql_model.drop_table(fail_silently=False)
request.addfinalizer(drop_tables_on_sql)

View File

@ -271,7 +271,7 @@ class Bucket(object):
_idx_key = ForeignKeyField(self._sql_model,
null=False,
index=True,
on_delete='cascade')
on_delete='CASCADE')
class IdxMeta(object):
db_table = idx_table_name

View File

@ -12,20 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import pytest
import random
import string
import time
import pytest
from solar.dblayer.model import get_bucket
from solar.dblayer.model import Model
from solar.dblayer.model import ModelMeta
def patched_get_bucket_name(cls):
return cls.__name__ + str(time.time())
class RndObj(object):
@ -63,18 +52,6 @@ def rt(request):
return obj
@pytest.fixture(autouse=True)
def setup(request):
for model in ModelMeta._defined_models:
model.bucket = get_bucket(None, model, ModelMeta)
def pytest_runtest_teardown(item, nextitem):
ModelMeta.session_end(result=True)
return nextitem
def pytest_runtest_setup(item):
# ALL Computable Inputs tests are in single file
# so for easy skip we need this
@ -86,10 +63,6 @@ def pytest_runtest_setup(item):
pytest.skip("Lupa is required to test lua")
def pytest_runtest_call(item):
ModelMeta.session_start()
def dicts_to_hashable(list_of_dics):
rst = []
for item in list_of_dics:
@ -99,6 +72,3 @@ def dicts_to_hashable(list_of_dics):
def pytest_namespace():
return {'dicts_to_hashable': dicts_to_hashable}
Model.get_bucket_name = classmethod(patched_get_bucket_name)

View File

@ -11,23 +11,16 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import time
import pytest
from solar.core.resource.repository import Repository
from solar.core.resource import Resource
from solar.dblayer.model import get_bucket
from solar.dblayer.model import Model
from solar.dblayer.model import ModelMeta
from solar.orchestration import graph
def patched_get_bucket_name(cls):
return cls.__name__ + str(os.getpid()) + str(time.time())
@pytest.fixture
def resources():
base_path = os.path.join(
@ -46,13 +39,6 @@ def resources():
}
@pytest.fixture(autouse=True)
def setup(request):
for model in ModelMeta._defined_models:
model.bucket = get_bucket(None, model, ModelMeta)
@pytest.fixture(scope='session', autouse=True)
def repos_path(tmpdir_factory):
Repository._REPOS_LOCATION = str(tmpdir_factory.mktemp('repositories'))
@ -61,27 +47,6 @@ def repos_path(tmpdir_factory):
repo.create(path)
def pytest_runtest_teardown(item, nextitem):
ModelMeta.session_end(result=True)
return nextitem
# It will run before all fixtures
def pytest_runtest_setup(item):
ModelMeta.session_start()
# it will run after fixtures but before test
def pytest_runtest_call(item):
ModelMeta.session_end()
ModelMeta.session_start()
Model.get_bucket_name = classmethod(patched_get_bucket_name)
def plan_from_fixture(name):
riak_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'orch_fixtures',