Skip to content

Instantly share code, notes, and snippets.

@Kostanos
Created December 30, 2017 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kostanos/736dddbc36d226962c3c43a7f95416d3 to your computer and use it in GitHub Desktop.
Save Kostanos/736dddbc36d226962c3c43a7f95416d3 to your computer and use it in GitHub Desktop.
python Settings class test
from . import Settings, cleanCache
import unittest
from unittest.mock import patch
import os
from pyfakefs import fake_filesystem, fake_filesystem_unittest
from dotmap import DotMap
# import pyfakefs.fake_filesystem_ as fake_glob
# Faking filesystem to test .yaml files
fs = fake_filesystem.FakeFilesystem()
mockTexts = DotMap({
"settings.yaml": {
"file": None,
"text": """testList:
- a:
aa: aa
bb: bb
- b:
aa: aa
bb: bb
testDict:
a:
aa: aa
bb: bb
b:
aa: aa
bb: bb
"""
},
"test.yaml": {
"file": None,
"text": """testList_test:
- a:
aa: aa
bb: bb
- b:
aa: aa
bb: bb
testDict_test:
a:
aa: aa
bb: bb
b:
aa: aa
bb: bb
"""
}
})
mockTexts2 = DotMap({
"settings.dev.yaml": {
"file": None,
"text": """testList_dev:
- a:
aa: aa
bb: bb
- b:
aa: aa
bb: bb
testDict:
a:
aa: aa_dev
cc: cc
b:
aa: aa
bb: bb
"""
},
})
curDir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
# def fakeOpen(filename):
# print('File Open call: {}'.format(filename))
class TestSettings(fake_filesystem_unittest.TestCase):
def setUp(self):
# this test fails because of issue: https://github.com/jmcgeheeiv/pyfakefs/issues/338
self.setUpPyfakefs()
# create fake *.yaml files
for filename in list(mockTexts):
self.fs.CreateFile(os.path.join(curDir, filename), contents=mockTexts[filename].text)
def tearDown(self):
# It is no longer necessary to add self.tearDownPyfakefs()
pass
@patch.dict(os.environ, {'ENV': 'dev'})
def testDefaultSettings(self):
# Testing configuration settings.yaml only
self.assertTrue(os.path.exists(os.path.join(curDir, 'settings.yaml')))
self.assertFalse(os.path.exists(os.path.join(curDir, 'settings.dev.yaml')))
cleanCache()
settings = Settings()
self.assertEqual(settings.testDict.a.aa, 'aa')
self.assertEqual(settings.testDict.a.bb, 'bb')
self.assertNotIn('cc', settings.testDict.a)
self.assertNotIn('testDict_test', settings)
@patch.dict(os.environ, {'ENV': 'dev'})
def testDevSettings(self):
# Testing configuration settings.yaml and settings.dev.yaml
for filename, data in mockTexts2.items():
self.fs.CreateFile(os.path.join(curDir, filename), contents=data.text)
self.assertTrue(os.path.exists(os.path.join(curDir, 'settings.yaml')))
self.assertTrue(os.path.exists(os.path.join(curDir, 'settings.dev.yaml')))
cleanCache()
settings = Settings()
self.assertEqual(settings.testDict.a.aa, 'aa_dev')
self.assertEqual(settings.testDict.a.bb, 'bb') # ensure that default settings record is still there
self.assertEqual(settings.testDict.a.cc, 'cc')
self.assertNotIn('testDict_test', settings)
@patch.dict(os.environ, {'ENV': 'dev'})
def testModuleSettings(self):
# Testing configuration settings.yaml and test.yaml
self.assertTrue(os.path.exists(os.path.join(curDir, 'settings.yaml')))
self.assertTrue(os.path.exists(os.path.join(curDir, 'test.yaml')))
cleanCache()
settings = Settings('test')
# We still should have records from 'settings.yaml'
self.assertEqual(settings.testDict.a.aa, 'aa')
self.assertEqual(settings.testDict.a.bb, 'bb')
self.assertNotIn('cc', settings.testDict.a) # but no from settings.dev.yaml
self.assertEqual(settings.testDict_test.a.aa, 'aa')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment