Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Under JSON Create containing directory for config file. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nativeconfig/configs/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JSONConfig(BaseConfig):
JSON_PATH = None

def __init__(self):
Path(self.JSON_PATH).parent.mkdir(parents= True, exist_ok = True)
if not Path(self.JSON_PATH).is_file():
with open(self.JSON_PATH, 'w+', encoding='utf-8') as f:
f.write(json.dumps({}))
Expand Down
11 changes: 11 additions & 0 deletions test/configs/test_json_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import json
import os
import shutil
import tempfile
import unittest
import unittest.mock
Expand All @@ -25,6 +26,8 @@ def setUp(self):

def tearDown(self):
try:
shutil.rmtree(self.CONFIG_TYPE.JSON_PATH)
except NotADirectoryError:
os.unlink(self.CONFIG_TYPE.JSON_PATH)
except FileNotFoundError:
pass
Expand Down Expand Up @@ -63,6 +66,14 @@ class MyConfig(self.CONFIG_TYPE):
MyConfig.get_instance()
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), True)

def test_config_is_created_if_not_found__and_app_directory_need_creating(self):
class MyConfig(self.CONFIG_TYPE):
JSON_PATH = os.path.join(self.CONFIG_TYPE.JSON_PATH,'_cfgtmp.json')
first_name = StringOption('FirstName', default='Ilya')
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), False)
MyConfig.get_instance()
self.assertEqual(os.path.isfile(MyConfig.JSON_PATH), True)

def test_order_is_preserved_in_json(self):
class MyConfig(self.CONFIG_TYPE):
second_name = StringOption('SecondName', default='Kulakov')
Expand Down