import pytest
from unittest.mock import patch

import rfidsecuritysvc.model.guest as model
from rfidsecuritysvc.model.color import Color
from rfidsecuritysvc.model.guest import Guest
from rfidsecuritysvc.model.sound import Sound
from rfidsecuritysvc.exception import SoundNotFoundError


def test_Guest(assert_model, default_sound, default_color):
    assert_model(_model(1, 'first', 'last', default_sound, default_color),
                 Guest(1, 'first', 'last', default_sound, default_color))


@patch('rfidsecuritysvc.model.guest.table')
def test_get(table):
    table.get.return_value = _default().test_to_row()
    assert model.get(1) == _default()
    table.get.assert_called_once_with(1)


@patch('rfidsecuritysvc.model.guest.table')
def test_get_notfound(table):
    table.get.return_value = None
    assert model.get(1) is None
    table.get.assert_called_once_with(1)


@patch('rfidsecuritysvc.model.guest.table')
def test_list(table):
    table.list.return_value = [
        _default().test_to_row(),
        _default(2).test_to_row(),
    ]
    models = model.list()
    table.list.assert_called_once()
    assert models == [_default(), _default(2)]


@patch('rfidsecuritysvc.model.guest.table')
def test_list_noresults(table):
    table.list.return_value = []
    models = model.list()
    table.list.assert_called_once()
    assert models == []


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_create(table, sound, default_sound):
    sound.get.return_value = default_sound
    table.create.return_value = None
    assert model.create('first', 'last', default_sound.id, 0xABCDEF) is None
    sound.get.assert_called_once_with(default_sound.id)
    table.create.assert_called_once_with('first', 'last', default_sound.id, 0xABCDEF)


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_create_SoundNotFoundError(table, sound, default_sound):
    sound.get.return_value = None
    with pytest.raises(SoundNotFoundError):
        model.create('first', 'last', default_sound.id, 0xABCDEF)
    sound.get.assert_called_once_with(default_sound.id)
    table.create.assert_not_called()


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_create_no_prefs(table, sound, default_sound):
    table.create.return_value = None
    assert model.create('first', 'last', None, None) is None
    sound.get.assert_not_called()
    table.create.assert_called_once_with('first', 'last', None, None)


@patch('rfidsecuritysvc.model.guest.table')
def test_delete(table):
    table.delete.return_value = 1
    assert model.delete(1) == 1
    table.delete.assert_called_with(1)


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_update(table, sound, default_sound):
    sound.get.return_value = default_sound
    table.update.return_value = 1
    assert model.update(1, 'first', 'last', default_sound.id, 0xABCDEF) == 1
    sound.get.assert_called_once_with(default_sound.id)
    table.update.assert_called_once_with(1, 'first', 'last', default_sound.id, 0xABCDEF)


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_update_no_prefs(table, sound, default_sound):
    table.update.return_value = 1
    assert model.update(1, 'first', 'last', None, None) == 1
    sound.get.assert_not_called()
    table.update.assert_called_once_with(1, 'first', 'last', None, None)


@patch('rfidsecuritysvc.model.guest.sound_model')
@patch('rfidsecuritysvc.model.guest.table')
def test_update_SoundNotFoundError(table, sound, default_sound):
    table.update.return_value = 1
    sound.get.return_value = None
    with pytest.raises(SoundNotFoundError):
        model.update(1, 'first', 'last', default_sound.id, 0xABCDEF)
    sound.get.assert_called_once_with(default_sound.id)
    table.update.assert_not_called()


def test__model_no_color(creatable_guest):
    row = creatable_guest.test_to_row()
    row['color'] = None
    g = model.__model(row)
    assert g.color is None


def test__model_no_sound(creatable_guest):
    row = creatable_guest.test_to_row()
    row['sound'] = None
    g = model.__model(row)
    assert g.sound is None


def _default(index=1):
    return _model(index, f'first {index}', f'last {index}', Sound(index, f'sound_name {index}', '2021-09-25 23:13:25'), Color(0xABCDEF))


def _model(id, first_name, last_name, sound, color):
    return Guest(id, first_name, last_name, sound, color)

