diff options
author | 2013-07-01 22:14:03 +0200 | |
---|---|---|
committer | 2013-07-01 22:14:03 +0200 | |
commit | 6faf8d5ab5dc16f193acc1d344bf473e1eaa4808 (patch) | |
tree | f372b03cb4937457f8462b7b773ca464d5c809cb /tests | |
parent | executable and the main module added (diff) | |
download | g-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.tar.gz g-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.tar.bz2 g-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.zip |
g_sorcery/fileutils: file utilities moved to separate module
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_fileutils.py | 121 | ||||
-rw-r--r-- | tests/test_package_db.py | 100 |
2 files changed, 121 insertions, 100 deletions
diff --git a/tests/test_fileutils.py b/tests/test_fileutils.py new file mode 100644 index 0000000..1760444 --- /dev/null +++ b/tests/test_fileutils.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + fileutils.py + ~~~~~~~~~~~~ + + file utilities test suite + + :copyright: (c) 2013 by Jauhien Piatlicki + :license: GPL-2, see LICENSE for more details. +""" + +import json, os, shutil, tempfile, unittest + +from g_sorcery import exceptions, fileutils + + +class TestFileJSON(unittest.TestCase): + + def setUp(self): + self.tempdir = tempfile.TemporaryDirectory() + self.path = os.path.join(self.tempdir.name, 'tst') + self.name = 'tst.json' + + def tearDown(self): + del self.tempdir + + def do_test_read_ok(self, mandatories, value_suffix=""): + f = fileutils.FileJSON(self.path, self.name, mandatories) + content = f.read() + for key in mandatories: + self.assertTrue(key in content) + if value_suffix: + value = key + value_suffix + else: + value = "" + self.assertEqual(content[key], value) + self.assertTrue(os.path.isfile(os.path.join(self.path, self.name))) + with open(os.path.join(self.path, self.name), 'r') as f: + content_f = json.load(f) + self.assertEqual(content, content_f) + + def test_read_dir_does_not_exist(self): + mandatories = ['tst1', 'tst2', 'tst3'] + self.do_test_read_ok(mandatories) + + def test_read_file_does_not_exist(self): + os.makedirs(self.path) + mandatories = ['tst1', 'tst2', 'tst3'] + self.do_test_read_ok(mandatories) + + def test_read_all_keys(self): + os.makedirs(self.path) + mandatories = ['tst1', 'tst2', 'tst3'] + content = {} + for key in mandatories: + content[key] = key + "_v" + with open(os.path.join(self.path, self.name), 'w') as f: + json.dump(content, f) + self.do_test_read_ok(mandatories, "_v") + + def test_read_missing_keys(self): + os.makedirs(self.path) + mandatories = ['tst1', 'tst2', 'tst3'] + content = {} + for key in mandatories: + content[key] = key + "_v" + with open(os.path.join(self.path, self.name), 'w') as f: + json.dump(content, f) + f = fileutils.FileJSON(self.path, self.name, mandatories) + mandatories.append("tst4") + self.assertRaises(exceptions.FileJSONError, f.read) + + def do_test_write_ok(self): + mandatories = ['tst1', 'tst2', 'tst3'] + content = {} + for key in mandatories: + content[key] = key + '_v' + f = fileutils.FileJSON(self.path, self.name, mandatories) + f.write(content) + self.assertTrue(os.path.isfile(os.path.join(self.path, self.name))) + with open(os.path.join(self.path, self.name), 'r') as f: + content_f = json.load(f) + self.assertEqual(content, content_f) + + def test_write_missing_keys(self): + content = {'tst1' : '', 'tst2' : ''} + mandatories = ['tst1', 'tst2', 'tst3'] + f = fileutils.FileJSON(self.path, self.name, mandatories) + self.assertRaises(exceptions.FileJSONError, f.write, content) + + def test_write_dir_does_not_exist(self): + self.do_test_write_ok() + + def test_write_file_does_not_exist(self): + os.makedirs(self.path) + self.do_test_write_ok() + + def test_write_all_keys(self): + os.makedirs(self.path) + mandatories = ['tst11', 'tst12'] + content = {} + for key in mandatories: + content[key] = key + "_v" + with open(os.path.join(self.path, self.name), 'w') as f: + json.dump(content, f) + self.do_test_write_ok() + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(TestFileJSON('test_read_dir_does_not_exist')) + suite.addTest(TestFileJSON('test_read_file_does_not_exist')) + suite.addTest(TestFileJSON('test_read_all_keys')) + suite.addTest(TestFileJSON('test_read_missing_keys')) + suite.addTest(TestFileJSON('test_write_missing_keys')) + suite.addTest(TestFileJSON('test_write_dir_does_not_exist')) + suite.addTest(TestFileJSON('test_write_file_does_not_exist')) + suite.addTest(TestFileJSON('test_write_all_keys')) + return suite diff --git a/tests/test_package_db.py b/tests/test_package_db.py index c5d7074..2994157 100644 --- a/tests/test_package_db.py +++ b/tests/test_package_db.py @@ -30,98 +30,6 @@ class Server(threading.Thread): self.httpd.shutdown() -class TestFileJSON(unittest.TestCase): - - def setUp(self): - self.tempdir = tempfile.TemporaryDirectory() - self.path = os.path.join(self.tempdir.name, 'tst') - self.name = 'tst.json' - - def tearDown(self): - del self.tempdir - - def do_test_read_ok(self, mandatories, value_suffix=""): - f = package_db.FileJSON(self.path, self.name, mandatories) - content = f.read() - for key in mandatories: - self.assertTrue(key in content) - if value_suffix: - value = key + value_suffix - else: - value = "" - self.assertEqual(content[key], value) - self.assertTrue(os.path.isfile(os.path.join(self.path, self.name))) - with open(os.path.join(self.path, self.name), 'r') as f: - content_f = json.load(f) - self.assertEqual(content, content_f) - - def test_read_dir_does_not_exist(self): - mandatories = ['tst1', 'tst2', 'tst3'] - self.do_test_read_ok(mandatories) - - def test_read_file_does_not_exist(self): - os.makedirs(self.path) - mandatories = ['tst1', 'tst2', 'tst3'] - self.do_test_read_ok(mandatories) - - def test_read_all_keys(self): - os.makedirs(self.path) - mandatories = ['tst1', 'tst2', 'tst3'] - content = {} - for key in mandatories: - content[key] = key + "_v" - with open(os.path.join(self.path, self.name), 'w') as f: - json.dump(content, f) - self.do_test_read_ok(mandatories, "_v") - - def test_read_missing_keys(self): - os.makedirs(self.path) - mandatories = ['tst1', 'tst2', 'tst3'] - content = {} - for key in mandatories: - content[key] = key + "_v" - with open(os.path.join(self.path, self.name), 'w') as f: - json.dump(content, f) - f = package_db.FileJSON(self.path, self.name, mandatories) - mandatories.append("tst4") - self.assertRaises(exceptions.FileJSONError, f.read) - - def do_test_write_ok(self): - mandatories = ['tst1', 'tst2', 'tst3'] - content = {} - for key in mandatories: - content[key] = key + '_v' - f = package_db.FileJSON(self.path, self.name, mandatories) - f.write(content) - self.assertTrue(os.path.isfile(os.path.join(self.path, self.name))) - with open(os.path.join(self.path, self.name), 'r') as f: - content_f = json.load(f) - self.assertEqual(content, content_f) - - def test_write_missing_keys(self): - content = {'tst1' : '', 'tst2' : ''} - mandatories = ['tst1', 'tst2', 'tst3'] - f = package_db.FileJSON(self.path, self.name, mandatories) - self.assertRaises(exceptions.FileJSONError, f.write, content) - - def test_write_dir_does_not_exist(self): - self.do_test_write_ok() - - def test_write_file_does_not_exist(self): - os.makedirs(self.path) - self.do_test_write_ok() - - def test_write_all_keys(self): - os.makedirs(self.path) - mandatories = ['tst11', 'tst12'] - content = {} - for key in mandatories: - content[key] = key + "_v" - with open(os.path.join(self.path, self.name), 'w') as f: - json.dump(content, f) - self.do_test_write_ok() - - class DummyDB(package_db.PackageDB): def __init__(self, directory, packages): super().__init__(directory) @@ -227,14 +135,6 @@ class TestDummyDB(unittest.TestCase): def suite(): suite = unittest.TestSuite() - suite.addTest(TestFileJSON('test_read_dir_does_not_exist')) - suite.addTest(TestFileJSON('test_read_file_does_not_exist')) - suite.addTest(TestFileJSON('test_read_all_keys')) - suite.addTest(TestFileJSON('test_read_missing_keys')) - suite.addTest(TestFileJSON('test_write_missing_keys')) - suite.addTest(TestFileJSON('test_write_dir_does_not_exist')) - suite.addTest(TestFileJSON('test_write_file_does_not_exist')) - suite.addTest(TestFileJSON('test_write_all_keys')) suite.addTest(TestDummyDB('test_manifest')) suite.addTest(TestDummyDB('test_read')) suite.addTest(TestDummyDB('test_list_categories')) |