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