def get_matches_by_sport(self, sport): self.init_table() key_sport = Key("_sport") response = self.table.query(IndexName="sport_startTime", KeyConditionExpression=key_sport.eq(sport)) items = response["Items"] items = [clean_dict(item) for item in items] return items
def test_build_with_is_key_condition(self): k = Key('myattr') self.assert_condition_expression_build( k.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'}, is_key_condition=True, )
class TestK(unittest.TestCase): def setUp(self): self.attr = Key('mykey') self.attr2 = Key('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_and(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'AND'): self.attr & self.attr2 def test_or(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'OR'): self.attr | self.attr2 def test_not(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'NOT'): ~self.attr def test_eq(self): self.assertEqual( self.attr.eq(self.value), Equals(self.attr, self.value)) def test_lt(self): self.assertEqual( self.attr.lt(self.value), LessThan(self.attr, self.value)) def test_lte(self): self.assertEqual( self.attr.lte(self.value), LessThanEquals(self.attr, self.value)) def test_gt(self): self.assertEqual( self.attr.gt(self.value), GreaterThan(self.attr, self.value)) def test_gte(self): self.assertEqual( self.attr.gte(self.value), GreaterThanEquals(self.attr, self.value)) def test_begins_with(self): self.assertEqual(self.attr.begins_with(self.value), BeginsWith(self.attr, self.value)) def test_between(self): self.assertEqual(self.attr.between(self.value, self.value2), Between(self.attr, self.value, self.value2))
class TestK(unittest.TestCase): def setUp(self): self.attr = Key('mykey') self.attr2 = Key('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_and(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, 'AND'): self.attr & self.attr2 def test_or(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, 'OR'): self.attr | self.attr2 def test_not(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, 'NOT'): ~self.attr def test_eq(self): self.assertEqual(self.attr.eq(self.value), Equals(self.attr, self.value)) def test_lt(self): self.assertEqual(self.attr.lt(self.value), LessThan(self.attr, self.value)) def test_lte(self): self.assertEqual(self.attr.lte(self.value), LessThanEquals(self.attr, self.value)) def test_gt(self): self.assertEqual(self.attr.gt(self.value), GreaterThan(self.attr, self.value)) def test_gte(self): self.assertEqual(self.attr.gte(self.value), GreaterThanEquals(self.attr, self.value)) def test_begins_with(self): self.assertEqual(self.attr.begins_with(self.value), BeginsWith(self.attr, self.value)) def test_between(self): self.assertEqual(self.attr.between(self.value, self.value2), Between(self.attr, self.value, self.value2))
def to_expression(key_name, op, value, value2=None): exp = Key(key_name) if op == Operator.EQ: exp = exp.eq(value) elif op == Operator.BEGINS_WITH: exp = exp.begins_with(value) elif op == Operator.LESS_THAN: exp = exp.lt(value) elif op == Operator.GREATER_THAN: exp = exp.gt(value) elif op == Operator.GREATER_THAN_OR_EQUAL: exp = exp.gte(value) elif op == Operator.LOWER_THAN_OR_EQUAL: exp = exp.lte(value) elif op == Operator.BETWEEN: if value2 is None: raise Exception('Undefined second value for BETWEEN sort key operator') exp = exp.between(value, value2) else: raise ValueError(f"Unknown operator {str(op)}") return exp
class TestK(unittest.TestCase): def setUp(self): self.attr = Key("mykey") self.attr2 = Key("myotherkey") self.value = "foo" self.value2 = "foo2" def test_and(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, "AND"): self.attr & self.attr2 def test_or(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, "OR"): self.attr | self.attr2 def test_not(self): with self.assertRaisesRegexp(DynanmoDBOperationNotSupportedError, "NOT"): ~self.attr def test_eq(self): self.assertEqual(self.attr.eq(self.value), Equals(self.attr, self.value)) def test_lt(self): self.assertEqual(self.attr.lt(self.value), LessThan(self.attr, self.value)) def test_lte(self): self.assertEqual(self.attr.lte(self.value), LessThanEquals(self.attr, self.value)) def test_gt(self): self.assertEqual(self.attr.gt(self.value), GreaterThan(self.attr, self.value)) def test_gte(self): self.assertEqual(self.attr.gte(self.value), GreaterThanEquals(self.attr, self.value)) def test_begins_with(self): self.assertEqual(self.attr.begins_with(self.value), BeginsWith(self.attr, self.value)) def test_between(self): self.assertEqual(self.attr.between(self.value, self.value2), Between(self.attr, self.value, self.value2))
class TestK(unittest.TestCase): def setUp(self): self.attr = Key('mykey') self.attr2 = Key('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_and(self): with self.assertRaisesRegexp(DynamoDBOperationNotSupportedError, 'AND'): self.attr & self.attr2 def test_or(self): with self.assertRaisesRegexp(DynamoDBOperationNotSupportedError, 'OR'): self.attr | self.attr2 def test_not(self): with self.assertRaisesRegexp(DynamoDBOperationNotSupportedError, 'NOT'): ~self.attr def test_eq(self): self.assertEqual(self.attr.eq(self.value), Equals(self.attr, self.value)) def test_lt(self): self.assertEqual(self.attr.lt(self.value), LessThan(self.attr, self.value)) def test_lte(self): self.assertEqual(self.attr.lte(self.value), LessThanEquals(self.attr, self.value)) def test_gt(self): self.assertEqual(self.attr.gt(self.value), GreaterThan(self.attr, self.value)) def test_gte(self): self.assertEqual(self.attr.gte(self.value), GreaterThanEquals(self.attr, self.value)) def test_begins_with(self): self.assertEqual(self.attr.begins_with(self.value), BeginsWith(self.attr, self.value)) def test_between(self): self.assertEqual(self.attr.between(self.value, self.value2), Between(self.attr, self.value, self.value2)) def test_attribute_equality(self): attr_copy = copy.deepcopy(self.attr) self.assertIsNot(self.attr, attr_copy) self.assertEqual(self.attr, attr_copy) def test_eq_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.eq(self.value) comp2 = attr_copy.eq(self.value) self.assertEqual(comp, comp2) def test_eq_inequality(self): attr_copy = copy.deepcopy(self.attr) self.assertNotEqual(self.attr.eq(self.value), attr_copy.eq(self.value2)) def test_lt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lt(self.value) comp2 = attr_copy.lt(self.value) self.assertEqual(comp, comp2) def test_lte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lte(self.value) comp2 = attr_copy.lte(self.value) self.assertEqual(comp, comp2) def test_gt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gt(self.value) comp2 = attr_copy.gt(self.value) self.assertEqual(comp, comp2) def test_gte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gte(self.value) comp2 = attr_copy.gte(self.value) self.assertEqual(comp, comp2) def test_begins_with_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.begins_with(self.value) comp2 = attr_copy.begins_with(self.value) self.assertEqual(comp, comp2) def test_between_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.between(self.value, self.value2) comp2 = attr_copy.between(self.value, self.value2) self.assertEqual(comp, comp2)
pkey_name, skey_name = get_key_names(ddl["Table"]["KeySchema"]) pkey, skey = cast_key_attr( ddl["Table"]["AttributeDefinitions"], pkey, pkey_name, skey, skey_name ) key_condtion_pkey = Key(pkey_name).eq(pkey) if skey is None: return {"KeyConditionExpression": key_condtion_pkey} else: key_condtion_skey = Key(skey_name) if skey_cond == "eq": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.eq(skey) } elif skey_cond == "ne": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.ne(skey) } elif skey_cond == "gt": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.gt(skey) } elif skey_cond == "ge": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.gte(skey)
def test_build_with_is_key_condition(self): k = Key('myattr') self.assert_condition_expression_build( k.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'}, is_key_condition=True)
class TestK(unittest.TestCase): def setUp(self): self.attr = Key('mykey') self.attr2 = Key('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_and(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'AND'): self.attr & self.attr2 def test_or(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'OR'): self.attr | self.attr2 def test_not(self): with self.assertRaisesRegexp( DynamoDBOperationNotSupportedError, 'NOT'): ~self.attr def test_eq(self): self.assertEqual( self.attr.eq(self.value), Equals(self.attr, self.value)) def test_lt(self): self.assertEqual( self.attr.lt(self.value), LessThan(self.attr, self.value)) def test_lte(self): self.assertEqual( self.attr.lte(self.value), LessThanEquals(self.attr, self.value)) def test_gt(self): self.assertEqual( self.attr.gt(self.value), GreaterThan(self.attr, self.value)) def test_gte(self): self.assertEqual( self.attr.gte(self.value), GreaterThanEquals(self.attr, self.value)) def test_begins_with(self): self.assertEqual(self.attr.begins_with(self.value), BeginsWith(self.attr, self.value)) def test_between(self): self.assertEqual(self.attr.between(self.value, self.value2), Between(self.attr, self.value, self.value2)) def test_attribute_equality(self): attr_copy = copy.deepcopy(self.attr) self.assertIsNot(self.attr, attr_copy) self.assertEqual(self.attr, attr_copy) def test_eq_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.eq(self.value) comp2 = attr_copy.eq(self.value) self.assertEqual(comp, comp2) def test_eq_inequality(self): attr_copy = copy.deepcopy(self.attr) self.assertNotEqual(self.attr.eq(self.value), attr_copy.eq(self.value2)) def test_lt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lt(self.value) comp2 = attr_copy.lt(self.value) self.assertEqual(comp, comp2) def test_lte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lte(self.value) comp2 = attr_copy.lte(self.value) self.assertEqual(comp, comp2) def test_gt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gt(self.value) comp2 = attr_copy.gt(self.value) self.assertEqual(comp, comp2) def test_gte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gte(self.value) comp2 = attr_copy.gte(self.value) self.assertEqual(comp, comp2) def test_begins_with_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.begins_with(self.value) comp2 = attr_copy.begins_with(self.value) self.assertEqual(comp, comp2) def test_between_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.between(self.value, self.value2) comp2 = attr_copy.between(self.value, self.value2) self.assertEqual(comp, comp2)
def test_build_with_is_key_condition(self): k = Key("myattr") self.assert_condition_expression_build( k.eq("foo"), "#n0 = :v0", {"#n0": "myattr"}, {":v0": "foo"}, is_key_condition=True )
def doquery(self, queryspec): db = boto3.resource('dynamodb') table = db.Table(queryspec.DataType) data = None if len(queryspec.Filters) == 0: response = table.scan() data = response['Items'] while 'LastEvaluatedKey' in response: response = table.scan( ExclusiveStartKey=response['LastEvaluatedKey']) data.extend(response['Items']) else: for prop in queryspec.Filters: if data is None and queryspec.Filters[prop][2]: # queryable key = Key(prop) if queryspec.Filters[prop][0] == '=': expr = key.eq(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<': expr = key.lt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>': expr = key.gt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<=': expr = key.lte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>=': expr = key.gte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == 'btw': expr = key.between(queryspec.Filters[prop][1][0], queryspec.Filters[prop][1][0]) response = table.query(KeyConditionExpression=expr) data = response['Items'] else: # queryable = False if data is None: attr = Attr(prop) if queryspec.Filters[prop][0] == '=': expr = attr.eq(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<': expr = attr.lt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>': expr = attr.gt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<=': expr = attr.lte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>=': expr = attr.gte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == 'btw': expr = attr.between(queryspec.Filters[prop][1][0], queryspec.Filters[prop][1][0]) response = table.scan(FilterExpression=expr) data = response['Items'] while 'LastEvaluatedKey' in response: response = table.scan( ExclusiveStartKey=response['LastEvaluatedKey']) data.extend(response['Items']) else: pass # TODO: Secondary value if data is None or len(data) == 0 or len(queryspec.Sorts) == 0: return data sortproperty = next(iter(queryspec.Sorts)) sorteddata = sorted(data, key=lambda item: item[sortproperty]) return sorteddata
class TestK(unittest.TestCase): def setUp(self): self.attr = Key('mykey') self.attr2 = Key('myotherkey') self.value = 'foo' self.value2 = 'foo2' def test_and(self): with pytest.raises(DynamoDBOperationNotSupportedError, match=r'AND'): self.attr & self.attr2 def test_or(self): with pytest.raises(DynamoDBOperationNotSupportedError, match=r'OR'): self.attr | self.attr2 def test_not(self): with pytest.raises(DynamoDBOperationNotSupportedError, match=r'NOT'): ~self.attr def test_eq(self): assert self.attr.eq(self.value) == Equals(self.attr, self.value) def test_lt(self): assert self.attr.lt(self.value) == LessThan(self.attr, self.value) def test_lte(self): assert self.attr.lte(self.value) == LessThanEquals( self.attr, self.value) def test_gt(self): assert self.attr.gt(self.value) == GreaterThan(self.attr, self.value) def test_gte(self): assert self.attr.gte(self.value) == GreaterThanEquals( self.attr, self.value) def test_begins_with(self): assert self.attr.begins_with(self.value) == BeginsWith( self.attr, self.value) def test_between(self): assert self.attr.between(self.value, self.value2) == Between( self.attr, self.value, self.value2) def test_attribute_equality(self): attr_copy = copy.deepcopy(self.attr) assert self.attr is not attr_copy assert self.attr == attr_copy def test_eq_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.eq(self.value) comp2 = attr_copy.eq(self.value) assert comp == comp2 def test_eq_inequality(self): attr_copy = copy.deepcopy(self.attr) assert self.attr.eq(self.value) != attr_copy.eq(self.value2) def test_lt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lt(self.value) comp2 = attr_copy.lt(self.value) assert comp == comp2 def test_lte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.lte(self.value) comp2 = attr_copy.lte(self.value) assert comp == comp2 def test_gt_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gt(self.value) comp2 = attr_copy.gt(self.value) assert comp == comp2 def test_gte_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.gte(self.value) comp2 = attr_copy.gte(self.value) assert comp == comp2 def test_begins_with_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.begins_with(self.value) comp2 = attr_copy.begins_with(self.value) assert comp == comp2 def test_between_equality(self): attr_copy = copy.deepcopy(self.attr) comp = self.attr.between(self.value, self.value2) comp2 = attr_copy.between(self.value, self.value2) assert comp == comp2