Пример #1
0
class TestListObjectType(unittest.TestCase):
    def __init__(self, method_name: str):
        super().__init__(methodName=method_name)
        self.users_table = PlaygroundDynamoDBBasicTable(data_model=TableModel)

    def test_untyped_list(self):
        random_list_values: List[Any] = list(str(uuid4()) for i in range(5))
        single_valid_list_item = random_list_values[2]
        random_list_values.append(42)
        # Add a value of different type, that is valid to the untypedList

        set_success: bool = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.untypedList',
            value_to_set=random_list_values
        )
        self.assertTrue(set_success)

        retrieved_list_item: Optional[str or int] = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.untypedList.{{listIndex}}',
            query_kwargs={'listIndex': 2}
        )
        self.assertEqual(retrieved_list_item, single_valid_list_item)

        retrieved_entire_list: Optional[list] = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.untypedList',
            query_kwargs={'listIndex': 2}
        )
        self.assertEqual(retrieved_entire_list, random_list_values)

        deletion_success: bool = self.users_table.delete_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.untypedList'
        )
        self.assertTrue(deletion_success)

    def test_typed_list(self):
        valid_random_list_values: List[str] = [str(uuid4()) for i in range(5)]
        single_valid_list_item = valid_random_list_values[2]
        random_list_values = valid_random_list_values.copy()
        random_list_values.append(42)
        # Add invalid value to the random_list_values

        set_success: bool = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.typedList',
            value_to_set=random_list_values
        )
        self.assertTrue(set_success)

        query_kwargs = {'listIndex': 2}
        retrieved_list_item: Optional[dict] = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.typedList.{{listIndex}}',
            query_kwargs=query_kwargs
        )
        self.assertEqual(retrieved_list_item, single_valid_list_item)

        retrieved_entire_list: Optional[list] = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.typedList',
            query_kwargs=query_kwargs
        )
        self.assertEqual(valid_random_list_values, retrieved_entire_list)

        deletion_success: bool = self.users_table.delete_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='container.typedList',
            query_kwargs=query_kwargs
        )
        self.assertTrue(deletion_success)
 def __init__(self, method_name: str):
     super().__init__(methodName=method_name)
     self.users_table = PlaygroundDynamoDBBasicTable(data_model=TableModel)
class TestsNestedStructuresInsideStructureValues(unittest.TestCase):
    def __init__(self, method_name: str):
        super().__init__(methodName=method_name)
        self.users_table = PlaygroundDynamoDBBasicTable(data_model=TableModel)

    def test_nested_dict_dict_structure(self):
        random_parent_key = f"parentKey_{uuid4()}"
        random_child_key = f"childKey_{uuid4()}"

        keys_fields_switch = list(self.users_table.fields_switch.keys())
        self.assertIn('nestedDictDictStructure.{{itemKey}}.{{itemKeyChild}}',
                      keys_fields_switch)

        update_success: bool = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='nestedDictDictStructure.{{itemKey}}.{{itemKeyChild}}',
            query_kwargs={
                'itemKey': random_parent_key,
                'itemKeyChild': random_child_key
            },
            value_to_set=True)
        self.assertTrue(update_success)

        retrieved_item = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='nestedDictDictStructure.{{itemKey}}',
            query_kwargs={'itemKey': random_parent_key})
        self.assertEqual(retrieved_item, {random_child_key: True})

        removed_item = self.users_table.remove_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='nestedDictDictStructure.{{itemKey}}',
            query_kwargs={'itemKey': random_parent_key})
        self.assertEqual(removed_item, {random_child_key: True})

        retrieved_expected_none_item = self.users_table.get_field(
            TEST_ACCOUNT_ID,
            field_path='nestedDictDictStructure.{{itemKey}}',
            query_kwargs={'itemKey': random_parent_key})
        self.assertIsNone(retrieved_expected_none_item)

    def test_nested_dict_list_structure(self):
        # todo: implement
        pass

    def test_nested_dict_set_structure(self):
        # todo: implement
        pass

    def test_nested_dict_dict_model_structure(self):
        random_parent_key = f"parentKey_{uuid4()}"
        random_child_key = f"childKey_{uuid4()}"
        random_nested_model_value = f"value_{uuid4()}"

        keys_fields_switch = list(self.users_table.fields_switch.keys())
        self.assertIn(
            'nestedDictDictModelStructure.{{itemKey}}.{{itemKeyChild}}',
            keys_fields_switch)

        update_success: bool = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID,
            field_path=
            'nestedDictDictModelStructure.{{itemKey}}.{{itemKeyChild}}',
            query_kwargs={
                'itemKey': random_parent_key,
                'itemKeyChild': random_child_key
            },
            value_to_set={'value': random_nested_model_value})
        self.assertTrue(update_success)

        retrieved_nested_model_value = self.users_table.get_field(
            key_value=TEST_ACCOUNT_ID,
            field_path=
            'nestedDictDictModelStructure.{{itemKey}}.{{itemKeyChild}}.value',
            query_kwargs={
                'itemKey': random_parent_key,
                'itemKeyChild': random_child_key
            })
        self.assertEqual(retrieved_nested_model_value,
                         random_nested_model_value)

        removed_item = self.users_table.remove_field(
            key_value=TEST_ACCOUNT_ID,
            field_path='nestedDictDictModelStructure.{{itemKey}}',
            query_kwargs={'itemKey': random_parent_key})
        self.assertEqual(
            removed_item,
            {random_child_key: {
                'value': random_nested_model_value
            }})

        retrieved_expected_none_item = self.users_table.get_field(
            TEST_ACCOUNT_ID,
            field_path='nestedDictDictModelStructure.{{itemKey}}',
            query_kwargs={'itemKey': random_parent_key})
        self.assertIsNone(retrieved_expected_none_item)
