Path: blob/main/singlestoredb/tests/test_config.py
469 views
#!/usr/bin/env python1# type: ignore2# encoding: utf-83#4# Copyright SAS Institute5#6# Licensed under the Apache License, Version 2.0 (the License);7# you may not use this file except in compliance with the License.8# You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing, software13# distributed under the License is distributed on an "AS IS" BASIS,14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15# See the License for the specific language governing permissions and16# limitations under the License.17#18# This file originally copied from https://github.com/sassoftware/python-swat19#20import unittest2122from singlestoredb.config import check_bool23from singlestoredb.config import check_float24from singlestoredb.config import check_int25from singlestoredb.config import check_str26from singlestoredb.config import check_url27from singlestoredb.config import describe_option28from singlestoredb.config import get_default29from singlestoredb.config import get_option30from singlestoredb.config import get_suboptions31from singlestoredb.config import options32from singlestoredb.config import reset_option33from singlestoredb.config import set_option343536class TestConfig(unittest.TestCase):3738def setUp(self):39reset_option()4041def tearDown(self):42reset_option()4344def test_basic(self):45self.assertEqual(get_option('local_infile'), False)4647set_option('local_infile', True)4849self.assertEqual(get_option('local_infile'), True)5051with self.assertRaises(ValueError):52options.local_infile = 'foo'5354options.local_infile = False55self.assertEqual(options.local_infile, False)5657with self.assertRaises(ValueError):58options.local_infile = 105960self.assertEqual(options.local_infile, False)6162self.assertEqual(type(options.results), type(options))6364options.local_infile = False6566reset_option('local_infile')6768self.assertEqual(options.local_infile, False)6970with self.assertRaises(KeyError):71reset_option('results.foo')7273with self.assertRaises(TypeError):74reset_option('results')7576def test_shortcut_options(self):77asize = get_option('results.arraysize')78token = get_option('management.token')7980self.assertEqual(get_option('arraysize'), asize)81self.assertEqual(options.arraysize, asize)8283options.arraysize = 208485self.assertEqual(get_option('results.arraysize'), 20)86self.assertEqual(options.results.arraysize, 20)87self.assertEqual(options.arraysize, 20)8889self.assertEqual(get_option('token'), token)90self.assertEqual(get_option('management.token'), token)91self.assertEqual(options.token, token)9293options.token = 'Foo'9495self.assertEqual(get_option('token'), 'Foo')96self.assertEqual(get_option('management.token'), 'Foo')97self.assertEqual(options.token, 'Foo')9899reset_option('token')100101self.assertEqual(get_option('token'), token)102self.assertEqual(get_option('management.token'), token)103self.assertEqual(options.token, token)104105def test_missing_options(self):106with self.assertRaises(KeyError):107set_option('results.foo', 10)108109with self.assertRaises(KeyError):110options.results.foo = 10111112with self.assertRaises(KeyError):113get_option('results.foo')114115with self.assertRaises(KeyError):116print(options.results.foo)117118# You can not access a midpoint in the hierarchy with (s|g)et_option119with self.assertRaises(TypeError):120set_option('results', 10)121122with self.assertRaises(TypeError):123get_option('results')124125def test_errors(self):126with self.assertRaises(ValueError):127set_option('credential_type', 'foo')128129def test_doc(self):130out = describe_option('results.arraysize', 'local_infile', _print_desc=False)131for line in out.split('\n'):132if not line or line.startswith(' '):133continue134self.assertRegex(line, r'^(results\.arraysize|local_infile)')135136# Displays entire option hierarchy137out = describe_option('management', _print_desc=False)138for line in out.split('\n'):139if not line or line.startswith(' '):140continue141self.assertRegex(line, r'^management\.')142143with self.assertRaises(KeyError):144describe_option('management.foo')145146out = describe_option(_print_desc=False)147self.assertRegex(out, r'\bmanagement\.token :')148self.assertRegex(out, r'\bhost :')149self.assertRegex(out, r'\bport :')150self.assertRegex(out, r'\buser :')151self.assertRegex(out, r'\bpassword :')152self.assertRegex(out, r'\bcharset :')153154def test_suboptions(self):155self.assertEqual(156list(sorted(get_suboptions('results').keys())),157['arraysize', 'type'],158)159160with self.assertRaises(KeyError):161get_suboptions('results.foo')162163# This is an option, not a level in the hierarchy164with self.assertRaises(TypeError):165get_suboptions('results.arraysize')166167def test_get_default(self):168self.assertEqual(get_default('results.arraysize'), 1)169170with self.assertRaises(KeyError):171get_default('results.foo')172173# This is a level in the hierarchy, not an option174with self.assertRaises(TypeError):175get_default('results')176177def test_check_int(self):178self.assertEqual(check_int(10), 10)179self.assertEqual(check_int(999999999999), 999999999999)180self.assertEqual(check_int('10'), 10)181182with self.assertRaises(ValueError):183check_int('foo')184185self.assertEqual(check_int(10, minimum=9), 10)186self.assertEqual(check_int(10, minimum=10), 10)187with self.assertRaises(ValueError):188check_int(10, minimum=11)189190self.assertEqual(check_int(10, minimum=9, exclusive_minimum=True), 10)191with self.assertRaises(ValueError):192check_int(10, minimum=10, exclusive_minimum=True)193with self.assertRaises(ValueError):194check_int(10, minimum=11, exclusive_minimum=True)195196self.assertEqual(check_int(10, maximum=11), 10)197self.assertEqual(check_int(10, maximum=10), 10)198with self.assertRaises(ValueError):199check_int(10, maximum=9)200201self.assertEqual(check_int(10, maximum=11, exclusive_minimum=True), 10)202with self.assertRaises(ValueError):203check_int(10, maximum=10, exclusive_maximum=True)204with self.assertRaises(ValueError):205check_int(10, maximum=9, exclusive_maximum=True)206207self.assertEqual(check_int(10, multiple_of=5), 10)208with self.assertRaises(ValueError):209check_int(10, multiple_of=3)210211def test_check_float(self):212self.assertEqual(check_float(123.567), 123.567)213self.assertEqual(check_float(999999999999.999), 999999999999.999)214self.assertEqual(check_float('123.567'), 123.567)215216with self.assertRaises(ValueError):217check_float('foo')218219self.assertEqual(check_float(123.567, minimum=123.566), 123.567)220self.assertEqual(check_float(123.567, minimum=123.567), 123.567)221with self.assertRaises(ValueError):222check_float(123.567, minimum=123.577)223224self.assertEqual(225check_float(226123.567, minimum=123.566,227exclusive_minimum=True,228), 123.567,229)230with self.assertRaises(ValueError):231check_float(123.567, minimum=123.567, exclusive_minimum=True)232with self.assertRaises(ValueError):233check_float(123.567, minimum=123.568, exclusive_minimum=True)234235self.assertEqual(check_float(123.567, maximum=123.568), 123.567)236self.assertEqual(check_float(123.567, maximum=123.567), 123.567)237with self.assertRaises(ValueError):238check_float(123.567, maximum=123.566)239240self.assertEqual(241check_float(242123.567, maximum=123.567,243exclusive_minimum=True,244), 123.567,245)246with self.assertRaises(ValueError):247check_float(123.567, maximum=123.567, exclusive_maximum=True)248with self.assertRaises(ValueError):249check_float(123.567, maximum=123.566, exclusive_maximum=True)250251with self.assertRaises(ValueError):252check_float(123.567, multiple_of=3)253254def test_check_str(self):255self.assertEqual(check_str('hi there'), 'hi there')256self.assertTrue(isinstance(check_str('hi there'), str))257258self.assertEqual(check_str('hi there', pattern=r' th'), 'hi there')259with self.assertRaises(ValueError):260check_str('hi there', pattern=r' th$')261262self.assertEqual(check_str('hi there', max_length=20), 'hi there')263self.assertEqual(check_str('hi there', max_length=8), 'hi there')264with self.assertRaises(ValueError):265check_str('hi there', max_length=7)266267self.assertEqual(check_str('hi there', min_length=3), 'hi there')268self.assertEqual(check_str('hi there', min_length=8), 'hi there')269with self.assertRaises(ValueError):270check_str('hi there', min_length=9)271272self.assertEqual(273check_str('hi there', valid_values=['hi there', 'bye now']),274'hi there',275)276with self.assertRaises(ValueError):277check_str('foo', valid_values=['hi there', 'bye now'])278279# Invalid utf8 data280with self.assertRaises(ValueError):281check_str(b'\xff\xfeW[')282283def test_check_url(self):284self.assertEqual(check_url('hi there'), 'hi there')285self.assertTrue(isinstance(check_url('hi there'), str))286287# Invalid utf8 data288with self.assertRaises(ValueError):289check_url(b'\xff\xfeW[')290291def test_check_bool(self):292self.assertEqual(check_bool(True), True)293self.assertEqual(check_bool(False), False)294self.assertEqual(check_bool(1), True)295self.assertEqual(check_bool(0), False)296self.assertEqual(check_bool('yes'), True)297self.assertEqual(check_bool('no'), False)298self.assertEqual(check_bool('T'), True)299self.assertEqual(check_bool('F'), False)300self.assertEqual(check_bool('true'), True)301self.assertEqual(check_bool('false'), False)302self.assertEqual(check_bool('on'), True)303self.assertEqual(check_bool('off'), False)304self.assertEqual(check_bool('enabled'), True)305self.assertEqual(check_bool('disabled'), False)306307with self.assertRaises(ValueError):308check_bool(2)309with self.assertRaises(ValueError):310check_bool('foo')311with self.assertRaises(ValueError):312check_bool(1.1)313314315if __name__ == '__main__':316import nose2317nose2.main()318319320