Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import json 

2import os 

3import sys 

4 

5import pytest 

6 

7sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../../") 

8from sdc_etl_libs.sdc_data_schema.schema_exceptions import (DataSchemaCriticalError, DataSchemaFailedValidation) 

9from sdc_etl_libs.sdc_data_schema.schema_validation import (ResultTypeStatuses, SchemaValidation) 

10 

11 

12def test_validation__key_optional_and_missing(): 

13 """ 

14 Ensure that an optional key that is missing is handled appropriately. 

15 #################################################################################################### 

16 # key_dv_1 = {"required": False, "data_type": str} # 

17 # key_dv_2 = {"required": False, "data_type": str, "default_value": "this"} # 

18 # key_dv_3 = {"required": False, "optional": ["test|test"], "data_type": str} # 

19 # key_dv_4 = {"required": False, "optional": ["test|test"], "data_type": int, "default_value": 10} # 

20 #################################################################################################### 

21 """ 

22 

23 ep_schema_raw = """ 

24 { 

25 "type": "test", 

26 "tag": "main_source", 

27 "info": { 

28 "type": "test", 

29 "access": { 

30 "type": "test_key_default_values", 

31 "test_details": { 

32 "key_dv_5": "value" 

33 } 

34 }, 

35 "opts": { 

36 } 

37 } 

38 } 

39 """ 

40 

41 ep_schema = json.loads(ep_schema_raw) 

42 

43 validation = SchemaValidation() 

44 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

45 

46 # Test that the missing "key_dv_1" did not add an UPDATE to the record, or, be added to the returned schema since 

47 # the key is optional without a default value set. 

48 assert { 

49 'status': 'UPDATE', 

50 'reason': 'DEFAULT_SET', 

51 'section': 'endpoint:info:access:test_details', 

52 'note': '"key_dv_1" was missing, but a default value was set. Added key with value "this".' 

53 } not in validation.results 

54 assert "key_dv_1" not in schema["info"]["access"]["test_details"] 

55 

56 # Test that the missing "key_dv_2" did add an UPDATE to the record and that it now exists in the returned schema 

57 # since it was optional and had a default value set. 

58 assert { 

59 'status': 'UPDATE', 

60 'reason': 'DEFAULT_SET', 

61 'section': 'endpoint:info:access:test_details', 

62 'note': '"key_dv_2" was missing, but a default value was set. Added key with value "this".' 

63 } in validation.results 

64 assert "key_dv_2" in schema["info"]["access"]["test_details"] 

65 assert schema["info"]["access"]["test_details"]["key_dv_2"] == "this" 

66 

67 # Test that the missing "key_dv_3" did not add an UPDATE to the record, or, be added to the returned schema since 

68 # the key is optional without a default value set. 

69 assert { 

70 'status': 'UPDATE', 

71 'reason': 'DEFAULT_SET', 

72 'section': 'endpoint:info:access:test_details', 

73 'note': '"key_dv_3" was missing, but a default value was set. Added key with value "this".' 

74 } not in validation.results 

75 assert "key_dv_3" not in schema["info"]["access"]["test_details"] 

76 

77 # Test that the missing "key_dv_4" did add an UPDATE to the record and that it now exists in the returned schema 

78 # since it was optional and had a default value set. 

79 assert { 

80 'status': 'UPDATE', 

81 'reason': 'DEFAULT_SET', 

82 'section': 'endpoint:info:access:test_details', 

83 'note': '"key_dv_4" was missing, but a default value was set. Added key with value "10".' 

84 } in validation.results 

85 assert "key_dv_4" in schema["info"]["access"]["test_details"] 

86 assert schema["info"]["access"]["test_details"]["key_dv_4"] == 10 

87 

88 

89def test_validation__default_value_does_not_violate_data_type(): 

90 """ 

91 Ensure if a default value is set and used, that it does not violate the data type. Should raise a DataSchemaCriticalError. 

92 #################################################################################################### 

93 # key_dv_5 = {"required": False, "data_type": str, "default_value": 2039} # 

94 #################################################################################################### 

95 """ 

96 

97 ep_schema_raw = """ 

98 { 

99 "type": "test", 

100 "tag": "main_source", 

101 "info": { 

102 "type": "test", 

103 "access": { 

104 "type": "test_key_default_values", 

105 "test_details": { 

106 "key_dv_1": "value" 

107 } 

108 }, 

109 "opts": { 

110 } 

111 } 

112 } 

113 """ 

114 

115 ep_schema = json.loads(ep_schema_raw) 

116 validation = SchemaValidation() 

117 

118 with pytest.raises(DataSchemaCriticalError): 

119 schema = validation.validate_schema(ep_schema, validation_type_="endpoint")