예제 #1
0
파일: ClerkTest.py 프로젝트: souca/sixthdev
 def setUp(self):
     self.storage = MockStorage()
     schema = Schema({
         Node: "Node",
         Node.parent: "parentID",
         Record: "Record",
         Record.next: "nextID",
     })
     self.clerk = Clerk(self.storage, schema)
예제 #2
0
    def setUp(self):

        self.storage = MockStorage()
        self.clerk = Clerk(self.storage, Schema({User: "******"}))
        self.app = AdminApp(self.clerk, {})

        # set up some templates to play with:
        tpl = open("frm_test.zb", "w")
        tpl.write("ID is {:ID:}")
        tpl.close()

        tpl = open("lst_test.zb", "w")
        tpl.write("*# zebra to print a dot for each user:\n"
                  "* for each:\n"
                  "    {:x:}\n")
        tpl.close()
예제 #3
0
    def test_onStore(self):
        queue = []
        schema = Schema({
            Thing: "thing",
            OtherThing: "other",
        })
            
        clerk = CallbackClerk(MockStorage(), schema)
        clerk.onStore(Thing, queue.append)
        
        clerk.store(Thing(x="a"))
        clerk.store(Thing(x="b"))
        clerk.store(OtherThing(x="not me"))


        queue2 = []
        clerk.onStore(Thing, queue2.append)
        clerk.store(Thing(x="c"))

        # "c" should wind up in both:
        assert len(queue) == 3
        assert "".join([t.x for t in queue]) == "abc"

        assert len(queue2) == 1
        assert queue2[0].x=="c"
예제 #4
0
    def check_inject(self):

        ms = MockStorage()
        ms.store("Package")
        ms.store("Content", data="I'm content", boxID=1)
        ms.store("Content", data="I'm mal content", boxID=1)

        schema = Schema({Content: "content", Content.box: "boxID", Package: "package"})

        clerk = Clerk(ms, schema)

        pak = Package()
        pak.refs << Content(data="I'm content", box=pak)
        pak.refs << Content(data="I'm malcontent", box=pak)
        pak = clerk.store(pak)

        # @TODO: should be able to add to the index without
        # triggering load (for performance reasons)
        # -- so long as any other use DOES trigger load --

        clerk.cache.clear()
        pak = clerk.fetch(Package, ID=1)

        # asking for .refs will trigger the load:
        assert len(pak.private.refs) == 0, pak.private.refs
        assert len(pak.refs) == 2

        # make sure it works with << on a fresh load too:
        newClerk = Clerk(clerk.storage, clerk.schema)
        pak = newClerk.fetch(Package, ID=1)
        assert len(pak.private.refs) == 0
        pak.refs << Content(data="I'm malcontent", box=pak)
        assert len(pak.refs) == 3
예제 #5
0
 def setUp(self):
     self.storage = MockStorage()
     schema = Schema({
         Node: "Node",
         Node.parent: "parentID",
         Record: "Record",
         Record.next: "nextID",
     })
     self.clerk = Clerk(self.storage, schema)
예제 #6
0
파일: sixthday.py 프로젝트: sabren/workshop
    def setUp(self):

        self.storage = MockStorage()
        self.clerk = Clerk(self.storage, Schema({
            User: "******"
        }))
        self.app = AdminApp(self.clerk, {})
        
        # set up some templates to play with:
        tpl = open("frm_test.zb", "w")
        tpl.write("ID is {:ID:}")
        tpl.close()

        tpl = open("lst_test.zb", "w")
        tpl.write(
            "*# zebra to print a dot for each user:\n"
            "* for each:\n" 
            "    {:x:}\n")
        tpl.close()
예제 #7
0
class DummyDiameterProtocol(BaseDiameterProtocol):
    """Diameter dummy protocol. For test purposes only."""

    storage = MockStorage()

    @inlineCallbacks
    def create_response(self, command: str, message: Message) -> Deferred:
        """
        Create response from request message.
        Building response will may need for doing request on data storage.
        So, it may be done in a way something like this:
            res = yield self._storage_method(cmd)()
        """
        yield

    @inlineCallbacks
    def create_request(self, command: str, message: Message) -> Deferred:
        """
        Create request message.
        Building request will may need for doing request on data storage.
        So, it may be done in a way something like this:
            res = yield self._storage_method(cmd)()
        """
        yield

    def on_request(self, command: str, message: Message) -> None:
        """
        Message handler for all request commands except those that
        have personal handler.

        Personal request message handler name format:
            Capabilities-Exchange -> on_capabilities_exchange
            Registration-Termination -> on_registration_termination
            ...
        """
        logger.debug("Command={command}, Message={message}",
                     command=command,
                     message=message)

    def on_error(self) -> None:
        """Errors handler."""
        logger.failure("Something went wrong")
