Ejemplo n.º 1
0
class TestHeterogeneous(unittest.TestCase):
    """Test class testing ``view`` over different access points."""
    def test_view(self):
        """Test the ``view`` method across access points."""
        aliases = {
            "alch_id": "id", "alch_label": "label", "mem_id": "memory.id",
            "mem_label": "memory.label"}
        items = list(self.site.view("alchemy", aliases, {}))
        eq_(len(items), 1)
        item = items[0]
        eq_(item["alch_id"], 1)
        eq_(item["alch_label"], "alchemytest")
        eq_(item["mem_id"], 1)
        eq_(item["mem_label"], "memorytest")

    # camelCase function names come from unittest
    # pylint: disable=C0103
    def setUp(self):
        self.site = Site()
        self.alchemy_ap = make_alchemy_ap()
        self.site.register("alchemy", self.alchemy_ap )
        self.site.register("memory", make_memory_ap())
        self.memitem = self.site.create(
            "memory", {"id": 1, "label": "memorytest"})
        self.memitem.save()
        self.dbitem = self.site.create(
            "alchemy",
            {"id": 1, "label": "alchemytest", "memory": self.memitem})
        self.dbitem.save()

    def tearDown(self):
        self.alchemy_ap._table.drop()
        Alchemy.__metadatas = {}
Ejemplo n.º 2
0
Archivo: common.py Proyecto: Kozea/Dyko
def make_site(access_point, fill=True):
    """Create a site from an ``access_point`` filled by ``fill`` values."""
    site = Site()
    site.register("things", access_point)

    if fill:
        fill_site(site)
    return site
Ejemplo n.º 3
0
class TestAlchemy(unittest.TestCase):
    """Class defining some simple tests on an Alchemy access point."""
    def test_search(self):
        """Test a simple search on the access point."""
        items = list(self.site.search("test"))
        eq_(len(items), 2)
        items = list(self.site.search("test", {"firstname": "John"}))
        eq_(len(items), 1)
        item = items[0]
        eq_(item["firstname"], "John")
        eq_(item["lastname"], "Doe")
        eq_(item["birthdate"], date(1950, 1, 1))

    def test_view(self):
        """Test a simple view on the access point."""
        items = list(
            self.site.view(
                "test", {"truc": "firstname", "name": "lastname"}, {}))
        eq_(len(items), 2)
        for item in items:
            assert "truc" in item.keys() and "name" in item.keys()
        items = list(
            self.site.view("test", {"truc": "firstname", "name": "lastname"},
                {"firstname": "John"}))
        eq_(len(items), 1)

    def test_update(self):
        """Assert that an item can be updated in the DB."""
        item = self.site.open(
            "test", {"firstname": "John", "lastname": "Doe"})
        item["birthdate"] = date(1951, 12, 12)
        item.save()
        item = self.site.open(
            "test", {"firstname": "John", "lastname": "Doe"})
        eq_(item["birthdate"],  date(1951, 12, 12))

    # camelCase function names come from unittest
    # pylint: disable=C0103
    def setUp(self):
        self.site = Site()
        self.access_point = make_table()
        self.site.register("test", self.access_point) 
        self.items = []
        item = self.site.create(
            "test", {"firstname": "John", "lastname": "Doe",
                     "birthdate": date(1950, 1, 1)})
        self.items.append(item)
        item.save()
        item = self.site.create(
            "test", {"firstname": "Jane", "lastname": "Doe",
                     "birthdate": date(1960, 2, 2)})
        self.items.append(item)
        item.save()

    def tearDown(self):
        for access_point in self.site.access_points.values(): 
            access_point._table.drop()
Ejemplo n.º 4
0
def make_site(first_ap, second_ap, fill=True):
    """Create a site from an ``access_point`` filled by ``fill`` values."""
    site = Site()
    site.register("first_ap", first_ap)
    site.register("second_ap", second_ap)

    if fill:
        fill_site(site)
    return site
Ejemplo n.º 5
0
def test_filesystem_unicode():
    """Assert that the filesystem access point can store unicode values"""
    with temporary_directory() as temp_dir:
        access_point = FileSystem(temp_dir, "(.*)", ["name"], "data")
        site = Site()
        site.register("tests", access_point)
        site.create("tests", {"name": to_unicode("Touché")}).save()
        items = list(site.search("tests"))
        eq_(len(items), 1)
        eq_(items[0]["name"], to_unicode("Touché"))
