def test_build_attribute_with_attr_value(self): a = Attr('myattr') value = Attr('myreference') self.assert_condition_expression_build(a.eq(value), '#n0 = #n1', { '#n0': 'myattr', '#n1': 'myreference' }, {})
def test_build_expression_between(self): a = Attr('myattr') self.assert_condition_expression_build(a.between('foo', 'foo2'), '#n0 BETWEEN :v0 AND :v1', {'#n0': 'myattr'}, { ':v0': 'foo', ':v1': 'foo2' })
def test_build_nested_attr_map_list(self): a = Attr('MyMap.MyList[2].MyElement') self.assert_condition_expression_build(a.eq('foo'), '#n0.#n1[2].#n2 = :v0', { '#n0': 'MyMap', '#n1': 'MyList', '#n2': 'MyElement' }, {':v0': 'foo'})
def test_build_expression_in(self): a = Attr('myattr') self.assert_condition_expression_build(a.is_in([1, 2, 3]), '#n0 IN (:v0, :v1, :v2)', {'#n0': 'myattr'}, { ':v0': 1, ':v1': 2, ':v2': 3 })
def test_reset(self): a = Attr('myattr') self.assert_condition_expression_build(a.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'}) self.assert_condition_expression_build(a.eq('foo'), '#n1 = :v1', {'#n1': 'myattr'}, {':v1': 'foo'}) self.builder.reset() self.assert_condition_expression_build(a.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_key_and_attr_conditon_expression_with_placeholders(self): params = { 'KeyCondition': Key('foo').eq('bar'), 'AttrCondition': Attr('biz').eq('baz'), 'ExpressionAttributeNames': { '#a': 'b' }, 'ExpressionAttributeValues': { ':c': 'd' } } self.injector.inject_condition_expressions(params, self.operation_model) self.assertEqual( params, { 'KeyCondition': '#n1 = :v1', 'AttrCondition': '#n0 = :v0', 'ExpressionAttributeNames': { '#n0': 'biz', '#n1': 'foo', '#a': 'b' }, 'ExpressionAttributeValues': { ':v0': 'baz', ':v1': 'bar', ':c': 'd' } })
class TestA(TestK): def setUp(self): self.attr = Attr('mykey') self.attr2 = Attr('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_ne(self): self.assertEqual(self.attr.ne(self.value), NotEquals(self.attr, self.value)) def test_is_in(self): self.assertEqual(self.attr.is_in([self.value]), In(self.attr, [self.value])) def test_exists(self): self.assertEqual(self.attr.exists(), AttributeExists(self.attr)) def test_not_exists(self): self.assertEqual(self.attr.not_exists(), AttributeNotExists(self.attr)) def test_contains(self): self.assertEqual(self.attr.contains(self.value), Contains(self.attr, self.value)) def test_size(self): self.assertEqual(self.attr.size(), Size(self.attr)) def test_attribute_type(self): self.assertEqual(self.attr.attribute_type(self.value), AttributeType(self.attr, self.value))
def test_resource(self): dynamodb = self.session.resource('dynamodb') table = dynamodb.Table('MyTable') # Make sure it uses the high level interface table.scan(FilterExpression=Attr('mykey').eq('myvalue')) request = self.make_request_mock.call_args_list[0][0][1] request_params = json.loads(request['body'].decode('utf-8')) self.assertEqual( request_params, {'TableName': 'MyTable', 'FilterExpression': '#n0 = :v0', 'ExpressionAttributeNames': {'#n0': 'mykey'}, 'ExpressionAttributeValues': {':v0': {'S': 'myvalue'}}} )
def test_build_double_nested_and_or(self): a = Attr('myattr') a2 = Attr('myattr2') self.assert_condition_expression_build( (a.eq('foo') & a2.eq('foo2')) | (a.eq('bar') & a2.eq('bar2')), '((#n0 = :v0 AND #n1 = :v1) OR (#n2 = :v2 AND #n3 = :v3))', { '#n0': 'myattr', '#n1': 'myattr2', '#n2': 'myattr', '#n3': 'myattr2' }, { ':v0': 'foo', ':v1': 'foo2', ':v2': 'bar', ':v3': 'bar2' })
def test_single_attr_condition_expression(self): params = {'AttrCondition': Attr('foo').eq('bar')} self.injector.inject_condition_expressions(params, self.operation_model) self.assertEqual( params, { 'AttrCondition': '#n0 = :v0', 'ExpressionAttributeNames': { '#n0': 'foo' }, 'ExpressionAttributeValues': { ':v0': 'bar' } })
def test_build_or(self): a = Attr('myattr') a2 = Attr('myattr2') self.assert_condition_expression_build( a.eq('foo') | a2.eq('bar'), '(#n0 = :v0 OR #n1 = :v1)', { '#n0': 'myattr', '#n1': 'myattr2' }, { ':v0': 'foo', ':v1': 'bar' })
def setUp(self): self.attr = Attr('mykey') self.attr2 = Attr('myotherkey') self.value = 'foo' self.value2 = 'foo2'
def test_bad_input(self): a = Attr('myattr') with self.assertRaises(DynamoDBNeedsConditionError): self.builder.build_expression(a)
def test_build_expression_lte(self): a1 = Attr('myattr') self.assert_condition_expression_build(a1.lte('foo'), '#n0 <= :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_attr_list(self): a = Attr('MyList[0]') self.assert_condition_expression_build(a.eq('foo'), '#n0[0] = :v0', {'#n0': 'MyList'}, {':v0': 'foo'})
def test_build_attr_map(self): a = Attr('MyMap.MyKey') self.assert_condition_expression_build(a.eq('foo'), '#n0.#n1 = :v0', { '#n0': 'MyMap', '#n1': 'MyKey' }, {':v0': 'foo'})
def test_build_expression_not_exists(self): a = Attr('myattr') self.assert_condition_expression_build(a.not_exists(), 'attribute_not_exists(#n0)', {'#n0': 'myattr'}, {})
def test_build_expression_begins_with(self): a = Attr('myattr') self.assert_condition_expression_build(a.begins_with('foo'), 'begins_with(#n0, :v0)', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_not(self): a = Attr('myattr') self.assert_condition_expression_build(~a.eq('foo'), '(NOT #n0 = :v0)', {'#n0': 'myattr'}, {':v0': 'foo'})
def setUp(self): self.value = Attr('mykey') self.value2 = 'foo'
def test_build_size_with_other_conditons(self): a = Attr('myattr') self.assert_condition_expression_build(a.size().eq(5), 'size(#n0) = :v0', {'#n0': 'myattr'}, {':v0': 5})
def test_build_size(self): a = Attr('myattr') self.assert_condition_expression_build(a.size(), 'size(#n0)', {'#n0': 'myattr'}, {})
def test_build_contains(self): a = Attr('myattr') self.assert_condition_expression_build(a.contains('foo'), 'contains(#n0, :v0)', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_with_is_key_condition_throws_error(self): a = Attr('myattr') with self.assertRaises(DynamoDBNeedsKeyConditionError): self.builder.build_expression(a.eq('foo'), is_key_condition=True)
def test_build_attribute_type(self): a = Attr('myattr') self.assert_condition_expression_build(a.attribute_type('foo'), 'attribute_type(#n0, :v0)', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_expression_ne(self): a = Attr('myattr') self.assert_condition_expression_build(a.ne('foo'), '#n0 <> :v0', {'#n0': 'myattr'}, {':v0': 'foo'})