Skip to content

Commit c7a1565

Browse files
Sam StelleShakeel Mohamed
authored andcommitted
Fix of #85, the commit was somehow lost
1 parent ea74bcc commit c7a1565

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

tests/test_utils.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import os
2+
from subprocess import PIPE, Popen
3+
from os.path import expanduser
4+
import time
5+
import sys
6+
7+
import testlib
8+
9+
import splunklib.client as client
10+
from splunklib.binding import UrlEncoded
11+
12+
try:
13+
from utils import *
14+
except ImportError:
15+
raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
16+
"(e.g., export PYTHONPATH=~/splunk-sdk-python.")
17+
18+
19+
TEST_DICT = {
20+
'username':'admin',
21+
'password':'changeme',
22+
'port' : 8089,
23+
'host' : 'localhost',
24+
'scheme': 'https'
25+
}
26+
27+
class TestUtils(testlib.SDKTestCase):
28+
def setUp(self):
29+
super(TestUtils, self).setUp()
30+
31+
# Test dslice when a dict is passed to change key names
32+
def test_dslice_dict_args(self):
33+
args = {
34+
'username':'user-name',
35+
'password':'new_password',
36+
'port': 'admin_port',
37+
'foo':'bar'
38+
}
39+
expected = {
40+
'user-name':'admin',
41+
'new_password':'changeme',
42+
'admin_port':8089
43+
}
44+
self.assertTrue(expected == dslice(TEST_DICT, args))
45+
46+
# Test dslice when a list is passed
47+
def test_dslice_list_args(self):
48+
test_list = [
49+
'username',
50+
'password',
51+
'port',
52+
'host',
53+
'foo'
54+
]
55+
expected = {
56+
'username':'admin',
57+
'password':'changeme',
58+
'port':8089,
59+
'host':'localhost'
60+
}
61+
self.assertTrue(expected == dslice(TEST_DICT, test_list))
62+
63+
# Test dslice when a single string is passed
64+
def test_dslice_arg(self):
65+
test_arg = 'username'
66+
expected = {
67+
'username':'admin'
68+
}
69+
self.assertTrue(expected == dslice(TEST_DICT, test_arg))
70+
71+
# Test dslice using all three types of arguments
72+
def test_dslice_all_args(self):
73+
test_args = [
74+
{'username':'new_username'},
75+
['password',
76+
'host'],
77+
'port'
78+
]
79+
expected = {
80+
'new_username':'admin',
81+
'password':'changeme',
82+
'host':'localhost',
83+
'port':8089
84+
}
85+
self.assertTrue(expected == dslice(TEST_DICT, *test_args))
86+
87+
88+
if __name__ == "__main__":
89+
try:
90+
import unittest2 as unittest
91+
except ImportError:
92+
import unittest
93+
unittest.main()

utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def dslice(value, *args):
9090
if value.has_key(k):
9191
result[k] = value[k]
9292
else:
93-
if value.has_key(arg): result[arg]
93+
if value.has_key(arg):
94+
result[arg] = value[arg]
9495
return result
9596

9697
def parse(argv, rules=None, config=None, **kwargs):

0 commit comments

Comments
 (0)