1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
"""
Test collections
"""
from sqlalchemy import Table
from elixir import *
import elixir
def setup():
metadata.bind = 'sqlite:///'
def teardown():
cleanup_all()
class TestCollections(object):
def teardown(self):
cleanup_all()
def test_no_collection(self):
class Person(Entity):
name = Field(String(30))
using_options(autosetup=False, tablename='person', collection=None)
# global collection should be empty
assert not elixir.entities
setup_entities([Person])
# check the table was correctly setup
assert isinstance(metadata.tables['person'], Table)
def test_several_collections(self):
collection1 = EntityCollection()
collection2 = EntityCollection()
class A(Entity):
name = Field(String(30))
using_options(collection=collection1, tablename='a')
class B(Entity):
name = Field(String(30))
using_options(collection=collection2, tablename='b')
# global collection should be empty
assert A not in elixir.entities
assert B not in elixir.entities
assert A in collection1
assert B in collection2
setup_entities(collection1)
setup_entities(collection2)
assert isinstance(metadata.tables['a'], Table)
assert isinstance(metadata.tables['b'], Table)
def test_getattr(self):
collection = EntityCollection()
class A(Entity):
name = Field(String(30))
using_options(collection=collection)
assert collection.A == A
def test_setup_after_cleanup(self):
class A(Entity):
name = Field(String(30))
using_options(autosetup=False, tablename='a')
setup_all()
assert isinstance(metadata.tables['a'], Table)
cleanup_all()
assert 'a' not in metadata.tables
# setup_all wouldn't work since the entities list is now empty
setup_entities([A])
assert isinstance(metadata.tables['a'], Table)
# cleanup manually
cleanup_entities([A])
# metadata is not in metadatas anymore (removed by cleanup_all) and not
# added back by setup_entities (maybe we should?)
metadata.clear()
|