2020
2121class TestDeviceConfig (unittest .TestCase ):
2222
23- def loads_default_values_when_env_vars_missing (self ):
23+ def test_config_creation_from_dict (self ):
24+ config_dict = {
25+ "host" : "localhost" ,
26+ "port" : 1883 ,
27+ "access_token" : "test_token" ,
28+ "username" : "test_user" ,
29+ "password" : "test_pass" ,
30+ "client_id" : "test_client" ,
31+ "ca_cert" : "test_ca" ,
32+ "client_cert" : "test_cert" ,
33+ "private_key" : "test_key" ,
34+ "qos" : 1
35+ }
36+ config = DeviceConfig (config_dict )
37+ self .assertEqual (config .host , "localhost" )
38+ self .assertEqual (config .port , 1883 )
39+ self .assertEqual (config .access_token , "test_token" )
40+ self .assertEqual (config .username , "test_user" )
41+ self .assertEqual (config .password , "test_pass" )
42+ self .assertEqual (config .client_id , "test_client" )
43+ self .assertEqual (config .ca_cert , "test_ca" )
44+ self .assertEqual (config .client_cert , "test_cert" )
45+ self .assertEqual (config .private_key , "test_key" )
46+ self .assertEqual (config .qos , 1 )
47+
48+ def test_loads_default_values_when_env_vars_missing (self ):
2449 os .environ .clear ()
2550 config = DeviceConfig ()
2651 self .assertEqual (config .host , None )
@@ -34,7 +59,7 @@ def loads_default_values_when_env_vars_missing(self):
3459 self .assertEqual (config .private_key , None )
3560 self .assertEqual (config .qos , 1 )
3661
37- def loads_values_from_env_vars (self ):
62+ def test_loads_values_from_env_vars (self ):
3863 os .environ ["TB_HOST" ] = "test_host"
3964 os .environ ["TB_PORT" ] = "8883"
4065 os .environ ["TB_ACCESS_TOKEN" ] = "test_token"
@@ -58,22 +83,22 @@ def loads_values_from_env_vars(self):
5883 self .assertEqual (config .private_key , "test_key" )
5984 self .assertEqual (config .qos , 2 )
6085
61- def detects_tls_auth_correctly (self ):
86+ def test_detects_tls_auth_correctly (self ):
6287 os .environ ["TB_CA_CERT" ] = "test_ca"
6388 os .environ ["TB_CLIENT_CERT" ] = "test_cert"
6489 os .environ ["TB_PRIVATE_KEY" ] = "test_key"
6590 config = DeviceConfig ()
6691 self .assertTrue (config .use_tls_auth ())
6792
68- def detects_tls_correctly (self ):
93+ def test_detects_tls_correctly (self ):
6994 os .environ ["TB_CA_CERT" ] = "test_ca"
7095 config = DeviceConfig ()
7196 self .assertTrue (config .use_tls ())
7297
7398
7499class TestGatewayConfig (unittest .TestCase ):
75100
76- def loads_gateway_specific_env_vars (self ):
101+ def test_loads_gateway_specific_env_vars (self ):
77102 os .environ ["TB_GW_HOST" ] = "gw_host"
78103 os .environ ["TB_GW_PORT" ] = "8884"
79104 os .environ ["TB_GW_ACCESS_TOKEN" ] = "gw_token"
@@ -97,10 +122,14 @@ def loads_gateway_specific_env_vars(self):
97122 self .assertEqual (config .private_key , "gw_key" )
98123 self .assertEqual (config .qos , 0 )
99124
100- def falls_back_to_device_config_when_gateway_env_vars_missing (self ):
125+ def test_falls_back_to_device_config_when_gateway_env_vars_missing (self ):
101126 os .environ .clear ()
102127 os .environ ["TB_HOST" ] = "device_host"
103128 os .environ ["TB_PORT" ] = "1884"
104129 config = GatewayConfig ()
105130 self .assertEqual (config .host , "device_host" )
106131 self .assertEqual (config .port , 1884 )
132+
133+
134+ if __name__ == "__main__" :
135+ unittest .main ()
0 commit comments