test_config_schema.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import json
  12. import pytest
  13. from jsonschema import ValidationError, validate
  14. with open("taipy/core/config/config.schema.json", "r") as f:
  15. json_schema = json.load(f)
  16. def test_validate_generic_datanode_config():
  17. generic_cfg_without_read_write_fct = {"DATA_NODE": {"properties": {"storage_type": "generic"}}}
  18. with pytest.raises(ValidationError):
  19. validate(generic_cfg_without_read_write_fct, json_schema)
  20. generic_cfg_without_write_fct = {
  21. "DATA_NODE": {"properties": {"storage_type": "generic", "read_fct": "module.read_fct"}}
  22. }
  23. with pytest.raises(ValidationError):
  24. validate(generic_cfg_without_write_fct, json_schema)
  25. generic_cfg_without_read_fct = {
  26. "DATA_NODE": {"properties": {"storage_type": "generic", "write_fct": "module.write_fct"}}
  27. }
  28. with pytest.raises(ValidationError):
  29. validate(generic_cfg_without_read_fct, json_schema)
  30. generic_cfg_with_read_write_fct = {
  31. "DATA_NODE": {
  32. "properties": {"storage_type": "generic", "read_fct": "module.read_fct", "write_fct": "module.write_fct"}
  33. }
  34. }
  35. validate(generic_cfg_with_read_write_fct, json_schema)
  36. def test_validate_sql_datanode_config():
  37. sql_cfg_sqlite_without_required_properties = {
  38. "DATA_NODE": {
  39. "properties": {
  40. "storage_type": "sql",
  41. "db_engine": "sqlite",
  42. }
  43. }
  44. }
  45. with pytest.raises(ValidationError):
  46. validate(sql_cfg_sqlite_without_required_properties, json_schema)
  47. sql_cfg_sqlite = {
  48. "DATA_NODE": {
  49. "properties": {
  50. "storage_type": "sql",
  51. "db_engine": "sqlite",
  52. "db_name": "name",
  53. "read_query": "SELECT * FROM table",
  54. "write_query_builder": "module.write_query_builder",
  55. }
  56. }
  57. }
  58. validate(sql_cfg_sqlite, json_schema)
  59. sql_cfg_notsqlite_without_required_properties = {
  60. "DATA_NODE": {
  61. "properties": {
  62. "storage_type": "sql",
  63. "db_engine": "mysql",
  64. }
  65. }
  66. }
  67. with pytest.raises(ValidationError):
  68. validate(sql_cfg_notsqlite_without_required_properties, json_schema)
  69. sql_cfg_notsqlite_without_username_password = {
  70. "DATA_NODE": {
  71. "properties": {
  72. "storage_type": "sql",
  73. "db_engine": "mysql",
  74. "db_name": "name",
  75. "read_query": "SELECT * FROM table",
  76. "write_query_builder": "module.write_query_builder",
  77. }
  78. }
  79. }
  80. with pytest.raises(ValidationError):
  81. validate(sql_cfg_notsqlite_without_username_password, json_schema)
  82. sql_cfg_notsqlite = {
  83. "DATA_NODE": {
  84. "properties": {
  85. "storage_type": "sql",
  86. "db_engine": "mysql",
  87. "db_name": "name",
  88. "db_username": "user",
  89. "db_password": "pass",
  90. "read_query": "SELECT * FROM table",
  91. "write_query_builder": "module.write_query_builder",
  92. }
  93. }
  94. }
  95. validate(sql_cfg_notsqlite, json_schema)
  96. def test_validate_sql_table_datanode_config():
  97. sql_table_cfg_sqlite_without_required_properties = {
  98. "DATA_NODE": {
  99. "properties": {
  100. "storage_type": "sql_table",
  101. "db_engine": "sqlite",
  102. }
  103. }
  104. }
  105. with pytest.raises(ValidationError):
  106. validate(sql_table_cfg_sqlite_without_required_properties, json_schema)
  107. sql_table_cfg_sqlite = {
  108. "DATA_NODE": {
  109. "properties": {
  110. "storage_type": "sql_table",
  111. "db_engine": "sqlite",
  112. "db_name": "name",
  113. "table_name": "table",
  114. }
  115. }
  116. }
  117. validate(sql_table_cfg_sqlite, json_schema)
  118. sql_table_cfg_notsqlite_without_required_properties = {
  119. "DATA_NODE": {
  120. "properties": {
  121. "storage_type": "sql_table",
  122. "db_engine": "mysql",
  123. }
  124. }
  125. }
  126. with pytest.raises(ValidationError):
  127. validate(sql_table_cfg_notsqlite_without_required_properties, json_schema)
  128. sql_table_cfg_notsqlite_without_username_password = {
  129. "DATA_NODE": {
  130. "properties": {
  131. "storage_type": "sql_table",
  132. "db_engine": "mysql",
  133. "db_name": "name",
  134. "table_name": "table",
  135. }
  136. }
  137. }
  138. with pytest.raises(ValidationError):
  139. validate(sql_table_cfg_notsqlite_without_username_password, json_schema)
  140. sql_table_cfg_notsqlite = {
  141. "DATA_NODE": {
  142. "properties": {
  143. "storage_type": "sql_table",
  144. "db_engine": "mysql",
  145. "db_name": "name",
  146. "db_username": "user",
  147. "db_password": "pass",
  148. "table_name": "table",
  149. }
  150. }
  151. }
  152. validate(sql_table_cfg_notsqlite, json_schema)
  153. def test_validate_mongo_collection_datanode_config():
  154. mongo_collection_cfg_without_required_properties = {
  155. "DATA_NODE": {
  156. "properties": {
  157. "storage_type": "mongo_collection",
  158. }
  159. }
  160. }
  161. with pytest.raises(ValidationError):
  162. validate(mongo_collection_cfg_without_required_properties, json_schema)
  163. mongo_collection_cfg = {
  164. "DATA_NODE": {
  165. "properties": {
  166. "storage_type": "mongo_collection",
  167. "db_name": "name",
  168. "collection_name": "collection",
  169. }
  170. }
  171. }
  172. validate(mongo_collection_cfg, json_schema)
  173. def test_validate_s3_object_datanode_config():
  174. s3_object_cfg_without_required_properties = {
  175. "DATA_NODE": {
  176. "properties": {
  177. "storage_type": "s3_object",
  178. }
  179. }
  180. }
  181. with pytest.raises(ValidationError):
  182. validate(s3_object_cfg_without_required_properties, json_schema)
  183. s3_object_cfg = {
  184. "DATA_NODE": {
  185. "properties": {
  186. "storage_type": "s3_object",
  187. "aws_access_key": "access_key",
  188. "aws_secret_access_key": "secret_access_key",
  189. "aws_s3_bucket_name": "bucket",
  190. "aws_s3_object_key": "object_key",
  191. }
  192. }
  193. }
  194. validate(s3_object_cfg, json_schema)