예제 #8
0
    def check_inject(self):

        ms = MockStorage()
        ms.store("Package")
        ms.store("Content", data="I'm content", boxID=1)
        ms.store("Content", data="I'm mal content", boxID=1)

        schema = Schema({
            Content: "content",
            Content.box: "boxID",
            Package: "package",
        })

        clerk = Clerk(ms, schema)

        pak = Package()
        pak.refs << Content(data="I'm content", box=pak)
        pak.refs << Content(data="I'm malcontent", box=pak)
        pak = clerk.store(pak)

        # @TODO: should be able to add to the index without
        # triggering load (for performance reasons)
        # -- so long as any other use DOES trigger load --


        clerk.cache.clear()
        pak = clerk.fetch(Package, ID=1)
        
        # asking for .refs will trigger the load:
        assert len(pak.private.refs) == 0, pak.private.refs
        assert len(pak.refs) == 2

        # make sure it works with << on a fresh load too:
        newClerk = Clerk(clerk.storage, clerk.schema)
        pak = newClerk.fetch(Package, ID=1)
        assert len(pak.private.refs) == 0
        pak.refs << Content(data="I'm malcontent",  box=pak)
        assert len(pak.refs) == 3
예제 #9
0
class MockStorageTest(unittest.TestCase):

    def setUp(self):
        self.s = MockStorage()

    def check_store_insert(self):
        row = self.s.store("test_person", name="fred")
        assert row == {"ID":1, "name":"fred"}

        row = self.s.store("test_person", name="wanda")
        assert row == {"ID":2, "name":"wanda"}

        assert self.wholedb()==[{"ID":1, "name":"fred"},
                                {"ID":2, "name":"wanda"}]

    def check_store_insertExtra(self):
        self.check_store_insert()
        self.s.store("test_person", name="rick")
        self.s.store("test_person", name="bob")
        self.s.store("test_person", name="jack")
        assert self.wholedb()==[{"ID":1, "name":"fred"},
                                {"ID":2, "name":"wanda"},
                                {"ID":3, "name":"rick"},
                                {"ID":4, "name":"bob"},
                                {"ID":5, "name":"jack"}]


    def check_oldmatch(self):
        self.check_store_insertExtra()
        match = self.s.match("test_person", where("ID")==2)
        assert match[0]["name"] == "wanda", "new style broke"
        match = self.s.match("test_person", ID=2)
        assert match[0]["name"] == "wanda", "old style broke"


    def check_querybuilder_matches(self):
        self.check_store_insertExtra()
        match = self.s.match("test_person", where("ID")==5 )
        assert match[0]['name'] == 'jack'

        match = self.s.match("test_person", ( ( where("name")=="fred" )
                                         |  ( where("name")=="bob" ) ), "name")
        self.assertEquals([u['name'] for u in match],
                          ['bob','fred'])
            
        
        
        match = self.s.match("test_person", ((where("ID") > 1)
                                             &(where("ID") <= 4))
                                           |(where("name").endswith('ck')),
                                         'name desc')
        self.assertEquals( [u['name'] for u in match],
                           ['wanda', 'rick', 'jack', 'bob'] )



    def check_querybuilder_sorting(self):
        self.check_store_insertExtra()
        assert [p['name'] for p in self.s.match("test_person", orderBy='name')] == ['bob', 'fred', 'jack', 'rick', 'wanda']

    def populate(self):
        self.check_store_insert()

    def wholedb(self):
        return self.s.match("test_person")

    def check_store_update(self):
        self.populate()
        row = self.s.fetch("test_person", 1)
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{"ID":1, "name":"frood"},
                                  {"ID":2, "name":"wanda"}]        

    def check_store_update_longs(self):
        # same as above but with lnogs
        self.populate()
        row = self.s.fetch("test_person", 1L)
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{"ID":1, "name":"frood"},
                                  {"ID":2, "name":"wanda"}]        

    def check_store_update_strings(self):
        # same as above but with lnogs
        self.populate()
        row = self.s.fetch("test_person", "1")
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{"ID":1, "name":"frood"},
                                  {"ID":2, "name":"wanda"}]        

    def check_match(self):
        assert self.wholedb() == []
        self.populate()
        results = self.s.match("test_person", where("ID") == 1)
        assert results == [{"ID":1, "name":"fred"}], str(results)

    def check_fetch(self):
        self.check_store_insert()
        wanda = self.s.fetch("test_person", 2)
        assert wanda["name"]=="wanda"

    def _check_delete(self, key_type):
        self.populate()
        self.s.delete("test_person", key_type(1))
        people = self.s.match("test_person")
        assert people == [{"ID":2, "name":"wanda"}]
        self.s.delete("test_person", key_type(2))
        people = self.s.match("test_person")
        assert people == []


    def check_delete_with_int_id(self):
        self._check_delete(int)

    def check_delete_with_long_id(self):
        self._check_delete(long)

    def check_delete_with_str_id(self):
        self._check_delete(str)        
