1313
1414
1515HERE = pathlib .Path (__file__ ).parent
16- TEST_IMAGE_PATH = HERE / "test_img.png"
17- TEST_IMAGE_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.png"
18- TEST_IMAGE_DATA = TEST_IMAGE_PATH .read_bytes ()
16+ TEST_PNG_PATH = HERE / "test_img.png"
17+ TEST_PNG_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.png"
18+ TEST_PNG_DATA = TEST_PNG_PATH .read_bytes ()
19+
20+ TEST_JPG_PATH = HERE / "test_img.jpg"
21+ TEST_JPG_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.jpg"
22+ TEST_JPG_DATA = TEST_JPG_PATH .read_bytes ()
1923
2024
2125class UnitTests (parameterized .TestCase ):
2226 @parameterized .named_parameters (
23- ["PIL" , PIL .Image .open (TEST_IMAGE_PATH )],
24- ["IPython" , IPython .display .Image (filename = TEST_IMAGE_PATH )],
27+ ["PIL" , PIL .Image .open (TEST_PNG_PATH )],
28+ ["IPython" , IPython .display .Image (filename = TEST_PNG_PATH )],
2529 )
26- def test_image_to_blob (self , image ):
30+ def test_png_to_blob (self , image ):
2731 blob = content_types .image_to_blob (image )
2832 self .assertIsInstance (blob , glm .Blob )
2933 self .assertEqual (blob .mime_type , "image/png" )
3034 self .assertStartsWith (blob .data , b"\x89 PNG" )
3135
3236 @parameterized .named_parameters (
33- ["BlobDict" , {"mime_type" : "image/png" , "data" : TEST_IMAGE_DATA }],
34- ["glm.Blob" , glm .Blob (mime_type = "image/png" , data = TEST_IMAGE_DATA )],
35- ["Image" , IPython .display .Image (filename = TEST_IMAGE_PATH )],
37+ ["PIL" , PIL .Image .open (TEST_JPG_PATH )],
38+ ["IPython" , IPython .display .Image (filename = TEST_JPG_PATH )],
39+ )
40+ def test_jpg_to_blob (self , image ):
41+ blob = content_types .image_to_blob (image )
42+ self .assertIsInstance (blob , glm .Blob )
43+ self .assertEqual (blob .mime_type , "image/jpeg" )
44+ self .assertStartsWith (blob .data , b"\xff \xd8 \xff \xe0 \x00 \x10 JFIF" )
45+
46+ @parameterized .named_parameters (
47+ ["BlobDict" , {"mime_type" : "image/png" , "data" : TEST_PNG_DATA }],
48+ ["glm.Blob" , glm .Blob (mime_type = "image/png" , data = TEST_PNG_DATA )],
49+ ["Image" , IPython .display .Image (filename = TEST_PNG_PATH )],
3650 )
3751 def test_to_blob (self , example ):
3852 blob = content_types .to_blob (example )
@@ -51,11 +65,11 @@ def test_to_part(self, example):
5165 self .assertEqual (part .text , "Hello world!" )
5266
5367 @parameterized .named_parameters (
54- ["Image" , IPython .display .Image (filename = TEST_IMAGE_PATH )],
55- ["BlobDict" , {"mime_type" : "image/png" , "data" : TEST_IMAGE_DATA }],
68+ ["Image" , IPython .display .Image (filename = TEST_PNG_PATH )],
69+ ["BlobDict" , {"mime_type" : "image/png" , "data" : TEST_PNG_DATA }],
5670 [
5771 "PartDict" ,
58- {"inline_data" : {"mime_type" : "image/png" , "data" : TEST_IMAGE_DATA }},
72+ {"inline_data" : {"mime_type" : "image/png" , "data" : TEST_PNG_DATA }},
5973 ],
6074 )
6175 def test_img_to_part (self , example ):
@@ -83,9 +97,9 @@ def test_to_content(self, example):
8397 self .assertEqual (part .text , "Hello world!" )
8498
8599 @parameterized .named_parameters (
86- ["ContentDict" , {"parts" : [PIL .Image .open (TEST_IMAGE_PATH )]}],
87- ["list[Image]" , [PIL .Image .open (TEST_IMAGE_PATH )]],
88- ["Image" , PIL .Image .open (TEST_IMAGE_PATH )],
100+ ["ContentDict" , {"parts" : [PIL .Image .open (TEST_PNG_PATH )]}],
101+ ["list[Image]" , [PIL .Image .open (TEST_PNG_PATH )]],
102+ ["Image" , PIL .Image .open (TEST_PNG_PATH )],
89103 )
90104 def test_img_to_content (self , example ):
91105 content = content_types .to_content (example )
@@ -140,10 +154,10 @@ def test_dict_to_content_fails(self):
140154 @parameterized .named_parameters (
141155 [
142156 "ContentDict" ,
143- [{"parts" : [{"inline_data" : PIL .Image .open (TEST_IMAGE_PATH )}]}],
157+ [{"parts" : [{"inline_data" : PIL .Image .open (TEST_PNG_PATH )}]}],
144158 ],
145- ["ContentDict-unwraped" , [{"parts" : [PIL .Image .open (TEST_IMAGE_PATH )]}]],
146- ["Image" , PIL .Image .open (TEST_IMAGE_PATH )],
159+ ["ContentDict-unwraped" , [{"parts" : [PIL .Image .open (TEST_PNG_PATH )]}]],
160+ ["Image" , PIL .Image .open (TEST_PNG_PATH )],
147161 )
148162 def test_img_to_contents (self , example ):
149163 contents = content_types .to_contents (example )
0 commit comments