|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from codemagic.apple.app_store_connect import IssuerId |
| 6 | +from codemagic.apple.app_store_connect import KeyIdentifier |
| 7 | +from codemagic.apple.resources import ResourceId |
| 8 | +from codemagic.tools import AppStoreConnect |
| 9 | +from codemagic.tools.app_store_connect.actions.latest_build_number_actions import _LatestBuildInfo |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def app_store_connect() -> AppStoreConnect: |
| 14 | + return AppStoreConnect( |
| 15 | + issuer_id=IssuerId("issuer-id"), |
| 16 | + key_identifier=KeyIdentifier("key-identifier"), |
| 17 | + private_key="private-key", |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def test_get_latest_build_number_forwards_version_to_both_sides(app_store_connect: AppStoreConnect): |
| 22 | + application_id = ResourceId("application-id") |
| 23 | + |
| 24 | + asc_info = _LatestBuildInfo( |
| 25 | + build_id="asc-build-id", |
| 26 | + build_number="42", |
| 27 | + app_store_version="1.0.0", |
| 28 | + ) |
| 29 | + tf_info = _LatestBuildInfo( |
| 30 | + build_id="tf-build-id", |
| 31 | + build_number="43", |
| 32 | + pre_release_version="1.0.0", |
| 33 | + ) |
| 34 | + |
| 35 | + with mock.patch.object( |
| 36 | + app_store_connect, |
| 37 | + "_get_app_store_latest_build_info", |
| 38 | + return_value=asc_info, |
| 39 | + ) as mock_app_store, mock.patch.object( |
| 40 | + app_store_connect, |
| 41 | + "_get_testflight_latest_build_info", |
| 42 | + return_value=tf_info, |
| 43 | + ) as mock_testflight, mock.patch.object( |
| 44 | + app_store_connect, |
| 45 | + "_log_latest_build_info", |
| 46 | + ): |
| 47 | + result = app_store_connect.get_latest_build_number(application_id, version="1.0.0") |
| 48 | + |
| 49 | + mock_app_store.assert_called_once_with( |
| 50 | + application_id, |
| 51 | + version_string="1.0.0", |
| 52 | + platform=None, |
| 53 | + ) |
| 54 | + mock_testflight.assert_called_once_with( |
| 55 | + application_id, |
| 56 | + pre_release_version="1.0.0", |
| 57 | + platform=None, |
| 58 | + ) |
| 59 | + assert result == "43" |
| 60 | + |
| 61 | + |
| 62 | +def test_get_latest_build_number_without_version_passes_none(app_store_connect: AppStoreConnect): |
| 63 | + application_id = ResourceId("application-id") |
| 64 | + |
| 65 | + with mock.patch.object( |
| 66 | + app_store_connect, |
| 67 | + "_get_app_store_latest_build_info", |
| 68 | + return_value=None, |
| 69 | + ) as mock_app_store, mock.patch.object( |
| 70 | + app_store_connect, |
| 71 | + "_get_testflight_latest_build_info", |
| 72 | + return_value=None, |
| 73 | + ) as mock_testflight: |
| 74 | + result = app_store_connect.get_latest_build_number(application_id) |
| 75 | + |
| 76 | + mock_app_store.assert_called_once_with( |
| 77 | + application_id, |
| 78 | + version_string=None, |
| 79 | + platform=None, |
| 80 | + ) |
| 81 | + mock_testflight.assert_called_once_with( |
| 82 | + application_id, |
| 83 | + pre_release_version=None, |
| 84 | + platform=None, |
| 85 | + ) |
| 86 | + assert result is None |
| 87 | + |
| 88 | + |
| 89 | +def test_get_latest_build_number_returns_match_when_only_one_side_has_build( |
| 90 | + app_store_connect: AppStoreConnect, |
| 91 | +): |
| 92 | + application_id = ResourceId("application-id") |
| 93 | + |
| 94 | + tf_info = _LatestBuildInfo( |
| 95 | + build_id="tf-build-id", |
| 96 | + build_number="7", |
| 97 | + pre_release_version="2.0.0", |
| 98 | + ) |
| 99 | + |
| 100 | + with mock.patch.object( |
| 101 | + app_store_connect, |
| 102 | + "_get_app_store_latest_build_info", |
| 103 | + return_value=None, |
| 104 | + ), mock.patch.object( |
| 105 | + app_store_connect, |
| 106 | + "_get_testflight_latest_build_info", |
| 107 | + return_value=tf_info, |
| 108 | + ), mock.patch.object( |
| 109 | + app_store_connect, |
| 110 | + "_log_latest_build_info", |
| 111 | + ): |
| 112 | + result = app_store_connect.get_latest_build_number(application_id, version="2.0.0") |
| 113 | + |
| 114 | + assert result == "7" |
| 115 | + |
| 116 | + |
| 117 | +def test_get_latest_build_number_picks_higher_build_when_versions_equal( |
| 118 | + app_store_connect: AppStoreConnect, |
| 119 | +): |
| 120 | + application_id = ResourceId("application-id") |
| 121 | + |
| 122 | + asc_info = _LatestBuildInfo( |
| 123 | + build_id="asc-build-id", |
| 124 | + build_number="120", |
| 125 | + app_store_version="3.2.46", |
| 126 | + ) |
| 127 | + tf_info = _LatestBuildInfo( |
| 128 | + build_id="tf-build-id", |
| 129 | + build_number="90", |
| 130 | + pre_release_version="3.2.46", |
| 131 | + ) |
| 132 | + |
| 133 | + with mock.patch.object( |
| 134 | + app_store_connect, |
| 135 | + "_get_app_store_latest_build_info", |
| 136 | + return_value=asc_info, |
| 137 | + ), mock.patch.object( |
| 138 | + app_store_connect, |
| 139 | + "_get_testflight_latest_build_info", |
| 140 | + return_value=tf_info, |
| 141 | + ), mock.patch.object( |
| 142 | + app_store_connect, |
| 143 | + "_log_latest_build_info", |
| 144 | + ): |
| 145 | + result = app_store_connect.get_latest_build_number(application_id, version="3.2.46") |
| 146 | + |
| 147 | + assert result == "120" |
0 commit comments