Add test for missing fields property in schema change.

This commit is contained in:
Kishore Nallan 2022-04-30 19:19:25 +05:30
parent da672d3629
commit b6908f2f81
4 changed files with 10 additions and 21 deletions

View File

@ -152,8 +152,6 @@ public:
const std::vector<std::string>& symbols_to_index = {},
const std::vector<std::string>& token_separators = {});
Option<bool> update_collection(const std::string& name, nlohmann::json& req_json);
locked_resource_view_t<Collection> get_collection(const std::string & collection_name) const;
locked_resource_view_t<Collection> get_collection_with_id(uint32_t collection_id) const;

View File

@ -2716,7 +2716,7 @@ Option<bool> Collection::validate_alter_payload(nlohmann::json& schema_changes,
const std::string err_msg = "The `fields` value should be an array of objects containing "
"the field `name` and other properties.";
if(!schema_changes["fields"].is_array() || schema_changes["fields"].empty()) {
if(!schema_changes.contains("fields") || !schema_changes["fields"].is_array() || schema_changes["fields"].empty()) {
return Option<bool>(400, err_msg);
}

View File

@ -1244,21 +1244,3 @@ Option<bool> CollectionManager::delete_preset(const string& preset_name) {
preset_configs.erase(preset_name);
return Option<bool>(true);
}
Option<bool> CollectionManager::update_collection(const std::string& name, nlohmann::json& req_json) {
if(!req_json["fields"].is_array() || req_json["fields"].empty()) {
return Option<bool>(400, "The `fields` value should be an array of objects containing "
"the field properties.");
}
auto collection = get_collection(name);
if(collection == nullptr) {
return Option<bool>(404, "Not found.");
}
// Supported operations:
// - Adding a new field
// - Dropping a field
return collection->alter(req_json);
}

View File

@ -443,6 +443,15 @@ TEST_F(CollectionSchemaChangeTest, AlterValidations) {
ASSERT_EQ("Field `desc` has been declared in the schema, but is not found in the documents already present "
"in the collection. If you still want to add this field, set it as `optional: true`.", alter_op.error());
// 7. schema JSON missing "fields" property
schema_changes = R"({
"foo": "bar"
})"_json;
alter_op = coll1->alter(schema_changes);
ASSERT_FALSE(alter_op.ok());
ASSERT_EQ("The `fields` value should be an array of objects containing the field `name` "
"and other properties.", alter_op.error());
collectionManager.drop_collection("coll1");
}