Exemple #1
0
 def put_item(self,
              table_name,
              hash_key,
              range_key=None,
              attributes=None,
              expected=None,
              conditional_operator=None,
              return_values=None,
              return_consumed_capacity=None,
              return_item_collection_metrics=None):
     """
     Performs the PutItem operation and returns the result
     """
     operation_kwargs = {pythonic(TABLE_NAME): table_name}
     operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key, key=ITEM))
     if attributes:
         attrs = self.get_item_attribute_map(table_name, attributes)
         operation_kwargs[pythonic(ITEM)].update(attrs[pythonic(ITEM)])
     if return_consumed_capacity:
         operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
     if return_item_collection_metrics:
         operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
     if return_values:
         operation_kwargs.update(self.get_return_values_map(return_values))
     if expected:
         operation_kwargs.update(self.get_expected_map(table_name, expected))
     if conditional_operator:
         operation_kwargs.update(self.get_conditional_operator(conditional_operator))
     response, data = self.dispatch(PUT_ITEM, operation_kwargs)
     if not response.ok:
         raise PutError("Failed to put item: {0}".format(response.content))
     return data
Exemple #2
0
 def put_item(self,
              table_name,
              hash_key,
              range_key=None,
              attributes=None,
              expected=None,
              conditional_operator=None,
              return_values=None,
              return_consumed_capacity=None,
              return_item_collection_metrics=None):
     """
     Performs the PutItem operation and returns the result
     """
     operation_kwargs = {TABLE_NAME: table_name}
     operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key, key=ITEM))
     if attributes:
         attrs = self.get_item_attribute_map(table_name, attributes)
         operation_kwargs[ITEM].update(attrs[ITEM])
     if return_consumed_capacity:
         operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
     if return_item_collection_metrics:
         operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
     if return_values:
         operation_kwargs.update(self.get_return_values_map(return_values))
     if expected:
         operation_kwargs.update(self.get_expected_map(table_name, expected))
     if conditional_operator:
         operation_kwargs.update(self.get_conditional_operator(conditional_operator))
     try:
         return self.dispatch(PUT_ITEM, operation_kwargs)
     except BOTOCORE_EXCEPTIONS as e:
         raise PutError("Failed to put item: {0}".format(e))
Exemple #3
0
 async def put_item(self,
                    table_name,
                    hash_key,
                    range_key=None,
                    attributes=None,
                    condition=None,
                    return_values=None,
                    return_consumed_capacity=None,
                    return_item_collection_metrics=None):
     """
     Performs the PutItem operation and returns the result
     """
     operation_kwargs = await self.get_operation_kwargs(
         table_name=table_name,
         hash_key=hash_key,
         range_key=range_key,
         key=ITEM,
         attributes=attributes,
         condition=condition,
         return_values=return_values,
         return_consumed_capacity=return_consumed_capacity,
         return_item_collection_metrics=return_item_collection_metrics)
     try:
         return await self.dispatch(PUT_ITEM, operation_kwargs)
     except BOTOCORE_EXCEPTIONS as e:
         raise PutError("Failed to put item: {}".format(e)) from e
    def test_shorten_link_id_race_condition(self):
        flexmock(Captcha).should_receive("verify_captcha").with_args(
            "fakeCaptcha").and_return(True).once()
        flexmock(SafeSite).should_receive("is_safe_site").with_args(
            "https://mrteefs.com").and_return(True).once()
        flexmock(Shortener).should_receive("_generate_id").and_return(
            "SMOLIO").once()
        self.mock_conn.should_receive("put_item").and_raise(PutError()).once()

        with pytest.raises(PutError):
            Shortener(self.mock_post_event).shorten_link()
Exemple #5
0
 def commit(self) -> None:
     """
     Writes all of the changes that are pending
     """
     log.debug("%s committing batch operation", self.model)
     put_items = []
     delete_items = []
     for item in self.pending_operations:
         if item['action'] == PUT:
             put_items.append(item['item'].serialize())
         elif item['action'] == DELETE:
             delete_items.append(item['item']._get_keys())
     self.pending_operations = []
     if not len(put_items) and not len(delete_items):
         return
     data = self.model._get_connection().batch_write_item(
         put_items=put_items,
         delete_items=delete_items,
         settings=self.settings,
     )
     if data is None:
         return
     retries = 0
     unprocessed_items = data.get(UNPROCESSED_ITEMS,
                                  {}).get(self.model.Meta.table_name)
     while unprocessed_items:
         sleep_time = random.randint(
             0, self.model.Meta.base_backoff_ms * (2**retries)) / 1000
         time.sleep(sleep_time)
         retries += 1
         if retries >= self.model.Meta.max_retry_attempts:
             self.failed_operations = unprocessed_items
             raise PutError(
                 "Failed to batch write items: max_retry_attempts exceeded")
         put_items = []
         delete_items = []
         for item in unprocessed_items:
             if PUT_REQUEST in item:
                 put_items.append(
                     item.get(PUT_REQUEST).get(ITEM))  # type: ignore
             elif DELETE_REQUEST in item:
                 delete_items.append(
                     item.get(DELETE_REQUEST).get(KEY))  # type: ignore
         log.info(
             "Resending %d unprocessed keys for batch operation after %d seconds sleep",
             len(unprocessed_items), sleep_time)
         data = self.model._get_connection().batch_write_item(
             put_items=put_items,
             delete_items=delete_items,
             settings=self.settings,
         )
         unprocessed_items = data.get(UNPROCESSED_ITEMS,
                                      {}).get(self.model.Meta.table_name)
    def test_start_error(self, save_mock):

        save_mock.side_effect = PutError()

        job = JobModel()
        with self.assertRaises(PutError):
            result = job.start()

        self.assertEqual(job.started,
                         datetime.strptime(frozen_datetime, '%Y-%m-%d'))
        self.assertIsNone(job.ended)
        self.assertTrue(job.running)
        save_mock.assert_called_once_with(_ended__null=False,
                                          _started__null=True,
                                          conditional_operator='or')
    def test_start_when_job_is_alread_running(self, save_mock):

        save_mock.side_effect = PutError('ConditionalCheckFailedException')

        job = JobModel()
        result = job.start()

        self.assertEqual(job.started,
                         datetime.strptime(frozen_datetime, '%Y-%m-%d'))
        self.assertIsNone(job.ended)
        self.assertTrue(job.running)
        save_mock.assert_called_once_with(_ended__null=False,
                                          _started__null=True,
                                          conditional_operator='or')
        self.assertFalse(result)
