Skip to content

Commit

Permalink
Merge pull request #10 from ilholmes/validators_tests
Browse files Browse the repository at this point in the history
Add tests for validators. Minor bug fixes in validators.py
  • Loading branch information
varunvarma committed Oct 4, 2018
2 parents 2ef6abe + 0cd9f5d commit b8f1ff0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
74 changes: 74 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import time
import unittest
import logging
import os

from yahoo_panoptes.framework.validators import PanoptesValidators

Expand Down Expand Up @@ -42,3 +44,75 @@ def test_valid_timestamp(self):
self.assertFalse(PanoptesValidators.valid_timestamp(timestamp=None))
self.assertFalse(PanoptesValidators.valid_timestamp(timestamp=time.time(), max_age=None))
self.assertFalse(PanoptesValidators.valid_timestamp(timestamp=time.time(), max_skew=None))

def test_valid_plugin_result_class(self):
self.assertFalse(PanoptesValidators.valid_plugin_result_class(None))
self.assertTrue(PanoptesValidators.valid_plugin_result_class(str))

def test_valid_logger(self):
self.assertFalse(PanoptesValidators.valid_logger(None))
self.assertTrue(PanoptesValidators.valid_logger(logging.Logger("test_logger")))

def test_valid_callback(self):
self.assertFalse(PanoptesValidators.valid_callback(object()))
self.assertTrue(PanoptesValidators.valid_callback(object.__init__))

def test_valid_hashable_object(self):
self.assertTrue(PanoptesValidators.valid_hashable_object(object()))
self.assertFalse(PanoptesValidators.valid_hashable_object(list()))

def test_valid_port(self):
self.assertTrue(PanoptesValidators.valid_port(1L))
self.assertFalse(PanoptesValidators.valid_port(1.0))
self.assertFalse(PanoptesValidators.valid_port(65540))
self.assertFalse(PanoptesValidators.valid_port(0))
self.assertFalse(PanoptesValidators.valid_port(-1))
self.assertFalse(PanoptesValidators.valid_port("0"))
self.assertFalse(PanoptesValidators.valid_port("1"))
self.assertFalse(PanoptesValidators.valid_port(True))
self.assertFalse(PanoptesValidators.valid_port(False))

def test_valid_number(self):
self.assertFalse(PanoptesValidators.valid_number(False))
self.assertFalse(PanoptesValidators.valid_number("1.234"))
self.assertTrue(PanoptesValidators.valid_number(2 ** (2 ** (2 ** (2 ** 2)))))
self.assertTrue(PanoptesValidators.valid_number(1J))

def test_valid_nonzero_integer(self):
self.assertFalse(PanoptesValidators.valid_nonzero_integer(-1))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(0))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(-0L))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(1.0))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(""))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(False))
self.assertFalse(PanoptesValidators.valid_nonzero_integer(True))
self.assertTrue(PanoptesValidators.valid_nonzero_integer(2 ** (2 ** (2 ** (2 ** 2)))))

def test_valid_positive_integer(self):
self.assertTrue(PanoptesValidators.valid_positive_integer(0L))
self.assertTrue(PanoptesValidators.valid_positive_integer(2 ** (2 ** (2 ** (2 ** 2)))))
self.assertFalse(PanoptesValidators.valid_positive_integer(False))
self.assertFalse(PanoptesValidators.valid_positive_integer(True))
self.assertFalse(PanoptesValidators.valid_positive_integer(""))
self.assertFalse(PanoptesValidators.valid_positive_integer(1.0))
self.assertFalse(PanoptesValidators.valid_positive_integer(-5))
self.assertTrue(PanoptesValidators.valid_positive_integer(-0L))

def test_valid_nonempty_string(self):
self.assertFalse(PanoptesValidators.valid_nonempty_string(""))
self.assertTrue(PanoptesValidators.valid_nonempty_string("hello world"))
self.assertFalse(PanoptesValidators.valid_nonempty_string(0))

def test_valid_readable_path(self):
self.assertTrue(PanoptesValidators.valid_readable_path(os.getcwd()))
self.assertFalse(PanoptesValidators.valid_readable_path(os.getcwd() + '/test_dir'))
self.assertFalse(PanoptesValidators.valid_readable_path('not a path'))
self.assertFalse(PanoptesValidators.valid_readable_path(None))

def test_valid_readable_file(self):
self.assertTrue(PanoptesValidators.valid_readable_file(os.path.realpath(__file__)))
self.assertFalse(PanoptesValidators.valid_readable_file(None))

def test_valid_numeric_snmp_oid(self):
self.assertTrue(PanoptesValidators.valid_numeric_snmp_oid(".1.3.6.1.4.1.2636.3.1.13.1.6.2.1.0.0"))
self.assertFalse(PanoptesValidators.valid_numeric_snmp_oid("1.3.6.1.4.1.2636.3.1.13.1.6.2.1.0.0"))
13 changes: 8 additions & 5 deletions yahoo_panoptes/framework/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import re
import logging

from six import string_types
from six import string_types, integer_types

NUMERIC_OID = re.compile(r'\.(\d+)(\.\d+)*')

Expand Down Expand Up @@ -65,23 +65,26 @@ def valid_hashable_object(cls, object_to_check):
bool: True if the object is hashable
"""

return hasattr(object_to_check, '__hash__')
return hasattr(object_to_check, '__hash__') and object_to_check.__hash__

@classmethod
def valid_port(cls, port):
return (type(port) == int) and (port > 0) and (port < 65536)
return type(port) in integer_types and (port > 0) and (port < 65536)

@classmethod
def valid_number(cls, value):
return isinstance(value, numbers.Number) and not isinstance(value, bool)

@classmethod
def valid_nonzero_integer(cls, value):
return (type(value) == int) and (value > 0)
return type(value) in integer_types and (value > 0)

@classmethod
def valid_positive_integer(cls, value):
return (type(value) == int) and (value > -1)
"""
N.b. In Panoptes, '0' is considered a positive integer.
"""
return type(value) in integer_types and (value > -1)

@classmethod
def valid_nonempty_string(cls, value):
Expand Down

0 comments on commit b8f1ff0

Please sign in to comment.