Added unit test for SyncCollectionHandler. Moved CollectionTestBase to a separate file.
This commit is contained in:
parent
99adc658d5
commit
6e881bfb85
54
tests/CollectionTestBase.py
Normal file
54
tests/CollectionTestBase.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
from mock import MagicMock
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
import anki
|
||||||
|
import anki.storage
|
||||||
|
|
||||||
|
|
||||||
|
class CollectionTestBase(unittest.TestCase):
|
||||||
|
"""Parent class for tests that need a collection set up and torn down."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.temp_dir = tempfile.mkdtemp()
|
||||||
|
self.collection_path = os.path.join(self.temp_dir, 'collection.anki2');
|
||||||
|
self.collection = anki.storage.Collection(self.collection_path)
|
||||||
|
self.mock_app = MagicMock()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.collection.close()
|
||||||
|
self.collection = None
|
||||||
|
shutil.rmtree(self.temp_dir)
|
||||||
|
self.mock_app.reset_mock()
|
||||||
|
|
||||||
|
# TODO: refactor into some kind of utility
|
||||||
|
def add_note(self, data):
|
||||||
|
from anki.notes import Note
|
||||||
|
|
||||||
|
model = self.collection.models.byName(data['model'])
|
||||||
|
|
||||||
|
note = Note(self.collection, model)
|
||||||
|
for name, value in data['fields'].items():
|
||||||
|
note[name] = value
|
||||||
|
|
||||||
|
if data.has_key('tags'):
|
||||||
|
note.setTagsFromStr(data['tags'])
|
||||||
|
|
||||||
|
self.collection.addNote(note)
|
||||||
|
|
||||||
|
# TODO: refactor into a parent class
|
||||||
|
def add_default_note(self, count=1):
|
||||||
|
data = {
|
||||||
|
'model': 'Basic',
|
||||||
|
'fields': {
|
||||||
|
'Front': 'The front',
|
||||||
|
'Back': 'The back',
|
||||||
|
},
|
||||||
|
'tags': "Tag1 Tag2",
|
||||||
|
}
|
||||||
|
for idx in range(0, count):
|
||||||
|
self.add_note(data)
|
||||||
56
tests/test_sync_app.py
Normal file
56
tests/test_sync_app.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import ankisyncd
|
||||||
|
from ankisyncd.sync_app import SyncCollectionHandler
|
||||||
|
from ankisyncd.sync_app import SyncApp
|
||||||
|
|
||||||
|
from CollectionTestBase import CollectionTestBase
|
||||||
|
|
||||||
|
|
||||||
|
class SyncCollectionHandlerTest(CollectionTestBase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
CollectionTestBase.setUp(self)
|
||||||
|
self.syncCollectionHandler = SyncCollectionHandler(self.collection)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
CollectionTestBase.tearDown(self)
|
||||||
|
self.syncCollectionHandler = None
|
||||||
|
|
||||||
|
def test_meta(self):
|
||||||
|
version_info = (None,
|
||||||
|
','.join(('ankidesktop', '2.0.12', 'lin::')),
|
||||||
|
','.join(('ankidesktop', '2.0.32', 'lin::')))
|
||||||
|
|
||||||
|
meta = self.syncCollectionHandler.meta(version_info[0])
|
||||||
|
self.assertEqual(meta[0], self.collection.mod)
|
||||||
|
self.assertEqual(meta[1], self.collection.scm)
|
||||||
|
self.assertEqual(meta[2], self.collection._usn)
|
||||||
|
self.assertTrue((type(meta[3]) == int) and meta[3] > 0)
|
||||||
|
self.assertEqual(meta[4], self.collection.media.usn())
|
||||||
|
|
||||||
|
meta = self.syncCollectionHandler.meta(version_info[1])
|
||||||
|
self.assertEqual(meta[0], self.collection.mod)
|
||||||
|
self.assertEqual(meta[1], self.collection.scm)
|
||||||
|
self.assertEqual(meta[2], self.collection._usn)
|
||||||
|
self.assertTrue((type(meta[3]) == int) and meta[3] > 0)
|
||||||
|
self.assertEqual(meta[4], self.collection.media.usn())
|
||||||
|
|
||||||
|
meta = self.syncCollectionHandler.meta(version_info[2])
|
||||||
|
self.assertEqual(meta['scm'], self.collection.scm)
|
||||||
|
self.assertTrue((type(meta['ts']) == int) and meta['ts'] > 0)
|
||||||
|
self.assertEqual(meta['mod'], self.collection.mod)
|
||||||
|
self.assertEqual(meta['usn'], self.collection._usn)
|
||||||
|
self.assertEqual(meta['musn'], self.collection.media.usn())
|
||||||
|
self.assertEqual(meta['msg'], '')
|
||||||
|
self.assertEqual(meta['cont'], True)
|
||||||
|
|
||||||
|
|
||||||
|
class SyncAppTest(unittest.TestCase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue
Block a user