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

test: add test_load_dot_env #10

Merged
merged 11 commits into from
Nov 23, 2023
35 changes: 35 additions & 0 deletions src/colony/test/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

import colony

try: import unittest.mock as mock
except ImportError: mock = None

class ConfigTest(unittest.TestCase):

def test_basic(self):
Expand Down Expand Up @@ -88,3 +91,35 @@ def test_none(self):
result = colony.conf("HEIGHT", cast = int)

self.assertEqual(result, None)

def test_load_dot_env(self):
if mock == None:
self.skipTest("Skipping test: mock unavailable")

mock_data = mock.mock_open(read_data = b"#This is a comment\nAGE=10\nNAME=colony\n")

with mock.patch("os.path.exists", return_value = True),\
mock.patch("builtins.open", mock_data, create = True) as mock_open:

ctx = dict(
configs = {},
config_f = []
)

colony.base.config.load_dot_env(".env", "utf-8", ctx)

result = colony.conf("AGE", cast = int)
self.assertEqual(type(result), int)
self.assertEqual(result, 10)

result = colony.conf("AGE", cast = str)

self.assertEqual(result, "10")
self.assertEqual(type(result), str)

result = colony.conf("HEIGHT", cast = int)
self.assertEqual(result, None)

self.assertEqual(len(ctx["configs"]), 2)

self.assertEqual(mock_open.return_value.close.call_count, 1)