def test_download(): if not TEST_REMOTE: return f = FullSyncer(ts.client.col, "abc", ts.server.con) assertException(Exception, f.download) f.hkey = TEST_HKEY f.download()
def test_create_open(): (fd, path) = tempfile.mkstemp(suffix=".anki2", prefix="test_attachNew") try: os.close(fd) os.unlink(path) except OSError: pass deck = aopen(path) # for open() newPath = deck.path deck.close() newMod = deck.mod del deck # reopen deck = aopen(newPath) assert deck.mod == newMod deck.close() # non-writeable dir if isWin: dir = "c:\root.anki2" else: dir = "/attachroot.anki2" assertException(Exception, lambda: aopen(dir)) # reuse tmp file from before, test non-writeable file os.chmod(newPath, 0) assertException(Exception, lambda: aopen(newPath)) os.chmod(newPath, 0o666) os.unlink(newPath)
def test_remove(): deck = getEmptyDeck() # can't remove the default deck assertException(AssertionError, lambda: deck.decks.rem(1)) # create a new deck, and add a note/card to it g1 = deck.decks.id("g1") f = deck.newNote() f['Front'] = u"1" f.model()['did'] = g1 deck.addNote(f) c = f.cards()[0] assert c.did == g1 # by default deleting the deck leaves the cards with an invalid did assert deck.cardCount() == 1 deck.decks.rem(g1) assert deck.cardCount() == 1 c.load() assert c.did == g1 # but if we try to get it, we get the default assert deck.decks.name(c.did) == "[no deck]" # let's create another deck and explicitly set the card to it g2 = deck.decks.id("g2") c.did = g2; c.flush() # this time we'll delete the card/note too deck.decks.rem(g2, cardsToo=True) assert deck.cardCount() == 0 assert deck.noteCount() == 0
def test_remove(): deck = getEmptyDeck() # can't remove the default deck assertException(AssertionError, lambda: deck.decks.rem(1)) # create a new deck, and add a note/card to it g1 = deck.decks.id("g1") f = deck.newNote() f['Front'] = u"1" f.did = g1 deck.addNote(f) c = f.cards()[0] assert c.did == g1 # by default deleting the deck leaves the cards with an invalid did assert deck.cardCount() == 1 deck.decks.rem(g1) assert deck.cardCount() == 1 c.load() assert c.did == g1 # but if we try to get it, we get the default assert deck.decks.name(c.did) == "Default" # let's create another deck and explicitly set the card to it g2 = deck.decks.id("g2") c.did = g2 c.flush() # this time we'll delete the card/note too deck.decks.rem(g2, cardsToo=True) assert deck.cardCount() == 0 assert deck.noteCount() == 0
def test_rename(): d = getEmptyCol() id = d.decks.id("hello::world") # should be able to rename into a completely different branch, creating # parents as necessary d.decks.rename(d.decks.get(id), "foo::bar") names = [n.name for n in d.decks.all_names_and_ids()] assert "foo" in names assert "foo::bar" in names assert "hello::world" not in names # create another deck id = d.decks.id("tmp") # automatically adjusted if a duplicate name d.decks.rename(d.decks.get(id), "FOO") names = [n.name for n in d.decks.all_names_and_ids()] assert "FOO+" in names # when renaming, the children should be renamed too d.decks.id("one::two::three") id = d.decks.id("one") d.decks.rename(d.decks.get(id), "yo") names = [n.name for n in d.decks.all_names_and_ids()] for n in "yo", "yo::two", "yo::two::three": assert n in names # over filtered filteredId = d.decks.newDyn("filtered") filtered = d.decks.get(filteredId) childId = d.decks.id("child") child = d.decks.get(childId) assertException(DeckRenameError, lambda: d.decks.rename(child, "filtered::child")) assertException(DeckRenameError, lambda: d.decks.rename(child, "FILTERED::child"))
def test_rename(): d = getEmptyCol() id = d.decks.id("hello::world") # should be able to rename into a completely different branch, creating # parents as necessary d.decks.rename(d.decks.get(id), "foo::bar") assert "foo" in d.decks.allNames() assert "foo::bar" in d.decks.allNames() assert "hello::world" not in d.decks.allNames() # create another deck id = d.decks.id("tmp") # we can't rename it if it conflicts assertException(Exception, lambda: d.decks.rename(d.decks.get(id), "foo")) # when renaming, the children should be renamed too d.decks.id("one::two::three") id = d.decks.id("one") d.decks.rename(d.decks.get(id), "yo") for n in "yo", "yo::two", "yo::two::three": assert n in d.decks.allNames() # over filtered filteredId = d.decks.newDyn("filtered") filtered = d.decks.get(filteredId) childId = d.decks.id("child") child = d.decks.get(childId) assertException(DeckRenameError, lambda: d.decks.rename(child, "filtered::child")) assertException(DeckRenameError, lambda: d.decks.rename(child, "FILTERED::child")) # changing case parentId = d.decks.id("PARENT") d.decks.id("PARENT::CHILD") assertException(DeckRenameError, lambda: d.decks.rename(child, "PARENT::CHILD")) assertException(DeckRenameError, lambda: d.decks.rename(child, "PARENT::child"))
def test_create_open(): global newPath, newMod (fd, path) = tempfile.mkstemp(suffix=".anki2", prefix="test_attachNew") try: os.close(fd) os.unlink(path) except OSError: pass deck = aopen(path) # for open() newPath = deck.path deck.close() newMod = deck.mod del deck # reopen deck = aopen(newPath) assert deck.mod == newMod deck.close() # non-writeable dir assertException(Exception, lambda: aopen("/attachroot.anki2")) # reuse tmp file from before, test non-writeable file os.chmod(newPath, 0) assertException(Exception, lambda: aopen(newPath)) os.chmod(newPath, 0o666) os.unlink(newPath)
def test_renameForDragAndDrop(): d = getEmptyCol() def deckNames(): return [name for name in sorted(d.decks.allNames()) if name != "Default"] languages_did = d.decks.id("Languages") chinese_did = d.decks.id("Chinese") hsk_did = d.decks.id("Chinese::HSK") # Renaming also renames children d.decks.renameForDragAndDrop(chinese_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] # Dragging a deck onto itself is a no-op d.decks.renameForDragAndDrop(languages_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] # Dragging a deck onto its parent is a no-op d.decks.renameForDragAndDrop(hsk_did, chinese_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] # Dragging a deck onto a descendant is a no-op d.decks.renameForDragAndDrop(languages_did, hsk_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] # Can drag a grandchild onto its grandparent. It becomes a child d.decks.renameForDragAndDrop(hsk_did, languages_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::HSK"] # Can drag a deck onto its sibling d.decks.renameForDragAndDrop(hsk_did, chinese_did) assert deckNames() == ["Languages", "Languages::Chinese", "Languages::Chinese::HSK"] # Can drag a deck back to the top level d.decks.renameForDragAndDrop(chinese_did, None) assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] # Dragging a top level deck to the top level is a no-op d.decks.renameForDragAndDrop(chinese_did, None) assert deckNames() == ["Chinese", "Chinese::HSK", "Languages"] # can't drack a deck where sibling have same name new_hsk_did = d.decks.id("HSK") assertException( DeckRenameError, lambda: d.decks.renameForDragAndDrop(new_hsk_did, chinese_did) ) d.decks.rem(new_hsk_did) # can't drack a deck where sibling have same name different case new_hsk_did = d.decks.id("hsk") assertException( DeckRenameError, lambda: d.decks.renameForDragAndDrop(new_hsk_did, chinese_did) ) d.decks.rem(new_hsk_did) # '' is a convenient alias for the top level DID d.decks.renameForDragAndDrop(hsk_did, "") assert deckNames() == ["Chinese", "HSK", "Languages"]
def test_openReadOnly(): # non-writeable dir assertException(Exception, lambda: aopen("/attachroot.anki2")) # reuse tmp file from before, test non-writeable file os.chmod(newPath, 0) assertException(Exception, lambda: aopen(newPath)) os.chmod(newPath, 0666) os.unlink(newPath)
def test_attachReadOnly(): # non-writeable dir assertException(Exception, lambda: DeckStorage.Deck("/attachroot")) # reuse tmp file from before, test non-writeable file os.chmod(newPath, 0) assertException(Exception, lambda: DeckStorage.Deck(newPath)) os.chmod(newPath, 0666) os.unlink(newPath)
def test_rename(): d = getEmptyCol() id = d.decks.id("hello::world") # should be able to rename into a completely different branch, creating # parents as necessary d.decks.rename(d.decks.get(id), "foo::bar") assert "foo" in d.decks.allNames() assert "foo::bar" in d.decks.allNames() assert "hello::world" not in d.decks.allNames() # create another deck id = d.decks.id("tmp") # we can't rename it if it conflicts assertException(Exception, lambda: d.decks.rename(d.decks.get(id), "foo")) # when renaming, the children should be renamed too d.decks.id("one::two::three") id = d.decks.id("one") d.decks.rename(d.decks.get(id), "yo") for n in "yo", "yo::two", "yo::two::three": assert n in d.decks.allNames()