forked from getmoto/moto
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement retrieving from _custom_id_ tag
- Loading branch information
1 parent
3f67c65
commit 64033bc
Showing
4 changed files
with
203 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from moto.utilities.id_generator import ( | ||
TAG_KEY_CUSTOM_ID, | ||
ExistingIds, | ||
ResourceIdentifier, | ||
Tags, | ||
moto_id, | ||
) | ||
|
||
ACCOUNT = "account" | ||
REGION = "us-east-1" | ||
RESOURCE_NAME = "my-resource" | ||
|
||
CUSTOM_ID = "custom" | ||
GENERATED_ID = "generated" | ||
TAG_ID = "fromTag" | ||
SERVICE = "test-service" | ||
RESOURCE = "test-resource" | ||
|
||
|
||
@moto_id | ||
def generate_test_id( | ||
resource_identifier: ResourceIdentifier, | ||
existing_ids: ExistingIds = None, | ||
tags: Tags = None, | ||
): | ||
return GENERATED_ID | ||
|
||
|
||
class TestResourceIdentifier(ResourceIdentifier): | ||
service = SERVICE | ||
resource = RESOURCE | ||
|
||
def generate(self, existing_ids: ExistingIds = None, tags: Tags = None) -> str: | ||
return generate_test_id( | ||
resource_identifier=self, existing_ids=existing_ids, tags=tags | ||
) | ||
|
||
|
||
def test_generate_with_no_resource_identifier(): | ||
generated_id = generate_test_id(None) | ||
assert generated_id == GENERATED_ID | ||
|
||
|
||
def test_generate_with_matching_resource_identifier(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
set_custom_id(resource_identifier, CUSTOM_ID) | ||
|
||
generated_id = generate_test_id(resource_identifier=resource_identifier) | ||
assert generated_id == CUSTOM_ID | ||
|
||
|
||
def test_generate_with_non_matching_resource_identifier(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
resource_identifier_2 = TestResourceIdentifier(ACCOUNT, REGION, "non-matching") | ||
|
||
set_custom_id(resource_identifier, CUSTOM_ID) | ||
|
||
generated_id = generate_test_id(resource_identifier=resource_identifier_2) | ||
assert generated_id == GENERATED_ID | ||
|
||
|
||
def test_generate_with_custom_id_tag(): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
generated_id = generate_test_id( | ||
resource_identifier=resource_identifier, tags={TAG_KEY_CUSTOM_ID: TAG_ID} | ||
) | ||
assert generated_id == TAG_ID | ||
|
||
|
||
def test_generate_with_custom_id_tag_has_priority(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
set_custom_id(resource_identifier, CUSTOM_ID) | ||
generated_id = generate_test_id( | ||
resource_identifier=resource_identifier, tags={TAG_KEY_CUSTOM_ID: TAG_ID} | ||
) | ||
assert generated_id == TAG_ID | ||
|
||
|
||
def test_generate_with_existing_id(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
set_custom_id(resource_identifier, CUSTOM_ID) | ||
generated_id = generate_test_id( | ||
resource_identifier=resource_identifier, existing_ids=[CUSTOM_ID] | ||
) | ||
assert generated_id == GENERATED_ID | ||
|
||
|
||
def test_generate_with_tags_and_existing_id(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
generated_id = generate_test_id( | ||
resource_identifier=resource_identifier, | ||
existing_ids=[TAG_ID], | ||
tags={TAG_KEY_CUSTOM_ID: TAG_ID}, | ||
) | ||
assert generated_id == GENERATED_ID | ||
|
||
|
||
def test_generate_with_tags_fallback(set_custom_id): | ||
resource_identifier = TestResourceIdentifier(ACCOUNT, REGION, RESOURCE_NAME) | ||
|
||
set_custom_id(resource_identifier, CUSTOM_ID) | ||
generated_id = generate_test_id( | ||
resource_identifier=resource_identifier, | ||
existing_ids=[TAG_ID], | ||
tags={TAG_KEY_CUSTOM_ID: TAG_ID}, | ||
) | ||
assert generated_id == CUSTOM_ID |