1515
1616
1717class TestExceptions (unittest .TestCase ):
18+
1819 def test_softlayer_api_error (self ):
1920 e = SoftLayer .SoftLayerAPIError ('fault code' , 'fault string' )
2021 self .assertEquals (e .faultCode , 'fault code' )
@@ -34,3 +35,68 @@ def test_parse_error(self):
3435 repr (e ), "<ParseError(fault code): fault string>" )
3536 self .assertEquals (
3637 str (e ), "ParseError(fault code): fault string" )
38+
39+
40+ class TestUtils (unittest .TestCase ):
41+
42+ def test_query_filter (self ):
43+ result = SoftLayer .utils .query_filter ('test' )
44+ self .assertEqual ({'operation' : 'test' }, result )
45+
46+ result = SoftLayer .utils .query_filter ('*test' )
47+ self .assertEqual ({'operation' : '$= test' }, result )
48+
49+ result = SoftLayer .utils .query_filter ('test*' )
50+ self .assertEqual ({'operation' : '^= test' }, result )
51+
52+ result = SoftLayer .utils .query_filter ('*test*' )
53+ self .assertEqual ({'operation' : '~ test' }, result )
54+
55+ result = SoftLayer .utils .query_filter ('> 10' )
56+ self .assertEqual ({'operation' : '> 10' }, result )
57+
58+ result = SoftLayer .utils .query_filter ('>10' )
59+ self .assertEqual ({'operation' : '> 10' }, result )
60+
61+ result = SoftLayer .utils .query_filter (10 )
62+ self .assertEqual ({'operation' : 10 }, result )
63+
64+
65+ class TestNestedDict (unittest .TestCase ):
66+
67+ def test_basic (self ):
68+ n = SoftLayer .utils .NestedDict ()
69+ self .assertEqual (n ['test' ], SoftLayer .utils .NestedDict ())
70+
71+ n ['test_set' ] = 1
72+ self .assertEqual (n ['test_set' ], 1 )
73+
74+ d = {
75+ 'test' : {
76+ 'nested' : 1
77+ }}
78+
79+ n = SoftLayer .utils .NestedDict (d )
80+ self .assertEqual (d , n )
81+ self .assertEqual (n ['test' ]['nested' ], 1 )
82+
83+ # new default top level elements should return a new NestedDict()
84+ self .assertEqual (n ['not' ]['nested' ], SoftLayer .utils .NestedDict ())
85+
86+ # NestedDict doesn't convert dict children, just the top level dict
87+ # so you can't assume the same behavior with children
88+ self .assertRaises (KeyError , lambda : n ['test' ]['not' ]['nested' ])
89+
90+ def test_to_dict (self ):
91+ n = SoftLayer .utils .NestedDict ()
92+ n ['test' ]['test1' ]['test2' ]['test3' ] = {}
93+ d = n .to_dict ()
94+
95+ self .assertEqual ({
96+ 'test' : {'test1' : {'test2' : {'test3' : {}}}}
97+ }, d )
98+ self .assertEqual (dict , type (d ))
99+ self .assertEqual (dict , type (d ['test' ]))
100+ self .assertEqual (dict , type (d ['test' ]['test1' ]))
101+ self .assertEqual (dict , type (d ['test' ]['test1' ]['test2' ]))
102+ self .assertEqual (dict , type (d ['test' ]['test1' ]['test2' ]['test3' ]))
0 commit comments