예제 #10
0
파일: ClerkTest.py 프로젝트: souca/sixthdev
class ClerkTest(unittest.TestCase):
    def setUp(self):
        self.storage = MockStorage()
        schema = Schema({
            Node: "Node",
            Node.parent: "parentID",
            Record: "Record",
            Record.next: "nextID",
        })
        self.clerk = Clerk(self.storage, schema)

    def test_store(self):
        self.clerk.store(Record())
        actual = self.storage.match("Record")
        assert actual == [{"ID": 1, "val": "", "nextID": 0}], actual
        r = self.clerk.fetch(Record, 1)
        assert r.next is None

    def test_store_again(self):
        self.clerk.store(Record())
        r = self.clerk.fetch(Record, 1)
        r.val = "abc"
        self.clerk.store(r)

    def test_store_link(self):
        r = Record(val="a")
        r.next = Record(val="b")

        self.clerk.store(r)
        del r
        r = self.clerk.match(Record, val="a")[0]
        assert r.ID == 2, "didn't save links first!"
        assert r.next is not None, "didn't store the link"
        assert r.next.val == "b", "didn't store link correctly"

        r.next = None
        self.clerk.store(r)
        r = self.clerk.match(Record, val="a")[0]
        assert r.next is None, "didn't delete link!"

        r = Record(val="noNext")
        self.clerk.store(r)
        r = self.clerk.fetch(Record, val="noNext")
        assert r.next is None

    def test_store_memo(self):
        rb = self.clerk.store(Record(val="b"))
        ra = self.clerk.store(Record(val="a", next=rb))

        a, b = self.clerk.match(Record, orderBy="val")
        assert a is ra
        assert b is rb

    def test_store_linksets(self):
        n1 = Node(data="a")
        n1.kids << Node(data="aa")
        n1.kids << Node(data="ab")
        n1.kids[1].kids << Node(data="aba")
        self.clerk.store(n1)
        assert len(n1.kids) == 2, [(k.ID, k.data) for k in n1.kids]

        n = self.clerk.fetch(Node, data="a")
        assert len(
            n1.kids) == 2, "fetch corrupted kids: %s" % [(k.ID, k.data)
                                                         for k in n1.kids]

        assert n.ID == 1, "didn't save parent of linkset first!"
        assert len(
            n.kids) == 2, "didn't store the linkset: %s" % [(k.ID, k.data)
                                                            for k in n.kids]
        assert n.kids[0].data == "aa", "didn't store link correctly"
        assert n.kids[1].data == "ab", "didn't store link correctly"
        assert n.kids[1].kids[0].data == "aba", "didn't store link correctly"
        assert n.kids[0].parent is n
        assert n.kids[1].parent is n

        n.kids[1].parent = None
        n.kids.remove(n.kids[1])
        self.clerk.store(n)
        n = self.clerk.match(Node, data="a")[0]
        assert len(n.kids) == 1

    def test_fetch(self):
        self.clerk.store(Record(val="howdy"))

        # we can pass in an ID:
        obj = self.clerk.fetch(Record, 1)
        assert obj.val == "howdy"

        # or we can use keywords:
        obj = self.clerk.fetch(Record, val="howdy")
        assert obj.val == "howdy"

    def test_delete(self):
        self.test_fetch()
        self.clerk.delete(Record, 1)
        assert self.storage.match("Record") == []

    def test_link_injection(self):
        self.storage.store("Record", val="a", nextID=2)
        self.storage.store("Record", val="b", nextID=3)
        self.storage.store("Record", val="c", nextID=None)

        a = self.clerk.fetch(Record, 1)

        assert a.val == "a"
        assert a.next.val == "b"
        assert a.next.next.val == "c"
        assert a.next.next.next is None

    def test_linkset_injection(self):
        self.storage.store("Node", data="top", parentID=None)
        self.storage.store("Node", data="a", parentID=1)
        self.storage.store("Node", data="a.a", parentID=2)
        self.storage.store("Node", data="b", parentID=1)

        top = self.clerk.fetch(Node, 1)
        assert top.kids[0].data == "a"
        assert top.kids[1].data == "b"
        assert top.kids[1].kids == []
        assert top.kids[0].kids[0].data == "a.a"

    def test_fetch_from_wide_table(self):
        """
        Supose a strongbox has 1 slot, but the table has 2+ columns.
        We can't just jam those columns into the strongbox,
        because strongbox is *designed* to blow up if you try
        to add new attributes.

        But on the other hand, a DBA should be able to add columns
        to the databaes without breaking the code and causing
        AttributeErrors all over the place.

        Instead, Clerk should only use the columns that have
        matching attributes, and simply ignore the others.

        This sorta violates the concept of OnceAndOnlyOnce,
        because now the tables can be out of sync with the
        data model, but I think it's better than the alternative,
        and this is the sort of thing one could check with
        an automated tool.

        #@TODO: write tool to compare DB and object models :)
        """
        try:
            self.storage.store("Record", val="a", extra_column="EEK!")
            a = self.clerk.fetch(Record, 1)
            a.val = "aa"
            self.clerk.store(a)
        except AttributeError:
            self.fail("shouldn't die when columns outnumber attributes")

    def test_dirt(self):
        # dirty by default (already tested in strongbox)
        r = Record()
        assert r.private.isDirty

        # but not after a store:
        r = self.clerk.store(r)
        assert not r.private.isDirty

        # and not after a fetch:
        r = self.clerk.fetch(Record, ID=1)
        assert not r.private.isDirty

        # or a match:
        r = self.clerk.match(Record)[0]
        assert not r.private.isDirty

    def test_recursion(self):
        r = Record()
        r.next = Record()
        r.next.next = r
        assert r.private.isDirty
        assert r.next.private.isDirty
        r = self.clerk.store(r)
        assert r.ID == 2
        assert r.next.ID == 1

        r = self.clerk.fetch(Record, 2)
        assert not r.private.isDirty
        assert not r.next.private.isDirty

        ## and the same thing for linksets:
        n = Node()
        n.kids << Node()
        n.kids[0].kids << n
        assert n.private.isDirty
        assert n.kids[0].private.isDirty
        n = self.clerk.store(n)

    def test_identity(self):
        self.clerk.store(Record(val="one"))
        rec1a = self.clerk.fetch(Record, 1)
        rec1b = self.clerk.fetch(Record, 1)
        assert rec1a is rec1b

        n = Record()
        r = Record(next=n)
        assert self.clerk.store(r) is r
        assert self.clerk.cache[(Record, r.ID)] is r
        assert self.clerk.cache[(Record, n.ID)] is n
        assert self.clerk.cache[(Record, n.ID)] is r.next

    def test_stub(self):
        self.clerk.store(Record(val="a", next=Record(val="b")))
        self.clerk.cache.clear()
        recA = self.clerk.fetch(Record, val="a")
        recB = self.clerk.fetch(Record, val="b")
        assert recA.next.ID == recB.ID
        assert recA.next is recB

    def test_match(self):
        self.clerk.store(Record(val="one"))
        self.clerk.store(Record(val="two"))
        self.clerk.store(Record(val="two"))
        assert len(self.clerk.match(Record, val="zero")) == 0
        assert len(self.clerk.match(Record, val="one")) == 1
        assert len(self.clerk.match(Record, val="two")) == 2

    def test_matchOne(self):
        self.clerk.store(Record(val="one"))
        self.clerk.store(Record(val="two"))
        self.clerk.store(Record(val="two"))

        try:
            self.clerk.matchOne(Record, val="zero")
            self.fail("should have failed for not matching")
        except LookupError:
            pass

        assert isinstance(self.clerk.matchOne(Record, val="one"), Record)

        try:
            self.clerk.matchOne(Record, val="two")
            self.fail("should have failed for matching two")
        except LookupError:
            pass
