def test_coerce_rejects_non_2tuples(self): t = xso.ConnectionLocation() values = [["foo", 1234], {"foo", 1234}, ("foo", 1234, "bar")] for value in values: with self.assertRaisesRegex(TypeError, "2-tuple required"): t.coerce(value)
def test_coerce_parses_ip_addresses(self): t = xso.ConnectionLocation() value_pairs = [ (("10.0.0.1", 1234), (ipaddress.IPv4Address("10.0.0.1"), 1234)), (("fe80::", 1234), (ipaddress.IPv6Address("fe80::"), 1234)), (("10.0.foobar", 1234), ("10.0.foobar", 1234)), ] for given, expected in value_pairs: self.assertEqual(expected, t.coerce(given))
def test_coerce_requires_integral_number(self): t = xso.ConnectionLocation() values = [ ("foobar", 1.2), ("foobar", "1"), ("foobar", decimal.Decimal("1")), ("foobar", fractions.Fraction(1, 1)), ] for value in values: with self.assertRaisesRegex(TypeError, "port number must be integral number"): t.coerce(value)
def test_coerce_restricts_port_numbers(self): t = xso.ConnectionLocation() err_values = [ ("foobar", -1), ("foobar", 65536), ] for err_value in err_values: with self.assertRaisesRegex(ValueError, "out of range"): t.coerce(err_value) ok_values = [ ("foobar", 0), ("foobar", 65535), ] for ok_value in ok_values: self.assertEqual(ok_value, t.coerce(ok_value))
def test_format_hostname(self): t = xso.ConnectionLocation() self.assertEqual("foo.bar.baz:5234", t.format(("foo.bar.baz", 5234)))
def test_format_ipv6(self): t = xso.ConnectionLocation() self.assertEqual("[fe80::]:5222", t.format((ipaddress.IPv6Address("fe80::"), 5222)))
def test_format_ipv4(self): t = xso.ConnectionLocation() self.assertEqual("10.0.0.1:1234", t.format((ipaddress.IPv4Address("10.0.0.1"), 1234)))
def test_parse_ipv4(self): t = xso.ConnectionLocation() self.assertEqual((ipaddress.IPv4Address("10.0.0.1"), 5223), t.parse("10.0.0.1:5223"))
def test_parse_hostname(self): t = xso.ConnectionLocation() self.assertEqual(("foo.bar.example", 5234), t.parse("foo.bar.example:5234"))
def test_reject_missing_colon(self): t = xso.ConnectionLocation() with self.assertRaises(ValueError): t.parse("foo.bar")
def test_reject_out_of_range_port_number(self): t = xso.ConnectionLocation() with self.assertRaises(ValueError): t.parse("[fe80::]:1000000")
def test_reject_non_integer_port_number(self): t = xso.ConnectionLocation() with self.assertRaises(ValueError): t.parse("[fe80::]:23.4")
def test_parse_ipv6(self): t = xso.ConnectionLocation() self.assertEqual((ipaddress.IPv6Address("fe80::"), 5222), t.parse("[fe80::]:5222"))
def test_is_abstract_type(self): self.assertIsInstance(xso.ConnectionLocation(), xso.AbstractType)