Remove unnecessary class from helpers.server_utils

This commit is contained in:
flan 2017-11-04 00:47:34 +01:00
parent a48ad44a65
commit f5050582ba
3 changed files with 80 additions and 92 deletions

View File

@ -8,16 +8,11 @@ import tempfile
from ankisyncd.sync_app import SyncApp, SyncCollectionHandler, SyncMediaHandler from ankisyncd.sync_app import SyncApp, SyncCollectionHandler, SyncMediaHandler
class ServerUtils(object): def create_server_paths():
def clean_up(self):
shutil.rmtree(self.dir)
def create_server_paths(self):
""" """
Creates temporary files and dirs for our app to use during tests. Creates temporary files and dirs for our app to use during tests.
""" """
dir = tempfile.mkdtemp(prefix="ServerUtils") dir = tempfile.mkdtemp(prefix="ServerUtils")
self.dir = dir
os.mkdir(os.path.join(dir, "data")) os.mkdir(os.path.join(dir, "data"))
return { return {
@ -26,8 +21,7 @@ class ServerUtils(object):
"data_root": os.path.join(dir, "data"), "data_root": os.path.join(dir, "data"),
} }
@staticmethod def create_sync_app(server_paths, config_path):
def create_sync_app(server_paths, config_path):
config = ConfigParser.SafeConfigParser() config = ConfigParser.SafeConfigParser()
config.read(config_path) config.read(config_path)
@ -38,34 +32,34 @@ class ServerUtils(object):
return SyncApp(config) return SyncApp(config)
def get_session_for_hkey(self, server, hkey): def get_session_for_hkey(server, hkey):
return server.session_manager.load(hkey) return server.session_manager.load(hkey)
def get_thread_for_hkey(self, server, hkey): def get_thread_for_hkey(server, hkey):
session = self.get_session_for_hkey(server, hkey) session = get_session_for_hkey(server, hkey)
thread = session.get_thread() thread = session.get_thread()
return thread return thread
def get_col_wrapper_for_hkey(self, server, hkey): def get_col_wrapper_for_hkey(server, hkey):
print("getting col wrapper for hkey " + hkey) print("getting col wrapper for hkey " + hkey)
print("all session keys: " + str(server.session_manager.sessions.keys())) print("all session keys: " + str(server.session_manager.sessions.keys()))
thread = self.get_thread_for_hkey(server, hkey) thread = get_thread_for_hkey(server, hkey)
col_wrapper = thread.wrapper col_wrapper = thread.wrapper
return col_wrapper return col_wrapper
def get_col_for_hkey(self, server, hkey): def get_col_for_hkey(server, hkey):
col_wrapper = self.get_col_wrapper_for_hkey(server, hkey) col_wrapper = get_col_wrapper_for_hkey(server, hkey)
col_wrapper.open() # Make sure the col is opened. col_wrapper.open() # Make sure the col is opened.
return col_wrapper._CollectionWrapper__col return col_wrapper._CollectionWrapper__col
def get_col_db_path_for_hkey(self, server, hkey): def get_col_db_path_for_hkey(server, hkey):
col = self.get_col_for_hkey(server, hkey) col = get_col_for_hkey(server, hkey)
return col.db._path return col.db._path
def get_syncer_for_hkey(self, server, hkey, syncer_type='collection'): def get_syncer_for_hkey(server, hkey, syncer_type='collection'):
col = self.get_col_for_hkey(server, hkey) col = get_col_for_hkey(server, hkey)
session = self.get_session_for_hkey(server, hkey) session = get_session_for_hkey(server, hkey)
syncer_type = syncer_type.lower() syncer_type = syncer_type.lower()
if syncer_type == 'collection': if syncer_type == 'collection':
@ -75,11 +69,8 @@ class ServerUtils(object):
return session.get_handler_for_operation(handler_method, col) return session.get_handler_for_operation(handler_method, col)
def add_files_to_mediasyncer(self, def add_files_to_mediasyncer(media_syncer, filepaths,
media_syncer, update_db=False, bump_last_usn=False):
filepaths,
update_db=False,
bump_last_usn=False):
""" """
If bumpLastUsn is True, the media syncer's lastUsn will be incremented If bumpLastUsn is True, the media syncer's lastUsn will be incremented
once for each added file. Use this when adding files to the server. once for each added file. Use this when adding files to the server.

View File

