Skip to content

Commit

Permalink
Merge pull request #31 from configcat/localfile-tests
Browse files Browse the repository at this point in the history
test_reload_file + test_invalid_file
  • Loading branch information
kp-cat authored Apr 8, 2022
2 parents 73ec2cc + a484702 commit 99786cb
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion configcatclient/localfiledatasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def _reload_file_content(self):
self._settings = data
except OSError as e:
log.error('Could not read the content of the file %s. %s' % (self._file_path, e))
except json.decoder.JSONDecodeError as e:
except ValueError as e:
log.error('Could not decode json from file %s. %s' % (self._file_path, e))

2 changes: 1 addition & 1 deletion configcatclient/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CONFIGCATCLIENT_VERSION = "6.0.0"
CONFIGCATCLIENT_VERSION = "6.0.1"
49 changes: 49 additions & 0 deletions configcatclienttests/test_local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import unittest
from os import path
import tempfile
import json
import time

from configcatclient import ConfigCatClient
from configcatclient.localdictionarydatasource import LocalDictionaryDataSource
Expand Down Expand Up @@ -65,6 +68,52 @@ def test_non_existent_file(self):
self.assertFalse(client.get_value('enabledFeature', False))
client.stop()

def test_reload_file(self):
temp = tempfile.NamedTemporaryFile(mode="w")
dictionary = {'flags': {
'enabledFeature': False
}}
json.dump(dictionary, temp)
temp.flush()

client = ConfigCatClient(sdk_key='test',
poll_interval_seconds=0,
max_init_wait_time_seconds=0,
flag_overrides=LocalFileDataSource(file_path=temp.name,
override_behaviour=OverrideBehaviour.LocalOnly))

self.assertFalse(client.get_value('enabledFeature', True))

time.sleep(0.5)

# clear the content of the temp file
temp.seek(0)
temp.truncate()

# change the temporary file
dictionary['flags']['enabledFeature'] = True
json.dump(dictionary, temp)
temp.flush()

self.assertTrue(client.get_value('enabledFeature', False))

client.stop()

def test_invalid_file(self):
temp = tempfile.NamedTemporaryFile(mode="w")
temp.write('{"flags": {"enabledFeature": true}')
temp.flush()

client = ConfigCatClient(sdk_key='test',
poll_interval_seconds=0,
max_init_wait_time_seconds=0,
flag_overrides=LocalFileDataSource(file_path=temp.name,
override_behaviour=OverrideBehaviour.LocalOnly))

self.assertFalse(client.get_value('enabledFeature', False))

client.stop()

def test_dictionary(self):
dictionary = {
'enabledFeature': True,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def parse_requirements(filename):
return [line for line in lines if line]


configcatclient_version = '6.0.0'
configcatclient_version = '6.0.1'

requirements = parse_requirements('requirements.txt')

Expand Down

0 comments on commit 99786cb

Please sign in to comment.