def test_localsync_media(): tmpdir = "/tmp/media-tests" try: shutil.rmtree(tmpdir) except OSError: pass shutil.copytree(os.path.join(os.getcwd(), "tests/syncing/media-tests"), tmpdir) deck1anki = os.path.join(tmpdir, "1.anki") deck2anki = os.path.join(tmpdir, "2.anki") deck1media = os.path.join(tmpdir, "1.media") deck2media = os.path.join(tmpdir, "2.media") setup_local((deck1anki, deck2anki)) assert len(os.listdir(deck1media)) == 2 assert len(os.listdir(deck2media)) == 1 client.sync() # metadata should have been copied assert deck1.s.scalar("select count(1) from media") == 3 assert deck2.s.scalar("select count(1) from media") == 3 # copy local files copyLocalMedia(deck1, deck2) assert len(os.listdir(deck1media)) == 2 assert len(os.listdir(deck2media)) == 3 copyLocalMedia(deck2, deck1) assert len(os.listdir(deck1media)) == 3 assert len(os.listdir(deck2media)) == 3 # check delete os.unlink(os.path.join(deck1media, "22161b29b0c18e068038021f54eee1ee.png")) os.system("sync") time.sleep(0.2) rebuildMediaDir(deck1) client.sync() assert deck1.s.scalar("select count(1) from media") == 2 assert deck2.s.scalar("select count(1) from media") == 2
def test_db(): deck = DeckStorage.Deck() deck.addModel(BasicModel()) dir = tempfile.mkdtemp(prefix="anki") path = os.path.join(dir, "foo.jpg") open(path, "w").write("hello") # add a new fact that references it twice f = deck.newFact() f['Front'] = u"<img src='foo.jpg'>" f['Back'] = u"back [sound:foo.jpg]" deck.addFact(f) # 1 entry in the media db, with two references, and missing file assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 2 assert deck.s.scalar("select not originalPath from media") # copy to media folder & check db path = m.copyToMedia(deck, path) m.rebuildMediaDir(deck) # md5 should be set now assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 2 assert deck.s.scalar("select originalPath from media") # edit the fact to remove a reference f['Back'] = u"" f.setModified(True, deck) deck.s.flush() assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select size from media") == 1 # remove the front reference too f['Front'] = u"" f.setModified(True, deck) assert deck.s.scalar("select size from media") == 0 # add the reference back f['Front'] = u"<img src='foo.jpg'>" f.setModified(True, deck) assert deck.s.scalar("select size from media") == 1 # detect file modifications oldsum = deck.s.scalar("select originalPath from media") open(path, "w").write("world") m.rebuildMediaDir(deck) newsum = deck.s.scalar("select originalPath from media") assert newsum and newsum != oldsum # delete underlying file and check db os.unlink(path) m.rebuildMediaDir(deck) # md5 should be gone again assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select not originalPath from media") # media db should pick up media defined via templates & bulk update f['Back'] = u"bar.jpg" f.setModified(True, deck) deck.s.flush() # modify template & regenerate assert deck.s.scalar("select count() from media") == 1 assert deck.s.scalar("select sum(size) from media") == 1 deck.currentModel.cardModels[0].aformat=u'<img src="{{{Back}}}">' deck.updateCardsFromModel(deck.currentModel) assert deck.s.scalar("select sum(size) from media") == 2 assert deck.s.scalar("select count() from media") == 2 deck.currentModel.cardModels[0].aformat=u'{{{Back}}}' deck.updateCardsFromModel(deck.currentModel) assert deck.s.scalar("select count() from media") == 2 assert deck.s.scalar("select sum(size) from media") == 1