import pytest
from functions.hr_calculations import *


@pytest.mark.parametrize("candidate, database, expected", [
    ('jack', [{'patient_id': 'jump'}, {'patient_id': 'jack'}], 1),
    ('jungle', [{'patient_id': 'jungle'}, {'patient_id': 'jack'}], 0),
    ('bo', [{'patient_id': 'james'}, {'patient_id': 'boo'},
            {'patient_id': 'bo'}], 2)])
def test_find_id_ind(candidate, database, expected):

    # Run the test
    assert find_id_ind(candidate, database) == expected


@pytest.mark.parametrize("candidate, database, expected", [
    ('jump', [{'patient_id': 'jump', 'heart_rate': [50, 60, 70]},
              {'patient_id': 'jack', 'heart_rate': [120, 112, 131]}],
     [50, 60, 70]),
    ('jack', [{'patient_id': 'jump', 'heart_rate': [50, 60, 70]},
              {'patient_id': 'jack', 'heart_rate': [120, 112, 131]}],
     [120, 112, 131])
     ])
def test_get_heart_rates(candidate, database, expected):

    # Run the test
    assert get_heart_rates(candidate, database) == expected


@pytest.mark.parametrize("candidate, expected", [
    ([50, 60, 70], 60),
    ([50, 55, 56], 53.66),
    ([0, 50, 100], 50),
    ])
def test_average_heart_rate(candidate, expected):

    # Run the test
    assert pytest.approx(average_heart_rate(candidate), 1e-2) == expected


@pytest.mark.parametrize("candidate", [
    '2018-03-09 11:00:36.372339',
    '2017-10-19 15:11:36.167854',
    ])
def test_get_date_as_numeric(candidate):

    # Run the test
    result = pytest.approx(get_date_as_numeric(candidate), rel=1e-6)

    # Generate expected result
    expression = "%Y-%m-%d %H:%M:%S.%f"
    expected = datetime.strptime(candidate, expression).timestamp()

    assert result == expected


@pytest.mark.parametrize("candidate, expected", [
    ('Mark', ['2018-03-09 11:00:36.372339', '2017-10-19 15:11:36.167854']),
    ('Matt', ['2018-03-10 11:00:32.372339', '2017-10-19 35:11:36.167854'])
    ])
def test_get_times(candidate, expected):
    database = [{'patient_id': 'Mark', 'time':
                ['2018-03-09 11:00:36.372339', '2017-10-19 15:11:36.167854']},
                {'patient_id': 'Matt', 'time':
                ['2018-03-10 11:00:32.372339', '2017-10-19 35:11:36.167854']}]

    # Run the test
    assert get_times(candidate, database) == expected


@pytest.mark.parametrize("ref_time, times, hr, expected", [
    ('2018-03-09 11:00:36.372339',
     ['2018-03-09 11:00:34.372339',
      '2018-03-09 11:00:35.372339',
      '2018-03-09 11:00:36.872339'], [0, 0, 0], [0]),
    ('2018-03-09 11:00:36.372339',
     ['2018-03-09 11:00:35.372339',
      '2018-03-09 11:00:36.372359',
      '2018-03-09 11:00:37.372339'], [0, 0, 0], [0, 0])])
def test_hr_after_time(ref_time, times, hr, expected):

    # Run the test
    assert hr_after_time(ref_time, times, hr) == expected


@pytest.mark.parametrize("times, ref_time, expected", [
    ([0, 1, 2, 3, 4], 3, 4),
    ([0, 1, 2, 3, 4], 2.5, 3),
    ([0, 1, 2, 3, 4], 1.5, 2)
    ])
def test_find_index_larger(ref_time, times, expected):

    # Run the test
    assert find_index_larger(times, ref_time) == expected


@pytest.mark.parametrize("times, ref_time, expected", [
    ([0, 1, 2, 3, 4], 3, True),
    ([0, 1, 2, 3, 4], 4, True),
    ([0, 1, 2, 3, 4], 4.5, False),
    ([0, 1, 2, 3, 4], 0, True)
    ])
def test_check_recent_timestamps(ref_time, times, expected):

    # Run the test
    assert check_recent_timestamps(times, ref_time) == expected

