From 18e6955bd21a97e57e57d775b9b32ba179408173 Mon Sep 17 00:00:00 2001 From: Stefano Scafiti Date: Sat, 20 Apr 2024 10:30:00 +0200 Subject: [PATCH] Add TestUniqueConstraintViolation --- embedded/document/engine_test.go | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/embedded/document/engine_test.go b/embedded/document/engine_test.go index 4be3987a28..1bce0a0f32 100644 --- a/embedded/document/engine_test.go +++ b/embedded/document/engine_test.go @@ -5,7 +5,7 @@ SPDX-License-Identifier: BUSL-1.1 you may not use this file except in compliance with the License. You may obtain a copy of the License at - https://mariadb.com/bsl11/ + https://mariadb.com/bsl11/ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -374,6 +374,41 @@ func TestListCollections(t *testing.T) { require.Equal(t, len(collections), len(collectionList)) } +func TestUniqueConstraintViolation(t *testing.T) { + engine := makeEngine(t) + + err := engine.CreateCollection( + context.Background(), + "admin", + "testCollection", + "", + []*protomodel.Field{ + {Name: "name", Type: protomodel.FieldType_STRING}, + {Name: "surname", Type: protomodel.FieldType_STRING}, + }, + []*protomodel.Index{ + {Fields: []string{"name"}, IsUnique: true}, + }, + ) + require.NoError(t, err) + + _, _, err = engine.InsertDocument(context.Background(), "admin", "testCollection", &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "name": structpb.NewStringValue("John"), + "surname": structpb.NewStringValue("Doe"), + }, + }) + require.NoError(t, err) + + _, _, err = engine.InsertDocument(context.Background(), "admin", "testCollection", &structpb.Struct{ + Fields: map[string]*structpb.Value{ + "name": structpb.NewStringValue("John"), + "surname": structpb.NewStringValue("Moore"), + }, + }) + require.ErrorIs(t, err, ErrConflict) +} + func TestGetDocument(t *testing.T) { ctx := context.Background() engine := makeEngine(t)