Exemple #8
0
 def batch_write_item(self,
                      table_name,
                      put_items=None,
                      delete_items=None,
                      return_consumed_capacity=None,
                      return_item_collection_metrics=None):
     """
     Performs the batch_write_item operation
     """
     if put_items is None and delete_items is None:
         raise ValueError(
             "Either put_items or delete_items must be specified")
     operation_kwargs = {REQUEST_ITEMS: {table_name: []}}
     if return_consumed_capacity:
         operation_kwargs.update(
             self.get_consumed_capacity_map(return_consumed_capacity))
     if return_item_collection_metrics:
         operation_kwargs.update(
             self.get_item_collection_map(return_item_collection_metrics))
     put_items_list = []
     if put_items:
         for item in put_items:
             put_items_list.append({
                 PUT_REQUEST:
                 self.get_item_attribute_map(table_name,
                                             item,
                                             pythonic_key=False)
             })
     delete_items_list = []
     if delete_items:
         for item in delete_items:
             delete_items_list.append({
                 DELETE_REQUEST:
                 self.get_item_attribute_map(table_name,
                                             item,
                                             item_key=KEY,
                                             pythonic_key=False)
             })
     operation_kwargs[REQUEST_ITEMS][
         table_name] = delete_items_list + put_items_list
     try:
         return self.dispatch(BATCH_WRITE_ITEM, operation_kwargs)
     except BOTOCORE_EXCEPTIONS as e:
         raise PutError("Failed to batch write items: {0}".format(e))
Exemple #9
0
    def put_item(self,
                 table_name,
                 hash_key,
                 range_key=None,
                 attributes=None,
                 condition=None,
                 return_values=None,
                 return_consumed_capacity=None,
                 return_item_collection_metrics=None):
        """
        Performs the PutItem operation and returns the result
        """
        self._check_condition('condition', condition)

        operation_kwargs = {TABLE_NAME: table_name}
        operation_kwargs.update(self.get_identifier_map(table_name, hash_key, range_key, key=ITEM))
        name_placeholders = {}
        expression_attribute_values = {}

        if attributes:
            attrs = self.get_item_attribute_map(table_name, attributes)
            operation_kwargs[ITEM].update(attrs[ITEM])
        if condition is not None:
            condition_expression = condition.serialize(name_placeholders, expression_attribute_values)
            operation_kwargs[CONDITION_EXPRESSION] = condition_expression
        if return_consumed_capacity:
            operation_kwargs.update(self.get_consumed_capacity_map(return_consumed_capacity))
        if return_item_collection_metrics:
            operation_kwargs.update(self.get_item_collection_map(return_item_collection_metrics))
        if return_values:
            operation_kwargs.update(self.get_return_values_map(return_values))
        if name_placeholders:
            operation_kwargs[EXPRESSION_ATTRIBUTE_NAMES] = self._reverse_dict(name_placeholders)
        if expression_attribute_values:
            operation_kwargs[EXPRESSION_ATTRIBUTE_VALUES] = expression_attribute_values

        try:
            return self.dispatch(PUT_ITEM, operation_kwargs)
        except BOTOCORE_EXCEPTIONS as e:
            raise PutError("Failed to put item: {}".format(e), e)
 def put_item_raise(*_args, **_kwargs):
     raise PutError()
Exemple #11
0
def test_get_cause_response_code():
    error = PutError(cause=ClientError(
        error_response={'Error': {
            'Code': 'hello'
        }}, operation_name='test'))
    assert error.cause_response_code == 'hello'
Exemple #12
0
def test_get_cause_response_message__no_message():
    error = PutError()
    assert error.cause_response_message is None
Exemple #13
0
def test_get_cause_response_message():
    error = PutError(cause=ClientError(
        error_response={'Error': {
            'Message': 'hiya'
        }}, operation_name='test'))
    assert error.cause_response_message == 'hiya'
Exemple #14
0
def test_get_cause_response_code__no_code():
    error = PutError()
    assert error.cause_response_code is None