Merge "Rework conftests + sql tables are cleared on success"

This commit is contained in:
Jenkins 2016-02-19 09:11:37 +00:00 committed by Gerrit Code Review
commit c2bb129b79
4 changed files with 72 additions and 69 deletions

View File

@ -13,12 +13,37 @@
# under the License. # under the License.
import os import os
import pytest
import time
from solar.config import C 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 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): def pytest_addoption(parser):
@ -32,3 +57,46 @@ def pytest_unconfigure(config):
db, opts = utils.parse_database_conn(C.solar_db) db, opts = utils.parse_database_conn(C.solar_db)
if db.mode == 'sqlite' and os.path.isfile(db.database): if db.mode == 'sqlite' and os.path.isfile(db.database):
os.unlink(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, _idx_key = ForeignKeyField(self._sql_model,
null=False, null=False,
index=True, index=True,
on_delete='cascade') on_delete='CASCADE')
class IdxMeta(object): class IdxMeta(object):
db_table = idx_table_name db_table = idx_table_name

View File

@ -12,20 +12,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import pytest
import random import random
import string 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): class RndObj(object):
@ -63,18 +52,6 @@ def rt(request):
return obj 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): def pytest_runtest_setup(item):
# ALL Computable Inputs tests are in single file # ALL Computable Inputs tests are in single file
# so for easy skip we need this # so for easy skip we need this
@ -86,10 +63,6 @@ def pytest_runtest_setup(item):
pytest.skip("Lupa is required to test lua") pytest.skip("Lupa is required to test lua")
def pytest_runtest_call(item):
ModelMeta.session_start()
def dicts_to_hashable(list_of_dics): def dicts_to_hashable(list_of_dics):
rst = [] rst = []
for item in list_of_dics: for item in list_of_dics:
@ -99,6 +72,3 @@ def dicts_to_hashable(list_of_dics):
def pytest_namespace(): def pytest_namespace():
return {'dicts_to_hashable': dicts_to_hashable} 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 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import os import os
import time
import pytest import pytest
from solar.core.resource.repository import Repository from solar.core.resource.repository import Repository
from solar.core.resource import Resource 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 from solar.orchestration import graph
def patched_get_bucket_name(cls):
return cls.__name__ + str(os.getpid()) + str(time.time())
@pytest.fixture @pytest.fixture
def resources(): def resources():
base_path = os.path.join( 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) @pytest.fixture(scope='session', autouse=True)
def repos_path(tmpdir_factory): def repos_path(tmpdir_factory):
Repository._REPOS_LOCATION = str(tmpdir_factory.mktemp('repositories')) Repository._REPOS_LOCATION = str(tmpdir_factory.mktemp('repositories'))
@ -61,27 +47,6 @@ def repos_path(tmpdir_factory):
repo.create(path) 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): def plan_from_fixture(name):
riak_path = os.path.join( riak_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'orch_fixtures', os.path.dirname(os.path.realpath(__file__)), 'orch_fixtures',