Skip to content

Commit

Permalink
[feature/PI-592-gsi_upgrade] gsi upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jaklinger committed Nov 8, 2024
1 parent ae2bad2 commit 21f0ad3
Show file tree
Hide file tree
Showing 43 changed files with 1,331 additions and 1,595 deletions.
18 changes: 5 additions & 13 deletions infrastructure/terraform/per_workspace/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,15 @@ module "table" {
attributes = [
{ name = "pk", type = "S" },
{ name = "sk", type = "S" },
{ name = "pk_1", type = "S" },
{ name = "sk_1", type = "S" },
{ name = "pk_2", type = "S" },
{ name = "sk_2", type = "S" }
{ name = "pk_read", type = "S" },
{ name = "sk_read", type = "S" },
]

global_secondary_indexes = [
{
name = "idx_gsi_1"
hash_key = "pk_1"
range_key = "sk_1"
projection_type = "ALL"
},
{
name = "idx_gsi_2"
hash_key = "pk_2"
range_key = "sk_2"
name = "idx_gsi_read"
hash_key = "pk_read"
range_key = "sk_read"
projection_type = "ALL"
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/api/createCpmProductForEpr/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_index():
table_name=TABLE_NAME, dynamodb_client=index.cache["DYNAMODB_CLIENT"]
)
read_product = repo.read(
product_team_id=product_team.id, product_id=created_product["id"]
product_team_id=product_team.id, id=created_product["id"]
).state()

assert created_product == read_product
Expand Down
10 changes: 7 additions & 3 deletions src/api/createDevice/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ def test_index() -> None:
assert device.name == DEVICE_NAME
assert device.ods_code == ODS_CODE
assert device.created_on.date() == datetime.today().date()
assert device.updated_on is None
assert device.deleted_on is None
assert not device.updated_on
assert not device.deleted_on

# Retrieve the created resource
repo = DeviceRepository(
table_name=TABLE_NAME, dynamodb_client=index.cache["DYNAMODB_CLIENT"]
)

created_device = repo.read(device.id)
created_device = repo.read(
product_team_id=device.product_team_id,
product_id=device.product_id,
id=device.id,
)
assert created_device == device


Expand Down
2 changes: 1 addition & 1 deletion src/api/createDeviceReferenceData/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_index() -> None:
created_device_reference_data = repo.read(
product_team_id=device_reference_data.product_team_id,
product_id=device_reference_data.product_id,
device_reference_data_id=device_reference_data.id,
id=device_reference_data.id,
)
assert created_device_reference_data == device_reference_data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_index_without_questionnaire() -> None:
created_device_reference_data = repo.read(
product_team_id=device_reference_data.product_team_id,
product_id=device_reference_data.product_id,
device_reference_data_id=device_reference_data.id,
id=device_reference_data.id,
)
assert created_device_reference_data == device_reference_data

Expand Down Expand Up @@ -157,6 +157,6 @@ def test_index_with_questionnaire() -> None:
created_device_reference_data = repo.read(
product_team_id=device_reference_data.product_team_id,
product_id=device_reference_data.product_id,
device_reference_data_id=device_reference_data.id,
id=device_reference_data.id,
)
assert created_device_reference_data == device_reference_data
36 changes: 8 additions & 28 deletions src/api/deleteCpmProduct/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from unittest import mock

import pytest
from domain.core.cpm_product.v1 import CpmProduct
from domain.core.cpm_system_id.v1 import ProductId
from domain.core.enum import Status
from domain.core.root.v3 import Root
from domain.repository.cpm_product_repository.v3 import CpmProductRepository
from domain.repository.cpm_product_repository.v3 import (
CpmProductRepository,
InactiveCpmProductRepository,
)
from domain.repository.errors import ItemNotFound
from domain.repository.keys.v3 import TableKey
from domain.repository.marshall import marshall, unmarshall
from domain.repository.product_team_repository.v2 import ProductTeamRepository

from test_helpers.dynamodb import mock_table
Expand All @@ -27,26 +27,6 @@
VERSION = 1


class MockCpmProductRepository(CpmProductRepository):
def read_inactive_product(
self, product_team_id: str, product_id: str
) -> CpmProduct:
pk = TableKey.CPM_PRODUCT_STATUS.key(Status.INACTIVE, product_team_id)
sk = TableKey.CPM_PRODUCT.key(product_id)
args = {
"TableName": self.table_name,
"KeyConditionExpression": "pk = :pk AND sk = :sk",
"ExpressionAttributeValues": marshall(**{":pk": pk, ":sk": sk}),
}
result = self.client.query(**args)
items = [unmarshall(i) for i in result["Items"]]
if len(items) == 0:
raise ItemNotFound(product_team_id, product_id, item_type=CpmProduct)
(item,) = items

return CpmProduct(**item)


@contextmanager
def mock_lambda():
org = Root.create_ods_organisation(ods_code=CPM_PRODUCT_TEAM_NO_ID["ods_code"])
Expand Down Expand Up @@ -100,13 +80,13 @@ def test_index():
table_name=TABLE_NAME, dynamodb_client=index.cache["DYNAMODB_CLIENT"]
)
with pytest.raises(ItemNotFound):
repo.read(product_team_id=product_team.id, product_id=PRODUCT_ID)
repo.read(product_team_id=product_team.id, id=PRODUCT_ID)

repo = MockCpmProductRepository(
repo = InactiveCpmProductRepository(
table_name=TABLE_NAME, dynamodb_client=index.cache["DYNAMODB_CLIENT"]
)
deleted_product = repo.read_inactive_product(
product_team_id=product_team.id, product_id=PRODUCT_ID
deleted_product = repo.read(
product_team_id=product_team.id, id=PRODUCT_ID
).dict()

# Sense checks on the deleted resource
Expand Down
8 changes: 6 additions & 2 deletions src/api/readDevice/src/v1/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def read_product(data, cache) -> CpmProduct:
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
cpm_product = product_repo.read(
product_id=path_params.product_id, product_team_id=path_params.product_team_id
id=path_params.product_id, product_team_id=path_params.product_team_id
)
return cpm_product

Expand All @@ -42,7 +42,11 @@ def read_device(data, cache) -> Device:
device_repo = DeviceRepository(
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
return device_repo.read(path_params.device_id)
return device_repo.read(
product_team_id=path_params.product_team_id,
product_id=path_params.product_id,
id=path_params.device_id,
)


def device_to_dict(data, cache) -> tuple[str, dict]:
Expand Down
2 changes: 1 addition & 1 deletion src/api/readDevice/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_index_no_such_device(version):
"errors": [
{
"code": "RESOURCE_NOT_FOUND",
"message": "Could not find Device for key ('does not exist')", # device saved by pk, sk = device.id still
"message": f"Could not find Device for key ('{product_team.id}', 'P.XXX-YYY', 'does not exist')",
}
],
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/readDeviceReferenceData/src/v1/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ def read_product(data, cache) -> CpmProduct:
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
cpm_product = product_repo.read(
product_id=path_params.product_id, product_team_id=path_params.product_team_id
id=path_params.product_id, product_team_id=path_params.product_team_id
)
return cpm_product


def read_device_reference_data(data, cache) -> DeviceReferenceData:
path_params: DeviceReferenceDataPathParams = data[parse_path_params]
product_repo = DeviceReferenceDataRepository(
device_reference_data_repo = DeviceReferenceDataRepository(
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
return product_repo.read(
return device_reference_data_repo.read(
product_id=path_params.product_id,
product_team_id=path_params.product_team_id,
device_reference_data_id=path_params.device_reference_data_id,
id=path_params.device_reference_data_id,
)


Expand Down
4 changes: 1 addition & 3 deletions src/api/searchCpmProduct/src/v1/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def query_products(data, cache) -> list:
cpm_product_repo = CpmProductRepository(
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
results = cpm_product_repo.query_products_by_product_team(
product_team_id=product_team_id
)
results = cpm_product_repo.search(product_team_id=product_team_id)
return results


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ Feature: Read Device - failure scenarios
And I note the response field "$.id" as "device_id"
When I make a "GET" request with "default" headers to "ProductTeam/${ note(product_team_id) }/Product/${ note(product_id) }/Device/not-a-device"
Then I receive a status code "404" with body
| path | value |
| errors.0.code | RESOURCE_NOT_FOUND |
| errors.0.message | Could not find Device for key ('not-a-device') |
| path | value |
| errors.0.code | RESOURCE_NOT_FOUND |
| errors.0.message | Could not find Device for key ('${ note(product_team_id) }', '${ note(product_id) }', 'not-a-device') |
And the response headers contain:
| name | value |
| Content-Type | application/json |
| Content-Length | 105 |
| Content-Length | 164 |
2 changes: 1 addition & 1 deletion src/layers/domain/api/common_steps/read_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def read_product(data, cache) -> CpmProduct:
table_name=cache["DYNAMODB_TABLE"], dynamodb_client=cache["DYNAMODB_CLIENT"]
)
cpm_product = product_repo.read(
product_id=path_params.product_id, product_team_id=path_params.product_team_id
id=path_params.product_id, product_team_id=path_params.product_team_id
)
return cpm_product

Expand Down
Loading

0 comments on commit 21f0ad3

Please sign in to comment.