77from google .genai .models import Models
88
99from genai_utils .gemini import (
10- GeminiError ,
1110 ModelConfig ,
1211 run_prompt_async ,
1312 validate_labels ,
@@ -24,74 +23,74 @@ async def get_dummy():
2423
2524
2625def test_validate_labels_valid ():
27- """Test that valid labels pass validation"""
26+ """Test that valid labels pass validation and are returned """
2827 valid_labels = {
2928 "team" : "ai" ,
3029 "project" : "genai-utils" ,
3130 "environment" : "production" ,
3231 "version" : "1-2-3" ,
3332 "my_label" : "my_value" ,
3433 }
35- # Should not raise any exception
36- validate_labels ( valid_labels )
34+ result = validate_labels ( valid_labels )
35+ assert result == valid_labels
3736
3837
3938def test_validate_labels_empty_key ():
40- """Test that empty keys are rejected """
41- with pytest . raises ( GeminiError , match = "cannot be empty" ):
42- validate_labels ({ " " : "value" })
39+ """Test that empty keys are filtered out """
40+ result = validate_labels ({ "" : "value" , "valid" : "label" })
41+ assert result == { "valid " : "label" }
4342
4443
4544def test_validate_labels_key_too_long ():
46- """Test that keys exceeding 63 characters are rejected """
45+ """Test that keys exceeding 63 characters are filtered out """
4746 long_key = "a" * 64
48- with pytest . raises ( GeminiError , match = "exceeds 63 characters" ):
49- validate_labels ({ long_key : "value" })
47+ result = validate_labels ({ long_key : "value" , "valid" : "label" })
48+ assert result == { "valid" : "label" }
5049
5150
5251def test_validate_labels_value_too_long ():
53- """Test that values exceeding 63 characters are rejected """
52+ """Test that values exceeding 63 characters are filtered out """
5453 long_value = "a" * 64
55- with pytest . raises ( GeminiError , match = "exceeds 63 characters" ):
56- validate_labels ({ "key " : long_value })
54+ result = validate_labels ({ "key" : long_value , "valid" : "label" })
55+ assert result == { "valid " : "label" }
5756
5857
5958def test_validate_labels_key_starts_with_number ():
60- """Test that keys starting with numbers are rejected """
61- with pytest . raises ( GeminiError , match = "must start with a lowercase letter" ):
62- validate_labels ({ "1key " : "value" })
59+ """Test that keys starting with numbers are filtered out """
60+ result = validate_labels ({ "1key" : "value" , "valid" : "label" })
61+ assert result == { "valid " : "label" }
6362
6463
6564def test_validate_labels_key_starts_with_uppercase ():
66- """Test that keys starting with uppercase are rejected """
67- with pytest . raises ( GeminiError , match = "must start with a lowercase letter" ):
68- validate_labels ({ "Key " : "value" })
65+ """Test that keys starting with uppercase are filtered out """
66+ result = validate_labels ({ "Key" : "value" , "valid" : "label" })
67+ assert result == { "valid " : "label" }
6968
7069
7170@pytest .mark .parametrize (
7271 "invalid_key" , ["key@value" , "key.name" , "key$" , "key with space" , "key/name" ]
7372)
7473def test_validate_labels_key_invalid_characters (invalid_key ):
75- """Test that keys with invalid characters are rejected """
76- with pytest . raises ( GeminiError , match = "contains invalid characters" ):
77- validate_labels ({ invalid_key : "value" })
74+ """Test that keys with invalid characters are filtered out """
75+ result = validate_labels ({ invalid_key : "value" , "valid" : "label" })
76+ assert result == { "valid" : "label" }
7877
7978
8079@pytest .mark .parametrize (
8180 "invalid_value" , ["value@" , "value.txt" , "value$" , "value with space" , "value/" ]
8281)
8382def test_validate_labels_value_invalid_characters (invalid_value ):
84- """Test that values with invalid characters are rejected """
85- with pytest . raises ( GeminiError , match = "contains invalid characters" ):
86- validate_labels ({ "key " : invalid_value })
83+ """Test that values with invalid characters are filtered out """
84+ result = validate_labels ({ "key" : invalid_value , "valid" : "label" })
85+ assert result == { "valid " : "label" }
8786
8887
8988def test_validate_labels_max_length_valid ():
9089 """Test that keys and values at exactly 63 characters are valid"""
9190 max_key = "a" * 63
9291 max_value = "b" * 63
93- # Should not raise any exception
94- validate_labels ( {max_key : max_value })
92+ result = validate_labels ({ max_key : max_value })
93+ assert result == {max_key : max_value }
9594
9695
9796def test_validate_labels_valid_special_chars ():
@@ -102,8 +101,8 @@ def test_validate_labels_valid_special_chars():
102101 "my-key_name" : "my-value_123" ,
103102 "key123" : "value456" ,
104103 }
105- # Should not raise any exception
106- validate_labels ( valid_labels )
104+ result = validate_labels ( valid_labels )
105+ assert result == valid_labels
107106
108107
109108@patch ("genai_utils.gemini.genai.Client" )
@@ -137,7 +136,7 @@ async def test_run_prompt_with_valid_labels(mock_client):
137136
138137@patch ("genai_utils.gemini.genai.Client" )
139138async def test_run_prompt_with_invalid_labels (mock_client ):
140- """Test that run_prompt rejects invalid labels"""
139+ """Test that run_prompt filters out invalid labels"""
141140 client = Mock (Client )
142141 models = Mock (Models )
143142 async_client = Mock (AsyncClient )
@@ -147,16 +146,22 @@ async def test_run_prompt_with_invalid_labels(mock_client):
147146 async_client .models = models
148147 mock_client .return_value = client
149148
150- invalid_labels = {"Invalid" : "value" } # uppercase key
149+ invalid_labels = {"Invalid" : "value" , "valid" : "label" } # uppercase key is invalid
151150
152- with pytest .raises (GeminiError , match = "must start with a lowercase letter" ):
153- await run_prompt_async (
154- "test prompt" ,
155- labels = invalid_labels ,
156- model_config = ModelConfig (
157- project = "project" , location = "location" , model_name = "model"
158- ),
159- )
151+ await run_prompt_async (
152+ "test prompt" ,
153+ labels = invalid_labels ,
154+ model_config = ModelConfig (
155+ project = "project" , location = "location" , model_name = "model"
156+ ),
157+ )
158+
159+ # Verify the call was made with only valid labels
160+ assert models .generate_content .called
161+ call_kwargs = models .generate_content .call_args [1 ]
162+ assert "config" in call_kwargs
163+ # The invalid "Invalid" key should be filtered out
164+ assert call_kwargs ["config" ].labels == {"valid" : "label" }
160165
161166
162167@patch ("genai_utils.gemini.genai.Client" )
0 commit comments