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)

[((16, 1, 16, 43), 'unittest.mock.patch', 'patch', ({(16, 7, 16, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((23, 1, 23, 43), 'unittest.mock.patch', 'patch', ({(23, 7, 23, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((30, 1, 30, 43), 'unittest.mock.patch', 'patch', ({(30, 7, 30, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((41, 1, 41, 43), 'unittest.mock.patch', 'patch', ({(41, 7, 41, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((49, 1, 49, 49), 'unittest.mock.patch', 'patch', ({(49, 7, 49, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((50, 1, 50, 43), 'unittest.mock.patch', 'patch', ({(50, 7, 50, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((59, 1, 59, 49), 'unittest.mock.patch', 'patch', ({(59, 7, 59, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((60, 1, 60, 43), 'unittest.mock.patch', 'patch', ({(60, 7, 60, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((69, 1, 69, 49), 'unittest.mock.patch', 'patch', ({(69, 7, 69, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((70, 1, 70, 43), 'unittest.mock.patch', 'patch', ({(70, 7, 70, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((78, 1, 78, 43), 'unittest.mock.patch', 'patch', ({(78, 7, 78, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((85, 1, 85, 49), 'unittest.mock.patch', 'patch', ({(85, 7, 85, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((86, 1, 86, 43), 'unittest.mock.patch', 'patch', ({(86, 7, 86, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((95, 1, 95, 49), 'unittest.mock.patch', 'patch', ({(95, 7, 95, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((96, 1, 96, 43), 'unittest.mock.patch', 'patch', ({(96, 7, 96, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((104, 1, 104, 49), 'unittest.mock.patch', 'patch', ({(104, 7, 104, 48): '"""rfidsecuritysvc.model.guest.sound_model"""'}, {}), "('rfidsecuritysvc.model.guest.sound_model')", False, 'from unittest.mock import patch\n'), ((105, 1, 105, 43), 'unittest.mock.patch', 'patch', ({(105, 7, 105, 42): '"""rfidsecuritysvc.model.guest.table"""'}, {}), "('rfidsecuritysvc.model.guest.table')", False, 'from unittest.mock import patch\n'), ((36, 13, 36, 25), 'rfidsecuritysvc.model.guest.list', 'model.list', ({}, {}), '()', True, 'import rfidsecuritysvc.model.guest as model\n'), ((44, 13, 44, 25), 'rfidsecuritysvc.model.guest.list', 'model.list', ({}, {}), '()', True, 'import rfidsecuritysvc.model.guest as model\n'), ((118, 8, 118, 26), 'rfidsecuritysvc.model.guest.__model', 'model.__model', ({(118, 22, 118, 25): 'row'}, {}), '(row)', True, 'import rfidsecuritysvc.model.guest as model\n'), ((125, 8, 125, 26), 'rfidsecuritysvc.model.guest.__model', 'model.__model', ({(125, 22, 125, 25): 'row'}, {}), '(row)', True, 'import rfidsecuritysvc.model.guest as model\n'), ((134, 11, 134, 57), 'rfidsecuritysvc.model.guest.Guest', 'Guest', ({(134, 17, 134, 19): 'id', (134, 21, 134, 31): 'first_name', (134, 33, 134, 42): 'last_name', (134, 44, 134, 49): 'sound', (134, 51, 134, 56): 'color'}, {}), '(id, first_name, last_name, sound, color)', False, 'from rfidsecuritysvc.model.guest import Guest\n'), ((13, 17, 13, 72), 'rfidsecuritysvc.model.guest.Guest', 'Guest', ({(13, 23, 13, 24): '(1)', (13, 26, 13, 33): '"""first"""', (13, 35, 13, 41): '"""last"""', (13, 43, 13, 56): 'default_sound', (13, 58, 13, 71): 'default_color'}, {}), "(1, 'first', 'last', default_sound, default_color)", False, 'from rfidsecuritysvc.model.guest import Guest\n'), ((19, 11, 19, 23), 'rfidsecuritysvc.model.guest.get', 'model.get', ({(19, 21, 19, 22): '(1)'}, {}), '(1)', True, 'import rfidsecuritysvc.model.guest as model\n'), ((26, 11, 26, 23), 'rfidsecuritysvc.model.guest.get', 'model.get', ({(26, 21, 26, 22): '(1)'}, {}), '(1)', True, 'import rfidsecuritysvc.model.guest as model\n'), ((54, 11, 54, 68), 'rfidsecuritysvc.model.guest.create', 'model.create', ({(54, 24, 54, 31): '"""first"""', (54, 33, 54, 39): '"""last"""', (54, 41, 54, 57): 'default_sound.id', (54, 59, 54, 67): '(11259375)'}, {}), "('first', 'last', default_sound.id, 11259375)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((63, 9, 63, 42), 'pytest.raises', 'pytest.raises', ({(63, 23, 63, 41): 'SoundNotFoundError'}, {}), '(SoundNotFoundError)', False, 'import pytest\n'), ((64, 8, 64, 65), 'rfidsecuritysvc.model.guest.create', 'model.create', ({(64, 21, 64, 28): '"""first"""', (64, 30, 64, 36): '"""last"""', (64, 38, 64, 54): 'default_sound.id', (64, 56, 64, 64): '(11259375)'}, {}), "('first', 'last', default_sound.id, 11259375)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((73, 11, 73, 52), 'rfidsecuritysvc.model.guest.create', 'model.create', ({(73, 24, 73, 31): '"""first"""', (73, 33, 73, 39): '"""last"""', (73, 41, 73, 45): 'None', (73, 47, 73, 51): 'None'}, {}), "('first', 'last', None, None)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((81, 11, 81, 26), 'rfidsecuritysvc.model.guest.delete', 'model.delete', ({(81, 24, 81, 25): '(1)'}, {}), '(1)', True, 'import rfidsecuritysvc.model.guest as model\n'), ((90, 11, 90, 71), 'rfidsecuritysvc.model.guest.update', 'model.update', ({(90, 24, 90, 25): '(1)', (90, 27, 90, 34): '"""first"""', (90, 36, 90, 42): '"""last"""', (90, 44, 90, 60): 'default_sound.id', (90, 62, 90, 70): '(11259375)'}, {}), "(1, 'first', 'last', default_sound.id, 11259375)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((99, 11, 99, 55), 'rfidsecuritysvc.model.guest.update', 'model.update', ({(99, 24, 99, 25): '(1)', (99, 27, 99, 34): '"""first"""', (99, 36, 99, 42): '"""last"""', (99, 44, 99, 48): 'None', (99, 50, 99, 54): 'None'}, {}), "(1, 'first', 'last', None, None)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((109, 9, 109, 42), 'pytest.raises', 'pytest.raises', ({(109, 23, 109, 41): 'SoundNotFoundError'}, {}), '(SoundNotFoundError)', False, 'import pytest\n'), ((110, 8, 110, 68), 'rfidsecuritysvc.model.guest.update', 'model.update', ({(110, 21, 110, 22): '(1)', (110, 24, 110, 31): '"""first"""', (110, 33, 110, 39): '"""last"""', (110, 41, 110, 57): 'default_sound.id', (110, 59, 110, 67): '(11259375)'}, {}), "(1, 'first', 'last', default_sound.id, 11259375)", True, 'import rfidsecuritysvc.model.guest as model\n'), ((130, 60, 130, 118), 'rfidsecuritysvc.model.sound.Sound', 'Sound', ({(130, 66, 130, 71): 'index', (130, 73, 130, 94): 'f"""sound_name {index}"""', (130, 96, 130, 117): '"""2021-09-25 23:13:25"""'}, {}), "(index, f'sound_name {index}', '2021-09-25 23:13:25')", False, 'from rfidsecuritysvc.model.sound import Sound\n'), ((130, 120, 130, 135), 'rfidsecuritysvc.model.color.Color', 'Color', ({(130, 126, 130, 134): '(11259375)'}, {}), '(11259375)', False, 'from rfidsecuritysvc.model.color import Color\n')]