Ejemplo n.º 6
0
class TestAlchemy(unittest.TestCase):
    """Class defining some simple tests on an Alchemy access point."""
    def test_search(self):
        """Test a simple search on the access point."""
        items = list(self.site.search("test"))
        eq_(len(items), 2)
        items = list(self.site.search("test", {"id": 1}))
        eq_(len(items), 1)
        item = items[0]
        eq_(item["id"], 1)
        eq_(item["name"], "Test")

    def test_view(self):
        """Test a simple view on the access point."""
        items = list(
            self.site.view("test", {"truc": "id", "name": "name"}, {}))
        eq_(len(items), 2)
        for item in items:
            assert "truc" in item.keys() and "name" in item.keys()
        items = list(
            self.site.view("test", {"truc": "id", "name": "name"}, {"id": 1}))
        eq_(len(items), 1)

    def test_update(self):
        """Assert that an item can be updated in the DB."""
        item = self.site.open("test", {"id": 1})
        item["name"] = "updated"
        item.save()
        item = self.site.open("test", {"id": 1})
        eq_(item["name"], "updated")

    # camelCase function names come from unittest
    # pylint: disable=C0103
    def setUp(self):
        self.site = Site()
        self.site.register("test", make_ap())
        self.items = []
        item = self.site.create("test", {"id": 1, "name": "Test"})
        self.items.append(item)
        item.save()
        item = self.site.create("test", {"id": 2, "name": "Test2"})
        self.items.append(item)
        item.save()

    def tearDown(self):
        for item in self.items:
            item.delete()
        for access_point in self.site.access_points.values():
            access_point._table.drop()
        Alchemy.__metadatas = {}
Ejemplo n.º 7
0
def test_shared_structure():
    """Test item with properties sharing the same structure."""
    with TemporaryDirectory() as temp_dir:
        file_ap = make_file_ap(temp_dir)
        properties = [
            ("name" , XMLProperty(unicode, "//bar/name")),
            ("color", XMLProperty(unicode, "//bar/color"))]
        access_point = XML(file_ap, properties, "stream", "foo")
        site = Site()
        site.register("test", access_point)
        item = site.create("test", {"name": "hulk", "color": "green", "id": 1})
        item.save()
        item = site.open("test", {"id": 1})
        eq_(item["stream"].read().decode("utf-8"),
            "<foo><bar><name>hulk</name><color>green</color></bar></foo>")
Ejemplo n.º 8
0
def make_site():
    question_ap = Alchemy("sqlite:///", "question", {
        "id": AlchemyProperty(int),
        "label": AlchemyProperty(unicode),
        "question_group": AlchemyProperty(Item, relation="many-to-one",
            remote_property="id", remote_ap="question_group"),
        "answer": AlchemyProperty(Item, relation="one-to-many",
            remote_property="question", remote_ap="answer")},
        ["id"], True, engine_opts={'echo': True})
    answer_ap = Alchemy("sqlite:///", "answer", {
        "id": AlchemyProperty(int),
        "label": AlchemyProperty(unicode),
        "question": AlchemyProperty(Item, relation="many-to-one",
            remote_property="id", remote_ap="question")},
        ["id"], True, engine_opts={'echo': True})
    question_group_ap = Alchemy("sqlite:///", "questiongroup", {
        "id": AlchemyProperty(int),
        "label": AlchemyProperty(unicode),
        "question": AlchemyProperty(Item, relation="one-to-many",
            remote_property="question_group", remote_ap="question")},
        ["id"], True, engine_opts={'echo': True})
    site = Site()
    site.register("question", question_ap)
    site.register("question_group", question_group_ap)
    site.register("answer", answer_ap)
    return site
Ejemplo n.º 9
0
def make_view_site():
    """Initialize a site with 2 access points and populate it with test data."""
    site = Site()
    site.register("test_ap", make_first_ap())
    site.register("test_remote_ap", make_second_ap())
    my_item = site.create("test_ap", {"id": 3, "name": "truc"})
    my_second_item = site.create("test_ap", {"id": 10, "name": "truc"})
    my_item.save()
    my_second_item.save()
    remote = site.create("test_remote_ap", {
            "id": 4,
            "label": "remote_item",
            "remote": my_item})
    remote.save()
    remote2 = site.create("test_remote_ap", {
            "id": 8,
            "label": "remote_item2",
            "remote": my_second_item})
    remote2.save()
    my_item['manies'] = [remote, remote2]
    my_item.save()
    return site
