192 |
192 |
193 __docformat__ = "restructuredtext en" |
193 __docformat__ = "restructuredtext en" |
194 |
194 |
195 import logging |
195 import logging |
196 from warnings import warn |
196 from warnings import warn |
|
197 from operator import eq |
197 |
198 |
198 from logilab.common.deprecation import class_renamed |
199 from logilab.common.deprecation import class_renamed |
199 from logilab.common.compat import all, any |
200 from logilab.common.compat import all, any |
200 from logilab.common.interface import implements as implements_iface |
201 from logilab.common.interface import implements as implements_iface |
201 |
202 |
528 return 1 |
529 return 1 |
529 return 0 |
530 return 0 |
530 |
531 |
531 |
532 |
532 class multi_lines_rset(Selector): |
533 class multi_lines_rset(Selector): |
533 """If `nb` is specified, return 1 if the result set has exactly `nb` row of |
534 """Return 1 if the operator expression matches between `num` elements |
534 result. Else (`nb` is None), return 1 if the result set contains *at least* |
535 in the result set and the `expected` value if defined. |
|
536 |
|
537 By default, multi_lines_rset(expected) matches equality expression: |
|
538 `nb` row(s) in result set equals to expected value |
|
539 But, you can perform richer comparisons by overriding default operator: |
|
540 multi_lines_rset(expected, operator.gt) |
|
541 |
|
542 If `expected` is None, return 1 if the result set contains *at least* |
535 two rows. |
543 two rows. |
536 """ |
544 If rset is None, return 0. |
537 def __init__(self, nb=None): |
545 """ |
538 self.expected = nb |
546 def __init__(self, expected=None, operator=eq): |
|
547 self.expected = expected |
|
548 self.operator = operator |
539 |
549 |
540 def match_expected(self, num): |
550 def match_expected(self, num): |
541 if self.expected is None: |
551 if self.expected is None: |
542 return num > 1 |
552 return num > 1 |
543 return num == self.expected |
553 return self.operator(num, self.expected) |
544 |
554 |
545 @lltrace |
555 @lltrace |
546 def __call__(self, cls, req, rset=None, **kwargs): |
556 def __call__(self, cls, req, rset=None, **kwargs): |
547 return int(rset is not None and self.match_expected(rset.rowcount)) |
557 return int(rset is not None and self.match_expected(rset.rowcount)) |
548 |
558 |