71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
|
# -*- coding: utf-8; -*-
|
||
|
|
||
|
from wuttjamaican import batch as mod
|
||
|
|
||
|
try:
|
||
|
import sqlalchemy as sa
|
||
|
from wuttjamaican.db import model
|
||
|
from wuttjamaican.testing import DataTestCase
|
||
|
except ImportError:
|
||
|
pass
|
||
|
else:
|
||
|
|
||
|
class MockBatch(model.BatchMixin, model.Base):
|
||
|
__tablename__ = 'testing_batch_mock'
|
||
|
|
||
|
class MockBatchRow(model.BatchRowMixin, model.Base):
|
||
|
__tablename__ = 'testing_batch_mock_row'
|
||
|
__batch_class__ = MockBatch
|
||
|
|
||
|
class MockBatchHandler(mod.BatchHandler):
|
||
|
model_class = MockBatch
|
||
|
|
||
|
class TestBatchHandler(DataTestCase):
|
||
|
|
||
|
def make_handler(self, **kwargs):
|
||
|
return MockBatchHandler(self.config, **kwargs)
|
||
|
|
||
|
def test_model_class(self):
|
||
|
handler = mod.BatchHandler(self.config)
|
||
|
self.assertRaises(NotImplementedError, getattr, handler, 'model_class')
|
||
|
|
||
|
def test_make_batch(self):
|
||
|
handler = self.make_handler()
|
||
|
batch = handler.make_batch(self.session)
|
||
|
self.assertIsInstance(batch, MockBatch)
|
||
|
|
||
|
def test_consume_batch_id(self):
|
||
|
handler = self.make_handler()
|
||
|
|
||
|
first = handler.consume_batch_id(self.session)
|
||
|
second = handler.consume_batch_id(self.session)
|
||
|
self.assertEqual(second, first + 1)
|
||
|
|
||
|
third = handler.consume_batch_id(self.session, as_str=True)
|
||
|
self.assertEqual(third, f'{first + 2:08d}')
|
||
|
|
||
|
def test_should_populate(self):
|
||
|
handler = self.make_handler()
|
||
|
batch = handler.make_batch(self.session)
|
||
|
self.assertFalse(handler.should_populate(batch))
|
||
|
|
||
|
def test_do_populate(self):
|
||
|
handler = self.make_handler()
|
||
|
batch = handler.make_batch(self.session)
|
||
|
# nb. coverage only; tests nothing
|
||
|
handler.do_populate(batch)
|
||
|
|
||
|
def test_make_row(self):
|
||
|
handler = self.make_handler()
|
||
|
row = handler.make_row()
|
||
|
self.assertIsInstance(row, MockBatchRow)
|
||
|
|
||
|
def test_add_row(self):
|
||
|
handler = self.make_handler()
|
||
|
batch = handler.make_batch(self.session)
|
||
|
self.session.add(batch)
|
||
|
row = handler.make_row()
|
||
|
self.assertIsNone(batch.row_count)
|
||
|
handler.add_row(batch, row)
|
||
|
self.assertEqual(batch.row_count, 1)
|