test_content_parser.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. import pytest
  18. from elyra.contents.parser import ContentParser
  19. def parse(filepath):
  20. absolute_filepath = os.path.join(os.path.dirname(__file__), filepath)
  21. parser = ContentParser()
  22. return parser.parse(absolute_filepath)
  23. def _get_variable_names(properties):
  24. return list(properties["env_vars"].keys())
  25. def test_python_notebook():
  26. expected_variable_names = ["VAR1", "VAR2", "VAR3", "VAR4", "VAR5", "VAR6", "VAR7", "VAR8"]
  27. properties = parse("resources/parse_python.ipynb")
  28. variable_names = _get_variable_names(properties)
  29. assert variable_names == expected_variable_names
  30. def test_r_notebook():
  31. expected_variable_names = ["VAR1", "VAR2", "VAR3", "VAR4", "VAR5", "VAR6"]
  32. properties = parse("resources/parse_r.ipynb")
  33. variable_names = _get_variable_names(properties)
  34. assert variable_names == expected_variable_names
  35. def test_python_script():
  36. expected_variable_names = ["VAR1", "VAR2", "VAR3", "VAR4", "VAR5", "VAR6", "VAR7", "VAR8"]
  37. properties = parse("resources/parse.py")
  38. variable_names = _get_variable_names(properties)
  39. assert variable_names == expected_variable_names
  40. def test_r_script():
  41. expected_variable_names = ["VAR1", "VAR2", "VAR3", "VAR4", "VAR5", "VAR6"]
  42. properties = parse("resources/parse.r")
  43. variable_names = _get_variable_names(properties)
  44. assert variable_names == expected_variable_names
  45. def test_empty_python_notebook():
  46. expected_variable_names = []
  47. properties = parse("resources/parse_python_empty.ipynb")
  48. variable_names = _get_variable_names(properties)
  49. assert variable_names == expected_variable_names
  50. def test_empty_r_notebook():
  51. expected_variable_names = []
  52. properties = parse("resources/parse_r_empty.ipynb")
  53. variable_names = _get_variable_names(properties)
  54. assert variable_names == expected_variable_names
  55. def test_empty_python_script():
  56. expected_variable_names = []
  57. properties = parse("resources/parse_empty.py")
  58. variable_names = _get_variable_names(properties)
  59. assert variable_names == expected_variable_names
  60. def test_empty_r_script():
  61. expected_variable_names = []
  62. properties = parse("resources/parse_empty.r")
  63. variable_names = _get_variable_names(properties)
  64. assert variable_names == expected_variable_names
  65. def test_file_not_found():
  66. with pytest.raises(FileNotFoundError) as e:
  67. parse("resources/none.py")
  68. assert "No such file or directory" in str(e.value)
  69. def test_file_is_not_directory():
  70. directory = "dir.py"
  71. dir_path = os.path.join(os.path.dirname(__file__), directory)
  72. os.mkdir(dir_path)
  73. with pytest.raises(IsADirectoryError) as e:
  74. parse(dir_path)
  75. assert "Is a directory" in str(e.value)
  76. os.rmdir(dir_path)
  77. def test_no_kernel():
  78. expected_variable_names = []
  79. properties = parse("resources/parse_no_kernel.ipynb")
  80. variable_names = _get_variable_names(properties)
  81. assert variable_names == expected_variable_names
  82. def test_parser_not_set():
  83. expected_variable_names = []
  84. properties = parse("resources/parse_no_language.ipynb")
  85. variable_names = _get_variable_names(properties)
  86. assert variable_names == expected_variable_names
  87. def test_unsupported_file_type():
  88. with pytest.raises(ValueError) as e:
  89. parse("resources/parse.txt")
  90. assert "File type" and "is not supported" in str(e.value)