Beispiel #1
0
 def test_items(self):
     with tempfile.TemporaryDirectory() as tmpdirname:
         d = DirDict(os.path.join(str(tmpdirname), 'dict5/storage'))
         d['lang'] = 'Python'
         d['lib'] = 'unittest'
         self.assertEqual([('lang', 'Python'), ('lib', 'unittest')],
                          d.items())
Beispiel #2
0
class TestDirDict(unittest.TestCase):
    def setUp(self):
        self.directory = 'd:\python\dirdict'
        self.file_names_list = ['first', 'second', 'third']
        self.dir_dict = DirDict(self.directory)

    def test_get_full_path_to_file(self):
        for name in self.file_names_list:
            print(self.directory.join(name))
            self.assertEqual(self.dir_dict._get_full_path_to_file(name),
                             self.directory + '\\' + name)

    # def test_get_list_of_files(self):
    #     self.assertEqual(
    #         self.dir_dict._get_list_of_files(),
    #         self.file_names_list
    #     )

    def test_setitem_getitem(self):
        for name in self.file_names_list:
            self.dir_dict[name] = 'value' + name
            with open(self.dir_dict._get_full_path_to_file(name),
                      'r',
                      encoding='utf8') as f:
                value = f.read()
                self.assertEqual(value, self.dir_dict[name])

    def test_getitem_keyerror(self):
        with self.assertRaises(KeyError):
            a = self.dir_dict['not_in_list']

    def test_del_faulty_item(self):
        with self.assertRaises(KeyError):
            del (self.dir_dict['not_in_list'])

    def test_del_ok_item(self):
        del (self.dir_dict['first'])
        self.assertEqual(os.listdir(self.directory), ['second', 'third'])

    def test_iter(self):
        keys_list_with_iter = []
        for key in self.dir_dict:
            keys_list_with_iter.append(key)
        self.assertEqual(keys_list_with_iter, ['second', 'third'])

    def test_len(self):
        self.assertEqual(len(self.dir_dict), 2)
Beispiel #3
0
 def test_add(self):
     p = DirDict("/tmp/dirdict")
     self.assertEqual(0, len(p))
     self.assertFalse(p)
     p['file'] = 'somestring\n'
     self.assertEqual(1, len(p))
     self.assertEqual('somestring\n', p['file'])
     del p['file']
     self.assertEqual(0, len(p))
Beispiel #4
0
    def test_dir_dict_base(self):
        d_dir = DirDict(TEST_PATH)
        self.assertEqual(0, len(d_dir))

        d_dir['el1'] = 'data1'
        d_dir['el2'] = 'data2'
        d_dir['el3'] = 'data3'

        self.assertEqual(3, len(d_dir))

        with self.assertRaises(KeyError):
            print(d_dir['el4'])

        self.assertEqual(d_dir['el1'], 'data1')
        self.assertEqual(d_dir['el2'], 'data2')
        self.assertEqual(d_dir['el3'], 'data3')

        del d_dir['el1']

        with self.assertRaises(KeyError):
            print(d_dir['el'])

        d = {'el2': 'data2', 'el3': 'data3'}
        self.assertEqual(str(d), str(d_dir))
Beispiel #5
0
 def setUp(self):
     self.path = './tmp' + str(time())
     self.dictionary = DirDict(self.path)
Beispiel #6
0
class TestAdvanced(TestCase):

    def setUp(self):
        self.path = './tmp' + str(time())
        self.dictionary = DirDict(self.path)

    def tearDown(self):
        for filename in os.listdir(self.path):
            os.remove(os.path.join(self.path, filename))
        os.rmdir(self.path)

    def test__list_info(self):
        filename = "first.txt"
        info = [1,2,3,4,5]

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary[filename], str(info))

    def test__class_info(self):
        filename = "first.txt"
        info = SomeInformation()

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary[filename], str(info))

    def test__no_cached(self):
        filename = "first.txt"
        info = "some info"

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary[filename], info)

        with open(os.path.join(self.path, filename), 'w') as f:
            new_info = "new info"
            f.write(new_info)
            info = new_info

        self.assertEqual(self.dictionary[filename], info)

    def test__clear(self):
        filenames = ["first.txt", "second.txt", "third.txt"]
        texts = ["one", "two", "three"]
        
        
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(self.dictionary.__setitem__(filenames[i], texts[i]), None)

        self.assertEqual(self.dictionary.clear(), None)
        self.assertEqual(len(self.dictionary), 0)

    def test__default_get(self):
        filename = "first.txt"
        info = "some info"
        no_existing_filename = "no_exist.txt"

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary.get(no_existing_filename, 42), 42)

    def test__pop(self):
        filename = "first.txt"
        info = "some info"
        no_existing_filename = "no_exist.txt"

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary.pop(no_existing_filename, 42), 42)

    def test__setdefault(self):
        filename = "first.txt"
        info = "some info"
        no_existing_filename = "no_exist.txt"

        self.assertEqual(self.dictionary.__setitem__(filename, info), None)
        self.assertEqual(self.dictionary.setdefault(no_existing_filename, '42'), '42')
        self.assertEqual(self.dictionary.setdefault(filename), info)

    def test__advanced_loop(self):
        filenames = ["first.txt", "second.txt", "third.txt"]
        texts = ["one", "two", "three"]
        
        
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(self.dictionary.__setitem__(filenames[i], texts[i]), None)

        dict_keys = self.dictionary.keys()
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(dict_keys[i], filenames[i])

        dict_values = self.dictionary.values()
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(dict_values[i], texts[i])
Beispiel #7
0
 def test__create_from_non_existing(self):
     non_existing_dir = './tmp' + str(time())
     dir_dict = DirDict(non_existing_dir)
     self.assertEqual(os.path.exists(non_existing_dir), True)
     os.rmdir(non_existing_dir)