Пример #4
0
class TestRequestFailsOnFieldsInitializationButRequireExistingFieldsToBeUpdateD(unittest.TestCase):
    """
    This test a rare case, where if the update_multiple_fields is used, and one of the field to update caused the
    request to failed (for example, trying to navigate into an item that does not exist), the library will of course
    try to initialize the missing fields. The library has no idea which field caused the request to fail, so, it will
    try to initialize all of the field, one per one, only if they are not existing. In a single update operation,
    this never cause issues, but in a multi fields update operation, we could have an existing field, that did not
    cause the operation the fail, and that would need to be modify. Yet, since it already exist, the library will not
    correctly modify it as the request instructed it to do. To prevent this issue, if an item already exist the library
    will retrieve its UPDATED_NEW value in the same request (to avoid a get request), and check if the value present in
    the database is the value that our request specified. If its not the case, we will send a new request to modify the
    value. This request will not try to initialize fields if she fails, to avoid potential infinite recursion.
    """

    def __init__(self, method_name: str):
        super().__init__(methodName=method_name)
        self.users_table = PlaygroundDynamoDBBasicTable(data_model=TableModel)

    def test_with_string_value_as_target(self):
        random_id = str(uuid4())
        random_value_one = f"one_{uuid4()}"
        random_value_two = f"two_{uuid4()}"

        initial_update_with_empty_value_success = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID, field_path='fieldAlreadyInitializedRequiringNewValue', value_to_set="initial"
        )
        self.assertTrue(initial_update_with_empty_value_success)

        update_success = self.users_table.update_multiple_fields(key_value=TEST_ACCOUNT_ID, setters=[
            FieldSetter(field_path='fieldAlreadyInitializedRequiringNewValue', value_to_set=random_value_one),
            FieldSetter(
                field_path='containerThatWillFailRequestAndRequireFieldsInitialization.{{id}}.firstNestedValue',
                query_kwargs={'id': random_id}, value_to_set=random_value_two
            )
        ])
        self.assertTrue(update_success)

        get_response_data = self.users_table.get_multiple_fields(key_value=TEST_ACCOUNT_ID, getters={
            'one': FieldGetter(field_path='fieldAlreadyInitializedRequiringNewValue'),
            'two': FieldGetter(field_path='containerThatWillFailRequestAndRequireFieldsInitialization.{{id}}.firstNestedValue', query_kwargs={'id': random_id})
        })
        self.assertEqual(get_response_data.get('one', None), random_value_one)
        self.assertEqual(get_response_data.get('two', None), random_value_two)

    def test_with_dict_values_as_target(self):
        nested_container_field_path = 'containerThatWillFailRequestAndRequireFieldsInitialization.{{id}}.nestedContainer'
        random_first_container_id_one = f"first_container_{uuid4()}"
        random_first_container_id_two = f"second_container_{uuid4()}"
        random_nested_container_id_one = f"id_one_{str(uuid4())}"
        random_nested_container_id_two = f"id_two_{str(uuid4())}"
        random_container_field_value_one = {'secondNestedValue': f"container_field_value_one_{uuid4()}"}
        random_container_field_value_two = {'secondNestedValue': f"container_field_value_two_{uuid4()}"}

        # Initial update that put a single item in the container without risking to override existing fields
        initial_update_with_empty_value_success = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID, field_path=nested_container_field_path + '.{{nestedContainerKey}}',
            query_kwargs={'id': random_first_container_id_one, 'nestedContainerKey': random_nested_container_id_one},
            value_to_set=random_container_field_value_one
        )
        self.assertTrue(initial_update_with_empty_value_success)

        # Multi update operation, that could cause the
        update_success = self.users_table.update_field(
            key_value=TEST_ACCOUNT_ID, field_path=nested_container_field_path + '.{{nestedContainerKey}}',
            query_kwargs={'id': random_first_container_id_two, 'nestedContainerKey': random_nested_container_id_two},
            value_to_set=random_container_field_value_two
        )
        self.assertTrue(update_success)

        retrieved_containers_data = self.users_table.get_multiple_fields(key_value=TEST_ACCOUNT_ID, getters={
            'one': FieldGetter(field_path=nested_container_field_path, query_kwargs={'id': random_first_container_id_one}),
            'two': FieldGetter(field_path=nested_container_field_path, query_kwargs={'id': random_first_container_id_two})
        })
        self.assertEqual(retrieved_containers_data['one'].get(random_nested_container_id_one, None), random_container_field_value_one)
        self.assertEqual(retrieved_containers_data['two'].get(random_nested_container_id_two, None), random_container_field_value_two)
    def __init__(self, method_name: str):
        super().__init__(methodName=method_name)
        self.users_table = PlaygroundDynamoDBBasicTable(data_model=DynamoDBTableModel)

        self.DYNAMODB_CASE_KWARGS = {'self': self, 'users_table': self.users_table, 'is_caching': False}
        self.SHARED_CASE_KWARGS = {**self.DYNAMODB_CASE_KWARGS, 'primary_key_name': 'accountId'}
 def init_table():
     class TableModel(TableDataModel):
         accountId = BaseField(field_type=str, required=True)
         restrictedRightBracket = BaseField(custom_field_name='restricted_[e', field_type=str, required=False)
     users_table = PlaygroundDynamoDBBasicTable(data_model=TableModel)