|
157 | 157 | end
|
158 | 158 | end
|
159 | 159 |
|
| 160 | +################################################################################ |
| 161 | +## schema validation |
| 162 | +################################################################################ |
| 163 | + |
| 164 | + context "schema validation:" do |
| 165 | + before do |
| 166 | + @cn = "UnitTestsCollectionBasics" |
| 167 | + ArangoDB.drop_collection(@cn) |
| 168 | + @cid = ArangoDB.create_collection(@cn, false) |
| 169 | + end |
| 170 | + |
| 171 | + after do |
| 172 | + ArangoDB.drop_collection(@cn) |
| 173 | + end |
| 174 | + |
| 175 | + it "sets an invalid schema" do |
| 176 | + cmd = api + "/" + @cn + "/properties" |
| 177 | + body = "{ \"validation\": { \"rule\": \"peng!\", \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" |
| 178 | + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) |
| 179 | + |
| 180 | + doc.code.should eq(400) |
| 181 | + doc.parsed_response['error'].should eq(true) |
| 182 | + doc.parsed_response['code'].should eq(400) |
| 183 | + doc.parsed_response['errorNum'].should eq(1621) |
| 184 | + end |
| 185 | + |
| 186 | + it "sets a valid schema" do |
| 187 | + cmd = api + "/" + @cn + "/properties" |
| 188 | + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" |
| 189 | + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) |
| 190 | + doc = ArangoDB.log_get("#{prefix}-get-schema", cmd) |
| 191 | + |
| 192 | + doc.code.should eq(200) |
| 193 | + doc.headers['content-type'].should eq("application/json; charset=utf-8") |
| 194 | + doc.parsed_response['error'].should eq(false) |
| 195 | + doc.parsed_response['code'].should eq(200) |
| 196 | + doc.parsed_response['id'].should eq(@cid) |
| 197 | + doc.parsed_response['name'].should eq(@cn) |
| 198 | + doc.parsed_response['validation']['level'].should eq("strict") |
| 199 | + doc.parsed_response['validation']['message'].should eq("document has an invalid schema!") |
| 200 | + end |
| 201 | + |
| 202 | + it "stores valid documents" do |
| 203 | + cmd = api + "/" + @cn + "/properties" |
| 204 | + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" |
| 205 | + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) |
| 206 | + doc.code.should eq(200) |
| 207 | + |
| 208 | + body = "{ \"name\": { \"first\": \"test\", \"last\": \"test\" }, \"status\": \"active\" }" |
| 209 | + doc = ArangoDB.log_post("#{prefix}-schema-doc", "/_api/document/?collection=" + @cn, :body => body) |
| 210 | + doc.code.should eq(202) |
| 211 | + |
| 212 | + body = "{ \"name\": { \"first\": \"a\", \"last\": \"b\" }, \"status\": \"inactive\" }" |
| 213 | + doc = ArangoDB.log_post("#{prefix}-schema-doc", "/_api/document/?collection=" + @cn, :body => body) |
| 214 | + doc.code.should eq(202) |
| 215 | + end |
| 216 | + |
| 217 | + it "stores invalid documents" do |
| 218 | + cmd = api + "/" + @cn + "/properties" |
| 219 | + body = "{ \"validation\": { \"rule\": { \"properties\": { \"_key\": { \"type\": \"string\" }, \"_rev\": { \"type\": \"string\" }, \"_id\": { \"type\": \"string\" }, \"name\": { \"type\": \"object\", \"properties\": { \"first\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 }, \"last\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 50 } }, \"required\": [\"first\", \"last\"] }, \"status\": { \"enum\": [\"active\", \"inactive\", \"deleted\"] } }, \"additionalProperties\": false, \"required\": [\"name\", \"status\"] }, \"level\": \"strict\", \"message\": \"document has an invalid schema!\" } }" |
| 220 | + doc = ArangoDB.log_put("#{prefix}-schema", cmd, :body => body) |
| 221 | + |
| 222 | + body = "{ \"name\": { \"first\" : \"\", \"last\": \"test\" }, \"status\": \"active\" }" |
| 223 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 224 | + doc.code.should eq(400) |
| 225 | + doc.parsed_response['errorNum'].should eq(1620) |
| 226 | + |
| 227 | + body = "{ \"name\": { \"first\" : \"\", \"last\": \"\" }, \"status\": \"active\" }" |
| 228 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 229 | + doc.code.should eq(400) |
| 230 | + doc.parsed_response['errorNum'].should eq(1620) |
| 231 | + |
| 232 | + body = "{ \"name\": { \"first\" : \"test\", \"last\": \"test\" } }" |
| 233 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 234 | + doc.code.should eq(400) |
| 235 | + doc.parsed_response['errorNum'].should eq(1620) |
| 236 | + |
| 237 | + body = "{ \"name\": { \"first\" : \"test\", \"last\": \"test\" }, \"status\": \"foo\" }" |
| 238 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 239 | + doc.code.should eq(400) |
| 240 | + doc.parsed_response['errorNum'].should eq(1620) |
| 241 | + |
| 242 | + body = "{ \"name\": { }, \"status\": \"active\" }" |
| 243 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 244 | + doc.code.should eq(400) |
| 245 | + doc.parsed_response['errorNum'].should eq(1620) |
| 246 | + |
| 247 | + body = "{ \"first\": \"abc\", \"last\": \"test\", \"status\": \"active\" }" |
| 248 | + doc = ArangoDB.log_post("#{prefix}-schema-doc-invalid", "/_api/document/?collection=" + @cn, :body => body) |
| 249 | + doc.code.should eq(400) |
| 250 | + doc.parsed_response['errorNum'].should eq(1620) |
| 251 | + end |
| 252 | + end |
| 253 | + |
160 | 254 | ################################################################################
|
161 | 255 | ## reading a collection
|
162 | 256 | ################################################################################
|
|
0 commit comments