Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/core/tests/test_model.py
# Copyright (C) 2017 the Software Heritage developers | # Copyright (C) 2017 the Software Heritage developers | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import unittest | import unittest | ||||
from nose.tools import istest | from nose.tools import istest | ||||
from sqlalchemy import Column, Integer | from sqlalchemy import Column, Integer | ||||
from swh.lister.core.models import ModelBase | from swh.lister.core.models import ModelBase, IndexingModelBase | ||||
class BadSubclass1(ModelBase): | class BadSubclass1(ModelBase): | ||||
__abstract__ = True | __abstract__ = True | ||||
pass | pass | ||||
class BadSubclass2(ModelBase): | class BadSubclass2(ModelBase): | ||||
__abstract__ = True | __abstract__ = True | ||||
__tablename__ = 'foo' | __tablename__ = 'foo' | ||||
class BadSubclass3(BadSubclass2): | class BadSubclass3(BadSubclass2): | ||||
__abstract__ = True | __abstract__ = True | ||||
pass | pass | ||||
class GoodSubclass(BadSubclass2): | class GoodSubclass(BadSubclass2): | ||||
uid = Column(Integer, primary_key=True) | uid = Column(Integer, primary_key=True) | ||||
indexable = Column(Integer, index=True) | indexable = Column(Integer, index=True) | ||||
class IndexingBadSubclass(IndexingModelBase): | |||||
__abstract__ = True | |||||
pass | |||||
class IndexingBadSubclass2(IndexingModelBase): | |||||
__abstract__ = True | |||||
__tablename__ = 'foo' | |||||
class IndexingBadSubclass3(IndexingBadSubclass2): | |||||
__abstract__ = True | |||||
pass | |||||
class IndexingGoodSubclass(IndexingModelBase): | |||||
uid = Column(Integer, primary_key=True) | |||||
indexable = Column(Integer, index=True) | |||||
__tablename__ = 'bar' | |||||
class TestModel(unittest.TestCase): | class TestModel(unittest.TestCase): | ||||
@istest | @istest | ||||
def test_model_instancing(self): | def test_model_instancing(self): | ||||
with self.assertRaises(TypeError): | with self.assertRaises(TypeError): | ||||
ModelBase() | ModelBase() | ||||
with self.assertRaises(TypeError): | with self.assertRaises(TypeError): | ||||
BadSubclass1() | BadSubclass1() | ||||
with self.assertRaises(TypeError): | with self.assertRaises(TypeError): | ||||
BadSubclass2() | BadSubclass2() | ||||
with self.assertRaises(TypeError): | with self.assertRaises(TypeError): | ||||
BadSubclass3() | BadSubclass3() | ||||
self.assertIsInstance(GoodSubclass(), GoodSubclass) | self.assertIsInstance(GoodSubclass(), GoodSubclass) | ||||
gsc = GoodSubclass(uid='uid', indexable='indexable') | gsc = GoodSubclass(uid='uid') | ||||
self.assertEqual(gsc.__tablename__, 'foo') | self.assertEqual(gsc.__tablename__, 'foo') | ||||
self.assertEqual(gsc.uid, 'uid') | self.assertEqual(gsc.uid, 'uid') | ||||
@istest | |||||
def test_indexing_model_instancing(self): | |||||
with self.assertRaises(TypeError): | |||||
IndexingModelBase() | |||||
with self.assertRaises(TypeError): | |||||
IndexingBadSubclass() | |||||
with self.assertRaises(TypeError): | |||||
IndexingBadSubclass2() | |||||
with self.assertRaises(TypeError): | |||||
IndexingBadSubclass3() | |||||
self.assertIsInstance(IndexingGoodSubclass(), IndexingGoodSubclass) | |||||
gsc = IndexingGoodSubclass(uid='uid', indexable='indexable') | |||||
self.assertEqual(gsc.__tablename__, 'bar') | |||||
self.assertEqual(gsc.uid, 'uid') | |||||
self.assertEqual(gsc.indexable, 'indexable') | self.assertEqual(gsc.indexable, 'indexable') |