Ejemplo n.º 10
0
def test_filesytem_init():
    """Assert that the filesystem access point can be properly initialized."""
    # Project root, contains kalamar dir
    root = os.path.dirname(os.path.dirname(kalamar.__file__))
    access_point = FileSystem(
        root, "(.*)/tests/access_point/test_(.*)\.py(.*)",
        ["package", ("module", FileSystemProperty(unicode)), "extension"])
    site = Site()
    site.register("tests", access_point)
    eq_(set(access_point.properties.keys()),
        set(["package", "module", "extension", "content"]))
    eq_(set((prop.name for prop in access_point.identity_properties)),
        set(["package", "module", "extension"]))
    
    properties = {"package": "kalamar", "module": "filesystem", "extension": ""}
    filename = __file__[:-1] if __file__.endswith(".pyc") else __file__
    eq_(access_point._item_filename(properties), filename)
    
    items_file = site.open("tests", properties)["content"]
    eq_(items_file.name, filename)
    # This test tests its own presence!
    assert "RANDOM STRING A6buCMTbAdCV98j00vK455UIAPC" in str(items_file.read())
Ejemplo n.º 11
0
class TestAlchemy(unittest.TestCase):
    """Class defining some simple ``view`` tests on an Alchemy access point."""
    def test_search(self):
        """Test a simple search on the access point."""
        items = list(self.site.search("root"))
        eq_(len(items), 2)
        items = list(self.site.search("root", {"id": 1}))
        eq_(len(items), 1)
        item = items[0]
        eq_(item["id"], 1)
        eq_(item["name"], "Test")

    def test_view(self):
        """Test a simple view on the access point."""
        mapping = {"truc": "id", "name": "name", "rootname": "root.name"}
        items = list(self.site.view("child", mapping, {}))
        eq_(len(items), 2)
        for item in items:
            assert all(a in item.keys() for a in ["truc", "name", "rootname"])
        items = list(self.site.view("child", mapping, {"root.id": 1}))
        eq_(len(items), 1)
        items = list(self.site.view("child", mapping, {"root.id": 2, "id": 2}))
        eq_(len(items), 1)
        items = list(self.site.view("child", mapping, {"root.id": 2, "id": 1}))
        eq_(len(items), 0)

    def test_star(self):
        """Test a star request on the access point."""
        mapping = {"root_": "root.*"}
        items = list(self.site.view("child", mapping, {}))
        eq_(len(items), 2)
        for item in items:
            assert all(attr in item.keys() for attr in ("root_name", "root_id"))

    # camelCase function names come from unittest
    # pylint: disable=C0103
    def setUp(self):
        id_property = AlchemyProperty(int, column_name="id")
        name = AlchemyProperty(unicode, column_name="name")
        aproot = Alchemy(
            URL, "root", {"id": id_property, "name": name}, ["id"], True)

        idchild_property = AlchemyProperty(int, column_name="id")
        namechild = AlchemyProperty(unicode, column_name="name")
        root_prop = AlchemyProperty(
            Item, column_name="root", relation="many-to-one", remote_ap="root",
            remote_property="id")
        apchild = Alchemy(
            URL, "child",
            {"id": idchild_property, "name": namechild, "root": root_prop},
            ["id"], True)

        self.site = Site()
        self.site.register("root", aproot)
        self.site.register("child", apchild)
        
        self.items = []
        item = self.site.create("root", {"id": 1, "name": "Test"})
        self.items.append(item)
        item.save()
        itemchild = self.site.create(
            "child", {"id": 1, "name": "TestChild", "root": item})
        itemchild.save()
        item = self.site.create("root", {"id": 2, "name": "Test2"})
        self.items.append(item)
        item.save()
        itemchild = self.site.create(
            "child", {"id": 2, "name": "TestChild2", "root": item})
        itemchild.save()
 
        self.items.append(itemchild)

    def tearDown(self):
        for access_point in self.site.access_points.values():
            access_point._table.drop()
        Alchemy.__metadatas = {}
Ejemplo n.º 12
0
def test_ap_name_conflict():
    """Registering two APs with the same name raises an exception."""
    site = Site()
    site.register("things", DummyAccessPoint())
    site.register("things", DummyAccessPoint())
