Compare commits
3 commits
bfc45bd0f0
...
ad80bc8113
| Author | SHA1 | Date | |
|---|---|---|---|
| ad80bc8113 | |||
| d7d0768a9c | |||
| ae282ab468 |
26 changed files with 876 additions and 38 deletions
|
|
@ -5,6 +5,13 @@ All notable changes to WuttaSync will be documented in this file.
|
|||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## v0.6.0 (2026-03-17)
|
||||
|
||||
### Feat
|
||||
|
||||
- add concept of (non-)default importers for handler
|
||||
- add support for Wutta <-> Wutta import/export
|
||||
|
||||
## v0.5.1 (2026-02-13)
|
||||
|
||||
### Fix
|
||||
|
|
|
|||
6
docs/api/wuttasync.cli.export_wutta.rst
Normal file
6
docs/api/wuttasync.cli.export_wutta.rst
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
``wuttasync.cli.export_wutta``
|
||||
==============================
|
||||
|
||||
.. automodule:: wuttasync.cli.export_wutta
|
||||
:members:
|
||||
6
docs/api/wuttasync.cli.import_wutta.rst
Normal file
6
docs/api/wuttasync.cli.import_wutta.rst
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
``wuttasync.cli.import_wutta``
|
||||
==============================
|
||||
|
||||
.. automodule:: wuttasync.cli.import_wutta
|
||||
:members:
|
||||
6
docs/api/wuttasync.conf.rst
Normal file
6
docs/api/wuttasync.conf.rst
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
``wuttasync.conf``
|
||||
==================
|
||||
|
||||
.. automodule:: wuttasync.conf
|
||||
:members:
|
||||
|
|
@ -74,8 +74,11 @@ cf. :doc:`rattail-manual:data/sync/index`.
|
|||
api/wuttasync.cli
|
||||
api/wuttasync.cli.base
|
||||
api/wuttasync.cli.export_csv
|
||||
api/wuttasync.cli.export_wutta
|
||||
api/wuttasync.cli.import_csv
|
||||
api/wuttasync.cli.import_versions
|
||||
api/wuttasync.cli.import_wutta
|
||||
api/wuttasync.conf
|
||||
api/wuttasync.emails
|
||||
api/wuttasync.exporting
|
||||
api/wuttasync.exporting.base
|
||||
|
|
|
|||
|
|
@ -27,6 +27,18 @@ Defined in: :mod:`wuttasync.cli.export_csv`
|
|||
.. program-output:: wutta export-csv --help
|
||||
|
||||
|
||||
.. _wutta-export-wutta:
|
||||
|
||||
``wutta export-wutta``
|
||||
----------------------
|
||||
|
||||
Export data to another Wutta :term:`app database`, from the local one.
|
||||
|
||||
Defined in: :mod:`wuttasync.cli.export_wutta`
|
||||
|
||||
.. program-output:: wutta export-wutta --help
|
||||
|
||||
|
||||
.. _wutta-import-csv:
|
||||
|
||||
``wutta import-csv``
|
||||
|
|
@ -64,3 +76,15 @@ in the :term:`app model`.
|
|||
Defined in: :mod:`wuttasync.cli.import_versions`
|
||||
|
||||
.. program-output:: wutta import-versions --help
|
||||
|
||||
|
||||
.. _wutta-import-wutta:
|
||||
|
||||
``wutta import-wutta``
|
||||
----------------------
|
||||
|
||||
Import data from another Wutta :term:`app database`, to the local one.
|
||||
|
||||
Defined in: :mod:`wuttasync.cli.import_wutta`
|
||||
|
||||
.. program-output:: wutta import-wutta --help
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ build-backend = "hatchling.build"
|
|||
|
||||
[project]
|
||||
name = "WuttaSync"
|
||||
version = "0.5.1"
|
||||
version = "0.6.0"
|
||||
description = "Wutta Framework for data import/export and real-time sync"
|
||||
readme = "README.md"
|
||||
authors = [{name = "Lance Edgar", email = "lance@wuttaproject.org"}]
|
||||
|
|
@ -30,7 +30,7 @@ dependencies = [
|
|||
"makefun",
|
||||
"rich",
|
||||
"SQLAlchemy-Utils",
|
||||
"WuttJamaican[db]>=0.28.1",
|
||||
"WuttJamaican[db]>=0.28.10",
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -42,10 +42,15 @@ tests = ["pylint", "pytest", "pytest-cov", "tox", "Wutta-Continuum>=0.3.0"]
|
|||
[project.entry-points."wutta.app.providers"]
|
||||
wuttasync = "wuttasync.app:WuttaSyncAppProvider"
|
||||
|
||||
[project.entry-points."wutta.config.extensions"]
|
||||
"wuttasync" = "wuttasync.conf:WuttaSyncConfig"
|
||||
|
||||
[project.entry-points."wuttasync.importing"]
|
||||
"export.to_csv.from_wutta" = "wuttasync.exporting.csv:FromWuttaToCsv"
|
||||
"export.to_wutta.from_wutta" = "wuttasync.importing.wutta:FromWuttaToWuttaExport"
|
||||
"import.to_versions.from_wutta" = "wuttasync.importing.versions:FromWuttaToVersions"
|
||||
"import.to_wutta.from_csv" = "wuttasync.importing.csv:FromCsvToWutta"
|
||||
"import.to_wutta.from_wutta" = "wuttasync.importing.wutta:FromWuttaToWuttaImport"
|
||||
|
||||
[project.entry-points."wutta.typer_imports"]
|
||||
wuttasync = "wuttasync.cli"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2025 Lance Edgar
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
|
|
@ -87,18 +87,32 @@ class WuttaSyncAppProvider(AppProvider):
|
|||
|
||||
:returns: List of all import/export handler classes
|
||||
"""
|
||||
# first load all "registered" Handler classes
|
||||
factories = load_entry_points("wuttasync.importing", ignore_errors=True)
|
||||
# first load all "registered" Handler classes. note we must
|
||||
# specify lists=True since handlers from different projects
|
||||
# can be registered with the same key.
|
||||
factory_lists = load_entry_points(
|
||||
"wuttasync.importing", lists=True, ignore_errors=True
|
||||
)
|
||||
|
||||
# organize registered classes by spec
|
||||
specs = {factory.get_spec(): factory for factory in factories.values()}
|
||||
specs = {}
|
||||
all_factories = []
|
||||
for factories in factory_lists.values():
|
||||
for factory in factories:
|
||||
specs[factory.get_spec()] = factory
|
||||
all_factories.append(factory)
|
||||
|
||||
# many handlers may not be registered per se, but may be
|
||||
# designated via config. so try to include those too
|
||||
for factory in factories.values():
|
||||
spec = self.get_designated_import_handler_spec(factory.get_key())
|
||||
seen = set()
|
||||
for factory in all_factories:
|
||||
key = factory.get_key()
|
||||
if key in seen:
|
||||
continue
|
||||
spec = self.get_designated_import_handler_spec(key)
|
||||
if spec and spec not in specs:
|
||||
specs[spec] = self.app.load_object(spec)
|
||||
seen.add(key)
|
||||
|
||||
# flatten back to simple list of classes
|
||||
factories = list(specs.values())
|
||||
|
|
@ -203,22 +217,26 @@ class WuttaSyncAppProvider(AppProvider):
|
|||
:param require: Set this to true if you want an error raised
|
||||
when no handler is found.
|
||||
|
||||
:param \\**kwargs: Remaining kwargs are passed as-is to the
|
||||
handler constructor.
|
||||
|
||||
:returns: The import/export handler instance. If no handler
|
||||
is found, then ``None`` is returned, unless ``require``
|
||||
param is true, in which case error is raised.
|
||||
"""
|
||||
# first try to fetch the handler per designated spec
|
||||
spec = self.get_designated_import_handler_spec(key, **kwargs)
|
||||
spec = self.get_designated_import_handler_spec(key)
|
||||
if spec:
|
||||
factory = self.app.load_object(spec)
|
||||
return factory(self.config)
|
||||
return factory(self.config, **kwargs)
|
||||
|
||||
# nothing was designated, so leverage logic which already
|
||||
# sorts out which handler is "designated" for given key
|
||||
designated = self.get_designated_import_handlers()
|
||||
for handler in designated:
|
||||
if handler.get_key() == key:
|
||||
return handler
|
||||
factory = type(handler)
|
||||
return factory(self.config, **kwargs)
|
||||
|
||||
if require:
|
||||
raise ValueError(f"Cannot locate import handler for key: {key}")
|
||||
|
|
|
|||
|
|
@ -40,5 +40,7 @@ from .base import (
|
|||
|
||||
# nb. must bring in all modules for discovery to work
|
||||
from . import export_csv
|
||||
from . import export_wutta
|
||||
from . import import_csv
|
||||
from . import import_versions
|
||||
from . import import_wutta
|
||||
|
|
|
|||
|
|
@ -65,7 +65,13 @@ class ImportCommandHandler(GenericHandler):
|
|||
:param key: Optional :term:`import/export key` to use for handler
|
||||
lookup. Only used if ``import_handler`` param is not set.
|
||||
|
||||
Typical usage for custom commands will be to provide the spec::
|
||||
:param \\**kwargs: Remaining kwargs are passed as-is to the
|
||||
import/export handler constructor, i.e. when making the
|
||||
:attr:`import_handler`. Note that if the ``import_handler``
|
||||
*instance* is specified, these kwargs will be ignored.
|
||||
|
||||
Typical usage for custom commands will be to provide the spec
|
||||
(please note the *colon*)::
|
||||
|
||||
handler = ImportCommandHandler(
|
||||
config, "poser.importing.foo:FromFooToPoser"
|
||||
|
|
@ -81,6 +87,14 @@ class ImportCommandHandler(GenericHandler):
|
|||
See also
|
||||
:meth:`~wuttasync.app.WuttaSyncAppProvider.get_import_handler()`
|
||||
which does the lookup by key.
|
||||
|
||||
Additional kwargs may be specified as needed. Typically these
|
||||
should wind up as attributes on the import/export handler
|
||||
instance::
|
||||
|
||||
handler = ImportCommandHandler(
|
||||
config, "poser.importing.foo:FromFooToPoser", dbkey="remote"
|
||||
)
|
||||
"""
|
||||
|
||||
import_handler = None
|
||||
|
|
@ -89,20 +103,22 @@ class ImportCommandHandler(GenericHandler):
|
|||
invoked when command runs. See also :meth:`run()`.
|
||||
"""
|
||||
|
||||
def __init__(self, config, import_handler=None, key=None):
|
||||
def __init__(self, config, import_handler=None, key=None, **kwargs):
|
||||
super().__init__(config)
|
||||
|
||||
if import_handler:
|
||||
if isinstance(import_handler, ImportHandler):
|
||||
self.import_handler = import_handler
|
||||
elif callable(import_handler):
|
||||
self.import_handler = import_handler(self.config)
|
||||
self.import_handler = import_handler(self.config, **kwargs)
|
||||
else: # spec
|
||||
factory = self.app.load_object(import_handler)
|
||||
self.import_handler = factory(self.config)
|
||||
self.import_handler = factory(self.config, **kwargs)
|
||||
|
||||
elif key:
|
||||
self.import_handler = self.app.get_import_handler(key, require=True)
|
||||
self.import_handler = self.app.get_import_handler(
|
||||
key, require=True, **kwargs
|
||||
)
|
||||
|
||||
def run(self, ctx, progress=None): # pylint: disable=unused-argument
|
||||
"""
|
||||
|
|
@ -161,7 +177,7 @@ class ImportCommandHandler(GenericHandler):
|
|||
# sort out which models to process
|
||||
models = kw.pop("models", None)
|
||||
if not models:
|
||||
models = list(self.import_handler.importers)
|
||||
models = self.import_handler.get_default_importer_keys()
|
||||
log.debug(
|
||||
"%s %s for models: %s",
|
||||
self.import_handler.actioning,
|
||||
|
|
@ -180,13 +196,31 @@ class ImportCommandHandler(GenericHandler):
|
|||
|
||||
This is what happens when command line has ``--list-models``.
|
||||
"""
|
||||
sys.stdout.write("\nALL MODELS:\n")
|
||||
sys.stdout.write("==============================\n")
|
||||
for key in self.import_handler.importers:
|
||||
sys.stdout.write(key)
|
||||
all_keys = list(self.import_handler.importers)
|
||||
default_keys = [k for k in all_keys if self.import_handler.is_default(k)]
|
||||
extra_keys = [k for k in all_keys if k not in default_keys]
|
||||
|
||||
sys.stdout.write("\n")
|
||||
sys.stdout.write("==============================\n")
|
||||
sys.stdout.write(f"for {self.import_handler.get_title()}\n\n")
|
||||
sys.stdout.write(" DEFAULT MODELS:\n")
|
||||
sys.stdout.write("==============================\n")
|
||||
if default_keys:
|
||||
for key in default_keys:
|
||||
sys.stdout.write(f"{key}\n")
|
||||
else:
|
||||
sys.stdout.write("(none)\n")
|
||||
|
||||
sys.stdout.write("==============================\n")
|
||||
sys.stdout.write(" EXTRA MODELS:\n")
|
||||
sys.stdout.write("==============================\n")
|
||||
if extra_keys:
|
||||
for key in extra_keys:
|
||||
sys.stdout.write(f"{key}\n")
|
||||
else:
|
||||
sys.stdout.write("(none)\n")
|
||||
|
||||
sys.stdout.write("==============================\n")
|
||||
sys.stdout.write(f" for {self.import_handler.get_title()}\n\n")
|
||||
|
||||
|
||||
def import_command_template( # pylint: disable=unused-argument,too-many-arguments,too-many-positional-arguments,too-many-locals
|
||||
|
|
|
|||
53
src/wuttasync/cli/export_wutta.py
Normal file
53
src/wuttasync/cli/export_wutta.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
# Wutta Framework is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# Wutta Framework is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
"""
|
||||
See also: :ref:`wutta-export-wutta`
|
||||
"""
|
||||
|
||||
from typing_extensions import Annotated
|
||||
|
||||
import typer
|
||||
|
||||
from wuttjamaican.cli import wutta_typer
|
||||
|
||||
from .base import import_command, ImportCommandHandler
|
||||
|
||||
|
||||
@wutta_typer.command()
|
||||
@import_command
|
||||
def export_wutta(
|
||||
ctx: typer.Context,
|
||||
dbkey: Annotated[
|
||||
str,
|
||||
typer.Option(help="Config key for app db engine to be used as data target."),
|
||||
] = None,
|
||||
**kwargs,
|
||||
): # pylint: disable=unused-argument
|
||||
"""
|
||||
Export data to another Wutta DB
|
||||
"""
|
||||
config = ctx.parent.wutta_config
|
||||
handler = ImportCommandHandler(
|
||||
config, key="export.to_wutta.from_wutta", dbkey=ctx.params["dbkey"]
|
||||
)
|
||||
handler.run(ctx)
|
||||
53
src/wuttasync/cli/import_wutta.py
Normal file
53
src/wuttasync/cli/import_wutta.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
# Wutta Framework is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# Wutta Framework is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
"""
|
||||
See also: :ref:`wutta-import-wutta`
|
||||
"""
|
||||
|
||||
from typing_extensions import Annotated
|
||||
|
||||
import typer
|
||||
|
||||
from wuttjamaican.cli import wutta_typer
|
||||
|
||||
from .base import import_command, ImportCommandHandler
|
||||
|
||||
|
||||
@wutta_typer.command()
|
||||
@import_command
|
||||
def import_wutta(
|
||||
ctx: typer.Context,
|
||||
dbkey: Annotated[
|
||||
str,
|
||||
typer.Option(help="Config key for app db engine to be used as data source."),
|
||||
] = None,
|
||||
**kwargs,
|
||||
): # pylint: disable=unused-argument
|
||||
"""
|
||||
Import data from another Wutta DB
|
||||
"""
|
||||
config = ctx.parent.wutta_config
|
||||
handler = ImportCommandHandler(
|
||||
config, key="import.to_wutta.from_wutta", dbkey=ctx.params["dbkey"]
|
||||
)
|
||||
handler.run(ctx)
|
||||
50
src/wuttasync/conf.py
Normal file
50
src/wuttasync/conf.py
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
# Wutta Framework is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License as published by the Free
|
||||
# Software Foundation, either version 3 of the License, or (at your option) any
|
||||
# later version.
|
||||
#
|
||||
# Wutta Framework is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
# more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
################################################################################
|
||||
"""
|
||||
WuttaSync config extension
|
||||
"""
|
||||
|
||||
from wuttjamaican.conf import WuttaConfigExtension
|
||||
|
||||
|
||||
class WuttaSyncConfig(WuttaConfigExtension):
|
||||
"""
|
||||
Config extension for WuttaSync.
|
||||
|
||||
This just configures some default import/export handlers.
|
||||
"""
|
||||
|
||||
key = "wuttasync"
|
||||
|
||||
def configure(self, config): # pylint: disable=empty-docstring
|
||||
""" """
|
||||
|
||||
# default import/export handlers
|
||||
config.setdefault(
|
||||
"wuttasync.importing.import.to_wutta.from_wutta.default_handler",
|
||||
"wuttasync.importing.wutta:FromWuttaToWuttaImport",
|
||||
)
|
||||
config.setdefault(
|
||||
"wuttasync.importing.export.to_wutta.from_wutta.default_handler",
|
||||
"wuttasync.importing.wutta:FromWuttaToWuttaExport",
|
||||
)
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2025 Lance Edgar
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
|
|
@ -150,6 +150,14 @@ class ImportExportWarning(EmailSetting):
|
|||
}
|
||||
|
||||
|
||||
class export_to_wutta_from_wutta_warning( # pylint: disable=invalid-name
|
||||
ImportExportWarning
|
||||
):
|
||||
"""
|
||||
Diff warning for Wutta → Wutta export.
|
||||
"""
|
||||
|
||||
|
||||
class import_to_versions_from_wutta_warning( # pylint: disable=invalid-name
|
||||
ImportExportWarning
|
||||
):
|
||||
|
|
@ -164,3 +172,11 @@ class import_to_wutta_from_csv_warning( # pylint: disable=invalid-name
|
|||
"""
|
||||
Diff warning for CSV → Wutta import.
|
||||
"""
|
||||
|
||||
|
||||
class import_to_wutta_from_wutta_warning( # pylint: disable=invalid-name
|
||||
ImportExportWarning
|
||||
):
|
||||
"""
|
||||
Diff warning for Wutta → Wutta import.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -239,7 +239,7 @@ class FromCsvToSqlalchemyHandlerMixin:
|
|||
raise NotImplementedError
|
||||
|
||||
# TODO: pylint (correctly) flags this as duplicate code, matching
|
||||
# on the wuttasync.importing.versions module - should fix?
|
||||
# on the wuttasync.importing.versions/wutta module - should fix?
|
||||
def define_importers(self):
|
||||
"""
|
||||
This mixin overrides typical (manual) importer definition, and
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2025 Lance Edgar
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
|
|
@ -203,7 +203,12 @@ class ImportHandler( # pylint: disable=too-many-public-methods,too-many-instanc
|
|||
|
||||
def __init__(self, config, **kwargs):
|
||||
""" """
|
||||
super().__init__(config, **kwargs)
|
||||
super().__init__(config)
|
||||
|
||||
# callers can set any attrs they want
|
||||
for k, v in kwargs.items():
|
||||
setattr(self, k, v)
|
||||
|
||||
self.importers = self.define_importers()
|
||||
|
||||
def __str__(self):
|
||||
|
|
@ -366,7 +371,7 @@ class ImportHandler( # pylint: disable=too-many-public-methods,too-many-instanc
|
|||
changes = OrderedDict()
|
||||
|
||||
if not keys:
|
||||
keys = list(self.importers)
|
||||
keys = self.get_default_importer_keys()
|
||||
|
||||
success = False
|
||||
try:
|
||||
|
|
@ -650,6 +655,36 @@ class ImportHandler( # pylint: disable=too-many-public-methods,too-many-instanc
|
|||
"""
|
||||
return kwargs
|
||||
|
||||
def is_default(self, key):
|
||||
"""
|
||||
Return a boolean indicating whether the importer corresponding
|
||||
to ``key`` should be considered "default" - i.e. included as
|
||||
part of a typical "import all" job.
|
||||
|
||||
The default logic here returns ``True`` in all cases; subclass can
|
||||
override as needed.
|
||||
|
||||
:param key: Key indicating the importer.
|
||||
|
||||
:rtype: bool
|
||||
"""
|
||||
return True
|
||||
|
||||
def get_default_importer_keys(self):
|
||||
"""
|
||||
Return the list of importer keys which should be considered
|
||||
"default" - i.e. which should be included as part of a typical
|
||||
"import all" job.
|
||||
|
||||
This inspects :attr:`importers` and calls :meth:`is_default()`
|
||||
for each, to determine the result.
|
||||
|
||||
:returns: List of importer keys (strings).
|
||||
"""
|
||||
keys = list(self.importers)
|
||||
keys = [k for k in keys if self.is_default(k)]
|
||||
return keys
|
||||
|
||||
def process_changes(self, changes):
|
||||
"""
|
||||
Run post-processing operations on the given changes, if
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ class FromWuttaToVersions(FromWuttaHandler, ToWuttaHandler):
|
|||
return kwargs
|
||||
|
||||
# TODO: pylint (correctly) flags this as duplicate code, matching
|
||||
# on the wuttasync.importing.csv module - should fix?
|
||||
# on the wuttasync.importing.csv/wutta module - should fix?
|
||||
def define_importers(self):
|
||||
"""
|
||||
This overrides typical (manual) importer definition, instead
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
################################################################################
|
||||
#
|
||||
# WuttaSync -- Wutta Framework for data import/export and real-time sync
|
||||
# Copyright © 2024-2025 Lance Edgar
|
||||
# Copyright © 2024-2026 Lance Edgar
|
||||
#
|
||||
# This file is part of Wutta Framework.
|
||||
#
|
||||
|
|
@ -24,10 +24,172 @@
|
|||
Wutta → Wutta import/export
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
from sqlalchemy_utils.functions import get_primary_keys
|
||||
|
||||
from wuttjamaican.db.util import make_topo_sortkey
|
||||
|
||||
from .base import FromSqlalchemyMirror
|
||||
from .model import ToWutta
|
||||
from .handlers import FromWuttaHandler, ToWuttaHandler, Orientation
|
||||
|
||||
|
||||
class FromWuttaMirror(FromSqlalchemyMirror): # pylint: disable=abstract-method
|
||||
"""
|
||||
Base class for Wutta -> Wutta data importers.
|
||||
Base class for Wutta → Wutta data :term:`importers/exporters
|
||||
<importer>`.
|
||||
|
||||
This inherits from
|
||||
:class:`~wuttasync.importing.base.FromSqlalchemyMirror`.
|
||||
"""
|
||||
|
||||
|
||||
class FromWuttaToWuttaBase(FromWuttaHandler, ToWuttaHandler):
|
||||
"""
|
||||
Base class for Wutta → Wutta data :term:`import/export handlers
|
||||
<import handler>`.
|
||||
|
||||
This inherits from
|
||||
:class:`~wuttasync.importing.handlers.FromWuttaHandler` and
|
||||
:class:`~wuttasync.importing.handlers.ToWuttaHandler`.
|
||||
"""
|
||||
|
||||
dbkey = None
|
||||
"""
|
||||
Config key for the "other" (non-local) :term:`app database`.
|
||||
Depending on context this will represent either the source or
|
||||
target for import/export.
|
||||
"""
|
||||
|
||||
def get_target_model(self): # pylint: disable=missing-function-docstring
|
||||
return self.app.model
|
||||
|
||||
# TODO: pylint (correctly) flags this as duplicate code, matching
|
||||
# on the wuttasync.importing.csv/versions module - should fix?
|
||||
def define_importers(self):
|
||||
"""
|
||||
This overrides typical (manual) importer definition, and
|
||||
instead dynamically generates a set of importers, e.g. one per
|
||||
table in the target DB.
|
||||
|
||||
It does this by calling :meth:`make_importer_factory()` for
|
||||
each class found in the :term:`app model`.
|
||||
"""
|
||||
importers = {}
|
||||
model = self.get_target_model()
|
||||
|
||||
# pylint: disable=duplicate-code
|
||||
# mostly try to make an importer for every data model
|
||||
for name in dir(model):
|
||||
cls = getattr(model, name)
|
||||
if (
|
||||
isinstance(cls, type)
|
||||
and issubclass(cls, model.Base)
|
||||
and cls is not model.Base
|
||||
):
|
||||
importers[name] = self.make_importer_factory(cls, name)
|
||||
|
||||
# sort importers according to schema topography
|
||||
topo_sortkey = make_topo_sortkey(model)
|
||||
importers = OrderedDict(
|
||||
[(name, importers[name]) for name in sorted(importers, key=topo_sortkey)]
|
||||
)
|
||||
|
||||
return importers
|
||||
|
||||
def make_importer_factory(self, model_class, name):
|
||||
"""
|
||||
Generate and return a new :term:`importer` class, targeting
|
||||
the given :term:`data model` class.
|
||||
|
||||
The newly-created class will inherit from:
|
||||
|
||||
* :class:`FromWuttaMirror`
|
||||
* :class:`~wuttasync.importing.model.ToWutta`
|
||||
|
||||
:param model_class: A data model class.
|
||||
|
||||
:param name: The "model name" for the importer/exporter. New
|
||||
class name will be based on this, so e.g. ``Widget`` model
|
||||
name becomes ``WidgetImporter`` class name.
|
||||
|
||||
:returns: The new class, meant to process import/export
|
||||
targeting the given data model.
|
||||
"""
|
||||
return type(
|
||||
f"{name}Importer",
|
||||
(FromWuttaMirror, ToWutta),
|
||||
{
|
||||
"model_class": model_class,
|
||||
"key": list(get_primary_keys(model_class)),
|
||||
},
|
||||
)
|
||||
|
||||
def is_default(self, key):
|
||||
""" """
|
||||
special = [
|
||||
"Setting",
|
||||
"Role",
|
||||
"Permission",
|
||||
"User",
|
||||
"UserRole",
|
||||
"UserAPIToken",
|
||||
"Upgrade",
|
||||
]
|
||||
if key in special:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class FromWuttaToWuttaImport(FromWuttaToWuttaBase):
|
||||
"""
|
||||
Handler for Wutta (other) → Wutta (local) data import.
|
||||
|
||||
This inherits from :class:`FromWuttaToWuttaBase`.
|
||||
"""
|
||||
|
||||
orientation = Orientation.IMPORT
|
||||
""" """ # nb. suppress docs
|
||||
|
||||
def make_source_session(self):
|
||||
"""
|
||||
This makes a "normal" :term:`db session`, but will use the
|
||||
engine corresponding to the
|
||||
:attr:`~FromWuttaToWuttaBase.dbkey`.
|
||||
"""
|
||||
if (
|
||||
not self.dbkey
|
||||
or self.dbkey == "default"
|
||||
or self.dbkey not in self.config.appdb_engines
|
||||
):
|
||||
raise ValueError(f"dbkey is not valid: {self.dbkey}")
|
||||
engine = self.config.appdb_engines[self.dbkey]
|
||||
return self.app.make_session(bind=engine)
|
||||
|
||||
|
||||
class FromWuttaToWuttaExport(FromWuttaToWuttaBase):
|
||||
"""
|
||||
Handler for Wutta (local) → Wutta (other) data export.
|
||||
|
||||
This inherits from :class:`FromWuttaToWuttaBase`.
|
||||
"""
|
||||
|
||||
orientation = Orientation.EXPORT
|
||||
""" """ # nb. suppress docs
|
||||
|
||||
def make_target_session(self):
|
||||
"""
|
||||
This makes a "normal" :term:`db session`, but will use the
|
||||
engine corresponding to the
|
||||
:attr:`~FromWuttaToWuttaBase.dbkey`.
|
||||
"""
|
||||
if (
|
||||
not self.dbkey
|
||||
or self.dbkey == "default"
|
||||
or self.dbkey not in self.config.appdb_engines
|
||||
):
|
||||
raise ValueError(f"dbkey is not valid: {self.dbkey}")
|
||||
engine = self.config.appdb_engines[self.dbkey]
|
||||
return self.app.make_session(bind=engine)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
import inspect
|
||||
import sys
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch, Mock
|
||||
from unittest.mock import patch, Mock, call
|
||||
|
||||
from wuttasync.cli import base as mod
|
||||
from wuttjamaican.testing import DataTestCase
|
||||
|
|
@ -25,19 +25,55 @@ class TestImportCommandHandler(DataTestCase):
|
|||
# as spec
|
||||
handler = self.make_handler(import_handler=FromCsvToWutta.get_spec())
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertFalse(hasattr(handler.import_handler, "foo"))
|
||||
|
||||
# as spec, w/ kwargs
|
||||
handler = self.make_handler(import_handler=FromCsvToWutta.get_spec(), foo="bar")
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertTrue(hasattr(handler.import_handler, "foo"))
|
||||
self.assertEqual(handler.import_handler.foo, "bar")
|
||||
|
||||
# as factory
|
||||
handler = self.make_handler(import_handler=FromCsvToWutta)
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertFalse(hasattr(handler.import_handler, "foo"))
|
||||
|
||||
# as factory, w/ kwargs
|
||||
handler = self.make_handler(import_handler=FromCsvToWutta, foo="bar")
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertTrue(hasattr(handler.import_handler, "foo"))
|
||||
self.assertEqual(handler.import_handler.foo, "bar")
|
||||
|
||||
# as instance
|
||||
myhandler = FromCsvToWutta(self.config)
|
||||
handler = self.make_handler(import_handler=myhandler)
|
||||
self.assertIs(handler.import_handler, myhandler)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertFalse(hasattr(handler.import_handler, "foo"))
|
||||
|
||||
# as instance, w/ kwargs (which are ignored)
|
||||
myhandler = FromCsvToWutta(self.config)
|
||||
handler = self.make_handler(import_handler=myhandler, foo="bar")
|
||||
self.assertIs(handler.import_handler, myhandler)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertFalse(hasattr(handler.import_handler, "foo"))
|
||||
|
||||
# as key
|
||||
handler = self.make_handler(key="import.to_wutta.from_csv")
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertFalse(hasattr(handler.import_handler, "foo"))
|
||||
|
||||
# as key, w/ kwargs
|
||||
handler = self.make_handler(key="import.to_wutta.from_csv", foo="bar")
|
||||
self.assertIsInstance(handler.import_handler, FromCsvToWutta)
|
||||
self.assertFalse(hasattr(handler, "foo"))
|
||||
self.assertTrue(hasattr(handler.import_handler, "foo"))
|
||||
self.assertEqual(handler.import_handler.foo, "bar")
|
||||
|
||||
def test_run(self):
|
||||
handler = self.make_handler(
|
||||
|
|
@ -133,22 +169,60 @@ class TestImportCommandHandler(DataTestCase):
|
|||
params={},
|
||||
),
|
||||
)
|
||||
# self.assertRaises(FileNotFoundError, handler.run, ctx)
|
||||
handler.run(ctx)
|
||||
exit_.assert_not_called()
|
||||
|
||||
def test_list_models(self):
|
||||
|
||||
# CSV -> Wutta (all importers are default)
|
||||
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"))
|
||||
sys.stdout.write.assert_has_calls(
|
||||
[
|
||||
call("==============================\n"),
|
||||
call(" EXTRA MODELS:\n"),
|
||||
call("==============================\n"),
|
||||
call("(none)\n"),
|
||||
call("==============================\n"),
|
||||
]
|
||||
)
|
||||
|
||||
# Wutta -> Wutta (only Person importer is default)
|
||||
handler = self.make_handler(
|
||||
import_handler="wuttasync.importing.wutta:FromWuttaToWuttaImport"
|
||||
)
|
||||
with patch.object(mod, "sys") as sys:
|
||||
handler.list_models({})
|
||||
sys.stdout.write.assert_has_calls(
|
||||
[
|
||||
call("==============================\n"),
|
||||
call(" DEFAULT MODELS:\n"),
|
||||
call("==============================\n"),
|
||||
call("Person\n"),
|
||||
call("==============================\n"),
|
||||
call(" EXTRA MODELS:\n"),
|
||||
call("==============================\n"),
|
||||
]
|
||||
)
|
||||
|
||||
# again, but pretend there are no default importers
|
||||
with patch.object(handler.import_handler, "is_default", return_value=False):
|
||||
with patch.object(mod, "sys") as sys:
|
||||
handler.list_models({})
|
||||
sys.stdout.write.assert_has_calls(
|
||||
[
|
||||
call("==============================\n"),
|
||||
call(" DEFAULT MODELS:\n"),
|
||||
call("==============================\n"),
|
||||
call("(none)\n"),
|
||||
call("==============================\n"),
|
||||
call(" EXTRA MODELS:\n"),
|
||||
call("==============================\n"),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class TestImporterCommand(TestCase):
|
||||
|
|
|
|||
23
tests/cli/test_export_wutta.py
Normal file
23
tests/cli/test_export_wutta.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from wuttasync.cli import export_wutta as mod, ImportCommandHandler
|
||||
|
||||
|
||||
class TestExportWutta(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
params = {
|
||||
"dbkey": "another",
|
||||
"models": [],
|
||||
"create": True,
|
||||
"update": True,
|
||||
"delete": False,
|
||||
"dry_run": True,
|
||||
}
|
||||
ctx = MagicMock(params=params)
|
||||
with patch.object(ImportCommandHandler, "run") as run:
|
||||
mod.export_wutta(ctx)
|
||||
run.assert_called_once_with(ctx)
|
||||
23
tests/cli/test_import_wutta.py
Normal file
23
tests/cli/test_import_wutta.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from wuttasync.cli import import_wutta as mod, ImportCommandHandler
|
||||
|
||||
|
||||
class TestImportWutta(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
params = {
|
||||
"dbkey": "another",
|
||||
"models": [],
|
||||
"create": True,
|
||||
"update": True,
|
||||
"delete": False,
|
||||
"dry_run": True,
|
||||
}
|
||||
ctx = MagicMock(params=params)
|
||||
with patch.object(ImportCommandHandler, "run") as run:
|
||||
mod.import_wutta(ctx)
|
||||
run.assert_called_once_with(ctx)
|
||||
|
|
@ -7,6 +7,7 @@ from uuid import UUID
|
|||
from wuttjamaican.testing import DataTestCase
|
||||
|
||||
from wuttasync.importing import handlers as mod, Importer, ToSqlalchemy
|
||||
from wuttasync.importing.wutta import FromWuttaToWuttaImport
|
||||
|
||||
|
||||
class FromFooToBar(mod.ImportHandler):
|
||||
|
|
@ -19,6 +20,17 @@ class TestImportHandler(DataTestCase):
|
|||
def make_handler(self, **kwargs):
|
||||
return mod.ImportHandler(self.config, **kwargs)
|
||||
|
||||
def test_constructor(self):
|
||||
|
||||
# attr missing by default
|
||||
handler = self.make_handler()
|
||||
self.assertFalse(hasattr(handler, "some_foo_attr"))
|
||||
|
||||
# but constructor can set it
|
||||
handler = self.make_handler(some_foo_attr="bar")
|
||||
self.assertTrue(hasattr(handler, "some_foo_attr"))
|
||||
self.assertEqual(handler.some_foo_attr, "bar")
|
||||
|
||||
def test_str(self):
|
||||
handler = self.make_handler()
|
||||
self.assertEqual(str(handler), "None → None")
|
||||
|
|
@ -242,6 +254,25 @@ class TestImportHandler(DataTestCase):
|
|||
KeyError, handler.get_importer, "BunchOfNonsense", model_class=model.Setting
|
||||
)
|
||||
|
||||
def test_is_default(self):
|
||||
handler = self.make_handler()
|
||||
# nb. anything is considered default, by default
|
||||
self.assertTrue(handler.is_default("there_is_no_way_this_is_valid"))
|
||||
|
||||
def test_get_default_importer_keys(self):
|
||||
|
||||
# use handler which already has some non/default keys
|
||||
handler = FromWuttaToWuttaImport(self.config)
|
||||
|
||||
# it supports many importers
|
||||
self.assertIn("Person", handler.importers)
|
||||
self.assertIn("User", handler.importers)
|
||||
self.assertIn("Setting", handler.importers)
|
||||
|
||||
# but only Person is default
|
||||
keys = handler.get_default_importer_keys()
|
||||
self.assertEqual(keys, ["Person"])
|
||||
|
||||
def test_get_warnings_email_key(self):
|
||||
handler = FromFooToBar(self.config)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,134 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from wuttjamaican.testing import DataTestCase
|
||||
|
||||
from wuttasync.importing import wutta as mod
|
||||
from wuttasync.importing import ToWutta
|
||||
|
||||
|
||||
class TestFromWuttaMirror(DataTestCase):
|
||||
|
||||
def make_importer(self, **kwargs):
|
||||
return mod.FromWuttaMirror(self.config, **kwargs)
|
||||
|
||||
def test_basic(self):
|
||||
importer = self.make_importer()
|
||||
self.assertIsInstance(importer, mod.FromWuttaMirror)
|
||||
|
||||
|
||||
class TestFromWuttaToWuttaBase(DataTestCase):
|
||||
|
||||
def make_handler(self, **kwargs):
|
||||
return mod.FromWuttaToWuttaBase(self.config, **kwargs)
|
||||
|
||||
def test_dbkey(self):
|
||||
|
||||
# null by default
|
||||
handler = self.make_handler()
|
||||
self.assertIsNone(handler.dbkey)
|
||||
|
||||
# but caller can specify
|
||||
handler = self.make_handler(dbkey="another")
|
||||
self.assertEqual(handler.dbkey, "another")
|
||||
|
||||
def test_make_importer_factory(self):
|
||||
model = self.app.model
|
||||
handler = self.make_handler()
|
||||
|
||||
# returns a typical importer
|
||||
factory = handler.make_importer_factory(model.User, "User")
|
||||
self.assertTrue(issubclass(factory, mod.FromWuttaMirror))
|
||||
self.assertTrue(issubclass(factory, ToWutta))
|
||||
self.assertIs(factory.model_class, model.User)
|
||||
self.assertEqual(factory.__name__, "UserImporter")
|
||||
|
||||
def test_define_importers(self):
|
||||
handler = self.make_handler()
|
||||
|
||||
# all models are included
|
||||
importers = handler.define_importers()
|
||||
self.assertIn("Setting", importers)
|
||||
self.assertIn("Person", importers)
|
||||
self.assertIn("Role", importers)
|
||||
self.assertIn("Permission", importers)
|
||||
self.assertIn("User", importers)
|
||||
self.assertIn("UserRole", importers)
|
||||
self.assertIn("UserAPIToken", importers)
|
||||
self.assertIn("Upgrade", importers)
|
||||
self.assertNotIn("BatchMixin", importers)
|
||||
self.assertNotIn("BatchRowMixin", importers)
|
||||
self.assertNotIn("Base", importers)
|
||||
|
||||
# also, dependencies are implied by sort order
|
||||
models = list(importers)
|
||||
self.assertLess(models.index("Person"), models.index("User"))
|
||||
self.assertLess(models.index("User"), models.index("UserRole"))
|
||||
self.assertLess(models.index("User"), models.index("Upgrade"))
|
||||
|
||||
|
||||
class TestFromWuttaToWuttaImport(DataTestCase):
|
||||
|
||||
def make_handler(self, **kwargs):
|
||||
return mod.FromWuttaToWuttaImport(self.config, **kwargs)
|
||||
|
||||
def test_make_source_session(self):
|
||||
|
||||
# error if null dbkey
|
||||
handler = self.make_handler()
|
||||
self.assertIsNone(handler.dbkey)
|
||||
self.assertRaises(ValueError, handler.make_source_session)
|
||||
|
||||
# error if dbkey not found
|
||||
handler = self.make_handler(dbkey="another")
|
||||
self.assertEqual(handler.dbkey, "another")
|
||||
self.assertNotIn("another", self.config.appdb_engines)
|
||||
self.assertRaises(ValueError, handler.make_source_session)
|
||||
|
||||
# error if dbkey is 'default'
|
||||
handler = self.make_handler(dbkey="default")
|
||||
self.assertEqual(handler.dbkey, "default")
|
||||
self.assertIn("default", self.config.appdb_engines)
|
||||
self.assertRaises(ValueError, handler.make_source_session)
|
||||
|
||||
# expected behavior
|
||||
another_engine = sa.create_engine("sqlite://")
|
||||
handler = self.make_handler(dbkey="another")
|
||||
with patch.dict(self.config.appdb_engines, {"another": another_engine}):
|
||||
session = handler.make_source_session()
|
||||
self.assertIs(session.bind, another_engine)
|
||||
|
||||
|
||||
class TestFromWuttaToWuttaExport(DataTestCase):
|
||||
|
||||
def make_handler(self, **kwargs):
|
||||
return mod.FromWuttaToWuttaExport(self.config, **kwargs)
|
||||
|
||||
def test_make_target_session(self):
|
||||
|
||||
# error if null dbkey
|
||||
handler = self.make_handler()
|
||||
self.assertIsNone(handler.dbkey)
|
||||
self.assertRaises(ValueError, handler.make_target_session)
|
||||
|
||||
# error if dbkey not found
|
||||
handler = self.make_handler(dbkey="another")
|
||||
self.assertEqual(handler.dbkey, "another")
|
||||
self.assertNotIn("another", self.config.appdb_engines)
|
||||
self.assertRaises(ValueError, handler.make_target_session)
|
||||
|
||||
# error if dbkey is 'default'
|
||||
handler = self.make_handler(dbkey="default")
|
||||
self.assertEqual(handler.dbkey, "default")
|
||||
self.assertIn("default", self.config.appdb_engines)
|
||||
self.assertRaises(ValueError, handler.make_target_session)
|
||||
|
||||
# expected behavior
|
||||
another_engine = sa.create_engine("sqlite://")
|
||||
handler = self.make_handler(dbkey="another")
|
||||
with patch.dict(self.config.appdb_engines, {"another": another_engine}):
|
||||
session = handler.make_target_session()
|
||||
self.assertIs(session.bind, another_engine)
|
||||
|
|
|
|||
|
|
@ -46,6 +46,18 @@ class TestWuttaSyncAppProvider(ConfigTestCase):
|
|||
self.assertIn(FromCsvToWutta, handlers)
|
||||
self.assertIn(FromFooToBar, handlers)
|
||||
|
||||
# now for something completely different..here we pretend there
|
||||
# are multiple handler entry points with same key. all should
|
||||
# be returned, including both which share the key.
|
||||
entry_points = {
|
||||
"import.to_baz.from_foo": [FromFooToBaz1, FromFooToBaz2],
|
||||
}
|
||||
with patch.object(mod, "load_entry_points", return_value=entry_points):
|
||||
handlers = self.app.get_all_import_handlers()
|
||||
self.assertEqual(len(handlers), 2)
|
||||
self.assertIn(FromFooToBaz1, handlers)
|
||||
self.assertIn(FromFooToBaz2, handlers)
|
||||
|
||||
def test_get_designated_import_handler_spec(self):
|
||||
|
||||
# fetch of unknown key returns none
|
||||
|
|
@ -139,6 +151,14 @@ class TestWuttaSyncAppProvider(ConfigTestCase):
|
|||
handler = self.app.get_import_handler("import.to_wutta.from_csv")
|
||||
self.assertIsInstance(handler, FromCsvToWutta)
|
||||
self.assertIsInstance(handler, FromCsvToPoser)
|
||||
self.assertFalse(hasattr(handler, "foo_attr"))
|
||||
|
||||
# can pass extra kwargs
|
||||
handler = self.app.get_import_handler(
|
||||
"import.to_wutta.from_csv", foo_attr="whatever"
|
||||
)
|
||||
self.assertTrue(hasattr(handler, "foo_attr"))
|
||||
self.assertEqual(handler.foo_attr, "whatever")
|
||||
|
||||
# unknown importer cannot be found
|
||||
handler = self.app.get_import_handler("bogus")
|
||||
|
|
|
|||
39
tests/test_conf.py
Normal file
39
tests/test_conf.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
from wuttjamaican.testing import ConfigTestCase
|
||||
|
||||
from wuttasync import conf as mod
|
||||
|
||||
|
||||
class TestWuttaSyncConfig(ConfigTestCase):
|
||||
|
||||
def make_extension(self):
|
||||
return mod.WuttaSyncConfig()
|
||||
|
||||
def test_default_import_handlers(self):
|
||||
|
||||
# base config has no default handlers
|
||||
spec = self.config.get(
|
||||
"wuttasync.importing.import.to_wutta.from_wutta.default_handler"
|
||||
)
|
||||
self.assertIsNone(spec)
|
||||
spec = self.config.get(
|
||||
"wuttasync.importing.export.to_wutta.from_wutta.default_handler"
|
||||
)
|
||||
self.assertIsNone(spec)
|
||||
|
||||
# extend config
|
||||
ext = self.make_extension()
|
||||
ext.configure(self.config)
|
||||
|
||||
# config now has default handlers
|
||||
spec = self.config.get(
|
||||
"wuttasync.importing.import.to_wutta.from_wutta.default_handler"
|
||||
)
|
||||
self.assertIsNotNone(spec)
|
||||
self.assertEqual(spec, "wuttasync.importing.wutta:FromWuttaToWuttaImport")
|
||||
spec = self.config.get(
|
||||
"wuttasync.importing.export.to_wutta.from_wutta.default_handler"
|
||||
)
|
||||
self.assertIsNotNone(spec)
|
||||
self.assertEqual(spec, "wuttasync.importing.wutta:FromWuttaToWuttaExport")
|
||||
|
|
@ -5,6 +5,7 @@ from wuttjamaican.testing import ConfigTestCase
|
|||
from wuttasync import emails as mod
|
||||
from wuttasync.importing import ImportHandler
|
||||
from wuttasync.testing import ImportExportWarningTestCase
|
||||
from wuttasync.conf import WuttaSyncConfig
|
||||
|
||||
|
||||
class FromFooToWutta(ImportHandler):
|
||||
|
|
@ -74,8 +75,24 @@ class TestImportExportWarning(ConfigTestCase):
|
|||
|
||||
class TestEmailSettings(ImportExportWarningTestCase):
|
||||
|
||||
def make_config(self, files=None, **kwargs):
|
||||
config = super().make_config(files, **kwargs)
|
||||
|
||||
# need this to ensure default import/export handlers. since
|
||||
# behavior can vary depending on what packages are installed.
|
||||
ext = WuttaSyncConfig()
|
||||
ext.configure(config)
|
||||
|
||||
return config
|
||||
|
||||
def test_export_to_wutta_from_wutta_warning(self):
|
||||
self.do_test_preview("export_to_wutta_from_wutta_warning")
|
||||
|
||||
def test_import_to_versions_from_wutta_warning(self):
|
||||
self.do_test_preview("import_to_versions_from_wutta_warning")
|
||||
|
||||
def test_import_to_wutta_from_csv_warning(self):
|
||||
self.do_test_preview("import_to_wutta_from_csv_warning")
|
||||
|
||||
def test_import_to_wutta_from_wutta_warning(self):
|
||||
self.do_test_preview("import_to_wutta_from_wutta_warning")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue