# Tai Sakuma <tai.sakuma@gmail.com>
import pytest

try:
    import unittest.mock as mock
except ImportError:
    import mock

from alphatwirl.concurrently import TaskPackageDropbox

##__________________________________________________________________||
@pytest.fixture()
def workingarea():
    return mock.MagicMock()

@pytest.fixture()
def dispatcher():
    return mock.MagicMock()

@pytest.fixture()
def obj(workingarea, dispatcher):
    ret = TaskPackageDropbox(workingArea=workingarea, dispatcher=dispatcher, sleep=0.01)
    ret.open()
    yield ret
    ret.close()

##__________________________________________________________________||
def test_repr(obj):
    repr(obj)

def test_open_terminate_close(workingarea, dispatcher):

    obj = TaskPackageDropbox(workingArea=workingarea, dispatcher=dispatcher, sleep=0.01)

    assert 0 == workingarea.open.call_count
    assert 0 == workingarea.close.call_count
    assert 0 == dispatcher.terminate.call_count

    obj.open()
    assert 1 == workingarea.open.call_count
    assert 0 == workingarea.close.call_count
    assert 0 == dispatcher.terminate.call_count

    obj.terminate()
    assert 1 == workingarea.open.call_count
    assert 0 == workingarea.close.call_count
    assert 1 == dispatcher.terminate.call_count

    obj.close()
    assert 1 == workingarea.open.call_count
    assert 1 == workingarea.close.call_count
    assert 1 == dispatcher.terminate.call_count

def test_put(obj, workingarea, dispatcher):

    workingarea.put_package.side_effect = [0, 1] # pkgidx
    dispatcher.run.side_effect = [1001, 1002] # runid

    package0 = mock.MagicMock(name='package0')
    package1 = mock.MagicMock(name='package1')

    assert 0 == obj.put(package0)
    assert 1 == obj.put(package1)

    assert [mock.call(package0), mock.call(package1)] == workingarea.put_package.call_args_list
    assert [mock.call(workingarea, 0), mock.call(workingarea, 1)] == dispatcher.run.call_args_list

def test_put_multiple(obj, workingarea, dispatcher):

    workingarea.put_package.side_effect = [0, 1] # pkgidx
    dispatcher.run_multiple.return_value = [1001, 1002] # runid

    package0 = mock.MagicMock(name='package0')
    package1 = mock.MagicMock(name='package1')

    assert [0, 1] == obj.put_multiple([package0, package1])

    assert [mock.call(package0), mock.call(package1)] == workingarea.put_package.call_args_list
    assert [mock.call(workingarea, [0, 1])] == dispatcher.run_multiple.call_args_list

##__________________________________________________________________||

[((254, 270), 'pytest.fixture', 'pytest.fixture', ([], {}), '()\n', (268, 270), False, 'import pytest\n'), ((320, 336), 'pytest.fixture', 'pytest.fixture', ([], {}), '()\n', (334, 336), False, 'import pytest\n'), ((385, 401), 'pytest.fixture', 'pytest.fixture', ([], {}), '()\n', (399, 401), False, 'import pytest\n'), ((301, 317), 'mock.MagicMock', 'mock.MagicMock', ([], {}), '()\n', (315, 317), False, 'import mock\n'), ((366, 382), 'mock.MagicMock', 'mock.MagicMock', ([], {}), '()\n', (380, 382), False, 'import mock\n'), ((446, 524), 'alphatwirl.concurrently.TaskPackageDropbox', 'TaskPackageDropbox', ([], {'workingArea': 'workingarea', 'dispatcher': 'dispatcher', 'sleep': '(0.01)'}), '(workingArea=workingarea, dispatcher=dispatcher, sleep=0.01)\n', (464, 524), False, 'from alphatwirl.concurrently import TaskPackageDropbox\n'), ((744, 822), 'alphatwirl.concurrently.TaskPackageDropbox', 'TaskPackageDropbox', ([], {'workingArea': 'workingarea', 'dispatcher': 'dispatcher', 'sleep': '(0.01)'}), '(workingArea=workingarea, dispatcher=dispatcher, sleep=0.01)\n', (762, 822), False, 'from alphatwirl.concurrently import TaskPackageDropbox\n'), ((1600, 1631), 'mock.MagicMock', 'mock.MagicMock', ([], {'name': '"""package0"""'}), "(name='package0')\n", (1614, 1631), False, 'import mock\n'), ((1647, 1678), 'mock.MagicMock', 'mock.MagicMock', ([], {'name': '"""package1"""'}), "(name='package1')\n", (1661, 1678), False, 'import mock\n'), ((2137, 2168), 'mock.MagicMock', 'mock.MagicMock', ([], {'name': '"""package0"""'}), "(name='package0')\n", (2151, 2168), False, 'import mock\n'), ((2184, 2215), 'mock.MagicMock', 'mock.MagicMock', ([], {'name': '"""package1"""'}), "(name='package1')\n", (2198, 2215), False, 'import mock\n'), ((1761, 1780), 'mock.call', 'mock.call', (['package0'], {}), '(package0)\n', (1770, 1780), False, 'import mock\n'), ((1782, 1801), 'mock.call', 'mock.call', (['package1'], {}), '(package1)\n', (1791, 1801), False, 'import mock\n'), ((1857, 1882), 'mock.call', 'mock.call', (['workingarea', '(0)'], {}), '(workingarea, 0)\n', (1866, 1882), False, 'import mock\n'), ((1884, 1909), 'mock.call', 'mock.call', (['workingarea', '(1)'], {}), '(workingarea, 1)\n', (1893, 1909), False, 'import mock\n'), ((2290, 2309), 'mock.call', 'mock.call', (['package0'], {}), '(package0)\n', (2299, 2309), False, 'import mock\n'), ((2311, 2330), 'mock.call', 'mock.call', (['package1'], {}), '(package1)\n', (2320, 2330), False, 'import mock\n'), ((2386, 2416), 'mock.call', 'mock.call', (['workingarea', '[0, 1]'], {}), '(workingarea, [0, 1])\n', (2395, 2416), False, 'import mock\n')]