<commit_before>#!/usr/bin/env python3

import sys, os, unittest, logging, pkg_resources, tempfile

import jasy.core.Config as Config

# Extend PYTHONPATH with local 'lib' folder
jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir))
sys.path.insert(0, jasyroot)


class Tests(unittest.TestCase):

    def test_write_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 1,"two": 2},{"three": 3,"four": 4}], os.path.join(tempDirectory, "test.json"))
        
        self.assertEqual(Config.findConfig(os.path.join(tempDirectory, "test.json")), os.path.join(tempDirectory, "test.json"))        
       
       
    def test_write_and_read_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], os.path.join(tempDirectory, "test.json"))
        data = Config.loadConfig(os.path.join(tempDirectory, "test.json"))

        self.assertEqual(data, [{'two': 2, 'one': 1}, {'four': 4, 'three': 3}])        
       

    def test_write_yaml(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 1,"two": 2},{"three": 3,"four": 4}], os.path.join(tempDirectory, "test.yaml"))
        
        self.assertEqual(Config.findConfig(os.path.join(tempDirectory, "test.yaml")), os.path.join(tempDirectory, "test.yaml"))        
    

    def test_write_and_read_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], os.path.join(tempDirectory, "test.yaml"))
        data = Config.loadConfig(os.path.join(tempDirectory, "test.yaml"))

        self.assertEqual(data, [{'two': 2, 'one': 1}, {'four': 4, 'three': 3}]) 


    def test_matching_types(self):

        self.assertTrue(Config.matchesType(42, "int"))
        self.assertTrue(Config.matchesType(11.0, "float"))
        self.assertTrue(Config.matchesType(11.1, "float"))
        self.assertTrue(Config.matchesType("hello", "string"))
        self.assertTrue(Config.matchesType(False, "bool"))
        self.assertTrue(Config.matchesType([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], "list"))
        self.assertTrue(Config.matchesType({"one": 10-9,"two": 5-3}, "dict"))
    

    def test_config_object_hasdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        self.assertTrue(config.has('two'))


    def test_config_object_getdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        self.assertEqual(config.get('ten'), 10)


    def test_config_object_setdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        config.set('ten', 15)
        self.assertEqual(config.get('ten'), 15)


if __name__ == '__main__':
    logging.getLogger().setLevel(logging.ERROR)
    suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
    unittest.TextTestRunner(verbosity=2).run(suite)
<commit_msg>config "dot" support test<commit_after>#!/usr/bin/env python3

import sys, os, unittest, logging, pkg_resources, tempfile

import jasy.core.Config as Config

# Extend PYTHONPATH with local 'lib' folder
jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir))
sys.path.insert(0, jasyroot)


class Tests(unittest.TestCase):

    def test_write_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 1,"two": 2},{"three": 3,"four": 4}], os.path.join(tempDirectory, "test.json"))
        
        self.assertEqual(Config.findConfig(os.path.join(tempDirectory, "test.json")), os.path.join(tempDirectory, "test.json"))        
       
       
    def test_write_and_read_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], os.path.join(tempDirectory, "test.json"))
        data = Config.loadConfig(os.path.join(tempDirectory, "test.json"))

        self.assertEqual(data, [{'two': 2, 'one': 1}, {'four': 4, 'three': 3}])        
       

    def test_write_yaml(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 1,"two": 2},{"three": 3,"four": 4}], os.path.join(tempDirectory, "test.yaml"))
        
        self.assertEqual(Config.findConfig(os.path.join(tempDirectory, "test.yaml")), os.path.join(tempDirectory, "test.yaml"))        
    

    def test_write_and_read_json(self):

        tempDirectory = tempfile.TemporaryDirectory().name
        os.makedirs(tempDirectory)

        Config.writeConfig([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], os.path.join(tempDirectory, "test.yaml"))
        data = Config.loadConfig(os.path.join(tempDirectory, "test.yaml"))

        self.assertEqual(data, [{'two': 2, 'one': 1}, {'four': 4, 'three': 3}]) 


    def test_matching_types(self):

        self.assertTrue(Config.matchesType(42, "int"))
        self.assertTrue(Config.matchesType(11.0, "float"))
        self.assertTrue(Config.matchesType(11.1, "float"))
        self.assertTrue(Config.matchesType("hello", "string"))
        self.assertTrue(Config.matchesType(False, "bool"))
        self.assertTrue(Config.matchesType([{"one": 10-9,"two": 5-3},{"three": 1+1+1,"four": 2*2}], "list"))
        self.assertTrue(Config.matchesType({"one": 10-9,"two": 5-3}, "dict"))
    

    def test_config_object_hasdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        self.assertTrue(config.has('two'))


    def test_config_object_getdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        self.assertEqual(config.get('ten'), 10)


    def test_config_object_setdata(self):

        config = Config.Config({'two': 2, 'one': 1, 'ten': 10})
        config.set('ten', 15)
        self.assertEqual(config.get('ten'), 15)


    def test_config_object_getdata_withdot(self):

        config = Config.Config({'foo': {'yeah': 42}, 'one': 1})
        self.assertEqual(config.get('foo.yeah'), 42)


    def test_config_object_setdata_withdot(self):

        config = Config.Config({'foo': {'yeah': 42}, 'one': 1})
        config.set('foo.yeah', 1337)
        self.assertEqual(config.get('foo')['yeah'], 1337)


if __name__ == '__main__':
    logging.getLogger().setLevel(logging.ERROR)
    suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
    unittest.TextTestRunner(verbosity=2).run(suite)

{'fraction_non_alphanumeric': 0.1805149210064365, 'fraction_numerical': 0.026038619075482737, 'mean_word_length': 3.5549633577614923, 'pattern_counts': {'":': 44, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352923', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2253, 'n_words': 367}