def test_attribute_between_condition(): cond = attr('state').between(4, 9) mock_attributes = [*[parse_attr(k, v) for k, v in cond.attributes.items()]] result = cond.expression(mock_attributes) assert result == '#state BETWEEN :state_a AND :state_b'
def test_update_with_conditions(): res = update(table_name='users', key={'username': '******'}, conditions=attr('rank').lt(2), attributes={'rank': 10}) condition_expression = res.description['ConditionExpression'] assert condition_expression is not None assert condition_expression == '#rank < :rank'
def test_condition_composition(): cond = cand( attr('username').equals('sunshie'), cor(cand(attr('rank').gt(12), attr('rank').lt(20)), cand(attr('kills').gt_or_eq(100), attr('kills').lt_or_eq(1000)))) mock_attributes = [ immutable({ 'original': 'rank', 'key': ':rank', 'value': { "N": 12 }, 'alias': 'rank', 'func': None }), immutable({ 'original': 'kills', 'key': ':kills', 'value': { "N": 300 }, 'alias': 'kills', 'func': None }), immutable({ 'original': 'username', 'key': ':username', 'value': { "S": "sunshie" }, 'alias': 'username', 'func': None }) ] expected = '(username = :username) AND (((rank > :rank) AND (rank < :rank)) OR ((kills >= :kills) AND (kills <= :kills)))' result = cond.expression(mock_attributes) assert result == expected
def test_query_builds_basic_description(): result = query( table_name='users', conditions=attr('username').equals('sunshie') ) expected = { 'TableName': 'users', 'KeyConditionExpression': 'username = :username', 'ExpressionAttributeValues': { ':username': { 'S': 'sunshie' } } } assertObjectsEqual(result.description, expected)
def test_condition_begins_with(): cond = attr('state').begins_with('Ca') mock_attributes = [ immutable({ 'original': 'state', 'key': ':state', 'value': { "S": 'California' }, 'alias': 'state', 'func': None }) ] result = cond.expression(mock_attributes) assert result == 'begins_with(state, :state)'
def test_query_builds_aliased_attr_description(): result = query( table_name='users', conditions=attr('item').equals('carl') ) expected = { 'TableName': 'users', 'KeyConditionExpression': '#item = :item', 'ExpressionAttributeNames': { '#item': 'item' }, 'ExpressionAttributeValues': { ':item': { 'S': 'carl' } } } assertObjectsEqual(result.description, expected)
def test_attribute_equals_condition(): cond = attr('username').equals('sunshie') mock_attributes = [ immutable({ 'original': 'username', 'key': ':username', 'value': { "S": "sunshie" }, 'alias': 'username', 'func': None }) ] result = cond.expression(mock_attributes) # username = :username # { ":username": { "S": "sunshie" } } assert result == 'username = :username'
def test_builder_creates_data(): build = ab.builder(table_name='products', key={'id': 13}, attributes={'items': ['glow', 'dust']}, gsi=[{ 'name': 'global_index', 'hash_key': 'state:int' }], conditions=attr('price').gt(10)) result = build(lambda r: r) expected_item = { "alias": "#items", "func": None, "key": ":items", "original": "items", "value": { "L": [{ "S": "glow" }, { "S": "dust" }] } } assert result.get('attributes').get('values')[0] == expected_item expected_gsi = { "hash_key": { "name": "state", "type": "N" }, "name": "global_index" } assert result.get('gsi')[0] == expected_gsi
def test_query_is_operation(): res = query( table_name='users', conditions=attr('username').equals('sunshie') ) assertIsOperation(res)
def test_attribute_between_adds_attributes(): cond = attr('state').between(4, 9) assert 'state_a' in cond.attributes assert 'state_b' in cond.attributes