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)
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.upsert_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(InvalidArgumentException, self.cb.append_items, ItemSequence([it])) itcoll.add(it) self.assertRaises(InvalidArgumentException, self.cb.append_items, itcoll)
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))
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.upsert_multi, ItemSequence([it]))
def save(self, cb): cb.replace_multi(ItemSequence([self]))
def load(cls, name, cb): it = Player(name) cb.get_multi(ItemSequence([it])) return it
return self.value['games'] @property 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]) INDENT = " " * 3