This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathAzureClientTests.cs
More file actions
538 lines (463 loc) · 26.4 KB
/
Copy pathAzureClientTests.cs
File metadata and controls
538 lines (463 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Threading;
using Azure.Quantum.Jobs.Models;
using Microsoft.Azure.Quantum;
using Microsoft.Azure.Quantum.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Jupyter.Core;
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.AzureClient;
using Microsoft.Quantum.IQSharp.Jupyter;
using Microsoft.Quantum.IQSharp.Kernel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Logging;
namespace Tests.IQSharp
{
[TestClass]
public class AzureClientTests
{
private T ExpectSuccess<T>(Task<ExecutionResult> task)
{
var result = task.GetAwaiter().GetResult();
Assert.AreEqual(ExecuteStatus.Ok, result.Status);
Assert.IsInstanceOfType(result.Output, typeof(T));
return (T)result.Output;
}
private async Task<T> ExpectSuccess<T>(Func<IChannel, Task<ExecutionResult>> task)
{
var channel = new MockChannel();
try
{
var result = await task(channel);
Assert.AreEqual(ExecuteStatus.Ok, result.Status);
Assert.IsInstanceOfType(result.Output, typeof(T));
return (T)result.Output;
}
catch
{
Logger.LogMessage($"Task reported failure. Errors:\n{string.Join("\n", channel.errors)}");
throw;
}
}
private void ExpectError(AzureClientError expectedError, Task<ExecutionResult> task)
{
var result = task.GetAwaiter().GetResult();
Assert.AreEqual(ExecuteStatus.Error, result.Status);
Assert.IsInstanceOfType(result.Output, typeof(AzureClientError));
Assert.AreEqual(expectedError, (AzureClientError)result.Output);
}
private Task<ExecutionResult> ConnectToWorkspaceAsync(
IAzureClient azureClient,
string workspaceName = "TEST_WORKSPACE_NAME",
string locationName = "TEST_LOCATION")
{
// Reset the global set of jobs and providers everytime we connect to a new workspace:
MockAzureWorkspace.MockJobIds = new string[] { };
MockAzureWorkspace.MockProviders = new HashSet<string>();
return azureClient.ConnectAsync(
new MockChannel(),
"TEST_SUBSCRIPTION_ID",
"TEST_RESOURCE_GROUP_NAME",
workspaceName,
"TEST_CONNECTION_STRING",
locationName,
CredentialType.Environment);
}
[TestMethod]
public void TestAzureExecutionTarget()
{
var targetId = "invalidname";
var executionTarget = AzureExecutionTarget.Create(targetId);
Assert.IsNull(executionTarget);
targetId = "ionq.targetId";
executionTarget = AzureExecutionTarget.Create(targetId);
Assert.AreEqual(targetId, executionTarget?.TargetId);
Assert.AreEqual("Microsoft.Quantum.Providers.IonQ", executionTarget?.PackageName);
targetId = "QuantiNUUm.targetId";
executionTarget = AzureExecutionTarget.Create(targetId);
Assert.AreEqual(targetId, executionTarget?.TargetId);
Assert.AreEqual("Microsoft.Quantum.Providers.Honeywell", executionTarget?.PackageName);
targetId = "qci.target.name.qpu";
executionTarget = AzureExecutionTarget.Create(targetId);
Assert.AreEqual(targetId, executionTarget?.TargetId);
Assert.AreEqual("Microsoft.Quantum.Providers.QCI", executionTarget?.PackageName);
}
[TestMethod]
public void TestJobStatus()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// not connected
ExpectError(AzureClientError.NotConnected, azureClient.GetJobStatusAsync(new MockChannel(), "JOB_ID_1"));
// connect
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
Assert.IsFalse(targets.Any());
// set up the mock workspace
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
MockAzureWorkspace.MockJobIds = new string[] { "JOB_ID_1", "JOB_ID_2" };
// valid job ID
var job = ExpectSuccess<CloudJob>(azureClient.GetJobStatusAsync(new MockChannel(), "JOB_ID_1"));
Assert.AreEqual("JOB_ID_1", job.Id);
// invalid job ID
ExpectError(AzureClientError.JobNotFound, azureClient.GetJobStatusAsync(new MockChannel(), "JOB_ID_3"));
// jobs list with no filter
var jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), string.Empty));
Assert.AreEqual(2, jobs.Count());
// jobs list with filter
jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), "JOB_ID_1"));
Assert.AreEqual(1, jobs.Count());
// jobs list with count
jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), string.Empty, 1));
Assert.AreEqual(1, jobs.Count());
// jobs list with invalid filter
jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), "INVALID_FILTER"));
Assert.AreEqual(0, jobs.Count());
// jobs list with partial filter
jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), "JOB_ID"));
Assert.AreEqual(2, jobs.Count());
// jobs list with filter and count
jobs = ExpectSuccess<IEnumerable<CloudJob>>(azureClient.GetJobListAsync(new MockChannel(), "JOB_ID", 1));
Assert.AreEqual(1, jobs.Count());
}
[TestMethod]
public void TestManualTargets()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// SetActiveTargetAsync with recognized target ID, but not yet connected
ExpectError(AzureClientError.NotConnected, azureClient.SetActiveTargetAsync(new MockChannel(), "ionq.simulator"));
// GetActiveTargetAsync, but not yet connected
ExpectError(AzureClientError.NotConnected, azureClient.GetActiveTargetAsync(new MockChannel()));
// connect
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
Assert.IsFalse(targets.Any());
// set up the mock workspace
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
azureWorkspace?.AddProviders("ionq", "quantinuum", "unrecognized");
// get connection status to verify list of targets
targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(azureClient.GetConnectionStatusAsync(new MockChannel()));
// Above, we added 3 valid quantum execution targets, each of which contributes three targets (simulator, mock, and mock-qir),
// for a total of six targets.
Assert.That.Enumerable(targets).HasCount(6);
// GetActiveTargetAsync, but no active target set yet
ExpectError(AzureClientError.NoTarget, azureClient.GetActiveTargetAsync(new MockChannel()));
// SetActiveTargetAsync with target ID not valid for quantum execution
ExpectError(AzureClientError.InvalidTarget, azureClient.SetActiveTargetAsync(new MockChannel(), "unrecognized.simulator"));
// SetActiveTargetAsync with valid target ID
var target = ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), "ionq.simulator"));
Assert.AreEqual("ionq.simulator", target.TargetId);
// GetActiveTargetAsync
target = ExpectSuccess<TargetStatusInfo>(azureClient.GetActiveTargetAsync(new MockChannel()));
Assert.AreEqual("ionq.simulator", target.TargetId);
}
[DataTestMethod]
[DataRow("--clear", "--clear", ExecuteStatus.Ok)]
[DataRow("--clear", "FullComputation", ExecuteStatus.Ok)]
[DataRow("quantinuum.mock", "FullComputation", ExecuteStatus.Error)]
[DataRow("quantinuum.mock", "BasicMeasurementFeedback", ExecuteStatus.Ok)]
[DataRow("quantinuum.mock", "AdaptiveExecution", ExecuteStatus.Ok)]
public async Task TestManualCapabilities(string targetId, string capabilityName, ExecuteStatus expectedResult) =>
await Assert.That
.UsingEngine(async services =>
{
var client = services.GetRequiredService<IAzureClient>();
await client.ConnectAsync(
new MockChannel(),
"TEST_SUBSCRIPTION_ID",
"TEST_RESOURCE_GROUP_NAME",
"TEST_WORKSPACE_NAME",
"TEST_CONNECTION_STRING",
"TEST_LOCATION",
CredentialType.Environment);
Assert.IsNotNull(client.ActiveWorkspace);
((MockAzureWorkspace)client.ActiveWorkspace)
.AddProviders("ionq", "quantinuum", "unrecognized");
})
.Input("%azure.connect " +
"subscription=TEST_SUBSCRIPTION_ID " +
"resourceGroup=TEST_RESOURCE_GROUP_NAME " +
"workspace=TEST_WORKSPACE_NAME " +
"location=TEST_LOCATION " +
"credential=Environment"
)
.ExecutesSuccessfully()
.Then(async input =>
{
// NB: This is not required, but provides some useful
// diagnostics in output logs.
var channel = new MockChannel();
channel.Display((await input.Engine.Execute("%azure.target", channel, default)).Output);
channel.Display((await input.Engine.Execute("%azure.target-capability", channel, default)).Output);
return input;
})
.Input($"%azure.target {targetId}")
.ExecutesSuccessfully()
.Input($"%azure.target-capability {capabilityName}")
.ExecutesWithStatus(expectedResult);
[TestMethod]
public void TestAllTargets()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// connect to mock workspace with all providers
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient, MockAzureWorkspace.NameWithMockProviders));
// 2 targets per provider: mock and simulator.
// We subtract two due to microsoft not having a mock target. See:
// GitHub Issue: https://github.com/microsoft/iqsharp/issues/609
Assert.That.Enumerable(targets).HasCount(3 * Enum.GetNames(typeof(AzureProvider)).Length - 3);
// set each target, which will load the corresponding package
foreach (var target in targets)
{
var returnedTarget = ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), target.TargetId ?? string.Empty));
Assert.AreEqual(target.TargetId, returnedTarget.TargetId);
}
}
[TestMethod]
public void TestJobSubmission()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
var submissionContext = new AzureSubmissionContext();
// not yet connected
ExpectError(AzureClientError.NotConnected, azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
// connect
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
Assert.IsFalse(targets.Any());
// no target yet
ExpectError(AzureClientError.NoTarget, azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
// add a target
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
azureWorkspace?.AddProviders("ionq");
// set the active target
var target = ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), "ionq.simulator"));
Assert.AreEqual("ionq.simulator", target.TargetId);
// no operation name specified
ExpectError(AzureClientError.NoOperationName, azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
// specify an operation name, but have missing parameters
submissionContext.OperationName = "Tests.qss.HelloAgain";
ExpectError(AzureClientError.JobSubmissionFailed, azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
// specify input parameters and verify that the job was submitted
submissionContext.InputParameters = AbstractMagic.ParseInputParameters("count=3 name=\"testing\"");
var job = ExpectSuccess<CloudJob>(azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
var retrievedJob = ExpectSuccess<CloudJob>(azureClient.GetJobStatusAsync(new MockChannel(), job.Id));
Assert.AreEqual(job.Id, retrievedJob.Id);
}
[TestMethod]
public void TestJobExecution()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// connect
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
Assert.IsFalse(targets.Any());
// add a target
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
azureWorkspace?.AddProviders("ionq");
// set the active target
var target = ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), "ionq.simulator"));
Assert.AreEqual("ionq.simulator", target.TargetId);
// execute the job and verify that the results are retrieved successfully
var submissionContext = new AzureSubmissionContext()
{
OperationName = "Tests.qss.HelloAgain",
InputParameters = AbstractMagic.ParseInputParameters("count=3 name=\"testing\""),
ExecutionTimeout = 5,
ExecutionPollingInterval = 1,
};
var histogram = ExpectSuccess<Histogram>(azureClient.ExecuteJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
Assert.IsNotNull(histogram);
}
[TestMethod]
public void TestJobExecutionWithArrayInput()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// connect
var targets = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
Assert.IsFalse(targets.Any());
// add a target
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
azureWorkspace?.AddProviders("ionq");
// set the active target
var target = ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), "ionq.simulator"));
Assert.AreEqual("ionq.simulator", target.TargetId);
// execute the job and verify that the results are retrieved successfully
var submissionContext = new AzureSubmissionContext()
{
OperationName = "Tests.qss.SayHelloWithArray",
InputParameters = AbstractMagic.ParseInputParameters("{\"names\": [\"foo\", \"bar\"]}"),
ExecutionTimeout = 5,
ExecutionPollingInterval = 1,
};
var histogram = ExpectSuccess<Histogram>(azureClient.ExecuteJobAsync(new MockChannel(), submissionContext, CancellationToken.None));
Assert.IsNotNull(histogram);
}
[DataTestMethod]
[DataRow("ionq.mock", null)]
[DataRow("quantinuum.mock", null)]
public async Task TestRuntimeCapabilities(string targetId, AzureClientError? expectedError)
{
var services = Startup.CreateServiceProvider("Workspace.QPRGen1");
var azureClient = services.GetRequiredService<IAzureClient>();
var packagesService = services.GetRequiredService<INugetPackages>();
// Choose an operation with measurement result comparison, which should
// generate warnings on QPRGen0 targets but succeed on QPRGen1 targets
var submissionContext = new AzureSubmissionContext() { OperationName = "Tests.qss.CompareMeasurementResult" };
ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient));
// Set up workspace with mock providers
var azureWorkspace = azureClient.ActiveWorkspace as MockAzureWorkspace;
Assert.IsNotNull(azureWorkspace);
azureWorkspace?.AddProviders("ionq", "quantinuum");
// Verify that IonQ job does not generate errors (QPRGen0)
ExpectSuccess<TargetStatusInfo>(azureClient.SetActiveTargetAsync(new MockChannel(), targetId));
var task = azureClient.SubmitJobAsync(new MockChannel(), submissionContext, CancellationToken.None);
if (expectedError is {} error)
{
ExpectError(error, task);
}
else
{
var job = ExpectSuccess<CloudJob>(task);
Assert.IsNotNull(job);
}
}
[TestMethod]
public void TestLocations()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
// Locations with whitespace should be converted correctly
_ = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient, locationName: "Australia Central 2"));
Assert.AreEqual("australiacentral2", azureClient.ActiveWorkspace?.Location);
// No location provided should fail
ExpectError(AzureClientError.NoWorkspaceLocation, ConnectToWorkspaceAsync(azureClient, locationName: ""));
ExpectError(AzureClientError.NoWorkspaceLocation, ConnectToWorkspaceAsync(azureClient, locationName: " "));
// Invalid locations should fail
ExpectError(AzureClientError.InvalidWorkspaceLocation, ConnectToWorkspaceAsync(azureClient, locationName: "#"));
ExpectError(AzureClientError.InvalidWorkspaceLocation, ConnectToWorkspaceAsync(azureClient, locationName: "/test/"));
}
[TestMethod]
public void TestConnectedEvent()
{
var services = Startup.CreateServiceProvider("Workspace");
var azureClient = services.GetRequiredService<IAzureClient>();
ConnectToWorkspaceEventArgs? lastArgs = null;
// connect
azureClient.ConnectToWorkspace += (object? sender, ConnectToWorkspaceEventArgs e) =>
{
lastArgs = e;
};
ExpectError(AzureClientError.WorkspaceNotFound, azureClient.ConnectAsync(
new MockChannel(),
"TEST_SUBSCRIPTION_ID",
"TEST_RESOURCE_GROUP_NAME",
MockAzureWorkspace.NameForInvalidWorkspace,
string.Empty,
"TEST_LOCATION",
CredentialType.Default));
Assert.IsNotNull(lastArgs);
if (lastArgs != null)
{
Assert.AreEqual(ExecuteStatus.Error, lastArgs.Status);
Assert.AreEqual(AzureClientError.WorkspaceNotFound, lastArgs.Error);
Assert.AreEqual(CredentialType.Default, lastArgs.CredentialType);
Assert.AreEqual("TEST_LOCATION", lastArgs.Location);
Assert.AreEqual(false, lastArgs.UseCustomStorage);
}
lastArgs = null;
_ = ExpectSuccess<IEnumerable<TargetStatusInfo>>(ConnectToWorkspaceAsync(azureClient, locationName: "TEST_LOCATION"));
Assert.IsNotNull(lastArgs);
if (lastArgs != null)
{
Assert.AreEqual(ExecuteStatus.Ok, lastArgs.Status);
Assert.AreEqual(null, lastArgs.Error);
Assert.AreEqual(CredentialType.Environment, lastArgs.CredentialType);
Assert.AreEqual("TEST_LOCATION", lastArgs.Location);
Assert.AreEqual(true, lastArgs.UseCustomStorage);
}
}
[TestMethod]
public void TestCloudJobExtensions()
{
// Note about currency formatting:
// It seems that there is a differency of implementations when using the "C" currency
// formatting accross different OSs.
// For example, in my dev box I was getting "R$ 12.00" and in the build agent
// it was producing "R$12,00" even when explicitly passing the CultureInfo
// So instead of using the expected string literals, we are using
// .ToString("C", CurrencyHelper.GetCultureInfoForCurrencyCode("USD")
// to guarantee consistency in the unit test.
const string jobId = "myjobid";
const string jobName = "myjobname";
var jobStatus = JobStatus.Succeeded;
const string jobProviderId = "microsoft";
const string jobTarget = "microsoft.paralleltempering-parameterfree.cpu";
var jobCreationTime = new DateTimeOffset(2021, 08, 12, 01, 02, 03, TimeSpan.Zero);
var jobBeginExecutionTime = new DateTimeOffset(2021, 08, 12, 02, 02, 03, TimeSpan.Zero);
var jobEndExecutionTime = new DateTimeOffset(2021, 08, 12, 03, 02, 03, TimeSpan.Zero);
var costEstimate = new MockCostEstimate("USD", new List<UsageEvent>(), 123.45f);
var costEstimateString = 123.45f.ToString("C", CurrencyHelper.GetCultureInfoForCurrencyCode("USD"));
// Test Cost Estimate formatting
var cloudJob = new MockCloudJob();
cloudJob.Details.CostEstimate = new MockCostEstimate("USD", new List<UsageEvent>(), 123.45f);
Assert.AreEqual(123.45f.ToString("C", CurrencyHelper.GetCultureInfoForCurrencyCode("USD")), cloudJob.GetCostEstimateText());
cloudJob.Details.CostEstimate = new MockCostEstimate("BRL", new List<UsageEvent>(), 12f);
Assert.AreEqual(12f.ToString("C", CurrencyHelper.GetCultureInfoForCurrencyCode("BRL")), cloudJob.GetCostEstimateText());
cloudJob.Details.CostEstimate = new MockCostEstimate("", new List<UsageEvent>(), 12f);
Assert.AreEqual(12f.ToString("F2"), cloudJob.GetCostEstimateText());
cloudJob.Details.CostEstimate = new MockCostEstimate("CustomCurrency", new List<UsageEvent>(), 12f);
Assert.AreEqual($"CustomCurrency {12f:F2}", cloudJob.GetCostEstimateText());
cloudJob.Details.CostEstimate = null;
Assert.AreEqual("", cloudJob.GetCostEstimateText());
// Test CloudJob to Dictionary
cloudJob = new MockCloudJob(id: jobId);
cloudJob.Details.Name = jobName;
cloudJob.Details.Status = jobStatus;
cloudJob.Details.ProviderId = jobProviderId;
cloudJob.Details.Target = jobTarget;
cloudJob.Details.CreationTime = jobCreationTime;
cloudJob.Details.BeginExecutionTime = jobBeginExecutionTime;
cloudJob.Details.EndExecutionTime = jobEndExecutionTime;
cloudJob.Details.CostEstimate = costEstimate;
var dictionary = cloudJob.ToDictionary();
Assert.AreEqual(jobId, dictionary["id"]);
Assert.AreEqual(jobName, dictionary["name"]);
Assert.AreEqual(jobStatus.ToString(), dictionary["status"]);
Assert.AreEqual(cloudJob.Uri.ToString(), dictionary["uri"]);
Assert.AreEqual(jobProviderId, dictionary["provider"]);
Assert.AreEqual(jobTarget, dictionary["target"]);
Assert.AreEqual(cloudJob.Details.CreationTime, dictionary["creation_time"]);
Assert.AreEqual(cloudJob.Details.BeginExecutionTime, dictionary["begin_execution_time"]);
Assert.AreEqual(cloudJob.Details.EndExecutionTime, dictionary["end_execution_time"]);
Assert.AreEqual(costEstimateString, dictionary["cost_estimate"]);
// Test CloudJob to JupyterTable
var cloudJobs = new List<CloudJob> { cloudJob, new MockCloudJob() };
var table = cloudJobs.ToJupyterTable();
var expectedValues = new List<(string, string)>
{
("Job Name", jobName),
("Job ID", $"<a href=\"{cloudJob.Uri}\" target=\"_blank\">{jobId}</a>"),
("Job Status", jobStatus.ToString()),
("Target", jobTarget),
("Creation Time", jobCreationTime.ToString()),
("Begin Execution Time", jobBeginExecutionTime.ToString()),
("End Execution Time", jobEndExecutionTime.ToString()),
("Cost Estimate", costEstimateString),
};
Assert.AreEqual(cloudJobs.Count, table.Rows.Count);
Assert.AreEqual(expectedValues.Count, table.Columns.Count);
foreach ((var expected, var actual) in Enumerable.Zip(expectedValues, table.Columns))
{
Assert.AreEqual(expected.Item1, actual.Item1);
Assert.AreEqual(expected.Item2, actual.Item2(cloudJob));
}
}
}
}