예제 #11
0
 def setUp(self):
     raise 'skip'  # because this whole app sucks and ther'es a better one
     # i just want to go through and mine it for ideas later...
     self.storage = MockStorage()
     self.clerk = Clerk(self.storage)
예제 #12
0
class ClerkTest(unittest.TestCase):

    def setUp(self):
        self.storage = MockStorage()
        schema = Schema({
            Node: "Node",
            Node.parent: "parentID",
            Record: "Record",
            Record.next: "nextID",
        })
        self.clerk = Clerk(self.storage, schema)


    def test_store(self):
        self.clerk.store(Record())
        actual = self.storage.match("Record")
        assert actual == [{"ID":1, "val":"", "nextID":0}], actual
        r = self.clerk.fetch(Record, 1)
        assert r.next is None
        

    def test_store_again(self):
        self.clerk.store(Record())
        r = self.clerk.fetch(Record, 1)
        r.val = "abc"
        self.clerk.store(r)

    def test_store_link(self):
        r = Record(val="a")
        r.next = Record(val="b")

        self.clerk.store(r)
        del r
        r = self.clerk.match(Record, val="a")[0]
        assert r.ID == 2, "didn't save links first!"
        assert r.next is not None, "didn't store the link"
        assert r.next.val=="b", "didn't store link correctly"

        r.next = None
        self.clerk.store(r)
        r = self.clerk.match(Record, val="a")[0]
        assert r.next is None, "didn't delete link!"

        r = Record(val="noNext")
        self.clerk.store(r)
        r = self.clerk.fetch(Record, val="noNext")
        assert r.next is None


    def test_store_memo(self):
        rb = self.clerk.store(Record(val="b"))
        ra = self.clerk.store(Record(val="a", next=rb))

        a,b = self.clerk.match(Record, orderBy="val")
        assert a is ra
        assert b is rb


    def test_store_linksets(self):
        n1 = Node(data="a")
        n1.kids << Node(data="aa")
        n1.kids << Node(data="ab")
        n1.kids[1].kids << Node(data="aba")
        self.clerk.store(n1)
        assert len(n1.kids)== 2, [(k.ID, k.data) for k in n1.kids]        
        
        n = self.clerk.fetch(Node, data="a")
        assert len(n1.kids)== 2, "fetch corrupted kids: %s" % [(k.ID, k.data) for k in n1.kids]
        
        assert n.ID == 1, "didn't save parent of linkset first!"
        assert len(n.kids)== 2, "didn't store the linkset: %s" % [(k.ID, k.data) for k in n.kids]
        assert n.kids[0].data=="aa", "didn't store link correctly"
        assert n.kids[1].data=="ab", "didn't store link correctly"
        assert n.kids[1].kids[0].data=="aba", "didn't store link correctly"
        assert n.kids[0].parent is n
        assert n.kids[1].parent is n

        n.kids[1].parent=None
        n.kids.remove(n.kids[1])
        self.clerk.store(n)
        n = self.clerk.match(Node, data="a")[0]
        assert len(n.kids) == 1

        
        
    def test_fetch(self):
        self.clerk.store(Record(val="howdy"))

        # we can pass in an ID:
        obj = self.clerk.fetch(Record, 1)
        assert obj.val == "howdy"

        # or we can use keywords:
        obj = self.clerk.fetch(Record, val="howdy")
        assert obj.val == "howdy"


    def test_delete(self):
        self.test_fetch()
        self.clerk.delete(Record, 1)
        assert self.storage.match("Record") == []


    def test_link_injection(self):
        self.storage.store("Record", val="a", nextID=2)
        self.storage.store("Record", val="b", nextID=3)
        self.storage.store("Record", val="c", nextID=None)

        a = self.clerk.fetch(Record, 1)
        
        assert a.val == "a"
        assert a.next.val == "b"
        assert a.next.next.val == "c"
        assert a.next.next.next is None


    def test_linkset_injection(self):
        self.storage.store("Node", data="top", parentID=None)
        self.storage.store("Node", data="a",   parentID=1)
        self.storage.store("Node", data="a.a", parentID=2)
        self.storage.store("Node", data="b",   parentID=1)
        
        top = self.clerk.fetch(Node, 1)
        assert top.kids[0].data == "a"
        assert top.kids[1].data == "b"
        assert top.kids[1].kids == []
        assert top.kids[0].kids[0].data == "a.a"

        

    def test_fetch_from_wide_table(self):
        """
        Supose a strongbox has 1 slot, but the table has 2+ columns.
        We can't just jam those columns into the strongbox,
        because strongbox is *designed* to blow up if you try
        to add new attributes.

        But on the other hand, a DBA should be able to add columns
        to the databaes without breaking the code and causing
        AttributeErrors all over the place.

        Instead, Clerk should only use the columns that have
        matching attributes, and simply ignore the others.

        This sorta violates the concept of OnceAndOnlyOnce,
        because now the tables can be out of sync with the
        data model, but I think it's better than the alternative,
        and this is the sort of thing one could check with
        an automated tool.

        #@TODO: write tool to compare DB and object models :)
        """
        try:
            self.storage.store("Record", val="a", extra_column="EEK!")
            a = self.clerk.fetch(Record, 1)
            a.val="aa"
            self.clerk.store(a)
        except AttributeError:
            self.fail("shouldn't die when columns outnumber attributes")

    def test_dirt(self):
        # dirty by default (already tested in strongbox)
        r = Record()
        assert r.private.isDirty

        # but not after a store:
        r = self.clerk.store(r)
        assert not r.private.isDirty

        # and not after a fetch:
        r = self.clerk.fetch(Record, ID=1)
        assert not r.private.isDirty

        # or a match:
        r = self.clerk.match(Record)[0]
        assert not r.private.isDirty


    def test_recursion(self):
        r = Record()
        r.next = Record()
        r.next.next = r
        assert r.private.isDirty
        assert r.next.private.isDirty
        r = self.clerk.store(r)
        assert r.ID == 2
        assert r.next.ID == 1

        r = self.clerk.fetch(Record, 2)
        assert not r.private.isDirty
        assert not r.next.private.isDirty


        ## and the same thing for linksets:
        n = Node()
        n.kids << Node()
        n.kids[0].kids << n
        assert n.private.isDirty
        assert n.kids[0].private.isDirty
        n = self.clerk.store(n)
        
        
    def test_identity(self):
        self.clerk.store(Record(val="one"))
        rec1a = self.clerk.fetch(Record, 1)
        rec1b = self.clerk.fetch(Record, 1)
        assert rec1a is rec1b

        n = Record()
        r = Record(next=n)        
        assert self.clerk.store(r) is r
        assert self.clerk.cache[(Record, r.ID)] is r
        assert self.clerk.cache[(Record, n.ID)] is n
        assert self.clerk.cache[(Record, n.ID)] is r.next

    def test_stub(self):
        self.clerk.store(Record(val="a", next=Record(val="b")))
        self.clerk.cache.clear()
        recA = self.clerk.fetch(Record, val="a")
        recB = self.clerk.fetch(Record, val="b")
        assert recA.next.ID == recB.ID
        assert recA.next is recB

    def test_match(self):
        self.clerk.store(Record(val="one"))
        self.clerk.store(Record(val="two"))
        self.clerk.store(Record(val="two"))
        assert len(self.clerk.match(Record, val="zero")) == 0
        assert len(self.clerk.match(Record, val="one")) == 1
        assert len(self.clerk.match(Record, val="two")) == 2
        
    def test_matchOne(self):
        self.clerk.store(Record(val="one"))
        self.clerk.store(Record(val="two"))
        self.clerk.store(Record(val="two"))
        
        try:
            self.clerk.matchOne(Record, val="zero")
            self.fail("should have failed for not matching")
        except LookupError: pass

        assert isinstance(self.clerk.matchOne(Record, val="one"),
                          Record)

        try:
            self.clerk.matchOne(Record, val="two")
            self.fail("should have failed for matching two")
        except LookupError: pass
