-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·68 lines (50 loc) · 1.84 KB
/
test.py
File metadata and controls
executable file
·68 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python3
from dataclasses import dataclass
from typing import Optional
from unittest import TestCase
@dataclass
class Addr:
tcp_host: Optional[str]
port: Optional[int]
unix_sock: Optional[str]
def effective_tcp_host(self):
if self.tcp_host is not None:
return self.tcp_host
if self.unix_sock is None:
return "localhost"
return None
def effective_port(self):
if self.port is not None:
return self.port
return 50_000
def effective_unix_sock(self):
if self.unix_sock is not None:
return self.unix_sock
if self.effective_tcp_host() == "localhost":
return f".s.{self.effective_port()}"
return None
def effective(self):
u = self.effective_unix_sock()
h = self.effective_tcp_host()
p = self.effective_port()
t = f"{h}:{p}" if h is not None else None
return (u, t)
def f(tcp_host, port, unix_sock):
return Addr(tcp_host, port, unix_sock).effective()
class Tests(TestCase):
def test_simple_cases(self):
self.assertEqual(f(None, None, None), (".s.50000", "localhost:50000"))
self.assertEqual(f("localhost", None, None), (".s.50000", "localhost:50000"))
self.assertEqual(
f("localhost.localdomain", None, None),
(None, "localhost.localdomain:50000"),
)
self.assertEqual(f("localhost.", None, None), (None, "localhost.:50000"))
self.assertEqual(
f("mdb.example.com", None, None), (None, "mdb.example.com:50000")
)
self.assertEqual(
f("mdb.example.com", 12345, None), (None, "mdb.example.com:12345")
)
self.assertEqual(f(None, None, "/tmp/x"), ("/tmp/x", None))
self.assertEqual(f(None, 12345, None), (".s.12345", "localhost:12345"))