# -*- coding: utf-8; -*-

from __future__ import unicode_literals, absolute_import

from unittest import TestCase

import sqlalchemy as sa

from rattail.commands import batch as mod
from rattail.config import make_config
from rattail.commands import Command
from rattail.db import Session
from rattail.core import Object


class TestBatchHandlerCommand(TestCase):

    def setUp(self):
        self.config = self.make_config()
        self.command = self.make_command()
        self.subcommand = self.make_subcommand()

    def make_config(self):
        return make_config([], extend=False)

    def make_command(self):
        command = Command()
        command.config = self.config
        return command

    def make_subcommand(self):
        return mod.BatchHandlerCommand(parent=self.command,
                                       config=self.config)

    def test_get_handler(self):
        engine = sa.create_engine('sqlite://')
        model = self.config.get_model()
        model.Base.metadata.create_all(bind=engine)
        session = Session(bind=engine)

        # TODO: these things should self-register somehow instead
        self.config.setdefault('rattail.batch', 'handheld.handler',
                               'rattail.batch.handheld:HandheldBatchHandler')

        # subcommand makes its own handler, for e.g. handheld batch
        args = Object(batch_type='handheld')
        handler = self.subcommand.get_handler(args)
        self.assertIsNotNone(handler)
        self.assertEqual(handler.batch_key, 'handheld')
