test_utilize.py
1 """Integration tests for the custom extra fields system.""" 2 3 import json 4 5 import httpx 6 7 from ..conftest import URL, assert_httpx_code, assert_httpx_success 8 9 10 def test_add_vendor_with_extra_field(): 11 """Test adding a vendor with a custom field.""" 12 result = httpx.post( 13 f"{URL}/api/v1/field/vendor/mytextfield", 14 json={ 15 "name": "My text field", 16 "field_type": "text", 17 "default_value": json.dumps("Hello World"), 18 }, 19 ) 20 assert_httpx_success(result) 21 22 result = httpx.post( 23 f"{URL}/api/v1/vendor", 24 json={ 25 "name": "My Vendor", 26 "extra": { 27 "mytextfield": '"My Value"', 28 }, 29 }, 30 ) 31 assert_httpx_success(result) 32 33 # Verify 34 result = httpx.get(f"{URL}/api/v1/vendor/{result.json()['id']}") 35 assert_httpx_success(result) 36 vendor = result.json() 37 assert vendor["name"] == "My Vendor" 38 assert vendor["extra"] == {"mytextfield": '"My Value"'} 39 40 # Clean up 41 result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") 42 assert_httpx_success(result) 43 44 result = httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}") 45 assert_httpx_success(result) 46 47 48 def test_add_vendor_with_invalid_extra_field(): 49 """Test adding a vendor with an invalid custom field.""" 50 result = httpx.post( 51 f"{URL}/api/v1/vendor", 52 json={ 53 "name": "My Vendor", 54 "extra": { 55 "somefield": 42, 56 }, 57 }, 58 ) 59 assert_httpx_code(result, 422) 60 61 62 def test_add_vendor_with_extra_field_then_delete(): 63 """Test adding a vendor with an extra field, then delete the field. 64 65 Vendor GET response should then not contain the extra field. 66 """ 67 result = httpx.post( 68 f"{URL}/api/v1/field/vendor/mytextfield", 69 json={ 70 "name": "My text field", 71 "field_type": "text", 72 "default_value": json.dumps("Hello World"), 73 }, 74 ) 75 assert_httpx_success(result) 76 77 result = httpx.post( 78 f"{URL}/api/v1/vendor", 79 json={ 80 "name": "My Vendor", 81 "extra": { 82 "mytextfield": '"My Value"', 83 }, 84 }, 85 ) 86 assert_httpx_success(result) 87 88 # Verify 89 result = httpx.get(f"{URL}/api/v1/vendor/{result.json()['id']}") 90 assert_httpx_success(result) 91 vendor = result.json() 92 assert vendor["name"] == "My Vendor" 93 assert vendor["extra"] == {"mytextfield": '"My Value"'} 94 95 # Remove field 96 result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") 97 assert_httpx_success(result) 98 99 # Verify 100 result = httpx.get(f"{URL}/api/v1/vendor/{vendor['id']}") 101 assert_httpx_success(result) 102 vendor = result.json() 103 assert vendor["name"] == "My Vendor" 104 assert "extra" not in vendor or vendor["extra"] == {} 105 106 result = httpx.delete(f"{URL}/api/v1/vendor/{vendor['id']}") 107 assert_httpx_success(result) 108 109 110 def test_update_existing_vendor_with_new_extra_field(): 111 """Test updating an existing vendor with a new extra field.""" 112 result = httpx.post( 113 f"{URL}/api/v1/vendor", 114 json={ 115 "name": "My Vendor", 116 }, 117 ) 118 assert_httpx_success(result) 119 vendor_id = result.json()["id"] 120 121 result = httpx.post( 122 f"{URL}/api/v1/field/vendor/mytextfield", 123 json={ 124 "name": "My text field", 125 "field_type": "text", 126 "default_value": json.dumps("Hello World"), 127 }, 128 ) 129 assert_httpx_success(result) 130 131 result = httpx.patch( 132 f"{URL}/api/v1/vendor/{vendor_id}", 133 json={ 134 "extra": { 135 "mytextfield": '"My Value"', 136 }, 137 }, 138 ) 139 assert_httpx_success(result) 140 141 # Verify 142 result = httpx.get(f"{URL}/api/v1/vendor/{vendor_id}") 143 assert_httpx_success(result) 144 vendor = result.json() 145 assert vendor["name"] == "My Vendor" 146 assert vendor["extra"] == {"mytextfield": '"My Value"'} 147 148 # Clean up 149 result = httpx.delete(f"{URL}/api/v1/field/vendor/mytextfield") 150 assert_httpx_success(result) 151 152 result = httpx.delete(f"{URL}/api/v1/vendor/{vendor_id}") 153 assert_httpx_success(result)