def get_request_time_expression(start, end): key = Key('request_time') formatted_start = (format_time(parse(start)) if start else None) formatted_end = (format_time(parse(end)) if end else None) if formatted_start and formatted_end: return key.between(formatted_start, formatted_end) if formatted_start: return key.gte(formatted_start) if formatted_end: return key.lte(formatted_end)
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)
& key_condtion_skey.gt(skey) } elif skey_cond == "ge": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.gte(skey) } elif skey_cond == "lt": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.lt(skey) } elif skey_cond == "le": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.lte(skey) } elif skey_cond == "begins_with": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.begins_with(skey) } elif skey_cond == "between": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.between(*[skey]) } elif skey_cond == "contains": return { "KeyConditionExpression": key_condtion_pkey & key_condtion_skey.contains(skey)
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 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