예제 #13
0
 def setUp(self):
     self.s = MockStorage()
예제 #14
0
class MockStorageTest(unittest.TestCase):
    def setUp(self):
        self.s = MockStorage()

    def check_store_insert(self):
        row = self.s.store("test_person", name="fred")
        assert row == {"ID": 1, "name": "fred"}

        row = self.s.store("test_person", name="wanda")
        assert row == {"ID": 2, "name": "wanda"}

        assert self.wholedb() == [{
            "ID": 1,
            "name": "fred"
        }, {
            "ID": 2,
            "name": "wanda"
        }]

    def check_store_insertExtra(self):
        self.check_store_insert()
        self.s.store("test_person", name="rick")
        self.s.store("test_person", name="bob")
        self.s.store("test_person", name="jack")
        assert self.wholedb() == [{
            "ID": 1,
            "name": "fred"
        }, {
            "ID": 2,
            "name": "wanda"
        }, {
            "ID": 3,
            "name": "rick"
        }, {
            "ID": 4,
            "name": "bob"
        }, {
            "ID": 5,
            "name": "jack"
        }]

    def check_oldmatch(self):
        self.check_store_insertExtra()
        match = self.s.match("test_person", where("ID") == 2)
        assert match[0]["name"] == "wanda", "new style broke"
        match = self.s.match("test_person", ID=2)
        assert match[0]["name"] == "wanda", "old style broke"

    def check_querybuilder_matches(self):
        self.check_store_insertExtra()
        match = self.s.match("test_person", where("ID") == 5)
        assert match[0]['name'] == 'jack'

        match = self.s.match("test_person", ((where("name") == "fred")
                                             | (where("name") == "bob")),
                             "name")
        self.assertEquals([u['name'] for u in match], ['bob', 'fred'])

        match = self.s.match("test_person", ((where("ID") > 1)
                                             & (where("ID") <= 4))
                             | (where("name").endswith('ck')), 'name desc')
        self.assertEquals([u['name'] for u in match],
                          ['wanda', 'rick', 'jack', 'bob'])

    def check_querybuilder_sorting(self):
        self.check_store_insertExtra()
        assert [
            p['name'] for p in self.s.match("test_person", orderBy='name')
        ] == ['bob', 'fred', 'jack', 'rick', 'wanda']

    def populate(self):
        self.check_store_insert()

    def wholedb(self):
        return self.s.match("test_person")

    def check_store_update(self):
        self.populate()
        row = self.s.fetch("test_person", 1)
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{
            "ID": 1,
            "name": "frood"
        }, {
            "ID": 2,
            "name": "wanda"
        }]

    def check_store_update_longs(self):
        # same as above but with lnogs
        self.populate()
        row = self.s.fetch("test_person", 1L)
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{
            "ID": 1,
            "name": "frood"
        }, {
            "ID": 2,
            "name": "wanda"
        }]

    def check_store_update_strings(self):
        # same as above but with lnogs
        self.populate()
        row = self.s.fetch("test_person", "1")
        row["name"] = "frood"
        self.s.store("test_person", **row)
        assert self.wholedb() == [{
            "ID": 1,
            "name": "frood"
        }, {
            "ID": 2,
            "name": "wanda"
        }]

    def check_match(self):
        assert self.wholedb() == []
        self.populate()
        results = self.s.match("test_person", where("ID") == 1)
        assert results == [{"ID": 1, "name": "fred"}], str(results)

    def check_fetch(self):
        self.check_store_insert()
        wanda = self.s.fetch("test_person", 2)
        assert wanda["name"] == "wanda"

    def _check_delete(self, key_type):
        self.populate()
        self.s.delete("test_person", key_type(1))
        people = self.s.match("test_person")
        assert people == [{"ID": 2, "name": "wanda"}]
        self.s.delete("test_person", key_type(2))
        people = self.s.match("test_person")
        assert people == []

    def check_delete_with_int_id(self):
        self._check_delete(int)

    def check_delete_with_long_id(self):
        self._check_delete(long)

    def check_delete_with_str_id(self):
        self._check_delete(str)
