|
1 | | -from typing import * |
| 1 | +import json |
| 2 | +import pytest |
| 3 | +import os |
| 4 | +from numpy.testing import assert_approx_equal |
2 | 5 | from objectbox import * |
3 | 6 | from objectbox.model import * |
4 | 7 | from objectbox.model.entity import _Entity |
5 | 8 | from objectbox.model.idsync import sync_model |
6 | 9 | from objectbox.c import CoreException |
7 | | -import json |
8 | | -from pprint import pprint |
9 | | -import os |
10 | 10 | from os import path |
11 | | -import tests.model |
12 | | -import pytest |
| 11 | + |
| 12 | +from tests.common import remove_json_model_file |
| 13 | + |
13 | 14 |
|
14 | 15 | class _TestEnv: |
15 | 16 | """ |
@@ -539,3 +540,56 @@ class EntityA: |
539 | 540 | with pytest.raises(ValueError) as e: |
540 | 541 | env.sync(model) |
541 | 542 | assert f"User supplied UID {entitya_uid} is already assigned elsewhere" == str(e.value) |
| 543 | + |
| 544 | + |
| 545 | +def test_models_named(env): |
| 546 | + @Entity(model="modelA") |
| 547 | + class EntityA: |
| 548 | + id = Id |
| 549 | + text_a = String |
| 550 | + |
| 551 | + @Entity(model="modelB") |
| 552 | + class EntityB: |
| 553 | + id = Id |
| 554 | + int_b = Int64 |
| 555 | + |
| 556 | + @Entity(model="modelB") |
| 557 | + class EntityB2: |
| 558 | + id = Id() |
| 559 | + float_b = Float64 |
| 560 | + |
| 561 | + Store.remove_db_files("test-db-model-a") |
| 562 | + Store.remove_db_files("test-db-model-b") |
| 563 | + remove_json_model_file() |
| 564 | + store_a = Store(model="modelA", directory="test-db-model-a") |
| 565 | + remove_json_model_file() |
| 566 | + store_b = Store(model="modelB", directory="test-db-model-b") |
| 567 | + |
| 568 | + box_a = store_a.box(EntityA) |
| 569 | + id = box_a.put(EntityA(text_a="ah")) |
| 570 | + assert id != 0 |
| 571 | + assert box_a.get(id).text_a == "ah" |
| 572 | + |
| 573 | + # TODO to make this work we Store/Box to check if the type is actually registered. |
| 574 | + # This might require to store the (Python) model in the Store. |
| 575 | + # with pytest.raises(ValueError): |
| 576 | + # store_a.box(EntityB) |
| 577 | + |
| 578 | + # TODO XXX this should never fail, but is flaky |
| 579 | + #with pytest.raises(CoreException): |
| 580 | + # store_a.box(EntityB2) |
| 581 | + |
| 582 | + box_b = store_b.box(EntityB) |
| 583 | + id = box_b.put(EntityB(int_b=42)) |
| 584 | + assert id != 0 |
| 585 | + assert box_b.get(id).int_b == 42 |
| 586 | + |
| 587 | + box_b2 = store_b.box(EntityB2) |
| 588 | + id = box_b2.put(EntityB2(float_b=3.141)) |
| 589 | + assert id != 0 |
| 590 | + assert_approx_equal(box_b2.get(id).float_b, 3.141) |
| 591 | + |
| 592 | + # TODO to make this work we Store/Box to check if the type is actually registered. |
| 593 | + # This might require to store the (Python) model in the Store. |
| 594 | + # with pytest.raises(ValueError): |
| 595 | + # store_b.box(EntityA) |
0 commit comments