def test_nested(self): """ Test nesting of objects """ server = Server() database = Database() database.id = 1 database.name = "Database A" server.databases.add(database) server.databases.remove(database) database = Database() database.id = 2 database.name = "Database B" item = Item() item.id = 3 item.name = "Item A" with self.assertRaises(ValueError): database.items.add(item) server.databases.add(database) database.items.add(item) self.assertEqual(server.databases.keys(), [2])
def test_invalid2(self): """ Test for adding item to unassociated database. """ database = Database() database.id = 1 database.name = "Database A" item = Item() item.id = 2 item.name = "Item A" with self.assertRaises(ValueError): database.items.add(item)
def test_nested(self): """ Test nesting of objects, with different revisions. """ server = Server() server.commit(2) server.commit(3) server.commit(4) server.commit(5) server.commit(6) database = Database(id=1, name="Database A") server.databases.add(database) server.databases.remove(database) database = Database(id=2, name="Database B") item = Item(id=3, name="Item A") database.items.add(item) server.databases.add(database) database.items.add(item) self.assertListEqual(server.databases.keys(), [2]) self.assertListEqual(server.databases(revision=6).keys(), [2]) self.assertListEqual(server.databases(revision=5).keys(), []) self.assertListEqual(server.databases(revision=4).keys(), []) self.assertListEqual(server.databases(revision=3).keys(), []) self.assertListEqual(server.databases(revision=2).keys(), []) self.assertListEqual(server.databases(revision=1).keys(), []) with self.assertRaises(KeyError): server.databases[1] self.assertListEqual(server.databases[2].items.keys(), [3]) self.assertListEqual(server.databases[2].items(revision=1).keys(), [3]) with self.assertRaises(ValueError): # Item was added to a database that was not in a server before # adding. self.assertListEqual( server.databases[2].items(revision=2).keys(), [3]) server.commit(7) self.assertListEqual(server.databases[2].items.keys(), [3]) self.assertListEqual(server.databases[2].items(revision=7).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=6).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=5).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=4).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=3).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=2).keys(), [3]) self.assertListEqual(server.databases[2].items(revision=1).keys(), [3])
def test_diff(self): """ Test diff of two sets, for updated and removed items. """ server = Server() database = Database(id=1, name="Database A") server.databases.add(database) item = Item(id=2, name="Item A") database.items.add(item) server.commit(2) item = Item(id=2, name="Item A, version 2") database.items.add(item) items_1 = database.items(revision=1) items_2 = database.items(revision=2) self.assertListEqual(items_2.keys(), [2]) self.assertListEqual(list(items_2.updated(items_1)), [2]) server.commit(3) database.items.remove(item) server.commit(4) items_1 = database.items(revision=1) items_2 = database.items(revision=2) items_3 = database.items(revision=3) self.assertListEqual(items_1.keys(), [2]) self.assertListEqual(items_2.keys(), [2]) self.assertListEqual(items_3.keys(), []) self.assertListEqual(list(items_3.removed(items_1)), [2]) self.assertListEqual(list(items_3.removed(items_2)), [2]) self.assertListEqual(list(items_3.removed(items_3)), [])
def test_diff(self): """ Test diff of two sets. """ server = Server() database = Database() database.id = 1 database.name = "Database A" server.databases.add(database) item = Item() item.id = 2 item.name = "Item A" database.items.add(item) item = Item() item.id = 2 item.name = "Item A, version 2" database.items.add(item) self.assertEqual(server.storage.revision, 3) items_1 = database.items(revision=2) items_2 = database.items(revision=3) self.assertEqual(items_2.edited(items_1), set([2]))
def test_invalid(self): """ Test for adding item to removed database, then add it again. """ server = Server() database = Database() database.id = 1 database.name = "Database A" server.databases.add(database) server.databases.remove(database) item = Item() item.id = 2 item.name = "Item A" with self.assertRaises(ValueError): database.items.add(item) server.databases.add(database) database.items.add(item)
def benchmark(self, count): # Save references server = self.server database = server.databases[1] container_one = database.containers[1] container_two = database.containers[2] container_three = database.containers[3] # Execute `count' operations of addition for i in xrange(count): item = Item( id=i, artist="SubDaap", album="RevisionServer", name="Item %d" % i, duration=i, bitrate=320, year=2014) container_item_a = ContainerItem(id=i, item_id=item.id) container_item_b = ContainerItem(id=i, item_id=item.id) database.items.add(item) container_one.container_items.add(container_item_a) if i % 2 == 0: container_two.container_items.add(container_item_b) else: container_three.container_items.add(container_item_b) # Update server and database database.containers.add(container_one) database.containers.add(container_two) database.containers.add(container_three) server.databases.add(database) # Clean old revision history server.clean(server.revision) # Iterate over items x = database.items.values() a = container_one.container_items.values() b = container_two.container_items.values() c = container_three.container_items.values() return len(x) + len(a) + len(b) + len(c)
def test_store(self): """ Test store references stay the same. """ server = Server() database = Database(id=1, name="Database A") server.databases.add(database) item = Item(id=2, name="Item A") database.items.add(item) self.assertEqual( server.databases.store, server.databases(revision=1).store) self.assertEqual( server.databases[1].items.store, server.databases[1].items(revision=1).store) self.assertEqual( server.databases(revision=1)[1].items.store, server.databases[1].items(revision=1).store)
def test_unicode_str_repr(self): """ Test model to unicode, string and representation methods. """ server = Server(name=u"Björn Borg") db = Database(id=1, name=u"Hellö Wörld") item = Item( id=2, artist=u"Slagsmålsklubben", album="Fest i valen", name="Sponsored by Destiny") container = Container(id=3, name=u"Knäckebröd") container_item = ContainerItem(id=4, item_id=2, container_id=3) for instance in [server, db, item, container, container_item]: # Type checking self.assertTrue(type(unicode(instance)), unicode) self.assertTrue(type(str(instance)), str) self.assertTrue(type(repr(instance)), str) # String variant replaces non-ascii characters self.assertTrue( unicode(instance).encode("ascii", "replace") == str(instance))
def do_random_things(self): database = self.server.databases[1] counter = 1 while True: # Decide what to do if not database.items: choice = "add" else: # More chance on an adding items. choice = random.choice(["add", "add", "update", "remove"]) if choice == "add": item = Item(id=counter, artist="SubDaap", album="RevisionServer", name="Item %d" % counter, duration=counter) container_item = ContainerItem(id=counter, item_id=item.id) counter += 1 database.items.add(item) database.containers[1].container_items.add(container_item) logger.info( "Item %d added, %d items in container. Revision is " "%d.", item.id, len(database.items), self.revision) elif choice == "update": item = random.choice(database.items.values()) container_item = database.containers[1] \ .container_items[item.id] # Copy the items. This step is optional if you don't care # if older revision will all have the same data. item = copy.copy(item) container_item = copy.copy(container_item) # Update some properties. item.duration += 1000 * 60 # One minute database.containers[1] \ .container_items \ .add(container_item) database.items.add(item) logger.info( "Item %d updated, %d items in container. Revision is " "%d.", item.id, len(database.items), self.revision) elif choice == "remove": item = random.choice(database.items.values()) container_item = database.containers[1] \ .container_items[item.id] database.containers[1] \ .container_items \ .remove(container_item) database.items.remove(item) logger.info( "Item %d removed, %d items in container. Revision is " "%d.", item.id, len(database.items), self.revision) # The server and database have to be re-added so they are # marked as updated. database.containers.add(database.containers[1]) self.server.databases.add(database) # Update the provider. This will unblock waiting clients. self.update() # Verify the change by computing the difference. This has nothing # to do with this example, except that it checks what happens is # correct. if choice in ["add", "update"]: assert list( database.items(self.revision).updated( database.items(self.revision - 1))) == [item.id] elif choice == "remove": assert list( database.items(self.revision).removed( database.items(self.revision - 1))) == [item.id] # Wait until next operation gevent.sleep(5.0)
def __init__(self): super( ExampleProvider, self, ).__init__() # It's important that `self.server' is initialized, since it is used # throughout the class. self.server = server = Server(name="DAAPServer") # Add example data to the library. Note that everything should be added # in the right order. For instance, you cannot add an item to a # database that has not been added to a server yet. database = Database(id=1, name="Library") server.databases.add(database) container_one = Container(id=1, name="My Music", is_base=True) container_two = Container(id=2, name="Cool Music", parent_id=container_one.id) container_three = Container(id=3, name="Empty Playlist", parent_id=container_two.id) database.containers.add(container_one) database.containers.add(container_two) database.containers.add(container_three) item_one = Item(id=1, artist="Tenacious D", album="The Pick of Destiny", name="The Metal", track=15, duration=166000, year=2006, genre="Rock", file_suffix="mp3", file_type="audio/mp3") item_two = Item(id=2, artist="Fait No More", album="The Real Thing", name="Epic", track=2, duration=291000, year=1989, genre="Rock", file_suffix="mp3", file_type="audio/mp3") database.items.add(item_one) database.items.add(item_two) container_item_one_a = ContainerItem(id=1, item_id=item_one.id) container_item_one_b = ContainerItem(id=2, item_id=item_two.id) container_item_two_a = ContainerItem(id=3, item_id=item_one.id, order=1) container_item_two_b = ContainerItem(id=4, item_id=item_two.id, order=2) container_item_two_c = ContainerItem(id=5, item_id=item_one.id, order=3) container_one.container_items.add(container_item_one_a) container_one.container_items.add(container_item_one_b) container_two.container_items.add(container_item_two_a) container_two.container_items.add(container_item_two_b) container_two.container_items.add(container_item_two_c) # Inform provider that the structure is ready. self.update()