|
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 |
|
| 6 | +import unittest |
| 7 | +from types import SimpleNamespace |
| 8 | + |
6 | 9 | import pytest |
7 | 10 |
|
8 | 11 | from azure.cli.testsdk import ScenarioTest |
9 | 12 | from .utils import issue_cmd_with_param_missing |
| 13 | +from ...commands import transform_offerings |
| 14 | +from ...operations.offerings import ( |
| 15 | + _get_publisher_and_offer_from_provider_id, |
| 16 | + PUBLISHER_NOT_AVAILABLE, |
| 17 | + OFFER_NOT_AVAILABLE, |
| 18 | +) |
10 | 19 |
|
11 | 20 |
|
12 | 21 | class QuantumOfferingsScenarioTest(ScenarioTest): |
13 | 22 |
|
14 | 23 | def test_offerings_errors(self): |
15 | 24 | issue_cmd_with_param_missing(self, "az quantum offerings accept-terms", "az quantum offerings accept-terms -p MyProviderId -k MySKU -l MyLocation\nOnce terms have been reviewed, accept the invoking this command.") |
16 | 25 | issue_cmd_with_param_missing(self, "az quantum offerings show-terms", "az quantum offerings show-terms -p MyProviderId -k MySKU -l MyLocation\nUse a Provider Id and SKU from `az quantum offerings list` to review the terms.") |
| 26 | + |
| 27 | + |
| 28 | +class QuantumOfferingsUnitTest(unittest.TestCase): |
| 29 | + |
| 30 | + def _make_provider(self, provider_id, managed_application): |
| 31 | + return SimpleNamespace( |
| 32 | + id=provider_id, |
| 33 | + properties=SimpleNamespace(managed_application=managed_application), |
| 34 | + ) |
| 35 | + |
| 36 | + def test_get_publisher_and_offer_from_provider_id_returns_values(self): |
| 37 | + providers = [ |
| 38 | + self._make_provider( |
| 39 | + "rigetti", |
| 40 | + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), |
| 41 | + ), |
| 42 | + ] |
| 43 | + |
| 44 | + self.assertEqual( |
| 45 | + _get_publisher_and_offer_from_provider_id(providers, "rigetti"), |
| 46 | + ("rigetti-computing", "rigetti-azure-basic"), |
| 47 | + ) |
| 48 | + |
| 49 | + def test_get_publisher_and_offer_from_provider_id_matches_case_insensitively(self): |
| 50 | + providers = [ |
| 51 | + self._make_provider( |
| 52 | + "Rigetti", |
| 53 | + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), |
| 54 | + ), |
| 55 | + ] |
| 56 | + |
| 57 | + self.assertEqual( |
| 58 | + _get_publisher_and_offer_from_provider_id(providers, "RIGETTI"), |
| 59 | + ("rigetti-computing", "rigetti-azure-basic"), |
| 60 | + ) |
| 61 | + |
| 62 | + def test_get_publisher_and_offer_from_provider_id_returns_none_when_not_found(self): |
| 63 | + providers = [ |
| 64 | + self._make_provider( |
| 65 | + "rigetti", |
| 66 | + SimpleNamespace(publisher_id="rigetti-computing", offer_id="rigetti-azure-basic"), |
| 67 | + ), |
| 68 | + ] |
| 69 | + |
| 70 | + self.assertEqual( |
| 71 | + _get_publisher_and_offer_from_provider_id(providers, "contoso-v2-provider"), |
| 72 | + (None, None), |
| 73 | + ) |
| 74 | + |
| 75 | + def test_get_publisher_and_offer_from_provider_id_handles_missing_managed_application(self): |
| 76 | + # V2-only providers (e.g. contoso-v2-provider) have no legacy marketplace ManagedApplication |
| 77 | + # association at all; this must not raise and should be treated like the existing |
| 78 | + # "N/A" sentinel (no terms to accept) instead of crashing. |
| 79 | + providers = [self._make_provider("contoso-v2-provider", None)] |
| 80 | + |
| 81 | + self.assertEqual( |
| 82 | + _get_publisher_and_offer_from_provider_id(providers, "contoso-v2-provider"), |
| 83 | + (PUBLISHER_NOT_AVAILABLE, OFFER_NOT_AVAILABLE), |
| 84 | + ) |
| 85 | + |
| 86 | + def test_transform_offerings_handles_missing_managed_application(self): |
| 87 | + offerings = [ |
| 88 | + { |
| 89 | + 'id': 'rigetti', |
| 90 | + 'properties': { |
| 91 | + 'skus': [{'id': 'azure-basic-qvm-only-unlimited'}], |
| 92 | + 'managedApplication': { |
| 93 | + 'publisherId': 'rigetticoinc1644276861431', |
| 94 | + 'offerId': 'rigetti-aq', |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + 'id': 'atom-dev', |
| 100 | + 'properties': { |
| 101 | + 'skus': [{'id': 'default'}], |
| 102 | + 'managedApplication': None, |
| 103 | + }, |
| 104 | + }, |
| 105 | + ] |
| 106 | + |
| 107 | + table = transform_offerings(offerings) |
| 108 | + |
| 109 | + self.assertEqual(len(table), 2) |
| 110 | + self.assertEqual( |
| 111 | + list(table[0].keys()), |
| 112 | + ['Provider ID', 'SKU', 'Publisher ID', 'Offer ID'], |
| 113 | + ) |
| 114 | + self.assertEqual(table[0]['Provider ID'], 'rigetti') |
| 115 | + self.assertEqual(table[0]['Publisher ID'], 'rigetticoinc1644276861431') |
| 116 | + self.assertEqual(table[0]['Offer ID'], 'rigetti-aq') |
| 117 | + self.assertEqual(table[1]['Provider ID'], 'atom-dev') |
| 118 | + self.assertEqual(table[1]['SKU'], 'default') |
| 119 | + self.assertEqual(table[1]['Publisher ID'], PUBLISHER_NOT_AVAILABLE) |
| 120 | + self.assertEqual(table[1]['Offer ID'], OFFER_NOT_AVAILABLE) |
0 commit comments