|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import uuid |
| 17 | + |
| 18 | +from google.api_core.exceptions import NotFound |
| 19 | +from google.cloud.modelarmor_v1 import ( |
| 20 | + DetectionConfidenceLevel, |
| 21 | + FilterMatchState, |
| 22 | +) |
| 23 | +import pytest |
| 24 | + |
| 25 | +from create_template import create_model_armor_template |
| 26 | +from delete_template import delete_model_armor_template |
| 27 | +from get_template import get_model_armor_template |
| 28 | +from list_templates import list_model_armor_templates |
| 29 | +from sanitize_user_prompt import sanitize_user_prompt |
| 30 | +from update_template import update_model_armor_template |
| 31 | + |
| 32 | +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 33 | +LOCATION = "us-central1" |
| 34 | +TEMPLATE_ID = f"test-model-armor-{uuid.uuid4()}" |
| 35 | + |
| 36 | + |
| 37 | +def test_create_template() -> None: |
| 38 | + template = create_model_armor_template(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 39 | + assert template is not None |
| 40 | + |
| 41 | + |
| 42 | +def test_get_template() -> None: |
| 43 | + template = get_model_armor_template(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 44 | + assert TEMPLATE_ID in template.name |
| 45 | + |
| 46 | + |
| 47 | +def test_list_templates() -> None: |
| 48 | + templates = list_model_armor_templates(PROJECT_ID, LOCATION) |
| 49 | + assert TEMPLATE_ID in str(templates) |
| 50 | + |
| 51 | + |
| 52 | +def test_user_prompt() -> None: |
| 53 | + response = sanitize_user_prompt(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 54 | + assert ( |
| 55 | + response.sanitization_result.filter_match_state == FilterMatchState.MATCH_FOUND |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def test_update_templates() -> None: |
| 60 | + template = update_model_armor_template(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 61 | + assert ( |
| 62 | + template.filter_config.pi_and_jailbreak_filter_settings.confidence_level == DetectionConfidenceLevel.LOW_AND_ABOVE |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_delete_template() -> None: |
| 67 | + delete_model_armor_template(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 68 | + with pytest.raises(NotFound) as exception_info: |
| 69 | + get_model_armor_template(PROJECT_ID, LOCATION, TEMPLATE_ID) |
| 70 | + assert TEMPLATE_ID in str(exception_info.value) |
0 commit comments