Skip to content

Commit a8fa962

Browse files
committed
Clarify config code and add a few test cases. Fixes #123. Thanks @ecebuzz!
1 parent 882d90e commit a8fa962

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

mrq/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,15 @@ def get_config(
438438

439439
# We only keep variables starting with an uppercase character.
440440
if k[0].isupper():
441-
default_config[k.lower()] = v
441+
from_file[k.lower()] = v
442442

443443
# Merge the config in the order given by the user
444444
merged_config = default_config
445+
446+
config_keys = set(default_config.keys() + from_file.keys())
447+
445448
for part in sources:
446-
for name in merged_config:
449+
for name in config_keys:
447450

448451
if part == "env":
449452
value = os.environ.get(env_prefix + name.upper())

tests/fixtures/config2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22

33
# There configs should be added transparently.
44
ADDITIONAL_UNEXPECTED_CONFIG = "1"
5+
6+
MONGODB_JOBS = "mongodb://127.0.0.1:27017/mrq?connectTimeoutMS=4242"

tests/test_config.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@
44
def test_config(worker):
55
""" Test different config passing options. """
66

7+
# Default config values
78
worker.start()
89

910
cfg = json.loads(
1011
worker.send_task("tests.tasks.general.GetConfig", {}, block=True))
1112

12-
assert "mongodb_jobs" in cfg
13+
assert cfg["mongodb_jobs"] == "mongodb://127.0.0.1:27017/mrq"
1314
assert cfg.get("additional_unexpected_config") is None
1415

1516
worker.stop()
1617

18+
# Values from config file
1719
worker.start(flags="--config tests/fixtures/config2.py")
1820

1921
cfg = json.loads(
2022
worker.send_task("tests.tasks.general.GetConfig", {}, block=True))
2123

24+
assert cfg["mongodb_jobs"] == "mongodb://127.0.0.1:27017/mrq?connectTimeoutMS=4242"
2225
assert cfg["name"] == "testworker"
2326
assert cfg.get("additional_unexpected_config") == "1"
2427

2528
worker.stop()
2629

27-
worker.start(flags="--config tests/fixtures/config2.py --name xxx")
30+
# CLI > Config file && CLI > ENV
31+
worker.start(flags="--config tests/fixtures/config2.py --name xxx", env={"MRQ_NAME": "yyy"})
2832

2933
cfg = json.loads(
3034
worker.send_task("tests.tasks.general.GetConfig", {}, block=True))
@@ -33,3 +37,14 @@ def test_config(worker):
3337
assert cfg.get("additional_unexpected_config") == "1"
3438

3539
worker.stop()
40+
41+
# ENV > Config file
42+
worker.start(flags="--config tests/fixtures/config2.py", env={"MRQ_NAME": "yyy"})
43+
44+
cfg = json.loads(
45+
worker.send_task("tests.tasks.general.GetConfig", {}, block=True))
46+
47+
assert cfg["name"] == "yyy"
48+
assert cfg.get("additional_unexpected_config") == "1"
49+
50+
worker.stop()

0 commit comments

Comments
 (0)