fix: format all code with black
and from now on should not deviate from that...
This commit is contained in:
parent
45dabce956
commit
33ac0e008e
17 changed files with 730 additions and 576 deletions
|
@ -1,4 +1,4 @@
|
|||
#-*- coding: utf-8; -*-
|
||||
# -*- coding: utf-8; -*-
|
||||
|
||||
import csv
|
||||
import uuid as _uuid
|
||||
|
@ -6,7 +6,12 @@ from unittest.mock import patch
|
|||
|
||||
from wuttjamaican.testing import DataTestCase
|
||||
|
||||
from wuttasync.importing import csv as mod, ImportHandler, ToSqlalchemyHandler, ToSqlalchemy
|
||||
from wuttasync.importing import (
|
||||
csv as mod,
|
||||
ImportHandler,
|
||||
ToSqlalchemyHandler,
|
||||
ToSqlalchemy,
|
||||
)
|
||||
|
||||
|
||||
class TestFromCsv(DataTestCase):
|
||||
|
@ -15,14 +20,17 @@ class TestFromCsv(DataTestCase):
|
|||
self.setup_db()
|
||||
self.handler = ImportHandler(self.config)
|
||||
|
||||
self.data_path = self.write_file('data.txt', """\
|
||||
self.data_path = self.write_file(
|
||||
"data.txt",
|
||||
"""\
|
||||
name,value
|
||||
foo,bar
|
||||
foo2,bar2
|
||||
""")
|
||||
""",
|
||||
)
|
||||
|
||||
def make_importer(self, **kwargs):
|
||||
kwargs.setdefault('handler', self.handler)
|
||||
kwargs.setdefault("handler", self.handler)
|
||||
return mod.FromCsv(self.config, **kwargs)
|
||||
|
||||
def test_get_input_file_name(self):
|
||||
|
@ -30,39 +38,41 @@ foo2,bar2
|
|||
imp = self.make_importer(model_class=model.Setting)
|
||||
|
||||
# name can be guessed
|
||||
self.assertEqual(imp.get_input_file_name(), 'Setting.csv')
|
||||
self.assertEqual(imp.get_input_file_name(), "Setting.csv")
|
||||
|
||||
# name can be explicitly set
|
||||
imp.input_file_name = 'data.txt'
|
||||
self.assertEqual(imp.get_input_file_name(), 'data.txt')
|
||||
imp.input_file_name = "data.txt"
|
||||
self.assertEqual(imp.get_input_file_name(), "data.txt")
|
||||
|
||||
def test_open_input_file(self):
|
||||
model = self.app.model
|
||||
imp = self.make_importer(model_class=model.Setting)
|
||||
|
||||
# normal operation, input file includes all fields
|
||||
imp = self.make_importer(model_class=model.Setting, input_file_path=self.data_path)
|
||||
self.assertEqual(imp.fields, ['name', 'value'])
|
||||
imp = self.make_importer(
|
||||
model_class=model.Setting, input_file_path=self.data_path
|
||||
)
|
||||
self.assertEqual(imp.fields, ["name", "value"])
|
||||
imp.open_input_file()
|
||||
self.assertEqual(imp.input_file.name, self.data_path)
|
||||
self.assertIsInstance(imp.input_reader, csv.DictReader)
|
||||
self.assertEqual(imp.fields, ['name', 'value'])
|
||||
self.assertEqual(imp.fields, ["name", "value"])
|
||||
imp.input_file.close()
|
||||
|
||||
# this file is missing a field, plus we'll pretend more are
|
||||
# supported - but should wind up with just the one field
|
||||
missing = self.write_file('missing.txt', 'name')
|
||||
missing = self.write_file("missing.txt", "name")
|
||||
imp = self.make_importer(model_class=model.Setting, input_file_path=missing)
|
||||
imp.fields.extend(['lots', 'more'])
|
||||
self.assertEqual(imp.fields, ['name', 'value', 'lots', 'more'])
|
||||
imp.fields.extend(["lots", "more"])
|
||||
self.assertEqual(imp.fields, ["name", "value", "lots", "more"])
|
||||
imp.open_input_file()
|
||||
self.assertEqual(imp.fields, ['name'])
|
||||
self.assertEqual(imp.fields, ["name"])
|
||||
imp.input_file.close()
|
||||
|
||||
# and what happens when no known fields are found
|
||||
bogus = self.write_file('bogus.txt', 'blarg')
|
||||
bogus = self.write_file("bogus.txt", "blarg")
|
||||
imp = self.make_importer(model_class=model.Setting, input_file_path=bogus)
|
||||
self.assertEqual(imp.fields, ['name', 'value'])
|
||||
self.assertEqual(imp.fields, ["name", "value"])
|
||||
self.assertRaises(ValueError, imp.open_input_file)
|
||||
|
||||
def test_close_input_file(self):
|
||||
|
@ -72,8 +82,8 @@ foo2,bar2
|
|||
imp.input_file_path = self.data_path
|
||||
imp.open_input_file()
|
||||
imp.close_input_file()
|
||||
self.assertFalse(hasattr(imp, 'input_reader'))
|
||||
self.assertFalse(hasattr(imp, 'input_file'))
|
||||
self.assertFalse(hasattr(imp, "input_reader"))
|
||||
self.assertFalse(hasattr(imp, "input_file"))
|
||||
|
||||
def test_get_source_objects(self):
|
||||
model = self.app.model
|
||||
|
@ -84,8 +94,8 @@ foo2,bar2
|
|||
objects = imp.get_source_objects()
|
||||
imp.close_input_file()
|
||||
self.assertEqual(len(objects), 2)
|
||||
self.assertEqual(objects[0], {'name': 'foo', 'value': 'bar'})
|
||||
self.assertEqual(objects[1], {'name': 'foo2', 'value': 'bar2'})
|
||||
self.assertEqual(objects[0], {"name": "foo", "value": "bar"})
|
||||
self.assertEqual(objects[1], {"name": "foo2", "value": "bar2"})
|
||||
|
||||
|
||||
class MockMixinImporter(mod.FromCsvToSqlalchemyMixin, mod.FromCsv, ToSqlalchemy):
|
||||
|
@ -99,7 +109,7 @@ class TestFromCsvToSqlalchemyMixin(DataTestCase):
|
|||
self.handler = ImportHandler(self.config)
|
||||
|
||||
def make_importer(self, **kwargs):
|
||||
kwargs.setdefault('handler', self.handler)
|
||||
kwargs.setdefault("handler", self.handler)
|
||||
return MockMixinImporter(self.config, **kwargs)
|
||||
|
||||
def test_constructor(self):
|
||||
|
@ -112,31 +122,50 @@ class TestFromCsvToSqlalchemyMixin(DataTestCase):
|
|||
# typical
|
||||
# nb. as of now Upgrade is the only table using proper UUID
|
||||
imp = self.make_importer(model_class=model.Upgrade)
|
||||
self.assertEqual(imp.uuid_keys, ['uuid'])
|
||||
self.assertEqual(imp.uuid_keys, ["uuid"])
|
||||
|
||||
def test_normalize_source_object(self):
|
||||
model = self.app.model
|
||||
|
||||
# no uuid keys
|
||||
imp = self.make_importer(model_class=model.Setting)
|
||||
result = imp.normalize_source_object({'name': 'foo', 'value': 'bar'})
|
||||
self.assertEqual(result, {'name': 'foo', 'value': 'bar'})
|
||||
result = imp.normalize_source_object({"name": "foo", "value": "bar"})
|
||||
self.assertEqual(result, {"name": "foo", "value": "bar"})
|
||||
|
||||
# source has proper UUID
|
||||
# nb. as of now Upgrade is the only table using proper UUID
|
||||
imp = self.make_importer(model_class=model.Upgrade, fields=['uuid', 'description'])
|
||||
result = imp.normalize_source_object({'uuid': _uuid.UUID('06753693-d892-77f0-8000-ce71bf7ebbba'),
|
||||
'description': 'testing'})
|
||||
self.assertEqual(result, {'uuid': _uuid.UUID('06753693-d892-77f0-8000-ce71bf7ebbba'),
|
||||
'description': 'testing'})
|
||||
imp = self.make_importer(
|
||||
model_class=model.Upgrade, fields=["uuid", "description"]
|
||||
)
|
||||
result = imp.normalize_source_object(
|
||||
{
|
||||
"uuid": _uuid.UUID("06753693-d892-77f0-8000-ce71bf7ebbba"),
|
||||
"description": "testing",
|
||||
}
|
||||
)
|
||||
self.assertEqual(
|
||||
result,
|
||||
{
|
||||
"uuid": _uuid.UUID("06753693-d892-77f0-8000-ce71bf7ebbba"),
|
||||
"description": "testing",
|
||||
},
|
||||
)
|
||||
|
||||
# source has string uuid
|
||||
# nb. as of now Upgrade is the only table using proper UUID
|
||||
imp = self.make_importer(model_class=model.Upgrade, fields=['uuid', 'description'])
|
||||
result = imp.normalize_source_object({'uuid': '06753693d89277f08000ce71bf7ebbba',
|
||||
'description': 'testing'})
|
||||
self.assertEqual(result, {'uuid': _uuid.UUID('06753693-d892-77f0-8000-ce71bf7ebbba'),
|
||||
'description': 'testing'})
|
||||
imp = self.make_importer(
|
||||
model_class=model.Upgrade, fields=["uuid", "description"]
|
||||
)
|
||||
result = imp.normalize_source_object(
|
||||
{"uuid": "06753693d89277f08000ce71bf7ebbba", "description": "testing"}
|
||||
)
|
||||
self.assertEqual(
|
||||
result,
|
||||
{
|
||||
"uuid": _uuid.UUID("06753693-d892-77f0-8000-ce71bf7ebbba"),
|
||||
"description": "testing",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class MockMixinHandler(mod.FromCsvToSqlalchemyHandlerMixin, ToSqlalchemyHandler):
|
||||
|
@ -149,27 +178,33 @@ class TestFromCsvToSqlalchemyHandlerMixin(DataTestCase):
|
|||
return MockMixinHandler(self.config, **kwargs)
|
||||
|
||||
def test_get_target_model(self):
|
||||
with patch.object(mod.FromCsvToSqlalchemyHandlerMixin, 'define_importers', return_value={}):
|
||||
with patch.object(
|
||||
mod.FromCsvToSqlalchemyHandlerMixin, "define_importers", return_value={}
|
||||
):
|
||||
handler = self.make_handler()
|
||||
self.assertRaises(NotImplementedError, handler.get_target_model)
|
||||
|
||||
def test_define_importers(self):
|
||||
model = self.app.model
|
||||
with patch.object(mod.FromCsvToSqlalchemyHandlerMixin, 'get_target_model', return_value=model):
|
||||
with patch.object(
|
||||
mod.FromCsvToSqlalchemyHandlerMixin, "get_target_model", return_value=model
|
||||
):
|
||||
handler = self.make_handler()
|
||||
importers = handler.define_importers()
|
||||
self.assertIn('Setting', importers)
|
||||
self.assertTrue(issubclass(importers['Setting'], mod.FromCsv))
|
||||
self.assertTrue(issubclass(importers['Setting'], ToSqlalchemy))
|
||||
self.assertIn('User', importers)
|
||||
self.assertIn('Person', importers)
|
||||
self.assertIn('Role', importers)
|
||||
self.assertIn("Setting", importers)
|
||||
self.assertTrue(issubclass(importers["Setting"], mod.FromCsv))
|
||||
self.assertTrue(issubclass(importers["Setting"], ToSqlalchemy))
|
||||
self.assertIn("User", importers)
|
||||
self.assertIn("Person", importers)
|
||||
self.assertIn("Role", importers)
|
||||
|
||||
def test_make_importer_factory(self):
|
||||
model = self.app.model
|
||||
with patch.object(mod.FromCsvToSqlalchemyHandlerMixin, 'define_importers', return_value={}):
|
||||
with patch.object(
|
||||
mod.FromCsvToSqlalchemyHandlerMixin, "define_importers", return_value={}
|
||||
):
|
||||
handler = self.make_handler()
|
||||
factory = handler.make_importer_factory(model.Setting, 'Setting')
|
||||
factory = handler.make_importer_factory(model.Setting, "Setting")
|
||||
self.assertTrue(issubclass(factory, mod.FromCsv))
|
||||
self.assertTrue(issubclass(factory, ToSqlalchemy))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue