fix: add --list-models option for import/export commands

also rename the command decorators for consistency
This commit is contained in:
Lance Edgar 2024-12-06 09:06:45 -06:00
parent 7ee551d446
commit 15b2cb07ba
8 changed files with 230 additions and 47 deletions

View file

@ -2,8 +2,59 @@
import inspect
from unittest import TestCase
from unittest.mock import patch
from wuttasync.cli import base as mod
from wuttjamaican.testing import DataTestCase
class TestImportCommandHandler(DataTestCase):
def make_handler(self, **kwargs):
return mod.ImportCommandHandler(self.config, **kwargs)
def test_import_handler(self):
# none
handler = self.make_handler()
self.assertIsNone(handler.import_handler)
FromCsvToWutta = self.app.load_object('wuttasync.importing.csv:FromCsvToWutta')
# as spec
handler = self.make_handler(import_handler=FromCsvToWutta.get_spec())
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
# as factory
handler = self.make_handler(import_handler=FromCsvToWutta)
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
# as instance
myhandler = FromCsvToWutta(self.config)
handler = self.make_handler(import_handler=myhandler)
self.assertIs(handler.import_handler, myhandler)
def test_run(self):
handler = self.make_handler(import_handler='wuttasync.importing.csv:FromCsvToWutta')
with patch.object(handler, 'list_models') as list_models:
handler.run({'list_models': True})
list_models.assert_called_once_with({'list_models': True})
with patch.object(handler, 'import_handler') as import_handler:
handler.run({'models': []})
import_handler.process_data.assert_called_once_with()
def test_list_models(self):
handler = self.make_handler(import_handler='wuttasync.importing.csv:FromCsvToWutta')
with patch.object(mod, 'sys') as sys:
handler.list_models({})
# just test a few random things we expect to see
self.assertTrue(sys.stdout.write.has_call('ALL MODELS:\n'))
self.assertTrue(sys.stdout.write.has_call('Person'))
self.assertTrue(sys.stdout.write.has_call('User'))
self.assertTrue(sys.stdout.write.has_call('Upgrade'))
class TestImporterCommand(TestCase):
@ -15,7 +66,7 @@ class TestImporterCommand(TestCase):
sig1 = inspect.signature(myfunc)
self.assertIn('kwargs', sig1.parameters)
self.assertNotIn('dry_run', sig1.parameters)
wrapt = mod.importer_command(myfunc)
wrapt = mod.import_command(myfunc)
sig2 = inspect.signature(wrapt)
self.assertNotIn('kwargs', sig2.parameters)
self.assertIn('dry_run', sig2.parameters)
@ -31,7 +82,7 @@ class TestFileImporterCommand(TestCase):
self.assertIn('kwargs', sig1.parameters)
self.assertNotIn('dry_run', sig1.parameters)
self.assertNotIn('input_file_path', sig1.parameters)
wrapt = mod.file_importer_command(myfunc)
wrapt = mod.file_import_command(myfunc)
sig2 = inspect.signature(wrapt)
self.assertNotIn('kwargs', sig2.parameters)
self.assertIn('dry_run', sig2.parameters)

View file

@ -1,24 +1,19 @@
#-*- coding: utf-8; -*-
import os
from unittest import TestCase
from unittest.mock import MagicMock, patch
from wuttasync.cli import import_csv as mod
from wuttasync.importing.csv import FromCsvToWutta
from wuttasync.cli import import_csv as mod, ImportCommandHandler
here = os.path.dirname(__file__)
example_conf = os.path.join(here, 'example.conf')
class TestImportCsv(TestCase):
def test_basic(self):
ctx = MagicMock(params={'models': [],
'create': True, 'update': True, 'delete': False,
'dry_run': True})
with patch.object(FromCsvToWutta, 'process_data') as process_data:
params = {'models': [],
'create': True, 'update': True, 'delete': False,
'dry_run': True}
ctx = MagicMock(params=params)
with patch.object(ImportCommandHandler, 'run') as run:
mod.import_csv(ctx)
process_data.assert_called_once_with(create=True, update=True, delete=False,
dry_run=True)
run.assert_called_once_with(params)