Beispiel #8
0
class TestFunctional(TestCase):

    def setUp(self):
        self.path = './tmp' + str(time())
        self.dictionary = DirDict(self.path)

    def tearDown(self):
        for filename in os.listdir(self.path):
            os.remove(os.path.join(self.path, filename))
        os.rmdir(self.path)

    def test__empty_dir(self):
        filename = "not_exist.txt"
        self.assertEqual(len(self.dictionary), 0)
        with self.assertRaises(IndexError):
            self.dictionary[filename]
        with self.assertRaises(SyntaxError):
            self.dictionary[''] = 'test'
        with self.assertRaises(SyntaxError):
            self.dictionary['']
    
    def test__get_and_set(self):
        filename1 = "first.txt"
        text1 = "first"
        text2 = "second"
        self.assertEqual(self.dictionary.__setitem__(filename1, text1), None)
        self.assertEqual(self.dictionary[filename1], text1)
        self.assertEqual(len(self.dictionary), 1)
        self.assertEqual(self.dictionary.__setitem__(filename1, text2), None)
        self.assertEqual(self.dictionary[filename1], text2)
        self.assertEqual(len(self.dictionary), 1)

    def test__delete(self):
        filename1 = "first.txt"
        filename2 = "second.txt"
        text1 = "one"
        text2 = "two"

        self.assertEqual(self.dictionary.__setitem__(filename1, text1), None)
        self.assertEqual(self.dictionary.__setitem__(filename2, text2), None)
        
        self.assertEqual(len(self.dictionary), 2)
        self.assertEqual(self.dictionary.__delitem__(filename1), text1)
        self.assertEqual(len(self.dictionary), 1)
        with self.assertRaises(IndexError):
            self.dictionary[filename1]
        self.assertEqual(self.dictionary.__delitem__(filename2), text2)
        self.assertEqual(len(self.dictionary), 0)

    def test__loop(self):
        filenames = ["first.txt", "second.txt", "third.txt"]
        texts = ["one", "two", "three"]
        
        
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(self.dictionary.__setitem__(filenames[i], texts[i]), None)
        
        self.assertEqual(len(self.dictionary), 3)
        
        iterator = self.dictionary.__iter__()
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(self.dictionary[iterator.__next__()], texts[i])
        
        dict_items = self.dictionary.items()
        for i in range(3):
            with self.subTest(i=i):
                self.assertEqual(dict_items[i][0], filenames[i])
                self.assertEqual(dict_items[i][1], texts[i])
Beispiel #9
0
 def test_make_dict(self):
     with tempfile.TemporaryDirectory() as tmpdirname:
         d = DirDict(os.path.join(str(tmpdirname), 'dict1/storage'))
         d['lang'] = 'Python'
         self.assertEqual('Python', d['lang'])
Beispiel #10
0
 def test_values(self):
     with tempfile.TemporaryDirectory() as tmpdirname:
         d = DirDict(os.path.join(str(tmpdirname), 'dict4/storage'))
         d['lang'] = 'Python'
         d['lib'] = 'unittest'
         self.assertEqual(['Python', 'unittest'], d.values())
Beispiel #11
0
 def test_del(self):
     with tempfile.TemporaryDirectory() as tmpdirname:
         d = DirDict(os.path.join(str(tmpdirname), 'dict2/storage'))
         d['lang'] = 'Python'
         del d['lang']
         self.assertEqual(0, len(d))
Beispiel #12
0
 def setUp(self):
     self.dir = "./tmp"
     self.dir_dict = DirDict(self.dir)
