1616
1717using System ;
1818using System . Collections . Generic ;
19+ using System . IO ;
1920using System . Linq ;
2021using System . Threading . Tasks ;
2122
@@ -32,6 +33,7 @@ public class FilesDownloadTest
3233 private static TestServerProcess ? _server ;
3334 private Client vertexClient ;
3435 private Client geminiClient ;
36+ private string fileUri ;
3537 public TestContext TestContext { get ; set ; }
3638
3739 [ ClassInitialize ]
@@ -76,52 +78,149 @@ public void TestInit()
7678 geminiClient =
7779 new Client ( apiKey : apiKey , vertexAI : false , httpOptions : geminiClientHttpOptions ) ;
7880
81+ // Specific setup for this test class
82+ // Note that replay mode does not return any bytes when dowloading a file.
83+ fileUri = "https://generativelanguage.googleapis.com/v1beta/files/p1crkz5nfru4:download?alt=media" ;
84+
7985 }
8086
8187 [ TestMethod ]
8288 public async Task DownloadFileNameVertexTest ( )
8389 {
84- var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
85- {
86- await vertexClient . Files . DownloadAsync ( fileName : "fileName" ) ;
87- } ) ;
90+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
91+ {
92+ await vertexClient . Files . DownloadAsync ( fileName : "fileName" ) ;
93+ } ) ;
8894
89- StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
95+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
96+ }
97+
98+ [ TestMethod ]
99+ public async Task DownloadFileNameGeminiTest ( )
100+ {
101+ var stream = await geminiClient . Files . DownloadAsync (
102+ fileName : fileUri
103+ ) ;
104+ using ( MemoryStream memoryStream = new MemoryStream ( ) ) {
105+ await stream . CopyToAsync ( memoryStream ) ;
106+ memoryStream . Position = 0 ;
107+ byte [ ] bytes = memoryStream . ToArray ( ) ;
108+ // Replay mode does not return any bytes when dowloading a file.
109+ if ( ! TestServer . IsReplayMode ) {
110+ Assert . IsTrue ( bytes . Length > 0 ) ;
111+ }
112+ }
90113 }
91114
92115 [ TestMethod ]
93116 public async Task DownloadToFileFileNameVertexTest ( )
94117 {
95- var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
96- {
97- await vertexClient . Files . DownloadToFileAsync ( fileName : "fileName" , outputPath : "outputPath" ) ;
98- } ) ;
118+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
119+ {
120+ await vertexClient . Files . DownloadToFileAsync ( fileName : "fileName" , outputPath : "outputPath" ) ;
121+ } ) ;
122+
123+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
124+ }
99125
100- StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
126+ [ TestMethod ]
127+ public async Task DownloadToFileFileNameGeminiTest ( )
128+ {
129+ await geminiClient . Files . DownloadToFileAsync (
130+ fileName : fileUri ,
131+ outputPath : "output.mp4"
132+ ) ;
101133 }
102134
103135 [ TestMethod ]
104136 public async Task DownloadFileVertexTest ( )
105137 {
106- var file = new Google . GenAI . Types . File { Name = "Name" } ;
107- var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
108- {
109- await vertexClient . Files . DownloadAsync ( file : file ) ;
110- } ) ;
138+ var file = new Google . GenAI . Types . File { Name = "Name" } ;
139+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
140+ {
141+ await vertexClient . Files . DownloadAsync ( file : file ) ;
142+ } ) ;
143+
144+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
145+ }
111146
112- StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
147+ [ TestMethod ]
148+ public async Task DownloadFileGeminiTest ( )
149+ {
150+ var file = new Google . GenAI . Types . File {
151+ Name = fileUri
152+ } ;
153+ await geminiClient . Files . DownloadAsync ( file : file ) ;
113154 }
114155
115156 [ TestMethod ]
116157 public async Task DownloadToFileFileVertexTest ( )
117158 {
118- var file = new Google . GenAI . Types . File { Name = "Name" } ;
119- var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
120- {
121- await vertexClient . Files . DownloadToFileAsync ( file : file , outputPath : "outputPath" ) ;
122- } ) ;
159+ var file = new Google . GenAI . Types . File { Name = "Name" } ;
160+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
161+ {
162+ await vertexClient . Files . DownloadToFileAsync ( file : file , outputPath : "outputPath" ) ;
163+ } ) ;
123164
124- StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
165+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
166+ }
167+
168+ [ TestMethod ]
169+ public async Task DownloadToFileFileGeminiTest ( )
170+ {
171+ var file = new Google . GenAI . Types . File {
172+ Name = fileUri
173+ } ;
174+ await geminiClient . Files . DownloadToFileAsync ( file : file , outputPath : "output.mp4" ) ;
175+ }
176+
177+ [ TestMethod ]
178+ public async Task DownloadToFileVideoVertexTest ( )
179+ {
180+ var video = new Google . GenAI . Types . Video {
181+ Uri = fileUri
182+ } ;
183+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
184+ {
185+ await vertexClient . Files . DownloadToFileAsync ( video : video , outputPath : "outputPath" ) ;
186+ } ) ;
187+
188+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
189+ }
190+
191+ [ TestMethod ]
192+ public async Task DownloadToFileVideoGeminiTest ( )
193+ {
194+ var video = new Google . GenAI . Types . Video {
195+ Uri = fileUri
196+ } ;
197+ await geminiClient . Files . DownloadToFileAsync ( video : video , outputPath : "output.mp4" ) ;
198+ }
199+
200+
201+ [ TestMethod ]
202+ public async Task DownloadToFileGeneratedVideoVertexTest ( )
203+ {
204+ var video = new Google . GenAI . Types . Video {
205+ Uri = fileUri
206+ } ;
207+ var generatedVideo = new Google . GenAI . Types . GeneratedVideo { Video = video } ;
208+ var ex = await Assert . ThrowsExceptionAsync < NotSupportedException > ( async ( ) =>
209+ {
210+ await vertexClient . Files . DownloadToFileAsync ( generatedVideo : generatedVideo , outputPath : "outputPath" ) ;
211+ } ) ;
212+
213+ StringAssert . Contains ( ex . Message , "This method is only supported in the Gemini Developer API client" ) ;
214+ }
215+
216+ [ TestMethod ]
217+ public async Task DownloadToFileGeneratedVideoGeminiTest ( )
218+ {
219+ var video = new Google . GenAI . Types . Video {
220+ Uri = fileUri
221+ } ;
222+ var generatedVideo = new Google . GenAI . Types . GeneratedVideo { Video = video } ;
223+ await geminiClient . Files . DownloadToFileAsync ( generatedVideo : generatedVideo , outputPath : "output.mp4" ) ;
125224 }
126225
127226}
0 commit comments