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.

80 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. import logging
  22. import os
  23. import shutil
  24. import tempfile
  25. from odoo.addons.muk_utils.tools import file
  26. from odoo.tests import common
  27. _logger = logging.getLogger(__name__)
  28. class FileTestCase(common.TransactionCase):
  29. def test_check_name(self):
  30. self.assertTrue(file.check_name("Test"))
  31. self.assertFalse(file.check_name("T/est"))
  32. def test_compute_name(self):
  33. self.assertEqual(file.compute_name("Test", 1, False), "Test(1)")
  34. self.assertEqual(file.compute_name("Test.png", 1, True), "Test(1).png")
  35. def test_unique_name(self):
  36. self.assertEqual(file.unique_name("Test", ["A", "B"]), "Test")
  37. self.assertEqual(file.unique_name("Test", ["Test"]), "Test(1)")
  38. def test_unique_files(self):
  39. files = file.unique_files(
  40. [("Test.png", b"\xff data"), ("Test.png", b"\xff data")]
  41. )
  42. self.assertEqual(
  43. files, [("Test.png", b"\xff data"), ("Test(1).png", b"\xff data")]
  44. )
  45. def test_guess_extension(self):
  46. self.assertEqual(file.guess_extension(filename="Test.png"), "png")
  47. self.assertEqual(file.guess_extension(mimetype="image/png"), "png")
  48. def test_ensure_path_directories(self):
  49. tmp_dir = tempfile.mkdtemp()
  50. try:
  51. path = os.path.join(tmp_dir, "Test/Test/")
  52. file.ensure_path_directories(path)
  53. self.assertTrue(os.path.exists(path))
  54. finally:
  55. shutil.rmtree(tmp_dir)
  56. return True
  57. def test_remove_empty_directories(self):
  58. tmp_dir = tempfile.mkdtemp()
  59. try:
  60. dir = os.path.join(tmp_dir, "Test/")
  61. path = os.path.join(dir, "Test/")
  62. os.makedirs(path)
  63. open(os.path.join(dir, "F"), "ab").close()
  64. file.remove_empty_directories(path)
  65. self.assertFalse(os.path.exists(path))
  66. self.assertTrue(os.path.exists(dir))
  67. finally:
  68. shutil.rmtree(tmp_dir)
  69. return True