From 8b2b9d361890d8baf3f8a03363a490a631021c1f Mon Sep 17 00:00:00 2001 From: cqueitzsch Date: Wed, 23 Oct 2019 12:59:19 -0700 Subject: [PATCH] Fixing comparisons for python 3 in dynamodb --- moto/dynamodb2/comparisons.py | 46 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/moto/dynamodb2/comparisons.py b/moto/dynamodb2/comparisons.py index b08a5c195..c6aee7a68 100644 --- a/moto/dynamodb2/comparisons.py +++ b/moto/dynamodb2/comparisons.py @@ -935,7 +935,13 @@ class OpLessThan(Op): def expr(self, item): lhs = self.lhs.expr(item) rhs = self.rhs.expr(item) - return lhs < rhs + # In python3 None is not a valid comparator when using < or > so must be handled specially + if lhs and rhs: + return lhs < rhs + elif lhs is None and rhs: + return True + else: + return False class OpGreaterThan(Op): @@ -944,7 +950,13 @@ class OpGreaterThan(Op): def expr(self, item): lhs = self.lhs.expr(item) rhs = self.rhs.expr(item) - return lhs > rhs + # In python3 None is not a valid comparator when using < or > so must be handled specially + if lhs and rhs: + return lhs > rhs + elif lhs and rhs is None: + return True + else: + return False class OpEqual(Op): @@ -971,7 +983,13 @@ class OpLessThanOrEqual(Op): def expr(self, item): lhs = self.lhs.expr(item) rhs = self.rhs.expr(item) - return lhs <= rhs + # In python3 None is not a valid comparator when using < or > so must be handled specially + if lhs and rhs: + return lhs <= rhs + elif lhs is None and rhs or lhs is None and rhs is None: + return True + else: + return False class OpGreaterThanOrEqual(Op): @@ -980,7 +998,13 @@ class OpGreaterThanOrEqual(Op): def expr(self, item): lhs = self.lhs.expr(item) rhs = self.rhs.expr(item) - return lhs >= rhs + # In python3 None is not a valid comparator when using < or > so must be handled specially + if lhs and rhs: + return lhs >= rhs + elif lhs and rhs is None or lhs is None and rhs is None: + return True + else: + return False class OpOr(Op): @@ -1099,7 +1123,19 @@ class FuncBetween(Func): super(FuncBetween, self).__init__(attribute, start, end) def expr(self, item): - return self.start.expr(item) <= self.attr.expr(item) <= self.end.expr(item) + # In python3 None is not a valid comparator when using < or > so must be handled specially + start = self.start.expr(item) + attr = self.attr.expr(item) + end = self.end.expr(item) + if start and attr and end: + return start <= attr <= end + elif start is None and attr is None: + # None is between None and None as well as None is between None and any number + return True + elif start is None and attr and end: + return attr <= end + else: + return False class FuncIn(Func):