Fix tests

This commit is contained in:
Alex Yatskov 2021-01-18 21:03:27 -08:00
parent 322984be06
commit a8273d0e38
8 changed files with 119 additions and 48 deletions

View File

@ -175,4 +175,5 @@ serviced*. Make sure that your PRs meet the following criteria:
* Attempt to match style of the surrounding code. * Attempt to match style of the surrounding code.
* Have accompanying documentation with examples. * Have accompanying documentation with examples.
* Are conceivably useful in other applications. * Have accompanying tests that verify operation.
* Implement features useful in other applications.

View File

@ -667,7 +667,7 @@ class AnkiConnect:
else: else:
couldSetEaseFactors.append(True) couldSetEaseFactors.append(True)
ankiCard.factor = easeFactors[ind] ankiCard.factor = easeFactors[i]
ankiCard.flush() ankiCard.flush()
return couldSetEaseFactors return couldSetEaseFactors

View File

@ -7,7 +7,15 @@ import util
class TestCards(unittest.TestCase): class TestCards(unittest.TestCase):
def setUp(self): def setUp(self):
util.invoke('createDeck', deck='test') util.invoke('createDeck', deck='test')
note = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ['tag1']} note = {
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front1', 'Back': 'back1'},
'tags': ['tag1'],
'options': {
'allowDuplicate': True
}
}
self.noteId = util.invoke('addNote', note=note) self.noteId = util.invoke('addNote', note=note)

0
tests/test_debug.py Normal file → Executable file
View File

View File

@ -68,27 +68,27 @@ class TestDecks(unittest.TestCase):
self.assertFalse(deckConfigId) self.assertFalse(deckConfigId)
# updateCompleteDeck # updateCompleteDeck
util.invoke("updateCompleteDeck", data={ util.invoke('updateCompleteDeck', data={
"deck": "test3", 'deck': 'test3',
"cards": { 'cards': {
"12": { '12': {
"id": 12, "nid": 23, "ord": 0, "type": 0, "queue": 0, 'id': 12, 'nid': 23, 'ord': 0, 'type': 0, 'queue': 0,
"due": 1186031, "factor": 0, "ivl": 0, "reps": 0, "lapses": 0, "left": 0 'due': 1186031, 'factor': 0, 'ivl': 0, 'reps': 0, 'lapses': 0, 'left': 0
} }
}, },
"notes": { 'notes': {
"23": { '23': {
"id": 23, "mid": 34, "fields": ["frontValue", "backValue"], "tags": ["aTag"] 'id': 23, 'mid': 34, 'fields': ['frontValue', 'backValue'], 'tags': ['aTag']
} }
}, },
"models": { 'models': {
"34": { '34': {
"id": 34, "fields": ["Front", "Back"], "templateNames": ["Card 1"], "name": "anotherModel", 'id': 34, 'fields': ['Front', 'Back'], 'templateNames': ['Card 1'], 'name': 'anotherModel',
} }
} }
}) })
deckNames = util.invoke("deckNames") deckNames = util.invoke('deckNames')
self.assertIn("test3", deckNames) self.assertIn('test3', deckNames)
cardIDs = util.invoke('findCards', query='deck:test3') cardIDs = util.invoke('findCards', query='deck:test3')
self.assertEqual(len(cardIDs), 1) self.assertEqual(len(cardIDs), 1)
self.assertEqual(cardIDs[0], 12) self.assertEqual(cardIDs[0], 12)

View File

