Skip to content

Commit

Permalink
Added tests for complex config files passed by arguments and fixed a …
Browse files Browse the repository at this point in the history
…bug that was causing them to fail.
  • Loading branch information
Paddy Foran committed May 1, 2012
1 parent 5cd9c61 commit c367f57
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
8 changes: 5 additions & 3 deletions iron_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ def configFromFile(config, path, product=None):
except IOError, e:
return config
raw = json.loads(file.read())
for k in raw.keys():
if k in config:
config[k] = raw[k]
if product is not None:
if product in raw:
raw = raw[product]
for k in raw.keys():
config[k] = raw[k]
for k in raw[product].keys():
config[k] = raw[product][k]
return config


Expand Down
59 changes: 59 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,64 @@ def test_fromArgsConfigFileGlobal(self):

os.remove("test_config.json")

def test_fromArgsConfigFileProduct(self):
test_config = {
"iron_worker": {
"host": "test-config-host",
"protocol": "test-config-protocol",
"port": "test-config-port",
"api_version": "test-config-api-version",
"project_id": "test-config-project-id",
"token": "test-config-token"
}
}

file = open("test_config.json", "w")
file.write(json.dumps(test_config))
file.close()

client = iron_core.IronClient(name="Test", version="0.1.0",
product="iron_worker", config_file="test_config.json")

self.assertEqual(client.host, test_config["iron_worker"]["host"])
self.assertEqual(client.protocol,
test_config["iron_worker"]["protocol"])
self.assertEqual(client.port, test_config["iron_worker"]["port"])
self.assertEqual(client.api_version,
test_config["iron_worker"]["api_version"])
self.assertEqual(client.project_id,
test_config["iron_worker"]["project_id"])
self.assertEqual(client.token, test_config["iron_worker"]["token"])

def test_fromArgsConfigFileMixed(self):
test_config = {
"host": "test-config-host-global",
"protocol": "test-config-protocol-global",
"port": "test-config-port-global",
"project_id": "test-config-project-id-global",
"iron_worker": {
"api_version": "test-config-api-version-product",
"project_id": "test-config-project-id-product",
"token": "test-config-token-product"
}
}

file = open("test_config.json", "w")
file.write(json.dumps(test_config))
file.close()

client = iron_core.IronClient(name="Test", version="0.1.0",
product="iron_worker", config_file="test_config.json")

self.assertEqual(client.host, test_config["host"])
self.assertEqual(client.protocol, test_config["protocol"])
self.assertEqual(client.port, test_config["port"])
self.assertEqual(client.api_version,
test_config["iron_worker"]["api_version"])
self.assertEqual(client.project_id,
test_config["iron_worker"]["project_id"])
self.assertEqual(client.token, test_config["iron_worker"]["token"])


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"iron_worker": {"protocol": "test-config-protocol", "host": "test-config-host", "token": "test-config-token", "project_id": "test-config-project-id", "port": "test-config-port", "api_version": "test-config-api-version"}}

0 comments on commit c367f57

Please sign in to comment.