"""Tests for VendorPaymentHold model.""" from ows_accounting.constants import payment_holds as constants from ows_accounting.models import vendor_payment_hold def test_get_hold_by_id(payment_holds_db_fixture, hold_active_fixture): """Test getting get_hold_by_id succeed.""" hold_active_fixture['hold_id'] = hold_active_fixture['id'] del hold_active_fixture['id'] response = vendor_payment_hold.get_hold_by_id(1) assert response assert hold_active_fixture.items() <= response.message.items() assert len(response.message['vendor_payment_hold_log']) == 2 def test_get_hold_by_id_invalid(payment_holds_db_fixture): """Test getting get_hold_by_id for invalid hold id.""" response = vendor_payment_hold.get_hold_by_id(100) assert response.status == 404 assert response.errors['message'] == 'No hold found with this id.' def test_get_all_holds_by_status( payment_holds_db_fixture, hold_inactive_fixture): """Test getting get_all_holds_by_status succeed.""" hold_inactive_fixture['hold_id'] = hold_inactive_fixture['id'] del hold_inactive_fixture['id'] response = vendor_payment_hold.get_all_holds_by_status('inactive') assert response assert len(response.message) == 1 assert hold_inactive_fixture.items() <= response.message[0].items() assert response.message[0]['vendor_payment_hold_log'] == [] def test_get_all_holds_by_status_and_vendor(payment_holds_db_fixture): """Test get_all_holds_by_status with vendor ids.""" vendor_ids = [7123, 25824] response = vendor_payment_hold.get_all_holds_by_status( status='active', vendor_ids=vendor_ids) assert response assert len(response.message) == 1 assert response.message[0]['status'] == 'active' assert response.message[0]['vendor_id'] == 7123 def test_get_all_holds_by_status_no_records(payment_holds_db_fixture): """Test getting get_all_holds_by_status with no matching status records.""" response = vendor_payment_hold.get_all_holds_by_status('nostatus') assert response assert len(response.message) == 0 def test_get_all_holds_by_status_pagination( payment_holds_db_fixture, hold_active_fixture, hold_active_fixture2): """Test getting get_all_holds_by_status succeed.""" record1 = vendor_payment_hold.get_all_holds_by_status( 'active', 1, 0).message record2 = vendor_payment_hold.get_all_holds_by_status( 'active', 1, 1).message record3 = vendor_payment_hold.get_all_holds_by_status( 'active', 1, 2).message assert record1[0]['hold_id'] == hold_active_fixture['id'] assert record2[0]['hold_id'] == hold_active_fixture2['id'] assert record3 == [] def test_count_holds_by_status(payment_holds_db_fixture): """Test count_holds_by_status succeed.""" num_holds = vendor_payment_hold.count_holds_by_status('active') assert num_holds.message == 2 def test_count_holds_by_status_and_vendor(payment_holds_db_fixture): """Test count_holds_by_status filter by vendor ids.""" num_holds = vendor_payment_hold.count_holds_by_status('active', [7123]) assert num_holds.message == 1 def test_count_holds_by_status_and_no_vendor(payment_holds_db_fixture): """Test count_holds_by_status for vendor that don't have holds.""" num_holds = vendor_payment_hold.count_holds_by_status('active', [1234]) assert num_holds.message == 0 def test_create_hold_for_vendor(payment_holds_db_fixture): """Test create_hold_for_vendor.""" response = vendor_payment_hold.create_hold_for_vendor( 8869, 'oa:1234', 'active', 'test desc') assert response assert response.message['description'] == 'test desc' assert response.message['creator_id'] == 'oa:1234' assert response.message['status'] == 'active' assert len(response.message['vendor_payment_hold_log']) == 1 actual_log = response.message['vendor_payment_hold_log'][0] assert actual_log['action'] == 'create' assert actual_log['description'] == 'test desc' assert actual_log['user_id'] == 'oa:1234' assert actual_log['status'] == 'active' def test_create_hold_for_vendor_db_error(payment_holds_db_fixture): """Test create_hold_for_vendor when DB error is caught by db wrapper.""" response = vendor_payment_hold.create_hold_for_vendor( 8869, 'oa:1234', 0, 'test desc') assert response.status == 500 def test_update_hold_by_id(payment_holds_db_fixture): """Test update_hold_by_id.""" hold_id = 1 before_put_obj = vendor_payment_hold.get_hold_by_id(hold_id).message response = vendor_payment_hold.update_hold_by_id( hold_id, 'oa:3333', 'active', 'desc 3333') assert response assert response.message['description'] == 'desc 3333' assert response.message['status'] == 'active' # vendor id & creator_id cannot be changed for hold. assert response.message['creator_id'] == before_put_obj['creator_id'] assert response.message['vendor_id'] == before_put_obj['vendor_id'] assert len(response.message['vendor_payment_hold_log']) == \ len(before_put_obj['vendor_payment_hold_log']) + 1 latest_log = response.message['vendor_payment_hold_log'][-1] assert latest_log['description'] == 'desc 3333' assert latest_log['user_id'] == 'oa:3333' def test_update_hold_by_id_invalid_hold(payment_holds_db_fixture): """Test update_hold_by_id with invalid hold id.""" response = vendor_payment_hold.update_hold_by_id( 1234, 'oa:3333', 'active', 'desc 3333') assert response.status == 404 assert response.errors['message'] == constants.INVALID_HOLD_ID def test_update_hold_by_id_inactive_hold(payment_holds_db_fixture): """Test update_hold_by_id with inactive hold id.""" response = vendor_payment_hold.update_hold_by_id( 2, 'oa:3333', 'inactive', 'desc 3333') assert response.status == 400 assert response.errors['message'] == constants.INACTIVE_HOLD_EDIT def test_get_all_holds_by_vendor( payment_holds_db_fixture, hold_active_fixture): """Test get_all_holds_by_vendor succeed.""" hold_active_fixture['hold_id'] = hold_active_fixture['id'] del hold_active_fixture['id'] response = vendor_payment_hold.get_all_holds_by_vendor(7123) assert response assert len(response.message) == 1 assert hold_active_fixture.items() <= response.message[0].items() def test_get_all_holds_by_vendor_no_data(payment_holds_db_fixture): """Test get_all_holds_by_vendor when no records exist for this vendor.""" response = vendor_payment_hold.get_all_holds_by_vendor(12345) assert response.status == 404 def test_get_active_holds_for_vendor( payment_holds_db_fixture, hold_active_fixture): """Test get_all_holds_by_vendor succeed.""" hold_active_fixture['hold_id'] = hold_active_fixture['id'] del hold_active_fixture['id'] response = vendor_payment_hold.get_active_holds_for_vendor(7123) assert response actual_result = response.message del (actual_result['created_time']) del (actual_result['last_updated']) del (actual_result['vendor_payment_hold_log']) assert hold_active_fixture == actual_result def test_get_active_holds_for_vendor_no_result(payment_holds_db_fixture): """Test get_all_holds_by_vendor when no active hold found.""" response = vendor_payment_hold.get_active_holds_for_vendor(25824) assert response.status == 404