# Copyright © 2020 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test Suite to ensure the PPR Search Query schema is valid.

"""
import copy

from registry_schemas import validate
from registry_schemas.example_data.ppr import SEARCH_QUERY


def test_valid_search_query_ind_debtor():
    """Assert that the schema is performing as expected for a search by individual debtor."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'INDIVIDUAL_DEBTOR'
    del query['criteria']['debtorName']['business']
    del query['criteria']['value']
    del query['clientReferenceId']
    del query['startDateTime']
    del query['endDateTime']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_valid_search_query_bus_debtor():
    """Assert that the schema is performing as expected for a search by business debtor."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'BUSINESS_DEBTOR'
    del query['criteria']['debtorName']['first']
    del query['criteria']['debtorName']['second']
    del query['criteria']['debtorName']['last']
    del query['criteria']['value']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_valid_search_query_airdot():
    """Assert that the schema is performing as expected for a search by aircraft DOT."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'AIRCRAFT_DOT'
    del query['criteria']['debtorName']
    query['criteria']['value'] = 'CFYXW'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_valid_search_query_regnum():
    """Assert that the schema is performing as expected for a search by registration number."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'REGISTRATION_NUMBER'
    del query['criteria']['debtorName']
    query['criteria']['value'] = '023001B'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_valid_search_query_mhrnum():
    """Assert that the schema is performing as expected for a search by MHR number."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'MHR_NUMBER'
    del query['criteria']['debtorName']
    query['criteria']['value'] = '21324'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_valid_search_query_serialnum():
    """Assert that the schema is performing as expected for a search by serial number."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'SERIAL_NUMBER'
    del query['criteria']['debtorName']
    query['criteria']['value'] = 'KM8J3CA46JU622994'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')


    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert is_valid


def test_invalid_search_query_missing_type():
    """Assert that an invalid search query fails - type is missing."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['type']
    del query['criteria']['debtorName']['business']
    del query['criteria']['value']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_missing_criteria():
    """Assert that an invalid search query fails - criteria is missing."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_type():
    """Assert that an invalid search query fails - type is invalid."""
    query = copy.deepcopy(SEARCH_QUERY)
    query['type'] = 'XXXXXXXX'
    del query['criteria']['debtorName']['business']
    del query['criteria']['value']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_criteria():
    """Assert that an invalid search query fails - criteria is invalid."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['debtorName']['business']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_busname():
    """Assert that an invalid search query fails - business name is too short."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['debtorName']['first']
    del query['criteria']['debtorName']['second']
    del query['criteria']['debtorName']['last']
    del query['criteria']['value']
    query['criteria']['debtorName']['business'] = 'XXXX'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_value():
    """Assert that an invalid search query fails - value is too long."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['debtorName']
    query['criteria']['value'] = 'XxxxxxxxxxxxxxxxxxxxXxxxxxxxxxxxxxxxxxxxXxxxxxxxxxx'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_debtor():
    """Assert that an invalid search query fails - debtor name is invalid."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_firstname():
    """Assert that an invalid search query fails - debtor first name is too long."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['criteria']['debtorName']['first'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 
    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_secondname():
    """Assert that an invalid search query fails - debtor second name is too long."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['criteria']['debtorName']['second'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 
    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid



def test_invalid_search_query_lastname():
    """Assert that an invalid search query fails - debtor last name is too long."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['criteria']['debtorName']['last'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
 
    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_clientref():
    """Assert that an invalid search query fails - client reference id is too long."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['clientReferenceId'] = 'XxxxxxxxxxXxxxxxxxxxX'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_startts():
    """Assert that an invalid search query fails - start date time format is invalid."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['startDateTime'] = 'Xxxxxxxxxx'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


def test_invalid_search_query_endts():
    """Assert that an invalid search query fails - end date time format is invalid."""
    query = copy.deepcopy(SEARCH_QUERY)
    del query['criteria']['value']
    del query['criteria']['debtorName']['business']
    query['endDateTime'] = 'Xxxxxxxxxx'

    is_valid, errors = validate(query, 'searchQuery', 'ppr')

    if errors:
        for err in errors:
            print(err.message)
    print(errors)

    assert not is_valid


[((921, 948), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (934, 948), False, 'import copy\n'), ((1195, 1232), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (1203, 1232), False, 'from registry_schemas import validate\n'), ((1495, 1522), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (1508, 1522), False, 'import copy\n'), ((1767, 1804), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (1775, 1804), False, 'from registry_schemas import validate\n'), ((2060, 2087), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (2073, 2087), False, 'import copy\n'), ((2228, 2265), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (2236, 2265), False, 'from registry_schemas import validate\n'), ((2528, 2555), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (2541, 2555), False, 'import copy\n'), ((2705, 2742), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (2713, 2742), False, 'from registry_schemas import validate\n'), ((2996, 3023), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (3009, 3023), False, 'import copy\n'), ((3162, 3199), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (3170, 3199), False, 'from registry_schemas import validate\n'), ((3459, 3486), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (3472, 3486), False, 'import copy\n'), ((3640, 3677), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (3648, 3677), False, 'from registry_schemas import validate\n'), ((3923, 3950), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (3936, 3950), False, 'import copy\n'), ((4084, 4121), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (4092, 4121), False, 'from registry_schemas import validate\n'), ((4378, 4405), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (4391, 4405), False, 'import copy\n'), ((4456, 4493), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (4464, 4493), False, 'from registry_schemas import validate\n'), ((4734, 4761), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (4747, 4761), False, 'import copy\n'), ((4904, 4941), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (4912, 4941), False, 'from registry_schemas import validate\n'), ((5190, 5217), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (5203, 5217), False, 'import copy\n'), ((5294, 5331), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (5302, 5331), False, 'from registry_schemas import validate\n'), ((5586, 5613), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (5599, 5613), False, 'import copy\n'), ((5877, 5914), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (5885, 5914), False, 'from registry_schemas import validate\n'), ((6158, 6185), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (6171, 6185), False, 'import copy\n'), ((6337, 6374), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (6345, 6374), False, 'from registry_schemas import validate\n'), ((6624, 6651), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (6637, 6651), False, 'import copy\n'), ((6711, 6748), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (6719, 6748), False, 'from registry_schemas import validate\n'), ((7008, 7035), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (7021, 7035), False, 'import copy\n'), ((7229, 7266), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (7237, 7266), False, 'from registry_schemas import validate\n'), ((7528, 7555), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (7541, 7555), False, 'import copy\n'), ((7750, 7787), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (7758, 7787), False, 'from registry_schemas import validate\n'), ((8046, 8073), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (8059, 8073), False, 'import copy\n'), ((8266, 8303), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (8274, 8303), False, 'from registry_schemas import validate\n'), ((8565, 8592), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (8578, 8592), False, 'import copy\n'), ((8761, 8798), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (8769, 8798), False, 'from registry_schemas import validate\n'), ((9060, 9087), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (9073, 9087), False, 'import copy\n'), ((9241, 9278), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (9249, 9278), False, 'from registry_schemas import validate\n'), ((9536, 9563), 'copy.deepcopy', 'copy.deepcopy', (['SEARCH_QUERY'], {}), '(SEARCH_QUERY)\n', (9549, 9563), False, 'import copy\n'), ((9715, 9752), 'registry_schemas.validate', 'validate', (['query', '"""searchQuery"""', '"""ppr"""'], {}), "(query, 'searchQuery', 'ppr')\n", (9723, 9752), False, 'from registry_schemas import validate\n')]