コード例 #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())
コード例 #2
0
ファイル: tests.py プロジェクト: Xenobyte42/py-hw5_1
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])
コード例 #3
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'))