def test_exit(self): with locks.authority_of(locks.SYSTEM): owner = db.Object("owner") db.store(owner) with locks.authority_of(owner): source = db.Room("Source") dest = db.Room("Dest") db.store(source) db.store(dest) player = db.Object("Player", location=source) player.send = mock.MagicMock() db.store(player) exit = db.Exit("Exit", source, dest) exit.go_message = ("You, {player}, go from {source} to " "{destination} via {exit}.") db.store(exit) sourceBystander = db.Object("source bystander", location=source) sourceBystander.send = mock.MagicMock() db.store(sourceBystander) destBystander = db.Object("dest bystander", location=dest) destBystander.send = mock.MagicMock() db.store(destBystander) self.assertIs(player.location, source) exit.go(player) self.assertIs(player.location, dest) self.assertEqual(sourceBystander.send.call_count, 1) self.assertEqual(destBystander.send.call_count, 1) sourceBystander.send.assert_called_once_with( "Player leaves through Exit.") destBystander.send.assert_called_once_with("Player arrives.") player.send.assert_called_with("You, Player, go from Source to " "Dest via Exit.")
def test_move_insert_remove(self): with locks.authority_of(locks.SYSTEM): hat = db.Object("hat") magician = db.Object("magician") db.store(hat) db.store(magician) try: with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) except locks.LockFailedError as e: self.assertEqual(str(e), "You can't put that in hat.") else: self.fail() with locks.authority_of(hat): hat.locks.insert = locks.Is(magician) with locks.authority_of(magician): rabbit = db.Object("rabbit", hat) db.store(rabbit) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You can't remove that from hat.") else: self.fail() with locks.authority_of(hat): hat.locks.remove = locks.Is(magician) rabbit.location = magician self.assertEqual(rabbit.location, magician)
def test_retrieve_none(self): with locks.authority_of(locks.SYSTEM): foo = db.Object("foo") self.assertRaises(KeyError, db.find, lambda obj: obj.name == "bar") found = db.find_all(lambda obj: obj.name == "bar") self.assertEqual(len(found), 0)
def setUp(self): super(AttrLockTestCase, self).setUp() self.obj_owner = db.Player("Objowner", "password") db.store(self.obj_owner) self.attr_owner = db.Player("Attrowner", "password") db.store(self.attr_owner) self.setter = db.Player("Setter", "password") db.store(self.setter) self.getter = db.Player("Getter", "password") db.store(self.getter) self.players = [self.obj_owner, self.attr_owner, self.getter, self.setter] with locks.authority_of(self.obj_owner): self.obj = db.Object("Object") db.store(self.obj) with locks.authority_of(self.attr_owner): self.obj.attr = "value" self.obj.lock_attr("attr", get_lock=locks.Is(self.getter), set_lock=locks.Is(self.setter))
def test_position_string(self): with locks.authority_of(locks.SYSTEM): model = db.Object("model") db.store(model) model.position = "vogueing for the camera" self.assertEqual(model.position_string(), "model (vogueing for the camera)")
def test_nearbyobject_here(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", self.lobby) with locks.authority_of(locks.SYSTEM): here = db.Object("here", self.lobby) db.store(here) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "here", here)
def test_create(self): expected_uid = db._nextUid with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") self.assertEqual(obj.uid, None) db.store(obj) self.assertEqual(obj.uid, expected_uid) self.assertEqual(db._nextUid, expected_uid + 1)
def test_nearbyobject_me(self): pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", self.player) with locks.authority_of(locks.SYSTEM): me = db.Object("me", self.lobby) db.store(me) pattern = parser.NearbyObject(self.player) self.assert_parse(pattern, "me", me)
def test_neighbors(self): with locks.authority_of(locks.SYSTEM): container = db.Object("container") foo = db.Object("foo", location=container) neighbor = db.Object("neighbor", location=container) containee = db.Object("containee", location=foo) distant = db.Object("distant") inside_neighbor = db.Object("inside neighbor", location=neighbor) inside_containee = db.Object("inside containee", location=containee) db.store(container) db.store(foo) db.store(neighbor) db.store(containee) db.store(distant) db.store(inside_neighbor) db.store(inside_containee) neighbors = foo.neighbors() self.assertIn(container, neighbors) self.assertIn(foo, neighbors) self.assertIn(neighbor, neighbors) self.assertIn(containee, neighbors) self.assertNotIn(distant, neighbors) self.assertNotIn(inside_neighbor, neighbors) self.assertNotIn(inside_containee, neighbors) self.assertEqual(len(neighbors), 4)
def test_move_get_drop_container(self): with locks.authority_of(locks.SYSTEM): magician = db.Object("magician") rabbit = db.Object("stubborn rabbit") db.store(magician) db.store(rabbit) with locks.authority_of(rabbit): rabbit.locks.take = locks.Fail() rabbit.locks.drop = locks.Fail() rabbit.locks.insert = locks.Is(magician) with locks.authority_of(magician): carrot = db.Object("carrot", magician) celery = db.Object("celery", magician) hat = db.Container("hat", magician) db.store(carrot) db.store(celery) db.store(hat) try: rabbit.location = magician except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot take stubborn rabbit.") else: self.fail() carrot.location = rabbit with locks.authority_of(rabbit): rabbit.locks.take = locks.Is(magician) rabbit.location = magician try: rabbit.location = hat except locks.LockFailedError as e: self.assertEqual(str(e), "You cannot drop stubborn rabbit.") else: self.fail() celery.location = rabbit with locks.authority_of(rabbit): rabbit.locks.drop = locks.Is(magician) rabbit.location = hat rabbit.location = magician
def test_has(self): with locks.authority_of(self.player): key = db.Object("a key") key.location = self.player db.store(key) lock = locks.Has(key) self.assertTrue(lock(self.player)) self.assertFalse(lock(self.player2))
def test_update(self): with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") db.store(obj) obj.name = "bar" db.store(obj) obj = db.get(obj.uid) self.assertEqual(obj.name, "bar") self.assertEqual(obj.type, "thing")
def setUp(self): super(ParserTestCase, self).setUp() self.setup_objects() tricky_names = ["me you", "cup of mead", "here there", "heretical thoughts"] # These are for confounding the me/here keywords. for name in tricky_names: with locks.authority_of(locks.SYSTEM): self.objects[name] = db.Object(name, self.lobby) db.store(self.objects[name])
def test_retrieve_many(self): with locks.authority_of(locks.SYSTEM): foo = db.Object("foo") bar = db.Object("bar") baz = db.Object("baz") db.store(foo) db.store(bar) db.store(baz) def starts_with_ba(obj): return obj.name.startswith("ba") self.assertRaises(KeyError, db.find, starts_with_ba) found = db.find_all(starts_with_ba) self.assertNotIn(foo, found) self.assertIn(bar, found) self.assertIn(baz, found) self.assertEqual(len(found), 2)
def test_destroy(self): with locks.authority_of(locks.SYSTEM): owner = db.Object("owner") not_owner = db.Object("not_owner") db.store(owner) db.store(not_owner) with locks.authority_of(owner): item = db.Object("item") db.store(item) item_uid = item.uid with locks.authority_of(not_owner): self.assertRaises(locks.LockFailedError, item.destroy) with locks.authority_of(owner): item.destroy() matches = db.find_all(lambda x: x.uid == item_uid) self.assertEqual(len(matches), 0) self.assertRaises(KeyError, db.get, item_uid)
def setUp(self): super(LockTestCase, self).setUp() with locks.authority_of(locks.SYSTEM): self.player = db.Player("Player", "password") db.store(self.player) self.player2 = db.Player("PlayerTwo", "password") db.store(self.player2) self.obj = db.Object("object", owner=self.player) with locks.authority_of(self.player2): self.obj.foreign_attr = 0 db.store(self.obj)
def test_retrieve_one(self): with locks.authority_of(locks.SYSTEM): obj_created = db.Object("foo") db.store(obj_created) obj_found = db.get(obj_created.uid) self.assertEqual(obj_created, obj_found) self.assertTrue(obj_created is obj_found) self.assertEqual(obj_found.name, "foo") self.assertEqual(obj_found.type, "thing") found = db.find_all(lambda x: x.uid == obj_created.uid) self.assertEqual(len(found), 1) self.assertEqual(obj_created, found.pop()) self.assertEqual(obj_created, db.get(obj_created.uid))
def setup_objects(self): """ Generates the following clutter: OBJECTS IN THE LOBBY: abacus, ant, balloon, Bucket, cat, frog, Fodor's Guide, horse IN PLAYER'S INVENTORY: Anabot doll, ape plushie, apple, cat, cherry, cheese, horse figurine, monster mask, monocle, moose, millipede IN NEIGHBOR'S INVENTORY: apple IN FROG'S INVENTORY: hat All of these are stored in self.objects[name], EXCEPT: * the cat in the room is objects["room_cat"] * the cat in player's inventory is objects["inv_cat"] * the apple in neighbor's inventory is ["neighbor_apple"] All are plain db.Objects, EXCEPT: * monocle and monster mask are equipment.Equipment * Bucket is a db.Container The player owns all the objects in its inventory. SYSTEM owns the rest. """ self.objects = {} with locks.authority_of(self.player): for inv_object in [ "apple", "horse figurine", "ape plushie", "Anabot doll", "cherry", "cheese", "moose", "millipede" ]: self.objects[inv_object] = db.Object(inv_object, self.player) self.objects["monocle"] = equipment.Equipment( "monocle", self.player) self.objects["monster mask"] = equipment.Equipment( "monster mask", self.player) with locks.authority_of(locks.SYSTEM): for room_object in [ "frog", "ant", "horse", "Fodor's Guide", "abacus", "balloon" ]: self.objects[room_object] = db.Object(room_object, location=self.lobby) self.objects["Bucket"] = db.Container("Bucket", self.lobby) self.objects["room_cat"] = db.Object("cat", self.lobby) self.objects["inv_cat"] = db.Object("cat", self.player) self.objects["neighbor_apple"] = db.Object("apple", self.neighbor) self.objects["hat"] = db.Object("hat", self.objects["frog"]) for key in self.objects: db.store(self.objects[key])
def test_delete(self): with locks.authority_of(locks.SYSTEM): obj = db.Object("foo") db.store(obj) db.delete(obj) self.assertRaises(IndexError, db.store, obj)
def test_nearbyobject_my_redherring(self): with locks.authority_of(locks.SYSTEM): obj = db.Object("my herring", self.lobby) db.store(obj) self.assert_parse(parser.NearbyObject(self.player), "my herr", obj)