| 1 | """ |
|---|
| 2 | Test spreading entities accross several modules |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import sys |
|---|
| 6 | |
|---|
| 7 | from elixir import * |
|---|
| 8 | |
|---|
| 9 | def setup(): |
|---|
| 10 | metadata.bind = 'sqlite://' |
|---|
| 11 | sys.modules.pop('tests.a', None) |
|---|
| 12 | sys.modules.pop('tests.b', None) |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | class TestPackages(object): |
|---|
| 16 | def teardown(self): |
|---|
| 17 | # This is an ugly workaround because when nosetest is run globally (ie |
|---|
| 18 | # either on the tests directory or in the "trunk" directory, it imports |
|---|
| 19 | # all modules, including a and b. Then when any other test calls |
|---|
| 20 | # setup_all(), A and B are also setup, but then the other test also |
|---|
| 21 | # calls cleanup_all(), so when we get here, A and B are already dead |
|---|
| 22 | # and reimporting their modules does nothing because they were already |
|---|
| 23 | # imported. Additionally, if I remove the __init__.py file from the |
|---|
| 24 | # tests/ directory, nosetests doesn't import all modules, but does |
|---|
| 25 | # import a and b (probably because they are not prefixed with "test_"). |
|---|
| 26 | # the result is almost the same, even if less messy. |
|---|
| 27 | sys.modules.pop('tests.a', None) |
|---|
| 28 | sys.modules.pop('tests.b', None) |
|---|
| 29 | cleanup_all(True) |
|---|
| 30 | |
|---|
| 31 | def test_full_entity_path(self): |
|---|
| 32 | from tests.a import A |
|---|
| 33 | from tests.b import B |
|---|
| 34 | |
|---|
| 35 | setup_all(True) |
|---|
| 36 | |
|---|
| 37 | b1 = B(name='b1', many_a=[A(name='a1')]) |
|---|
| 38 | |
|---|
| 39 | session.commit() |
|---|
| 40 | session.clear() |
|---|
| 41 | |
|---|
| 42 | a = A.query.one() |
|---|
| 43 | |
|---|
| 44 | assert a in a.b.many_a |
|---|
| 45 | |
|---|
| 46 | def test_ref_to_imported_entity_using_class(self): |
|---|
| 47 | from tests.a import A |
|---|
| 48 | from tests.b import B |
|---|
| 49 | |
|---|
| 50 | class C(Entity): |
|---|
| 51 | name = Field(String(30)) |
|---|
| 52 | a = ManyToOne(A) |
|---|
| 53 | |
|---|
| 54 | setup_all(True) |
|---|
| 55 | |
|---|
| 56 | assert 'a_id' in C.table.columns |
|---|
| 57 | |
|---|
| 58 | def test_ref_to_imported_entity_using_name(self): |
|---|
| 59 | from tests.a import A |
|---|
| 60 | from tests.b import B |
|---|
| 61 | |
|---|
| 62 | class C(Entity): |
|---|
| 63 | name = Field(String(30)) |
|---|
| 64 | a = ManyToOne('A') |
|---|
| 65 | |
|---|
| 66 | setup_all(True) |
|---|
| 67 | |
|---|
| 68 | assert 'a_id' in C.table.columns |
|---|