Ejemplo n.º 13
0
def test_double_register():
    """Registering the same AP twice raises an exception."""
    site = Site()
    access_point = DummyAccessPoint()
    site.register("things", access_point)
    site.register("stuff", access_point)
Ejemplo n.º 14
0
def test_simple_setup():
    """Setup a Site with a single access point, no exception raised."""
    site = Site()
    access_point = DummyAccessPoint()
    site.register("things", access_point)
    eq_(site.access_points, {"things": access_point})
Ejemplo n.º 15
0
class TestView(unittest.TestCase):
    """Test class testing simple ``view`` requests."""

    def test_lazy(self):
        """Assert that the lazy props (one to many relationships) are set up."""
        root = self.site.open("root", {"id": 0})
        eq_(len(list(root["children"])), 2)

    def test_bad_request(self):
        """Assert that common programmer error raises a BadQueryException."""
        try:
            list(self.site.view("root", {"leaf_label": "children.grou"}, {}))
        except BadQueryException as detail:
            assert isinstance(detail.query, QuerySelect)
        else:
            assert False, "Expected BadQueryException."

        try:
            list(self.site.view("root", {"leaf_label": "children.label"}, {"children.grou": 4}))
        except BadQueryException as detail:
            assert isinstance(detail.query, (QuerySelect, QueryFilter))
        else:
            assert False, "Expected BadQueryException."

        try:
            list(self.site.view("root", {"leaf_label": "children.label"}, {"children.children.id": "abc"}))
        except BadQueryException as detail:
            assert isinstance(detail.query, QueryFilter)
        else:
            assert False, "Expected BadQueryException."

        try:
            list(self.site.view("root", {"leaf_label": "childr.label"}))
        except BadQueryException as detail:
            assert isinstance(detail.query, QuerySelect)
        else:
            assert False, "Expected BadQueryException"

    def test_first_level(self):
        """Assert that the tree can be viewed to the first level."""
        aliases = {"leaf_label": "children.label", "root_label": "label"}
        items = list(self.site.view("root", aliases, {}))
        eq_(len(items), 2)

    def test_star_request(self):
        """Assert that an ``*`` request correctly selects the properties"""
        aliases = {"": "*", "children_": "children.*"}
        items = list(self.site.view("root", aliases, {}))
        eq_(len(items), 2)
        item = items[0]
        assert all(alias in item for alias in ("label", "id", "children_label", "children_id"))

    def test_star_request_with_cond(self):
        """Assert that ``*``-request-generated alias can be tested in conds."""
        aliases = {"": "*", "children_": "children.*"}
        request = {"label": "root", "children_id": 1}
        items = list(self.site.view("root", aliases, request))
        eq_(len(items), 1)
        item = items[0]
        assert all(alias in item for alias in ("label", "id", "children_label", "children_id"))

    def test_first_level_with_cond(self):
        """Assert that remote properties can be used in conditions."""
        aliases = {"leaf_label": "children.label", "root_label": "label"}
        conditions = {"children.label": "1"}
        items = list(self.site.view("root", aliases, conditions))
        eq_(len(items), 1)

    def test_leaf_nodes(self):
        """Assert that query involving 2+ chained APs can be executed."""
        aliases = {"leaf_label": "children.children.label", "root_label": "label", "middle_label": "children.label"}
        condition = Condition("children.children.label", "=", "2.2")
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 1)
        assert all(item["leaf_label"] == "2.2" for item in items)
        condition = Condition("children.label", "=", "2")
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 2)
        assert all(item["middle_label"] == "2" for item in items)
        condition = {"children.label": "2", "children.children.label": "1.1"}
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 0)

    def test_and_condition(self):
        """Assert that And conditions can be tested accross multiple APs."""
        aliases = {"root_label": "label", "middle_label": "children.label"}
        condition = And(Condition("children.label", "=", "1"), Condition("children.children.label", "=", "1.1"))
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 1)

    def test_or_condition(self):
        """Assert that Or conditions can be tested accross multiple APs."""
        aliases = {"root_label": "label", "middle_label": "children.label"}
        condition = Or(Condition("children.label", "=", "1"), Condition("children.children.label", "=", "2.1"))
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 3)

    def test_not_condition(self):
        """Assert that Not conditions can be tested accross multiple APs."""
        aliases = {"root_label": "label", "middle_label": "children.label"}
        condition = Not(Condition("children.children.label", "=", "1.1"))
        items = list(self.site.view("root", aliases, condition))
        eq_(len(items), 3)

    def test_empty_view(self):
        """Assert that ``view`` with props matching nothing return nothing."""
        aliases = {"label": "label"}
        items = list(self.site.view("level2", aliases, {"label": "3"}))
        eq_(len(items), 0)

    def test_parent_property(self):
        """Assert that many-to-one properties works across multiple APs."""
        aliases = {"label": "label", "parent": "parent"}
        items = list(self.site.view("level2", aliases, {"parent.label": "1"}))
        eq_(len(items), 2)
        for item in items:
            eq_(item["parent"].access_point.name, "level1")

    def test_deep_parent_property(self):
        """Assert that deep many-to-one properties works across multiple APs."""
        aliases = {"label": "label", "parent": "parent"}
        items = list(self.site.view("level2", aliases, {"parent.parent.label": "root"}))
        eq_(len(items), 4)
        for item in items:
            eq_(item["parent"].access_point.name, "level1")
            eq_(item["parent"]["parent"].access_point.name, "root")

    def test_children_property(self):
        """Assert that one-to-many properties works across multiple APs."""
        aliases = {"label": "label", "children": "children"}
        items = list(self.site.view("level1", aliases, {"children.label": "1.1"}))
        eq_(len(items), 1)
        item = items[0]
        eq_(len(item["children"]), 2)
        for child in item["children"]:
            eq_(child.access_point.name, "level2")

    def test_deep_children_property(self):
        """Assert that deep one-to-many properties works across multiple APs."""
        aliases = {"label": "label", "children": "children"}
        items = list(self.site.view("root", aliases, {"children.children.label": "1.1"}))
        eq_(len(items), 1)
        item = items[0]
        eq_(len(item["children"]), 2)
        for child in item["children"]:
            eq_(child.access_point.name, "level1")
            grandchildren = child["children"]
            eq_(len(grandchildren), 2)
            for grandchild in grandchildren:
                eq_(grandchild.access_point.name, "level2")

    # camelCase function names come from unittest
    # pylint: disable=C0103
    def setUp(self):
        """Create a test site with 3 access points forming a tree structure.

        The structure looks like this::

            root -> level1 -> level2

        The structure is filled with test data::

            root -> 1 -> 1.1
                      -> 1.2
                 -> 2 -> 2.1
                      -> 2.2

        """
        child_property = Property(tuple, relation="one-to-many", remote_ap="level1", remote_property="parent")
        root_ap = Memory({"id": Property(int), "label": Property(unicode), "children": child_property}, ("id",))

        parent_property = Property(Item, relation="many-to-one", remote_ap="root")
        child_property = Property(tuple, relation="one-to-many", remote_ap="level2", remote_property="parent")
        level1_ap = Memory(
            {"id": Property(int), "label": Property(unicode), "parent": parent_property, "children": child_property},
            ("id",),
        )

        parent_property = Property(Item, relation="many-to-one", remote_ap="level1")
        level2_ap = Memory({"id": Property(int), "label": Property(unicode), "parent": parent_property}, ("id",))

        self.site = Site()
        self.site.register("root", root_ap)
        self.site.register("level1", level1_ap)
        self.site.register("level2", level2_ap)

        rootitem = self.site.create("root", {"label": "root", "id": 0})
        rootitem.save()

        item1 = self.site.create("level1", {"label": "1", "id": 1, "parent": rootitem})
        item1.save()
        item2 = self.site.create("level1", {"label": "2", "id": 2, "parent": rootitem})
        item2.save()

        item11 = self.site.create("level2", {"label": "1.1", "id": 1, "parent": item1})
        item11.save()
        item12 = self.site.create("level2", {"label": "1.2", "id": 2, "parent": item1})
        item12.save()

        item21 = self.site.create("level2", {"label": "2.1", "id": 3, "parent": item2})
        item21.save()
        item22 = self.site.create("level2", {"label": "2.2", "id": 4, "parent": item2})
        item22.save()

        rootitem["children"] = [item1, item2]
        rootitem.save()

        item1["children"] = [item11, item12]
        item1.save()
        item2["children"] = [item21, item22]
        item2.save()