Skip to content

Commit 065185a

Browse files
OpenHTF Ownersdbhatman
authored andcommitted
Internal change
PiperOrigin-RevId: 488770756
1 parent c669a9c commit 065185a

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

openhtf/util/validators.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""Module containing canned measurement validators.
1615
1716
Additional validators may be registered by passing them to the Register()
@@ -246,8 +245,8 @@ def all_equals(value, type=None): # pylint: disable=redefined-builtin
246245
if isinstance(value, numbers.Number):
247246
return AllInRangeValidator(minimum=value, maximum=value)
248247
elif isinstance(value, str):
249-
assert type is None or issubclass(type, str), (
250-
'Cannot use a non-string type when matching a string')
248+
assert type is None or issubclass(
249+
type, str), ('Cannot use a non-string type when matching a string')
251250
return matches_regex('^{}$'.format(re.escape(value)))
252251
else:
253252
return AllEqualsValidator(value)
@@ -385,8 +384,8 @@ def equals(value, type=None): # pylint: disable=redefined-builtin
385384
if isinstance(value, numbers.Number):
386385
return InRange(minimum=value, maximum=value, type=type)
387386
elif isinstance(value, str):
388-
assert type is None or issubclass(type, str), (
389-
'Cannot use a non-string type when matching a string')
387+
assert type is None or issubclass(
388+
type, str), ('Cannot use a non-string type when matching a string')
390389
return matches_regex('^{}$'.format(re.escape(value)))
391390
else:
392391
return Equals(value, type=type)
@@ -449,10 +448,10 @@ def __init__(self, expected, percent, marginal_percent=None) -> None:
449448
super(WithinPercent, self).__init__()
450449
if percent < 0:
451450
raise ValueError('percent argument is {}, must be >0'.format(percent))
452-
if marginal_percent is not None and marginal_percent < percent:
451+
if marginal_percent is not None and marginal_percent >= percent:
453452
raise ValueError(
454-
'marginal_percent argument is {}, must be < percent'.format(
455-
marginal_percent))
453+
'marginal_percent argument is {}, must be < {} percent'.format(
454+
marginal_percent, percent))
456455
self.expected = expected
457456
self.percent = percent
458457
self.marginal_percent = marginal_percent
@@ -481,7 +480,7 @@ def marginal_minimum(self):
481480

482481
@property
483482
def marginal_maximum(self):
484-
return (self.expected -
483+
return (self.expected +
485484
self._applied_marginal_percent if self.marginal_percent else None)
486485

487486
def __call__(self, value) -> bool:

test/util/validators_test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
"""Unit tests for util/validators.py."""
1615

1716
import copy
@@ -136,8 +135,7 @@ def test_is_marginal_false_within_bounds(self):
136135

137136
def test_is_marginal_false_fully_out_of_range(self):
138137
self.assertFalse(
139-
self.validator.is_marginal(
140-
[self.minimum - 1, self.maximum + 1]))
138+
self.validator.is_marginal([self.minimum - 1, self.maximum + 1]))
141139

142140
def test_is_marginal_false_without_marginal_bounds(self):
143141
validator = validators.AllInRangeValidator(self.minimum, self.minimum)
@@ -266,6 +264,18 @@ def test_raises_for_negative_percentage(self):
266264
with self.assertRaisesRegex(ValueError, 'percent argument is'):
267265
validators.WithinPercent(expected=100, percent=-1)
268266

267+
def test_raises_for_larger_marginal_percent(self):
268+
with self.assertRaisesRegex(ValueError, 'marginal_percent argument is'):
269+
validators.WithinPercent(expected=100, percent=2, marginal_percent=3)
270+
271+
def test_is_marginal(self):
272+
validator = validators.WithinPercent(
273+
expected=100, percent=10, marginal_percent=5)
274+
with self.subTest('returns_true_on_marginal_measurement'):
275+
self.assertTrue(validator.is_marginal(106))
276+
with self.subTest('returns_false_on_non_marginal_measurement'):
277+
self.assertFalse(validator.is_marginal(102))
278+
269279
def test_within_percent_less_than_one_hundred(self):
270280
validator = validators.WithinPercent(expected=100, percent=5)
271281
for valid_value in [95, 95.0, 100, 100.0, 105, 105.0]:

0 commit comments

Comments
 (0)