[((500, 542), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (505, 542), False, 'from unittest.mock import patch\n'), ((700, 742), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (705, 742), False, 'from unittest.mock import patch\n'), ((883, 925), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (888, 925), False, 'from unittest.mock import patch\n'), ((1167, 1209), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (1172, 1209), False, 'from unittest.mock import patch\n'), ((1364, 1412), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (1369, 1412), False, 'from unittest.mock import patch\n'), ((1414, 1456), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (1419, 1456), False, 'from unittest.mock import patch\n'), ((1805, 1853), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (1810, 1853), False, 'from unittest.mock import patch\n'), ((1855, 1897), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (1860, 1897), False, 'from unittest.mock import patch\n'), ((2203, 2251), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (2208, 2251), False, 'from unittest.mock import patch\n'), ((2253, 2295), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (2258, 2295), False, 'from unittest.mock import patch\n'), ((2556, 2598), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (2561, 2598), False, 'from unittest.mock import patch\n'), ((2731, 2779), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (2736, 2779), False, 'from unittest.mock import patch\n'), ((2781, 2823), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (2786, 2823), False, 'from unittest.mock import patch\n'), ((3172, 3220), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (3177, 3220), False, 'from unittest.mock import patch\n'), ((3222, 3264), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (3227, 3264), False, 'from unittest.mock import patch\n'), ((3525, 3573), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.sound_model"""'], {}), "('rfidsecuritysvc.model.guest.sound_model')\n", (3530, 3573), False, 'from unittest.mock import patch\n'), ((3575, 3617), 'unittest.mock.patch', 'patch', (['"""rfidsecuritysvc.model.guest.table"""'], {}), "('rfidsecuritysvc.model.guest.table')\n", (3580, 3617), False, 'from unittest.mock import patch\n'), ((1068, 1080), 'rfidsecuritysvc.model.guest.list', 'model.list', ([], {}), '()\n', (1078, 1080), True, 'import rfidsecuritysvc.model.guest as model\n'), ((1288, 1300), 'rfidsecuritysvc.model.guest.list', 'model.list', ([], {}), '()\n', (1298, 1300), True, 'import rfidsecuritysvc.model.guest as model\n'), ((4074, 4092), 'rfidsecuritysvc.model.guest.__model', 'model.__model', (['row'], {}), '(row)\n', (4087, 4092), True, 'import rfidsecuritysvc.model.guest as model\n'), ((4237, 4255), 'rfidsecuritysvc.model.guest.__model', 'model.__model', (['row'], {}), '(row)\n', (4250, 4255), True, 'import rfidsecuritysvc.model.guest as model\n'), ((4511, 4557), 'rfidsecuritysvc.model.guest.Guest', 'Guest', (['id', 'first_name', 'last_name', 'sound', 'color'], {}), '(id, first_name, last_name, sound, color)\n', (4516, 4557), False, 'from rfidsecuritysvc.model.guest import Guest\n'), ((440, 495), 'rfidsecuritysvc.model.guest.Guest', 'Guest', (['(1)', '"""first"""', '"""last"""', 'default_sound', 'default_color'], {}), "(1, 'first', 'last', default_sound, default_color)\n", (445, 495), False, 'from rfidsecuritysvc.model.guest import Guest\n'), ((629, 641), 'rfidsecuritysvc.model.guest.get', 'model.get', (['(1)'], {}), '(1)\n', (638, 641), True, 'import rfidsecuritysvc.model.guest as model\n'), ((818, 830), 'rfidsecuritysvc.model.guest.get', 'model.get', (['(1)'], {}), '(1)\n', (827, 830), True, 'import rfidsecuritysvc.model.guest as model\n'), ((1594, 1651), 'rfidsecuritysvc.model.guest.create', 'model.create', (['"""first"""', '"""last"""', 'default_sound.id', '(11259375)'], {}), "('first', 'last', default_sound.id, 11259375)\n", (1606, 1651), True, 'import rfidsecuritysvc.model.guest as model\n'), ((2006, 2039), 'pytest.raises', 'pytest.raises', (['SoundNotFoundError'], {}), '(SoundNotFoundError)\n', (2019, 2039), False, 'import pytest\n'), ((2049, 2106), 'rfidsecuritysvc.model.guest.create', 'model.create', (['"""first"""', '"""last"""', 'default_sound.id', '(11259375)'], {}), "('first', 'last', default_sound.id, 11259375)\n", (2061, 2106), True, 'import rfidsecuritysvc.model.guest as model\n'), ((2399, 2440), 'rfidsecuritysvc.model.guest.create', 'model.create', (['"""first"""', '"""last"""', 'None', 'None'], {}), "('first', 'last', None, None)\n", (2411, 2440), True, 'import rfidsecuritysvc.model.guest as model\n'), ((2668, 2683), 'rfidsecuritysvc.model.guest.delete', 'model.delete', (['(1)'], {}), '(1)\n', (2680, 2683), True, 'import rfidsecuritysvc.model.guest as model\n'), ((2958, 3018), 'rfidsecuritysvc.model.guest.update', 'model.update', (['(1)', '"""first"""', '"""last"""', 'default_sound.id', '(11259375)'], {}), "(1, 'first', 'last', default_sound.id, 11259375)\n", (2970, 3018), True, 'import rfidsecuritysvc.model.guest as model\n'), ((3365, 3409), 'rfidsecuritysvc.model.guest.update', 'model.update', (['(1)', '"""first"""', '"""last"""', 'None', 'None'], {}), "(1, 'first', 'last', None, None)\n", (3377, 3409), True, 'import rfidsecuritysvc.model.guest as model\n'), ((3760, 3793), 'pytest.raises', 'pytest.raises', (['SoundNotFoundError'], {}), '(SoundNotFoundError)\n', (3773, 3793), False, 'import pytest\n'), ((3803, 3863), 'rfidsecuritysvc.model.guest.update', 'model.update', (['(1)', '"""first"""', '"""last"""', 'default_sound.id', '(11259375)'], {}), "(1, 'first', 'last', default_sound.id, 11259375)\n", (3815, 3863), True, 'import rfidsecuritysvc.model.guest as model\n'), ((4368, 4426), 'rfidsecuritysvc.model.sound.Sound', 'Sound', (['index', 'f"""sound_name {index}"""', '"""2021-09-25 23:13:25"""'], {}), "(index, f'sound_name {index}', '2021-09-25 23:13:25')\n", (4373, 4426), False, 'from rfidsecuritysvc.model.sound import Sound\n'), ((4428, 4443), 'rfidsecuritysvc.model.color.Color', 'Color', (['(11259375)'], {}), '(11259375)\n', (4433, 4443), False, 'from rfidsecuritysvc.model.color import Color\n')]