예제 #15
0
파일: sixthday.py 프로젝트: sabren/workshop
class AdminAppTest(unittest.TestCase):

    def setUp(self):

        self.storage = MockStorage()
        self.clerk = Clerk(self.storage, Schema({
            User: "******"
        }))
        self.app = AdminApp(self.clerk, {})
        
        # set up some templates to play with:
        tpl = open("frm_test.zb", "w")
        tpl.write("ID is {:ID:}")
        tpl.close()

        tpl = open("lst_test.zb", "w")
        tpl.write(
            "*# zebra to print a dot for each user:\n"
            "* for each:\n" 
            "    {:x:}\n")
        tpl.close()
        
    def test_generic_create(self):
        """
        generic_create should show a page with a view of a new object.
        """
        self.app.generic_create(User, "frm_test")
        output = self.app.out.getvalue()
        assert output.startswith("ID is None"), \
               "generic_create didn't populate form correctly:\n%s" \
               % output


    def test_generic_show(self):
        """
        generic_edit should show a form with a specific object's data
        """
        self.test_generic_save()
        self.app.input = {"ID":1}
        self.app.generic_show(User, "frm_test")
        output = self.app.out.getvalue()
        assert output.startswith("ID is 1"), \
               "generic_show didn't populate the page correctly:\n%s" \
               % output

    def test_generic_list(self):
        #@TODO: this method should probably go away.
        view = [{"x":"a"}, {"x":"b"}]
        self.app.generic_list(view, "lst_test")
        output = self.app.out.getvalue()
        assert output.startswith("a\nb"), \
               "generic_list didn't populate the form correctly:\n%s" \
               % output

    def test_generic_save(self):
        self.app.generic_save(User)
        obj = self.clerk.fetch(User, 1)

    def test_generic_delete(self):
        self.storage.store("User", username="******")
        self.app.input = {"ID":1}
        self.app.generic_delete(User)
        assert self.storage.match("User") == []

   
    def tearDown(self):
        os.unlink("frm_test.zb")
        os.unlink("lst_test.zb")
