# 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


[((25, 12, 25, 39), 'copy.deepcopy', 'copy.deepcopy', ({(25, 26, 25, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((33, 23, 33, 60), 'registry_schemas.validate', 'validate', ({(33, 32, 33, 37): 'query', (33, 39, 33, 52): '"""searchQuery"""', (33, 54, 33, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((46, 12, 46, 39), 'copy.deepcopy', 'copy.deepcopy', ({(46, 26, 46, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((53, 23, 53, 60), 'registry_schemas.validate', 'validate', ({(53, 32, 53, 37): 'query', (53, 39, 53, 52): '"""searchQuery"""', (53, 54, 53, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((66, 12, 66, 39), 'copy.deepcopy', 'copy.deepcopy', ({(66, 26, 66, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((71, 23, 71, 60), 'registry_schemas.validate', 'validate', ({(71, 32, 71, 37): 'query', (71, 39, 71, 52): '"""searchQuery"""', (71, 54, 71, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((84, 12, 84, 39), 'copy.deepcopy', 'copy.deepcopy', ({(84, 26, 84, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((89, 23, 89, 60), 'registry_schemas.validate', 'validate', ({(89, 32, 89, 37): 'query', (89, 39, 89, 52): '"""searchQuery"""', (89, 54, 89, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((102, 12, 102, 39), 'copy.deepcopy', 'copy.deepcopy', ({(102, 26, 102, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((107, 23, 107, 60), 'registry_schemas.validate', 'validate', ({(107, 32, 107, 37): 'query', (107, 39, 107, 52): '"""searchQuery"""', (107, 54, 107, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((120, 12, 120, 39), 'copy.deepcopy', 'copy.deepcopy', ({(120, 26, 120, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((125, 23, 125, 60), 'registry_schemas.validate', 'validate', ({(125, 32, 125, 37): 'query', (125, 39, 125, 52): '"""searchQuery"""', (125, 54, 125, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((138, 12, 138, 39), 'copy.deepcopy', 'copy.deepcopy', ({(138, 26, 138, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((143, 23, 143, 60), 'registry_schemas.validate', 'validate', ({(143, 32, 143, 37): 'query', (143, 39, 143, 52): '"""searchQuery"""', (143, 54, 143, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((155, 12, 155, 39), 'copy.deepcopy', 'copy.deepcopy', ({(155, 26, 155, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((158, 23, 158, 60), 'registry_schemas.validate', 'validate', ({(158, 32, 158, 37): 'query', (158, 39, 158, 52): '"""searchQuery"""', (158, 54, 158, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((170, 12, 170, 39), 'copy.deepcopy', 'copy.deepcopy', ({(170, 26, 170, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((175, 23, 175, 60), 'registry_schemas.validate', 'validate', ({(175, 32, 175, 37): 'query', (175, 39, 175, 52): '"""searchQuery"""', (175, 54, 175, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((187, 12, 187, 39), 'copy.deepcopy', 'copy.deepcopy', ({(187, 26, 187, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((190, 23, 190, 60), 'registry_schemas.validate', 'validate', ({(190, 32, 190, 37): 'query', (190, 39, 190, 52): '"""searchQuery"""', (190, 54, 190, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((202, 12, 202, 39), 'copy.deepcopy', 'copy.deepcopy', ({(202, 26, 202, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((209, 23, 209, 60), 'registry_schemas.validate', 'validate', ({(209, 32, 209, 37): 'query', (209, 39, 209, 52): '"""searchQuery"""', (209, 54, 209, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((221, 12, 221, 39), 'copy.deepcopy', 'copy.deepcopy', ({(221, 26, 221, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((225, 23, 225, 60), 'registry_schemas.validate', 'validate', ({(225, 32, 225, 37): 'query', (225, 39, 225, 52): '"""searchQuery"""', (225, 54, 225, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((237, 12, 237, 39), 'copy.deepcopy', 'copy.deepcopy', ({(237, 26, 237, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((240, 23, 240, 60), 'registry_schemas.validate', 'validate', ({(240, 32, 240, 37): 'query', (240, 39, 240, 52): '"""searchQuery"""', (240, 54, 240, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((252, 12, 252, 39), 'copy.deepcopy', 'copy.deepcopy', ({(252, 26, 252, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((257, 23, 257, 60), 'registry_schemas.validate', 'validate', ({(257, 32, 257, 37): 'query', (257, 39, 257, 52): '"""searchQuery"""', (257, 54, 257, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((269, 12, 269, 39), 'copy.deepcopy', 'copy.deepcopy', ({(269, 26, 269, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((274, 23, 274, 60), 'registry_schemas.validate', 'validate', ({(274, 32, 274, 37): 'query', (274, 39, 274, 52): '"""searchQuery"""', (274, 54, 274, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((287, 12, 287, 39), 'copy.deepcopy', 'copy.deepcopy', ({(287, 26, 287, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((292, 23, 292, 60), 'registry_schemas.validate', 'validate', ({(292, 32, 292, 37): 'query', (292, 39, 292, 52): '"""searchQuery"""', (292, 54, 292, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((304, 12, 304, 39), 'copy.deepcopy', 'copy.deepcopy', ({(304, 26, 304, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((309, 23, 309, 60), 'registry_schemas.validate', 'validate', ({(309, 32, 309, 37): 'query', (309, 39, 309, 52): '"""searchQuery"""', (309, 54, 309, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((321, 12, 321, 39), 'copy.deepcopy', 'copy.deepcopy', ({(321, 26, 321, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((326, 23, 326, 60), 'registry_schemas.validate', 'validate', ({(326, 32, 326, 37): 'query', (326, 39, 326, 52): '"""searchQuery"""', (326, 54, 326, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n'), ((338, 12, 338, 39), 'copy.deepcopy', 'copy.deepcopy', ({(338, 26, 338, 38): 'SEARCH_QUERY'}, {}), '(SEARCH_QUERY)', False, 'import copy\n'), ((343, 23, 343, 60), 'registry_schemas.validate', 'validate', ({(343, 32, 343, 37): 'query', (343, 39, 343, 52): '"""searchQuery"""', (343, 54, 343, 59): '"""ppr"""'}, {}), "(query, 'searchQuery', 'ppr')", False, 'from registry_schemas import validate\n')]