Tools used by OpenStack Documentation
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.

99 lines
4.0KB

  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import mock
  13. from os_doc_tools import jsoncheck
  14. import unittest
  15. class MockOpen(object):
  16. def read(self):
  17. return "raw"
  18. def write(self):
  19. return True
  20. class TestFileFunctions(unittest.TestCase):
  21. def test_indent_note(self):
  22. note = "Hello\nWorld"
  23. with mock.patch.object(jsoncheck.textwrap, 'fill') as mock_fill:
  24. mock_fill.return_value = "Hello World"
  25. jsoncheck._indent_note(note)
  26. mock_fill.assert_any_call('Hello', initial_indent=' ',
  27. subsequent_indent=' ',
  28. width=80)
  29. mock_fill.assert_any_call('World', initial_indent=' ',
  30. subsequent_indent=' ',
  31. width=80)
  32. def test_get_demjson_diagnostics(self):
  33. raw = "raw"
  34. with mock.patch.object(jsoncheck.demjson, 'decode', return_value=True):
  35. errstr = jsoncheck._get_demjson_diagnostics(raw)
  36. self.assertTrue(errstr is None)
  37. with mock.patch.object(jsoncheck.demjson, 'decode') as mock_decode:
  38. mock_decode.side_effect = jsoncheck.demjson.JSONError(raw)
  39. errstr = jsoncheck._get_demjson_diagnostics(raw)
  40. expected_error_str = " Error: raw"
  41. self.assertEqual(errstr, expected_error_str)
  42. def test_parse_json(self):
  43. raw = "raw"
  44. with mock.patch.object(jsoncheck.json, 'loads',
  45. return_value="Success"):
  46. parsed = jsoncheck._parse_json(raw)
  47. self.assertEqual(parsed, "Success")
  48. with mock.patch.object(jsoncheck.json, 'loads') as mock_loads:
  49. mock_loads.side_effect = ValueError()
  50. with self.assertRaises(jsoncheck.ParserException):
  51. parsed = jsoncheck._parse_json(raw)
  52. def test_format_parsed_json(self):
  53. with mock.patch.object(jsoncheck.json, 'dumps') as mock_dumps:
  54. mock_dumps.return_value = "Success"
  55. returned_value = jsoncheck._format_parsed_json('raw')
  56. self.assertEqual(returned_value, "Success\n")
  57. self.assertTrue(mock_dumps.called)
  58. def test_process_file(self):
  59. with mock.patch.object(jsoncheck, 'open', returned_value=MockOpen()):
  60. with mock.patch.object(jsoncheck, '_parse_json') as mock_parse:
  61. mock_parse.side_effect = jsoncheck.ParserException
  62. with self.assertRaises(ValueError):
  63. jsoncheck._process_file('path')
  64. with mock.patch.object(jsoncheck, 'open', returned_value=MockOpen()):
  65. with mock.patch.object(jsoncheck, '_parse_json',
  66. returned_value="Success"):
  67. with mock.patch.object(jsoncheck, '_format_parsed_json',
  68. returned_value="not_raw"):
  69. with self.assertRaises(ValueError):
  70. jsoncheck._process_file('path', 'check')
  71. with mock.patch.object(jsoncheck, 'open', returned_value=MockOpen()):
  72. with mock.patch.object(jsoncheck, '_parse_json',
  73. returned_value="Success"):
  74. with mock.patch.object(jsoncheck, '_format_parsed_json',
  75. returned_value="not_raw"):
  76. with self.assertRaises(ValueError):
  77. jsoncheck._process_file('path', 'formatting')
  78. if __name__ == '__main__':
  79. unittest.main()