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