class BaseLocalRegistry(object): """ Make sure registries are local to the test """ type = None types = () def setup(self): """ override the global registry """ self.registry = TypeRegistry() type_registry.set(self.registry) if self.type: self.registry.register(self.type) for type in self.types: self.registry.register(type)
def localtyperegistry(request): registry = TypeRegistry() type_registry.set(registry) if hasattr(request.cls, 'type') and request.cls.type: registry.register(request.cls.type) if hasattr(request.cls, 'other') and request.cls.other: registry.register(request.cls.other) for type in getattr(request.cls, 'types', []): registry.register(type) for type in getattr(request.cls, 'extra_types', []): registry.register(type)
class TestImageCreateUpdate(object): def setup(self): self.registry = TypeRegistry() self.old_registry = type_registry.wrapped type_registry.set(self.registry) self.registry.register(TestImageType) def teardown(self): type_registry.set(self.old_registry) def test_create_image(self, client): request = superuser_request("/create", method="POST", title="Test", slug="test", language="en", storage=filedata) root = Node.root() handler = MainHandler(request=request, post=True, instance=dict(instance=root)) pytest.raises(Redirect, handler.create, type=TestImage.get_name()) node = Node.get("/test") filedata.seek(0) assert node.content().storage.read() == filedata.read() def test_update_image(self, client): root = Node.root() node = root.add("test") TestImage(node=node, title="image", storage=filedata).save() request = superuser_request("/test/edit", method="POST", title="Test", slug="", language="en", storage=filedata2) handler = MainHandler(request=request, post=True, instance=node) pytest.raises(Redirect, handler.update) node = Node.get("/test") filedata2.seek(0) assert node.content().storage.read() == filedata2.read()
class BaseTestSearch(object): type = None other = Type1Type def construct_type(self, **kw): """ allow construction to be overridden for more complex types """ return self.type.model(**kw).save() def construct_other(self, **kw): """ 'other' is a type unequal to 'type', to test against """ return self.other.model(**kw).save() def setup(self): self.registry = TypeRegistry() type_registry.set(self.registry) self.registry.register(self.type) self.registry.register(self.other) self.sqs = SearchQuerySet() @pytest.mark.xfail def test_trivial_match(self, client): t = self.construct_type(title="hi") res = self.sqs.auto_query("hi") assert res[0].object == t def test_trivial_nonmatch(self, client): t = self.construct_type(title="hi") res = self.sqs.auto_query("frop") assert not res ## randomly failing with tox @pytest.mark.xfail def test_find_metatype(self, client): """ should become a spoke-related match """ t = self.construct_type(title="hi") o = self.construct_other(title="hi") res = self.sqs.filter(content="hi", type=self.type.model.classname) assert len(res) == 1 ## not 2! assert res[0].object == t ## randomly failing with tox def disabled_test_find_metaother(self, client): """ should become a spoke-related match """ t = self.construct_type(title="hi") o = self.construct_other(title="hi") res = self.sqs.filter(content="hi", type=self.other.model.classname) assert len(res) == 1 ## not 2! assert res[0].object == o @pytest.mark.xfail def test_workflow_state(self, client): t1 = self.construct_type(title="hi", state="private") t2 = self.construct_type(title="hi", state="published") res = self.sqs.filter(state="published") assert len(res) == 1 assert res[0].object == t2 def xtest_path(self, client): """ One of the few node attributes to be indexed """ ## Test won't work on simple backend root = Node.root() c1 = root.add("child1") c2 = c1.add("child2") t1 = self.construct_type(title="hi", node=c2) t2 = self.construct_type(title="hi", node=c1) res = self.sqs.filter(content="hi", path="/child1/child2") assert len(res) == 1 assert res[0].object == t1 def xtest_slug(self, client): ## Test won't work on simple backend root = Node.root() c1 = root.add("child1") c2 = c1.add("child2") t1 = self.construct_type(title="hi", node=c2) t2 = self.construct_type(title="hi", node=c1) res = self.sqs.filter(content="hi", slug="child2") assert len(res) == 1 assert res[0].object == t1 @pytest.mark.xfail def test_searchable(self, client): t1 = self.construct_type(title="hello world") t2 = self.construct_type(description="hello world") res = self.sqs.filter(content="hello") assert len(res) == 2 assert res[0].object in (t1, t2) assert res[1].object in (t1, t2) @pytest.mark.xfail def test_indexfactory(self, client): root = Node.root() c1 = root.add("child1") c2 = c1.add("child2") t1 = self.construct_type(title="hi", state="private", node=c2) idf = indexfactory(self.type) idx = idf(self.type.model) data = idx.prepare(t1) assert data['title'] == 'hi' assert data['state'] == 'private' assert data['path'] == c2.get_absolute_url()
class TestImplicitAddition(object): """ Test implicit/explicit addition of children """ def setup(self): """ local registry, install it globally """ self.registry = TypeRegistry() type_registry.set(self.registry) def test_explicit(self, client): """ Simple case, no restrictions """ class T1(ModellessSpoke): implicit_add = True ## default class T2(ModellessSpoke): children = None self.registry.register(T1) self.registry.register(T2) assert T1 in T2.addable_children() def test_non_implicit(self, client): """ T1 cannot be added explicitly """ class T1(ModellessSpoke): implicit_add = False class T2(ModellessSpoke): children = None self.registry.register(T1) self.registry.register(T2) assert T1 not in T2.addable_children() def test_non_implicit_but_children(self, client): """ T1 cannot be added explicitly but is in T2's children """ class T1(ModellessSpoke): implicit_add = False class T2(ModellessSpoke): children = (T1, ) self.registry.register(T1) self.registry.register(T2) assert T1 in T2.addable_children() def test_non_implicit_but_exp_children(self, client): """ T1 cannot be added explicitly but is in T2's explicit children """ class T1(ModellessSpoke): implicit_add = False class T2(ModellessSpoke): explicit_children = (T1, ) self.registry.register(T1) self.registry.register(T2) assert T1 in T2.addable_children()