Coverage for tests\test_reconcile_config.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-12-20 00:57 +0000

1import pytest 

2from datasette.utils.asgi import NotFound 

3 

4from datasette_reconcile.utils import ReconcileError, check_config 

5 

6 

7@pytest.mark.asyncio 

8async def test_plugin_configuration_missing(ds): 

9 with pytest.raises(NotFound, match="datasette-reconcile not configured .*"): 

10 await check_config({}, ds.get_database("test"), "dogs") 

11 

12 

13@pytest.mark.asyncio 

14async def test_plugin_configuration_no_name(ds): 

15 with pytest.raises(ReconcileError, match="Name field must be defined to activate reconciliation"): 

16 await check_config({"id_field": "id"}, ds.get_database("test"), "dogs") 

17 

18 

19@pytest.mark.asyncio 

20async def test_plugin_configuration_table_not_found(ds): 

21 with pytest.raises(NotFound, match="Table not found: test"): 

22 await check_config({"name_field": "name"}, ds.get_database("test"), "test") 

23 

24 

25@pytest.mark.asyncio 

26async def test_plugin_configuration_use_pk(ds): 

27 config = await check_config({"name_field": "name"}, ds.get_database("test"), "dogs") 

28 assert config["name_field"] == "name" 

29 assert config["id_field"] == "id" 

30 assert config["type_default"] == [ 

31 { 

32 "name": "Object", 

33 "id": "object", 

34 } 

35 ] 

36 assert "type_field" not in config 

37 

38 

39@pytest.mark.asyncio 

40async def test_plugin_configuration_max_limit(ds): 

41 with pytest.raises(TypeError, match="max_limit in reconciliation config must be an integer"): 

42 await check_config({"name_field": "name", "max_limit": "BLAH"}, ds.get_database("test"), "dogs") 

43 

44 

45@pytest.mark.asyncio 

46async def test_plugin_configuration_type_default(ds): 

47 with pytest.raises(ReconcileError, match="type_default should be a list of objects"): 

48 await check_config({"name_field": "name", "type_default": "BLAH"}, ds.get_database("test"), "dogs") 

49 with pytest.raises(ReconcileError, match="type_default values should be objects"): 

50 await check_config({"name_field": "name", "type_default": ["BLAH"]}, ds.get_database("test"), "dogs") 

51 with pytest.raises(ReconcileError, match="type_default 'id' values should be strings"): 

52 await check_config( 

53 {"name_field": "name", "type_default": [{"id": 1, "name": "test"}]}, ds.get_database("test"), "dogs" 

54 ) 

55 with pytest.raises(ReconcileError, match="type_default 'name' values should be strings"): 

56 await check_config( 

57 {"name_field": "name", "type_default": [{"name": 1, "id": "test"}]}, ds.get_database("test"), "dogs" 

58 ) 

59 

60 

61@pytest.mark.asyncio 

62async def test_plugin_configuration_use_id_field(ds): 

63 config = await check_config( 

64 { 

65 "name_field": "name", 

66 "id_field": "id", 

67 }, 

68 ds.get_database("test"), 

69 "dogs", 

70 ) 

71 assert config["name_field"] == "name" 

72 assert config["id_field"] == "id" 

73 assert config["type_default"] == [ 

74 { 

75 "name": "Object", 

76 "id": "object", 

77 } 

78 ] 

79 assert "type_field" not in config 

80 

81 

82@pytest.mark.asyncio 

83async def test_plugin_configuration_use_type_field(ds): 

84 config = await check_config( 

85 { 

86 "name_field": "name", 

87 "id_field": "id", 

88 "type_field": [ 

89 { 

90 "name": "Status", 

91 "id": "status", 

92 } 

93 ], 

94 }, 

95 ds.get_database("test"), 

96 "dogs", 

97 ) 

98 assert config["name_field"] == "name" 

99 assert config["id_field"] == "id" 

100 assert config["type_field"] == [ 

101 { 

102 "name": "Status", 

103 "id": "status", 

104 } 

105 ] 

106 assert "type_default" not in config 

107 

108 

109@pytest.mark.asyncio 

110async def test_plugin_configuration_use_type_default_incorrect(ds): 

111 with pytest.raises(ReconcileError, match="type_default should"): 

112 await check_config( 

113 { 

114 "name_field": "name", 

115 "id_field": "id", 

116 "type_default": "dog", 

117 }, 

118 ds.get_database("test"), 

119 "dogs", 

120 ) 

121 

122 

123@pytest.mark.asyncio 

124async def test_plugin_configuration_use_type_default(ds): 

125 config = await check_config( 

126 { 

127 "name_field": "name", 

128 "id_field": "id", 

129 "type_default": [ 

130 { 

131 "name": "Dog", 

132 "id": "dog", 

133 } 

134 ], 

135 }, 

136 ds.get_database("test"), 

137 "dogs", 

138 ) 

139 assert config["name_field"] == "name" 

140 assert config["id_field"] == "id" 

141 assert config["type_default"] == [ 

142 { 

143 "name": "Dog", 

144 "id": "dog", 

145 } 

146 ] 

147 assert "type_field" not in config 

148 

149 

150@pytest.mark.asyncio 

151async def test_plugin_configuration_use_fts_table(ds): 

152 config = await check_config( 

153 { 

154 "name_field": "name", 

155 "id_field": "id", 

156 "type_default": [ 

157 { 

158 "name": "Dog", 

159 "id": "dog", 

160 } 

161 ], 

162 }, 

163 ds.get_database("test"), 

164 "dogs", 

165 ) 

166 assert config["fts_table"] is None 

167 

168 

169@pytest.mark.asyncio 

170async def test_view_url_set(ds): 

171 config = await check_config( 

172 { 

173 "name_field": "name", 

174 "id_field": "id", 

175 "view_url": "https://example.com/{{id}}", 

176 }, 

177 ds.get_database("test"), 

178 "dogs", 

179 ) 

180 assert config["view_url"] == "https://example.com/{{id}}" 

181 

182 

183@pytest.mark.asyncio 

184async def test_view_url_no_id(ds): 

185 with pytest.raises(ReconcileError, match="View URL must contain {{id}}"): 

186 _ = await check_config( 

187 { 

188 "name_field": "name", 

189 "id_field": "id", 

190 "view_url": "https://example.com/", 

191 }, 

192 ds.get_database("test"), 

193 "dogs", 

194 )