You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.0 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Utils
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. import os
  24. import shutil
  25. import tempfile
  26. from odoo.addons.muk_utils.tools import file
  27. from odoo.tests import common
  28. _logger = logging.getLogger(__name__)
  29. class FileTestCase(common.TransactionCase):
  30. def test_check_name(self):
  31. self.assertTrue(file.check_name("Test"))
  32. self.assertFalse(file.check_name("T/est"))
  33. def test_compute_name(self):
  34. self.assertEqual(file.compute_name("Test", 1, False), "Test(1)")
  35. self.assertEqual(file.compute_name("Test.png", 1, True), "Test(1).png")
  36. def test_unique_name(self):
  37. self.assertEqual(file.unique_name("Test", ["A", "B"]), "Test")
  38. self.assertEqual(file.unique_name("Test", ["Test"]), "Test(1)")
  39. def test_unique_files(self):
  40. files = file.unique_files(
  41. [("Test.png", b"\xff data"), ("Test.png", b"\xff data")]
  42. )
  43. self.assertEqual(
  44. files, [("Test.png", b"\xff data"), ("Test(1).png", b"\xff data")]
  45. )
  46. def test_guess_extension(self):
  47. self.assertEqual(file.guess_extension(filename="Test.png"), "png")
  48. self.assertEqual(file.guess_extension(mimetype="image/png"), "png")
  49. def test_ensure_path_directories(self):
  50. tmp_dir = tempfile.mkdtemp()
  51. try:
  52. path = os.path.join(tmp_dir, "Test/Test/")
  53. file.ensure_path_directories(path)
  54. self.assertTrue(os.path.exists(path))
  55. finally:
  56. shutil.rmtree(tmp_dir)
  57. return True
  58. def test_remove_empty_directories(self):
  59. tmp_dir = tempfile.mkdtemp()
  60. try:
  61. dir = os.path.join(tmp_dir, "Test/")
  62. path = os.path.join(dir, "Test/")
  63. os.makedirs(path)
  64. open(os.path.join(dir, "F"), "ab").close()
  65. file.remove_empty_directories(path)
  66. self.assertFalse(os.path.exists(path))
  67. self.assertTrue(os.path.exists(dir))
  68. finally:
  69. shutil.rmtree(tmp_dir)
  70. return True