예제 #16
0
 def setUp(self):
     self.s = MockStorage()
예제 #17
0
def MockClerk(dbmap=None):
    from storage import MockStorage
    return Clerk(MockStorage(), dbmap or AutoSchema())
예제 #18
0
class AdminAppTest(unittest.TestCase):
    def setUp(self):

        self.storage = MockStorage()
        self.clerk = Clerk(self.storage, Schema({User: "******"}))
        self.app = AdminApp(self.clerk, {})

        # set up some templates to play with:
        tpl = open("frm_test.zb", "w")
        tpl.write("ID is {:ID:}")
        tpl.close()

        tpl = open("lst_test.zb", "w")
        tpl.write("*# zebra to print a dot for each user:\n"
                  "* for each:\n"
                  "    {:x:}\n")
        tpl.close()

    def test_generic_create(self):
        """
        generic_create should show a page with a view of a new object.
        """
        self.app.generic_create(User, "frm_test")
        output = self.app.out.getvalue()
        assert output.startswith("ID is None"), \
               "generic_create didn't populate form correctly:\n%s" \
               % output

    def test_generic_show(self):
        """
        generic_edit should show a form with a specific object's data
        """
        self.test_generic_save()
        self.app.input = {"ID": 1}
        self.app.generic_show(User, "frm_test")
        output = self.app.out.getvalue()
        assert output.startswith("ID is 1"), \
               "generic_show didn't populate the page correctly:\n%s" \
               % output

    def test_generic_list(self):
        #@TODO: this method should probably go away.
        view = [{"x": "a"}, {"x": "b"}]
        self.app.generic_list(view, "lst_test")
        output = self.app.out.getvalue()
        assert output.startswith("a\nb"), \
               "generic_list didn't populate the form correctly:\n%s" \
               % output

    def test_generic_save(self):
        self.app.generic_save(User)
        obj = self.clerk.fetch(User, 1)

    def test_generic_delete(self):
        self.storage.store("User", username="******")
        self.app.input = {"ID": 1}
        self.app.generic_delete(User)
        assert self.storage.match("User") == []

    def tearDown(self):
        os.unlink("frm_test.zb")
        os.unlink("lst_test.zb")
예제 #19
0
def MockClerk(dbmap=None):
    return Clerk(MockStorage(), dbmap or AutoSchema())