Esempio n. 1
0
    def test_simple_get(self):
        k = self.gen_key("itm_simple_get")
        it = Item(k, "simple_value")

        rvs = self.cb.upsert_multi(ItemSequence([it]))
        self.assertTrue(rvs.all_ok)

        it_out = rvs[it.key]
        self.assertEqual(it_out, it)

        it = Item()
        it.key = k
        itcoll = ItemSequence([it])

        rvs = self.cb.get_multi(ItemSequence([it]))
        self.assertTrue(rvs.all_ok)
        it_out = rvs[it.key]
        self.assertEqual(it_out, it)
        self.assertEqual(it_out.value, "simple_value")

        # Now, set it again
        self.cb.replace_multi(itcoll)

        # Now, delete it
        self.cb.remove_multi(itcoll)

        self.assertRaises(NotFoundError, self.cb.get_multi, itcoll)
Esempio n. 2
0
    def test_invalid_item(self):
        itcoll = ItemOptionDict()
        itcoll.add(None)
        self.assertRaises(ArgumentError, self.cb.upsert_multi, itcoll)

        self.assertRaises(ArgumentError, self.cb.upsert_multi,
                          ItemSequence([None]))
    def test_items_append(self):
        k = self.gen_key("itm_append")
        it = Item(k, "MIDDLE")
        itcoll = ItemOptionDict()
        itcoll.add(it)

        self.cb.set_multi(itcoll, format=FMT_UTF8)

        itcoll.add(it, fragment="_END")
        self.cb.append_items(itcoll, format=FMT_UTF8)
        self.assertEqual(it.value, "MIDDLE_END")

        itcoll.add(it, fragment="BEGIN_")
        self.cb.prepend_items(itcoll, format=FMT_UTF8)
        self.assertEqual(it.value, "BEGIN_MIDDLE_END")

        rv = self.cb.get(it.key)
        self.assertEqual(rv.value, "BEGIN_MIDDLE_END")

        # Try without a 'fragment' specifier
        self.assertRaises(ArgumentError,
                          self.cb.append_items, ItemSequence([it]))
        itcoll.add(it)
        self.assertRaises(ArgumentError,
                          self.cb.append_items, itcoll)
Esempio n. 4
0
    def test_apiwrap(self):
        it = Item(self.gen_key("item_apiwrap"))
        self.cb.upsert_multi(it.as_itcoll())
        self.assertTrue(it.cas)

        # Set with 'ignorecas'
        it.cas = 1234
        self.cb.upsert_multi(it.as_itcoll(ignore_cas=True))

        self.cb.upsert_multi(ItemSequence(it))
Esempio n. 5
0
    def create(cls, name, email, cb):
        """
        Create the basic structure of a player
        """
        it = cls(name, create_structure=True)
        it.value['email'] = email

        # In an actual application you'd probably want to use 'add',
        # but since this app might be run multiple times, you don't
        # want to get KeyExistsError
        cb.upsert_multi(ItemSequence([it]))
        return it
    def test_subclass_descriptors(self):
        class MyItem(Item):
            def __init__(self):
                pass
            @property
            def value(self):
                return "This should not be present!!!"
            @value.setter
            def value(self, other):
                return

        k = self.gen_key("itm_desc")
        it = MyItem()
        it.key = k
        it.value = "hi!"
        self.assertRaises(ArgumentError,
                          self.cb.set_multi,
                          ItemSequence([it]))
Esempio n. 7
0
 def save(self, cb):
     cb.replace_multi(ItemSequence([self]))
Esempio n. 8
0
 def load(cls, name, cb):
     it = Player(name)
     cb.get_multi(ItemSequence([it]))
     return it
Esempio n. 9
0
    def email(self):
        return self.value['email']

    @email.setter
    def email(self, value):
        self.value['email'] = value


cb = Bucket('couchbase://localhost/default')
single_player = Player.create("bob", "*****@*****.**", cb)
single_player.score += 100
single_player.save(cb)

# Let's try multiple players
players = ItemSequence([
    Player(x, create_structure=True) for x in ("joe", "jim", "bill", "larry")
])

# Save them all
cb.upsert_multi(players)

# Give them all some points
for p, options in players:
    p.score += randint(20, 2000)
    # also set the email?
    if not p.email:
        p.email = "{0}@{0}.notspecified.com".format(p.name)

cb.replace_multi(players)
all_players = ItemSequence([x[0] for x in players] + [single_player])