Beispiel #13
0
class DirDictTests(TestCase):
    def setUp(self):
        self.dir = "./tmp"
        self.dir_dict = DirDict(self.dir)

    def tearDown(self):
        for file in os.listdir(path=self.dir):
            os.remove(os.path.join(self.dir, file))

    def get_path(self, path):
        return os.path.join(self.dir, path)

    def test_set_item(self):
        self.dir_dict['file'] = 'Python\n'
        with open(self.get_path('file'), 'r') as f:
            lines = reduce(lambda a, b: a + b, f.readlines())
        self.assertEqual(lines, 'Python\n')

    def test_set_item_advanced(self):
        self.dir_dict['file'] = None
        with open(self.get_path('file'), 'r') as f:
            lines = reduce(lambda a, b: a + b, f.readlines())
        self.assertEqual(lines, str(None))

    def get_item_base(self):
        self.dir_dict['file'] = 'hello\n'
        self.assertEqual('hello\n', self.dir_dict['file'])

    def get_item_advanced(self):
        self.dir_dict['file'] = 'hello\n'
        with open(self.get_path('file'), 'a') as f:
            f.write('qwe\n')
        self.assertEqual('hello\nqwe\n', self.dir_dict['file'])
        with self.asserRaises(KeyError):
            a = self.dir_dict['qwe']
    
    def test_contain(self):
        self.dir_dict['file'] = None
        self.assertTrue('file' in self.dir_dict)
        self.assertFalse('file1' in self.dir_dict)

    def test_del_item(self):
        self.dir_dict['file1'] = None
        self.dir_dict['file2'] = None
        del self.dir_dict['file2']
        self.assertFalse(os.path.exists(self.get_path('file2')))

    def test_del_item_advanced(self):
        self.dir_dict['file1'] = None
        self.dir_dict['file2'] = None
        os.remove(self.get_path('file2'))
        with self.assertRaises(KeyError):
            del self.dir_dict['file2']

    def test_len(self):
        self.dir_dict['file'] = "qweqw"
        self.dir_dict['qwq'] = 'alsd'
        self.dir_dict['wwww'] = 'czx'
        self.assertEqual(len(self.dir_dict), 3)
        del self.dir_dict['file']
        self.assertEqual(len(self.dir_dict), 2)
        os.remove(self.get_path('qwq'))
        self.assertEqual(len(self.dir_dict), 1)

    def test_clear(self):
        self.dir_dict['file1'] = None
        self.dir_dict['file2'] = None
        self.dir_dict['file3'] = 'qw'
        self.dir_dict.clear()
        self.assertEqual(len(self.dir_dict), 0)

    def test_keys_base(self):
        self.dir_dict['file1'] = 'a'
        self.dir_dict['file2'] = 'b'
        self.dir_dict['file3'] = 'c'
        self.assertEqual({'file1', 'file2', 'file3'}, set(self.dir_dict.keys()))

    def test_keys_advanced(self):
        with open(self.get_path('file1'), 'w') as f:
            f.write('a')
        with open(self.get_path('file2'), 'w') as f:
            f.write('b')
        with open(self.get_path('file3'), 'w') as f:
            f.write('c')
        self.assertEqual({'file1', 'file2', 'file3'}, set(self.dir_dict.keys()))

    def test_values_base(self):
        self.dir_dict['file1'] = 'a'
        self.dir_dict['file2'] = 'b'
        self.dir_dict['file3'] = 'c'
        self.assertEqual({'a', 'b', 'c'}, set(self.dir_dict.values()))

    def test_values_advanced(self):
        with open(self.get_path('file1'), 'w') as f:
            f.write('a')
        with open(self.get_path('file2'), 'w') as f:
            f.write('b')
        with open(self.get_path('file3'), 'w') as f:
            f.write('c')
        self.assertEqual({'a', 'b', 'c'}, set(self.dir_dict.values()))

    def test_items_base(self):
        self.dir_dict['file1'] = 'a'
        self.dir_dict['file2'] = 'b'
        self.dir_dict['file3'] = 'c'
        self.assertEqual({('file1', 'a'), ('file2', 'b'), ('file3', 'c')}, set(self.dir_dict.items()))

    def test_items_advanced(self):
        with open(self.get_path('file1'), 'w') as f:
            f.write('a')
        with open(self.get_path('file2'), 'w') as f:
            f.write('b')
        with open(self.get_path('file3'), 'w') as f:
            f.write('c')
        self.assertEqual({('file1', 'a'), ('file2', 'b'), ('file3', 'c')}, set(self.dir_dict.items()))

    def test_get(self):
        self.assertEqual(None, self.dir_dict.get('file1'))
        self.dir_dict['file1'] = 'qwe'
        self.assertEqual('qwe', self.dir_dict.get('file1'))

    def popTest(self):
        self.assertEqual(str(None), self.dir_dict.pop('file'))
        self.dir_dict['file'] = 'qwe'
        self.assertEqual('qwe', self.dir_dict.pop('file'))
        self.assertFalse('file' in self.dir_dict)

    def test_setdefault(self):
        self.assertEqual(None, self.dir_dict.setdefault('file'))
        self.dir_dict['file'] = 'qwe'
        self.assertEqual('qwe', self.dir_dict.pop('file'))
Beispiel #14
0
 def setUp(self):
     self.directory = 'd:\python\dirdict'
     self.file_names_list = ['first', 'second', 'third']
     self.dir_dict = DirDict(self.directory)