Exemplo n.º 1
0
        def test_that_class_reference_is_resolved_on_domain_activation(self):
            domain = Domain("Inline Domain")

            class Post(BaseAggregate):
                content = Text(required=True)
                comments = HasMany("Comment")

            domain.register(Post)

            # Still a string
            assert isinstance(declared_fields(Post)["comments"].to_cls, str)

            class Comment(BaseEntity):
                content = Text()
                added_on = DateTime()

                post = Reference("Post")

                class Meta:
                    aggregate_cls = Post

            domain.register(Comment)

            with domain.domain_context():
                # Resolved references
                assert declared_fields(Post)["comments"].to_cls == Comment
                assert declared_fields(Comment)["post"].to_cls == Post

                assert len(domain._pending_class_resolutions) == 0
Exemplo n.º 2
0
        def test_that_domain_throws_exception_on_unknown_class_references_during_activation(
            self, ):
            domain = Domain("Inline Domain")

            class Post(BaseAggregate):
                content = Text(required=True)
                comments = HasMany("Comment")

            domain.register(Post)

            # Still a string
            assert isinstance(declared_fields(Post)["comments"].to_cls, str)

            class Comment(BaseEntity):
                content = Text()
                added_on = DateTime()

                post = Reference("Post")
                foo = Reference("Foo")

                class Meta:
                    aggregate_cls = Post

            domain.register(Comment)

            with pytest.raises(ConfigurationError) as exc:
                with domain.domain_context():
                    pass

            assert (exc.value.args[0]["element"] ==
                    "Element Foo not registered in domain Inline Domain")

            # Remove domain context manually, as we lost it when the exception was raised
            from protean.globals import _domain_context_stack

            _domain_context_stack.pop()