13
13
14
14
15
15
HERE = 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 ()
19
23
20
24
21
25
class UnitTests (parameterized .TestCase ):
22
26
@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 )],
25
29
)
26
- def test_image_to_blob (self , image ):
30
+ def test_png_to_blob (self , image ):
27
31
blob = content_types .image_to_blob (image )
28
32
self .assertIsInstance (blob , glm .Blob )
29
33
self .assertEqual (blob .mime_type , "image/png" )
30
34
self .assertStartsWith (blob .data , b"\x89 PNG" )
31
35
32
36
@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 )],
36
50
)
37
51
def test_to_blob (self , example ):
38
52
blob = content_types .to_blob (example )
@@ -51,11 +65,11 @@ def test_to_part(self, example):
51
65
self .assertEqual (part .text , "Hello world!" )
52
66
53
67
@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 }],
56
70
[
57
71
"PartDict" ,
58
- {"inline_data" : {"mime_type" : "image/png" , "data" : TEST_IMAGE_DATA }},
72
+ {"inline_data" : {"mime_type" : "image/png" , "data" : TEST_PNG_DATA }},
59
73
],
60
74
)
61
75
def test_img_to_part (self , example ):
@@ -83,9 +97,9 @@ def test_to_content(self, example):
83
97
self .assertEqual (part .text , "Hello world!" )
84
98
85
99
@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 )],
89
103
)
90
104
def test_img_to_content (self , example ):
91
105
content = content_types .to_content (example )
@@ -140,10 +154,10 @@ def test_dict_to_content_fails(self):
140
154
@parameterized .named_parameters (
141
155
[
142
156
"ContentDict" ,
143
- [{"parts" : [{"inline_data" : PIL .Image .open (TEST_IMAGE_PATH )}]}],
157
+ [{"parts" : [{"inline_data" : PIL .Image .open (TEST_PNG_PATH )}]}],
144
158
],
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 )],
147
161
)
148
162
def test_img_to_contents (self , example ):
149
163
contents = content_types .to_contents (example )
0 commit comments