@ -3,11 +3,11 @@ import os
import unittest import unittest
from webtest import TestApp from webtest import TestApp
import helpers.server_utils
from ankisyncd.users import SqliteUserManager from ankisyncd.users import SqliteUserManager
from helpers.collection_utils import CollectionUtils from helpers.collection_utils import CollectionUtils
from helpers.mock_servers import MockRemoteServer from helpers.mock_servers import MockRemoteServer
from helpers.monkey_patches import monkeypatch_db, unpatch_db from helpers.monkey_patches import monkeypatch_db, unpatch_db
from helpers.server_utils import ServerUtils
class SyncAppFunctionalTestBase(unittest.TestCase): class SyncAppFunctionalTestBase(unittest.TestCase):
@ -15,21 +15,17 @@ class SyncAppFunctionalTestBase(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.colutils = CollectionUtils() cls.colutils = CollectionUtils()
cls.serverutils = ServerUtils()
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
cls.colutils.clean_up() cls.colutils.clean_up()
cls.colutils = None cls.colutils = None
cls.serverutils.clean_up()
cls.serverutils = None
def setUp(self): def setUp(self):
monkeypatch_db() monkeypatch_db()
# Create temporary files and dirs the server will use. # Create temporary files and dirs the server will use.
self.server_paths = self.serverutils.create_server_paths() self.server_paths = helpers.server_utils.create_server_paths()
# Add a test user to the temp auth db the server will use. # Add a test user to the temp auth db the server will use.
self.user_manager = SqliteUserManager(self.server_paths['auth_db'], self.user_manager = SqliteUserManager(self.server_paths['auth_db'],
@ -44,7 +40,7 @@ class SyncAppFunctionalTestBase(unittest.TestCase):
# Create SyncApp instance using the dev ini file and the temporary # Create SyncApp instance using the dev ini file and the temporary
# paths. # paths.
self.server_app = self.serverutils.create_sync_app(self.server_paths, self.server_app = helpers.server_utils.create_sync_app(self.server_paths,
ini_file_path) ini_file_path)
# Wrap the SyncApp object in TestApp instance for testing. # Wrap the SyncApp object in TestApp instance for testing.

View File

@ -6,6 +6,7 @@ import os
import shutil import shutil
import helpers.file_utils import helpers.file_utils
import helpers.server_utils
import helpers.db_utils import helpers.db_utils
from anki.sync import MediaSyncer from anki.sync import MediaSyncer
from helpers.mock_servers import MockRemoteMediaServer from helpers.mock_servers import MockRemoteMediaServer
@ -94,7 +95,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
the identical file in their media directories and media databases. the identical file in their media directories and media databases.
""" """
client = self.client_syncer client = self.client_syncer
server = self.serverutils.get_syncer_for_hkey(self.server_app, server = helpers.server_utils.get_syncer_for_hkey(self.server_app,
self.hkey, self.hkey,
'media') 'media')
@ -102,7 +103,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello")
# Add the test file to the server's collection. # Add the test file to the server's collection.
self.serverutils.add_files_to_mediasyncer(server, helpers.server_utils.add_files_to_mediasyncer(server,
[temp_file_path], [temp_file_path],
update_db=True, update_db=True,
bump_last_usn=True) bump_last_usn=True)
@ -126,7 +127,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
""" """
join = os.path.join join = os.path.join
client = self.client_syncer client = self.client_syncer
server = self.serverutils.get_syncer_for_hkey(self.server_app, server = helpers.server_utils.get_syncer_for_hkey(self.server_app,
self.hkey, self.hkey,
'media') 'media')
@ -134,7 +135,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello")
# Add the test file to the client's media collection. # Add the test file to the client's media collection.
self.serverutils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[temp_file_path], [temp_file_path],
update_db=True, update_db=True,
bump_last_usn=False) bump_last_usn=False)
@ -165,7 +166,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
join = os.path.join join = os.path.join
isfile = os.path.isfile isfile = os.path.isfile
client = self.client_syncer client = self.client_syncer
server = self.serverutils.get_syncer_for_hkey(self.server_app, server = helpers.server_utils.get_syncer_for_hkey(self.server_app,
self.hkey, self.hkey,
'media') 'media')
@ -173,10 +174,10 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello") file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello")
file_for_server = helpers.file_utils.create_named_file(u"bar.jpg", "goodbye") file_for_server = helpers.file_utils.create_named_file(u"bar.jpg", "goodbye")
self.serverutils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[file_for_client], [file_for_client],
update_db=True) update_db=True)
self.serverutils.add_files_to_mediasyncer(server, helpers.server_utils.add_files_to_mediasyncer(server,
[file_for_server], [file_for_server],
update_db=True, update_db=True,
bump_last_usn=True) bump_last_usn=True)
@ -212,7 +213,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
join = os.path.join join = os.path.join
isfile = os.path.isfile isfile = os.path.isfile
client = self.client_syncer client = self.client_syncer
server = self.serverutils.get_syncer_for_hkey(self.server_app, server = helpers.server_utils.get_syncer_for_hkey(self.server_app,
self.hkey, self.hkey,
'media') 'media')
@ -221,10 +222,10 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello") file_for_client = helpers.file_utils.create_named_file(u"foo.jpg", "hello")
file_for_server = helpers.file_utils.create_named_file(u"foo.jpg", "goodbye") file_for_server = helpers.file_utils.create_named_file(u"foo.jpg", "goodbye")
self.serverutils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[file_for_client], [file_for_client],
update_db=True) update_db=True)
self.serverutils.add_files_to_mediasyncer(server, helpers.server_utils.add_files_to_mediasyncer(server,
[file_for_server], [file_for_server],
update_db=True, update_db=True,
bump_last_usn=True) bump_last_usn=True)
@ -258,7 +259,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
join = os.path.join join = os.path.join
isfile = os.path.isfile isfile = os.path.isfile
client = self.client_syncer client = self.client_syncer
server = self.serverutils.get_syncer_for_hkey(self.server_app, server = helpers.server_utils.get_syncer_for_hkey(self.server_app,
self.hkey, self.hkey,
'media') 'media')
@ -266,7 +267,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello") temp_file_path = helpers.file_utils.create_named_file(u"foo.jpg", "hello")
# Add the test file to client's media collection. # Add the test file to client's media collection.
self.serverutils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[temp_file_path], [temp_file_path],
update_db=True, update_db=True,
bump_last_usn=False) bump_last_usn=False)
@ -313,7 +314,7 @@ class SyncAppFunctionalMediaTest(SyncAppFunctionalTestBase):
# findChanges(), only during syncs. # findChanges(), only during syncs.
support_file = helpers.file_utils.get_asset_path(u'blue.jpg') support_file = helpers.file_utils.get_asset_path(u'blue.jpg')
self.assertTrue(os.path.isfile(support_file)) self.assertTrue(os.path.isfile(support_file))
self.serverutils.add_files_to_mediasyncer(client, helpers.server_utils.add_files_to_mediasyncer(client,
[support_file], [support_file],
update_db=False) update_db=False)