From: Christian Herdtweck Date: Tue, 12 Jan 2016 12:27:18 +0000 (+0100) Subject: created unittest for file_helpers X-Git-Tag: v1.2~81 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=d4b393e77274ceee40bdcd3ba9c05ed5af199dec;p=pyi2ncommon created unittest for file_helpers --- diff --git a/test/file_helper_unittest.py b/test/file_helper_unittest.py new file mode 100644 index 0000000..db672f0 --- /dev/null +++ b/test/file_helper_unittest.py @@ -0,0 +1,73 @@ +""" file_helper_unittest.py: unit tests for file_helpers + +Tests classes and functions in file_helpers + +Should be able to run from python2 and python3! + +For help see :py:mod:`unittest` + +.. codeauthor:: Christian Herdtweck, christian.herdtweck@intra2net.com +""" + +import unittest + +import file_helpers + +from call_helpers import call_and_capture +import os + + +class FileHelperTester(unittest.TestCase): + + def test_cd(self): + """ tests the cd context manager """ + + test_dir = '/' + + start_cwd = os.getcwd() + + with file_helpers.cd(test_dir): + self.assertEqual(os.getcwd(), test_dir) + self.assertEqual(os.getcwd(), start_cwd) + + + def test_disc_stats(self): + """ tests get_disc_stats """ + + stats = file_helpers.get_disc_stats() + + # check number + code, out, err = call_and_capture(file_helpers.DF_CMD) + self.assertEqual(code, 0) + self.assertEqual(len(err), 0) + self.assertEqual(len(out)-1, len(stats)) + + for stat in stats: + # do numbers make sense? + self.assertGreaterEqual(stat.size, 0) + self.assertGreaterEqual(stat.used, 0) + self.assertLessEqual(stat.used, stat.size) + self.assertGreaterEqual(stat.available, 0) + self.assertLessEqual(stat.available, stat.size) + self.assertGreaterEqual(stat.capacity, 0) + self.assertLessEqual(stat.capacity, 100) + + # are strings non-empty + self.assertGreater(len(stat.name), 0) + self.assertGreater(len(stat.mount_point), 0) + + # does match capacity? + capacity = 100. * stat.used / stat.size + self.assertLess(abs(capacity - stat.capacity), 5., + 'capacity deviates from used/size by >5%!') + + # is size approx equal to used + available? + size = stat.used + stat.available + self.assertLess(float(abs(stat.size - size)) + / float(max(stat.size, size)), + 0.10, + 'size deviates from used+free by more than 10%!') + + +if __name__ == '__main__': + unittest.main()