[((5, 1, 9, 39), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(5, 25, 5, 56): '"""candidate, database, expected"""', (5, 58, 9, 38): "[('jack', [{'patient_id': 'jump'}, {'patient_id': 'jack'}], 1), ('jungle',\n    [{'patient_id': 'jungle'}, {'patient_id': 'jack'}], 0), ('bo', [{\n    'patient_id': 'james'}, {'patient_id': 'boo'}, {'patient_id': 'bo'}], 2)]"}, {}), "('candidate, database, expected', [('jack', [{\n    'patient_id': 'jump'}, {'patient_id': 'jack'}], 1), ('jungle', [{\n    'patient_id': 'jungle'}, {'patient_id': 'jack'}], 0), ('bo', [{\n    'patient_id': 'james'}, {'patient_id': 'boo'}, {'patient_id': 'bo'}], 2)])", False, 'import pytest\n'), ((16, 1, 23, 7), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(16, 25, 16, 56): '"""candidate, database, expected"""', (16, 58, 23, 6): "[('jump', [{'patient_id': 'jump', 'heart_rate': [50, 60, 70]}, {\n    'patient_id': 'jack', 'heart_rate': [120, 112, 131]}], [50, 60, 70]), (\n    'jack', [{'patient_id': 'jump', 'heart_rate': [50, 60, 70]}, {\n    'patient_id': 'jack', 'heart_rate': [120, 112, 131]}], [120, 112, 131])]"}, {}), "('candidate, database, expected', [('jump', [{\n    'patient_id': 'jump', 'heart_rate': [50, 60, 70]}, {'patient_id':\n    'jack', 'heart_rate': [120, 112, 131]}], [50, 60, 70]), ('jack', [{\n    'patient_id': 'jump', 'heart_rate': [50, 60, 70]}, {'patient_id':\n    'jack', 'heart_rate': [120, 112, 131]}], [120, 112, 131])])", False, 'import pytest\n'), ((30, 1, 34, 6), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(30, 25, 30, 46): '"""candidate, expected"""', (30, 48, 34, 5): '[([50, 60, 70], 60), ([50, 55, 56], 53.66), ([0, 50, 100], 50)]'}, {}), "('candidate, expected', [([50, 60, 70], 60), ([50, \n    55, 56], 53.66), ([0, 50, 100], 50)])", False, 'import pytest\n'), ((41, 1, 44, 6), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(41, 25, 41, 36): '"""candidate"""', (41, 38, 44, 5): "['2018-03-09 11:00:36.372339', '2017-10-19 15:11:36.167854']"}, {}), "('candidate', ['2018-03-09 11:00:36.372339',\n    '2017-10-19 15:11:36.167854'])", False, 'import pytest\n'), ((57, 1, 60, 6), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(57, 25, 57, 46): '"""candidate, expected"""', (57, 48, 60, 5): "[('Mark', ['2018-03-09 11:00:36.372339', '2017-10-19 15:11:36.167854']), (\n    'Matt', ['2018-03-10 11:00:32.372339', '2017-10-19 35:11:36.167854'])]"}, {}), "('candidate, expected', [('Mark', [\n    '2018-03-09 11:00:36.372339', '2017-10-19 15:11:36.167854']), ('Matt',\n    ['2018-03-10 11:00:32.372339', '2017-10-19 35:11:36.167854'])])", False, 'import pytest\n'), ((71, 1, 79, 57), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(71, 25, 71, 56): '"""ref_time, times, hr, expected"""', (71, 58, 79, 56): "[('2018-03-09 11:00:36.372339', ['2018-03-09 11:00:34.372339',\n    '2018-03-09 11:00:35.372339', '2018-03-09 11:00:36.872339'], [0, 0, 0],\n    [0]), ('2018-03-09 11:00:36.372339', ['2018-03-09 11:00:35.372339',\n    '2018-03-09 11:00:36.372359', '2018-03-09 11:00:37.372339'], [0, 0, 0],\n    [0, 0])]"}, {}), "('ref_time, times, hr, expected', [(\n    '2018-03-09 11:00:36.372339', ['2018-03-09 11:00:34.372339',\n    '2018-03-09 11:00:35.372339', '2018-03-09 11:00:36.872339'], [0, 0, 0],\n    [0]), ('2018-03-09 11:00:36.372339', ['2018-03-09 11:00:35.372339',\n    '2018-03-09 11:00:36.372359', '2018-03-09 11:00:37.372339'], [0, 0, 0],\n    [0, 0])])", False, 'import pytest\n'), ((86, 1, 90, 6), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(86, 25, 86, 52): '"""times, ref_time, expected"""', (86, 54, 90, 5): '[([0, 1, 2, 3, 4], 3, 4), ([0, 1, 2, 3, 4], 2.5, 3), ([0, 1, 2, 3, 4], 1.5, 2)]'}, {}), "('times, ref_time, expected', [([0, 1, 2, 3, 4], 3, \n    4), ([0, 1, 2, 3, 4], 2.5, 3), ([0, 1, 2, 3, 4], 1.5, 2)])", False, 'import pytest\n'), ((97, 1, 102, 6), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(97, 25, 97, 52): '"""times, ref_time, expected"""', (97, 54, 102, 5): '[([0, 1, 2, 3, 4], 3, True), ([0, 1, 2, 3, 4], 4, True), ([0, 1, 2, 3, 4], \n    4.5, False), ([0, 1, 2, 3, 4], 0, True)]'}, {}), "('times, ref_time, expected', [([0, 1, 2, 3, 4], 3, \n    True), ([0, 1, 2, 3, 4], 4, True), ([0, 1, 2, 3, 4], 4.5, False), ([0, \n    1, 2, 3, 4], 0, True)])", False, 'import pytest\n')]