|
| 1 | +# (c) 2019 Open Source Geospatial Foundation - all rights reserved |
| 2 | +# (c) 2014 - 2015 Centre for Maritime Research and Experimentation (CMRE) |
| 3 | +# (c) 2013 - 2014 German Aerospace Center (DLR) |
| 4 | +# This code is licensed under the GPL 2.0 license, available at the root |
| 5 | +# application directory. |
| 6 | + |
| 7 | +import unittest |
| 8 | +import wpsremote.path as path |
| 9 | + |
| 10 | +__author__ = "Alessio Fabiani" |
| 11 | +__copyright__ = "Copyright 2019 Open Source Geospatial Foundation - all rights reserved" |
| 12 | +__license__ = "GPL" |
| 13 | + |
| 14 | + |
| 15 | +class TestPath(unittest.TestCase): |
| 16 | + |
| 17 | + LIST_DIRS = [ |
| 18 | + path.path('./src/wpsremote/xmpp_data/test/asset_schema.json'), |
| 19 | + path.path('./src/wpsremote/xmpp_data/test/test_dir'), |
| 20 | + path.path('./src/wpsremote/xmpp_data/test/wcsOAAonDemand_template.properties.txt'), |
| 21 | + path.path('./src/wpsremote/xmpp_data/test/service.config'), |
| 22 | + path.path('./src/wpsremote/xmpp_data/test/OAAonDemandWrapper_template.properties.txt'), |
| 23 | + path.path('./src/wpsremote/xmpp_data/test/test_file'), |
| 24 | + path.path('./src/wpsremote/xmpp_data/test/test_file_to_write'), |
| 25 | + path.path('./src/wpsremote/xmpp_data/test/test_service.config'), |
| 26 | + path.path('./src/wpsremote/xmpp_data/test/geoserverCommands_template.properties.txt'), |
| 27 | + path.path('./src/wpsremote/xmpp_data/test/oaaOnDemand_template.properties.txt'), |
| 28 | + path.path('./src/wpsremote/xmpp_data/test/test_remote.config'), |
| 29 | + path.path('./src/wpsremote/xmpp_data/test/CMREOAA_MainConfigFile_template.json'), |
| 30 | + path.path('./src/wpsremote/xmpp_data/test/logger_test.properties') |
| 31 | + ] |
| 32 | + DIRS = [path.path('./src/wpsremote/xmpp_data/test/test_dir')] |
| 33 | + |
| 34 | + def test_path_methods(self): |
| 35 | + p = path.path("test/path") |
| 36 | + self.assertEqual(p.__repr__(), "path('test/path')") |
| 37 | + self.assertEqual(p.__add__("/add"), "test/path/add") |
| 38 | + self.assertEqual(p.__radd__("radd/"), "radd/test/path") |
| 39 | + self.assertEqual(p.__div__("rel"), "test/path/rel") |
| 40 | + self.assertEqual(p.__truediv__("rel"), "test/path/rel") |
| 41 | + self.assertFalse(p.isabs()) |
| 42 | + self.assertEqual(p.basename(), "path") |
| 43 | + self.assertEqual(p.name, "path") |
| 44 | + self.assertEqual(p.normcase(), "test/path") |
| 45 | + self.assertEqual(p.normpath(), "test/path") |
| 46 | + self.assertEqual(p.expanduser(), "test/path") |
| 47 | + self.assertEqual(p.expandvars(), "test/path") |
| 48 | + self.assertEqual(p.dirname(), "test") |
| 49 | + self.assertEqual(p.parent, "test") |
| 50 | + self.assertEqual(p.expand(), "test/path") |
| 51 | + self.assertEqual(p._get_namebase(), "path") |
| 52 | + self.assertEqual(p.namebase, "path") |
| 53 | + self.assertEqual(p._get_ext(), "") |
| 54 | + self.assertEqual(p.ext, "") |
| 55 | + self.assertEqual(p._get_drive(), "") |
| 56 | + self.assertEqual(p.drive, "") |
| 57 | + self.assertEqual(p.splitpath(), (path.path('test'), 'path')) |
| 58 | + self.assertEqual(p.splitdrive(), (path.path(''), path.path('test/path'))) |
| 59 | + self.assertEqual(p.splitext(), (path.path('test/path'), '')) |
| 60 | + self.assertEqual(p.stripext(), "test/path") |
| 61 | + self.assertEqual(p.joinpath("join", "path"), "test/path/join/path") |
| 62 | + self.assertEqual(p.splitall(), [path.path(''), 'test', 'path']) |
| 63 | + self.assertEqual(p.relpath(), "test/path") |
| 64 | + self.assertEqual(p.relpathto("dest"), "../../dest") |
| 65 | + with self.assertRaises(OSError): |
| 66 | + p.listdir(pattern=None) |
| 67 | + existing_path = path.path("./src/wpsremote/xmpp_data/test") |
| 68 | + self.assertEqual(len(existing_path.listdir(pattern=None)), 12) |
| 69 | + for d in existing_path.listdir(pattern=None): |
| 70 | + self.assertIn(d, TestPath.LIST_DIRS) |
| 71 | + self.assertEqual(len(existing_path.dirs(pattern=None)), 1) |
| 72 | + for d in existing_path.dirs(pattern=None): |
| 73 | + self.assertIn(d, TestPath.DIRS) |
| 74 | + self.assertEqual(len(existing_path.files()), 11) |
| 75 | + for f in existing_path.files(): |
| 76 | + self.assertIn(f, TestPath.LIST_DIRS) |
| 77 | + self.assertTrue(existing_path.fnmatch("*test*")) |
| 78 | + self.assertEqual( |
| 79 | + existing_path.glob("*test_dir*"), |
| 80 | + [path.path('./src/wpsremote/xmpp_data/test/test_dir')] |
| 81 | + ) |
| 82 | + existing_file = path.path("./src/wpsremote/xmpp_data/test/test_file") |
| 83 | + self.assertIsInstance(existing_file.open(), file) |
| 84 | + self.assertEqual(existing_file.bytes(), "test content") |
| 85 | + existing_file.write_bytes("test file to write") |
| 86 | + self.assertEqual(existing_file.bytes(), "test file to write") |
| 87 | + self.assertEqual(existing_file.text(), "test file to write") |
| 88 | + existing_file.write_text("test content") |
| 89 | + self.assertEqual(existing_file.text(), "test content") |
| 90 | + existing_file.write_lines(["line 1", "line 2"]) |
| 91 | + self.assertEqual( |
| 92 | + existing_file.lines(), [ |
| 93 | + "line 1\n", |
| 94 | + "line 2\n" |
| 95 | + ] |
| 96 | + ) |
| 97 | + existing_file.write_text("test content") |
| 98 | + # Methods for querying the filesystem |
| 99 | + self.assertTrue(existing_file.exists()) |
| 100 | + self.assertFalse(path.path("./not/existing/path/test_file").exists()) |
| 101 | + self.assertTrue(existing_path.isdir()) |
| 102 | + self.assertTrue(existing_file.isfile()) |
| 103 | + self.assertTrue(path.path("/").ismount()) |
| 104 | + # mkdir |
| 105 | + new_dir = path.path('./src/wpsremote/xmpp_data/test/new_dir') |
| 106 | + new_dir.mkdir() |
| 107 | + self.assertTrue(new_dir.exists()) |
| 108 | + new_dirs = path.path('./src/wpsremote/xmpp_data/test/new_dirs/new_dir') |
| 109 | + new_dirs.makedirs() |
| 110 | + self.assertTrue(new_dirs.exists()) |
| 111 | + # rmdir |
| 112 | + new_dir.rmdir() |
| 113 | + self.assertFalse(new_dir.exists()) |
| 114 | + new_dirs.removedirs() |
| 115 | + self.assertFalse(new_dirs.exists()) |
| 116 | + # touch |
| 117 | + touch_file = path.path('./src/wpsremote/xmpp_data/test/new_file') |
| 118 | + self.assertFalse(touch_file.exists()) |
| 119 | + touch_file.touch() |
| 120 | + self.assertTrue(touch_file.exists()) |
| 121 | + # remove |
| 122 | + touch_file.remove() |
| 123 | + self.assertFalse(touch_file.exists()) |
| 124 | + # unlink |
| 125 | + touch_file.touch() |
| 126 | + self.assertTrue(touch_file.exists()) |
| 127 | + touch_file.unlink() |
| 128 | + self.assertFalse(touch_file.exists()) |
| 129 | + # copyfile |
| 130 | + existing_file.copy('./src/wpsremote/xmpp_data/test/copy_of_test_file') |
| 131 | + copied_file = path.path('./src/wpsremote/xmpp_data/test/copy_of_test_file') |
| 132 | + self.assertTrue(copied_file.exists()) |
| 133 | + copied_file.remove() |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == '__main__': |
| 137 | + unittest.main() |
0 commit comments