Skip to content

Commit

Permalink
add helper for getting linked products from a file id
Browse files Browse the repository at this point in the history
  • Loading branch information
adamarnesen committed Aug 9, 2024
1 parent d60e2af commit 56f975a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 23 deletions.
32 changes: 32 additions & 0 deletions nisystemlink/clients/testmonitor/_test_monitor_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import List

from nisystemlink.clients.testmonitor._test_monitor_client import TestMonitorClient
from nisystemlink.clients.testmonitor.models._paged_products import PagedProducts
from nisystemlink.clients.testmonitor.models._product import Product
from nisystemlink.clients.testmonitor.models._query_products_request import (
QueryProductsRequest,
)


def get_products_linked_to_file(
client: TestMonitorClient, file_id: str
) -> List[Product]:
"""Gets a list of all the products that are linked to the file.
Args:
`client` : The `TestMonitorClient` to use for the request.
`file_id`: The id of the file to query links for.
Returns:
A list of all the products that are linked to the file with `file_id`
"""
query_request = QueryProductsRequest(
filter=f'fileIds.Contains("{file_id}")', take=100
)
response: PagedProducts = client.query_products(query_request)
products = response.products
while response.continuation_token:
query_request.continuation_token = response.continuation_token
response = client.query_products(query_request)
products.extend(response.products)
return products
70 changes: 47 additions & 23 deletions tests/integration/testmonitor/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import pytest
from nisystemlink.clients.core._http_configuration import HttpConfiguration
from nisystemlink.clients.testmonitor import models, TestMonitorClient
from nisystemlink.clients.testmonitor._test_monitor_utilities import (
get_products_linked_to_file,
)
from nisystemlink.clients.testmonitor.models import (
CreateProductsPartialSuccess,
Product,
Expand All @@ -23,7 +26,7 @@ def client(enterprise_config: HttpConfiguration) -> TestMonitorClient:


@pytest.fixture
def unique_part_number() -> str:
def unique_identifier() -> str:
"""Unique product id for this test."""
product_id = uuid.uuid1().hex
return product_id
Expand Down Expand Up @@ -93,42 +96,42 @@ def test__create_multiple_products__multiple_creates_succeed(
assert len(response.products) == 2

def test__create_single_product_and_get_products__at_least_one_product_exists(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
products = [Product(part_number=unique_part_number)]
products = [Product(part_number=unique_identifier)]
create_products(products)
get_response = client.get_products()
assert get_response is not None
assert len(get_response.products) >= 1

def test__create_multiple_products_and_get_products_with_take__only_take_returned(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
products = [
Product(part_number=unique_part_number),
Product(part_number=unique_part_number),
Product(part_number=unique_identifier),
Product(part_number=unique_identifier),
]
create_products(products)
get_response = client.get_products(take=1)
assert get_response is not None
assert len(get_response.products) == 1

def test__create_multiple_products_and_get_products_with_count_at_least_one_count(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
products = [
Product(part_number=unique_part_number),
Product(part_number=unique_part_number),
Product(part_number=unique_identifier),
Product(part_number=unique_identifier),
]
create_products(products)
get_response: models.PagedProducts = client.get_products(return_count=True)
assert get_response is not None
assert get_response.total_count is not None and get_response.total_count >= 2

def test__get_product_by_id__product_matches_expected(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
part_number = unique_part_number
part_number = unique_identifier
products = [Product(part_number=part_number)]
create_response: CreateProductsPartialSuccess = create_products(products)
assert create_response is not None
Expand All @@ -138,9 +141,9 @@ def test__get_product_by_id__product_matches_expected(
assert product.part_number == part_number

def test__query_product_by_part_number__matches_expected(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
part_number = unique_part_number
part_number = unique_identifier
products = [Product(part_number=part_number)]
create_response: CreateProductsPartialSuccess = create_products(products)
assert create_response is not None
Expand All @@ -152,9 +155,9 @@ def test__query_product_by_part_number__matches_expected(
assert query_response.products[0].part_number == part_number

def test__query_product_values_for_name__name_matches(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
part_number = unique_part_number
part_number = unique_identifier
test_name = "query values test"
create_response: CreateProductsPartialSuccess = create_products(
[Product(part_number=part_number, name=test_name)]
Expand All @@ -169,12 +172,12 @@ def test__query_product_values_for_name__name_matches(
assert query_response[0] == test_name

def test__update_keywords_with_replace__keywords_replaced(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
original_keyword = "originalKeyword"
updated_keyword = "updatedKeyword"
create_response: CreateProductsPartialSuccess = create_products(
[Product(part_number=unique_part_number, keywords=[original_keyword])]
[Product(part_number=unique_identifier, keywords=[original_keyword])]
)
assert create_response is not None
assert len(create_response.products) == 1
Expand All @@ -190,12 +193,12 @@ def test__update_keywords_with_replace__keywords_replaced(
assert original_keyword not in update_response.products[0].keywords

def test__update_keywords_no_replace__keywords_appended(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
original_keyword = "originalKeyword"
additional_keyword = "additionalKeyword"
create_response: CreateProductsPartialSuccess = create_products(
[Product(part_number=unique_part_number, keywords=[original_keyword])]
[Product(part_number=unique_identifier, keywords=[original_keyword])]
)
assert create_response is not None
assert len(create_response.products) == 1
Expand All @@ -214,13 +217,13 @@ def test__update_keywords_no_replace__keywords_appended(
)

def test__update_properties_with_replace__properties_replaced(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
new_key = "newKey"
original_properties = {"originalKey": "originalValue"}
new_properties = {new_key: "newValue"}
create_response: CreateProductsPartialSuccess = create_products(
[Product(part_number=unique_part_number, properties=original_properties)]
[Product(part_number=unique_identifier, properties=original_properties)]
)
assert create_response is not None
assert len(create_response.products) == 1
Expand All @@ -239,14 +242,14 @@ def test__update_properties_with_replace__properties_replaced(
)

def test__update_properties_append__properties_appended(
self, client: TestMonitorClient, create_products, unique_part_number
self, client: TestMonitorClient, create_products, unique_identifier
):
original_key = "originalKey"
new_key = "newKey"
original_properties = {original_key: "originalValue"}
new_properties = {new_key: "newValue"}
create_response: CreateProductsPartialSuccess = create_products(
[Product(part_number=unique_part_number, properties=original_properties)]
[Product(part_number=unique_identifier, properties=original_properties)]
)
assert create_response is not None
assert len(create_response.products) == 1
Expand All @@ -267,3 +270,24 @@ def test__update_properties_append__properties_appended(
== original_properties[original_key]
)
assert updated_product.properties[new_key] == new_properties[new_key]

def test__query_products_linked_to_files_correct_products_returned(
self, client: TestMonitorClient, create_products
):
file_id = uuid.uuid1().hex
product_name_with_file = "Has File"
products = [
Product(
part_number=uuid.uuid1().hex,
name=product_name_with_file,
file_ids=[file_id],
),
Product(part_number=uuid.uuid1().hex, name="No File Link"),
]
print(products)
create_response: CreateProductsPartialSuccess = create_products(products)
assert create_response is not None
assert len(create_response.products) == 2
linked_products = get_products_linked_to_file(client, file_id)
names = [product.name for product in linked_products]
assert product_name_with_file in names

0 comments on commit 56f975a

Please sign in to comment.