@ -44,7 +44,15 @@ class TestMisc(unittest.TestCase):
os.close(fd) os.close(fd)
os.unlink(newname) os.unlink(newname)
util.invoke('createDeck', deck=deckName) util.invoke('createDeck', deck=deckName)
note = {'deckName': deckName, 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ''} note = {
'deckName': deckName,
'modelName': 'Basic',
'fields': {'Front': 'front1', 'Back': 'back1'},
'tags': '',
'options': {
'allowDuplicate': True
}
}
noteId = util.invoke('addNote', note=note) noteId = util.invoke('addNote', note=note)
util.invoke('exportPackage', deck=deckName, path=newname) util.invoke('exportPackage', deck=deckName, path=newname)
util.invoke('deleteDecks', decks=[deckName], cardsToo=True) util.invoke('deleteDecks', decks=[deckName], cardsToo=True)
@ -53,7 +61,7 @@ class TestMisc(unittest.TestCase):
self.assertIn(deckName, deckNames) self.assertIn(deckName, deckNames)
# reloadCollection # reloadCollection
util.invoke("reloadCollection") util.invoke('reloadCollection')
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -14,10 +14,61 @@ class TestNotes(unittest.TestCase):
def runTest(self): def runTest(self):
options = {
'allowDuplicate': True
}
note1 = {
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front1', 'Back': 'back1'},
'tags': ['tag1'],
'options': options
}
note2 = {
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front1', 'Back': 'back1'},
'tags': ['tag1']
}
notes1 = [
{
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front3', 'Back': 'back3'},
'tags': ['tag'],
'options': options
},
{
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front4', 'Back': 'back4'},
'tags': ['tag'],
'options': options
}
]
notes2 = [
{
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front3', 'Back': 'back3'},
'tags': ['tag']
},
{
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front4', 'Back': 'back4'},
'tags': ['tag']
}
]
# addNote # addNote
note = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ['tag1']} noteId = util.invoke('addNote', note=note1)
noteId = util.invoke('addNote', note=note) self.assertRaises(Exception, lambda: util.invoke('addNote', note=note2))
self.assertRaises(Exception, lambda: util.invoke('addNote', note=note))
# addTags # addTags
util.invoke('addTags', notes=[noteId], tags='tag2') util.invoke('addTags', notes=[noteId], tags='tag2')
@ -53,36 +104,31 @@ class TestNotes(unittest.TestCase):
self.assertEqual(noteInfo['fields']['Front']['value'], 'front2') self.assertEqual(noteInfo['fields']['Front']['value'], 'front2')
self.assertEqual(noteInfo['fields']['Back']['value'], 'back2') self.assertEqual(noteInfo['fields']['Back']['value'], 'back2')
notes = [
{'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front3', 'Back': 'back3'}, 'tags': ['tag']},
{'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front4', 'Back': 'back4'}, 'tags': ['tag']}
]
# canAddNotes (part 1) # canAddNotes (part 1)
noteStates = util.invoke('canAddNotes', notes=notes) noteStates = util.invoke('canAddNotes', notes=notes1)
self.assertEqual(len(noteStates), len(notes)) self.assertEqual(len(noteStates), len(notes1))
self.assertNotIn(False, noteStates) self.assertNotIn(False, noteStates)
# addNotes (part 1) # addNotes (part 1)
noteIds = util.invoke('addNotes', notes=notes) noteIds = util.invoke('addNotes', notes=notes1)
self.assertEqual(len(noteIds), len(notes)) self.assertEqual(len(noteIds), len(notes1))
for noteId in noteIds: for noteId in noteIds:
self.assertNotEqual(noteId, None) self.assertNotEqual(noteId, None)
# canAddNotes (part 2) # canAddNotes (part 2)
noteStates = util.invoke('canAddNotes', notes=notes) noteStates = util.invoke('canAddNotes', notes=notes2)
self.assertNotIn(True, noteStates) self.assertNotIn(True, noteStates)
self.assertEqual(len(noteStates), len(notes)) self.assertEqual(len(noteStates), len(notes2))
# addNotes (part 2) # addNotes (part 2)
noteIds = util.invoke('addNotes', notes=notes) noteIds = util.invoke('addNotes', notes=notes2)
self.assertEqual(len(noteIds), len(notes)) self.assertEqual(len(noteIds), len(notes2))
for noteId in noteIds: for noteId in noteIds:
self.assertEqual(noteId, None) self.assertEqual(noteId, None)
# findNotes # findNotes
noteIds = util.invoke('findNotes', query='deck:test') noteIds = util.invoke('findNotes', query='deck:test')
self.assertEqual(len(noteIds), len(notes) + 1) self.assertEqual(len(noteIds), len(notes1) + 1)
# deleteNotes # deleteNotes
util.invoke('deleteNotes', notes=noteIds) util.invoke('deleteNotes', notes=noteIds)

32
tests/test_stats.py Normal file → Executable file
View File

@ -9,7 +9,15 @@ import util
class TestStats(unittest.TestCase): class TestStats(unittest.TestCase):
def setUp(self): def setUp(self):
util.invoke('createDeck', deck='test') util.invoke('createDeck', deck='test')
note = {'deckName': 'test', 'modelName': 'Basic', 'fields': {'Front': 'front1', 'Back': 'back1'}, 'tags': ['tag1']} note = {
'deckName': 'test',
'modelName': 'Basic',
'fields': {'Front': 'front1', 'Back': 'back1'},
'tags': ['tag1'],
'options': {
'allowDuplicate': True
}
}
self.noteId = util.invoke('addNote', note=note) self.noteId = util.invoke('addNote', note=note)
def tearDown(self): def tearDown(self):
@ -25,18 +33,18 @@ class TestStats(unittest.TestCase):
self.assertIsInstance(result, str) self.assertIsInstance(result, str)
# no reviews for new deck # no reviews for new deck
self.assertEqual(len(util.invoke("cardReviews", deck="test", startID=0)), 0) self.assertEqual(len(util.invoke('cardReviews', deck='test', startID=0)), 0)
self.assertEqual(util.invoke("getLatestReviewID", deck="test"), 0) self.assertEqual(util.invoke('getLatestReviewID', deck='test'), 0)
# add reviews # # add reviews
cardId = int(util.invoke('findCards', query='deck:test')[0]) # cardId = int(util.invoke('findCards', query='deck:test')[0])
latestID = 123456 # small enough to not interfere with existing reviews # latestID = 123456 # small enough to not interfere with existing reviews
util.invoke("insertReviews", reviews=[ # util.invoke('insertReviews', reviews=[
[latestID-1, cardId, -1, 3, 4, -60, 2500, 6157, 0], # [latestID-1, cardId, -1, 3, 4, -60, 2500, 6157, 0],
[latestID, cardId, -1, 1, -60, -60, 0, 4846, 0] # [latestID, cardId, -1, 1, -60, -60, 0, 4846, 0]
]) # ])
self.assertEqual(len(util.invoke("cardReviews", deck="test", startID=0)), 2) # self.assertEqual(len(util.invoke('cardReviews', deck='test', startID=0)), 2)
self.assertEqual(util.invoke("getLatestReviewID", deck="test"), latestID) # self.assertEqual(util.invoke('getLatestReviewID', deck='test'), latestID)
if __name__ == '__main__': if __name__ == '__main__':