mycroft-core/test/unittests/skills/test_settings.py

130 lines
4.3 KiB
Python
Raw Normal View History

Change to Apache 2.0 license from GPLv3.0 This commit officially switches the mycroft-core repository from GPLv3.0 licensing to Apache 2.0. All dependencies on GPL'ed code have been removed and we have contacted all previous contributors with still-existing code in the repository to agree to this change. Going forward, all contributors will sign a Contributor License Agreement (CLA) by visiting https://mycroft.ai/cla, then they will be included in the Mycroft Project's overall Contributor list, found at: https://github.com/MycroftAI/contributors. This cleanly protects the project, the contributor and all who use the technology to build upon. Futher discussion can be found at this blog post: https://mycroft.ai/blog/right-license/ This commit also removes all __author__="" from the code. These lines are painful to maintain and the etiquette surrounding their maintainence is unclear. Do you remove a name from the list if the last line of code the wrote gets replaced? Etc. Now all contributors are publicly acknowledged in the aforementioned repo, and actual authorship is maintained by Github in a much more effective and elegant way! Finally, a few references to "Mycroft AI" were changed to the correct legal entity name "Mycroft AI Inc." ==== Fixed Issues ==== #403 Update License.md and file headers to Apache 2.0 #400 Update LICENSE.md ==== Documentation Notes ==== Deprecated the ScheduledSkill and ScheduledCRUDSkill classes. These capabilities have been superceded by the more flexible MycroftSkill class methods schedule_event(), schedule_repeating_event(), update_event(), and cancel_event().
2017-10-04 06:28:44 +00:00
# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
2017-04-18 16:59:06 +00:00
import unittest
from os import remove
from os.path import join, dirname
from mycroft.skills.settings import SkillSettings
2017-04-18 16:59:06 +00:00
class SkillSettingsTest(unittest.TestCase):
def setUp(self):
try:
remove(join(dirname(__file__), 'settings', 'settings.json'))
2017-04-18 16:59:06 +00:00
except OSError:
pass
def test_new(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-04-18 16:59:06 +00:00
self.assertEqual(len(s), 0)
def test_add_value(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-04-18 16:59:06 +00:00
s['test_val'] = 1
self.assertEqual(s['test_val'], 1)
2017-04-18 16:59:06 +00:00
def test_store(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s.allow_overwrite = True
s.load_skill_settings_from_file()
2017-04-18 16:59:06 +00:00
s['bool'] = True
s['int'] = 42
s['float'] = 4.2
s['string'] = 'Always carry a towel'
s['list'] = ['batman', 2, True, 'superman']
s.store()
2017-07-27 21:38:24 +00:00
s2 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s2.allow_overwrite = True
s2.load_skill_settings_from_file()
2017-04-18 16:59:06 +00:00
for key in s:
self.assertEqual(s[key], s2[key])
2017-07-27 21:38:24 +00:00
def test_update_list(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s.allow_overwrite = True
s.load_skill_settings_from_file()
s['l'] = ['a', 'b', 'c']
s.store()
s2 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s2.allow_overwrite = True
s2.load_skill_settings_from_file()
self.assertEqual(s['l'], s2['l'])
# Update list
s2['l'].append('d')
s2.store()
s3 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s3.allow_overwrite = True
s3.load_skill_settings_from_file()
self.assertEqual(s2['l'], s3['l'])
def test_update_dict(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s.allow_overwrite = True
s['d'] = {'a': 1, 'b': 2}
s.store()
s2 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s2.allow_overwrite = True
s2.load_skill_settings_from_file()
self.assertEqual(s['d'], s2['d'])
# Update dict
s2['d']['c'] = 3
s2.store()
s3 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s3.allow_overwrite = True
s3.load_skill_settings_from_file()
self.assertEqual(s2['d'], s3['d'])
def test_no_change(self):
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s.allow_overwrite = True
s['d'] = {'a': 1, 'b': 2}
s.store()
s2 = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s2.allow_overwrite = True
s2.load_skill_settings_from_file()
2017-07-06 17:40:21 +00:00
self.assertTrue(len(s) == len(s2))
def test_load_existing(self):
directory = join(dirname(__file__), 'settings', 'settings.json')
with open(directory, 'w') as f:
json.dump({"test": "1"}, f)
s = SkillSettings(join(dirname(__file__), 'settings'),
"test-skill-settings")
2017-12-19 23:51:35 +00:00
s.allow_overwrite = True
s.load_skill_settings_from_file()
self.assertEqual(len(s), 1)
if __name__ == '__main__':
unittest.main()