Skip to content

Commit

Permalink
Remove dead and misplaced collection schema field validation
Browse files Browse the repository at this point in the history
This was validating stuff on the schema.  This stuff has already been validated on the schema before reaching this code block.
  • Loading branch information
AndrewSisley committed Oct 2, 2023
1 parent 0f37f7b commit 3d4b4a0
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 224 deletions.
21 changes: 1 addition & 20 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,7 @@ func (db *db) newCollection(desc client.CollectionDescription) (*collection, err
return nil, client.NewErrUninitializeProperty("Collection", "Name")
}

if len(desc.Schema.Fields) == 0 {
return nil, client.NewErrUninitializeProperty("Collection", "Fields")
}

docKeyField := desc.Schema.Fields[0]
if docKeyField.Kind != client.FieldKind_DocKey || docKeyField.Name != request.KeyFieldName {
return nil, ErrSchemaFirstFieldDocKey
}

for i, field := range desc.Schema.Fields {
if field.Name == "" {
return nil, client.NewErrUninitializeProperty("Collection.Schema", "Name")
}
if field.Kind == client.FieldKind_None {
return nil, client.NewErrUninitializeProperty("Collection.Schema", "FieldKind")
}
if (field.Kind != client.FieldKind_DocKey && !field.IsObject()) &&
field.Typ == client.NONE_CRDT {
return nil, client.NewErrUninitializeProperty("Collection.Schema", "CRDT type")
}
for i := range desc.Schema.Fields {
desc.Schema.Fields[i].ID = client.FieldID(i)
}

Expand Down
203 changes: 0 additions & 203 deletions db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ func newTestCollectionWithSchema(
return col, txn.Commit(ctx)
}

func TestNewCollection_ReturnsError_GivenNoSchema(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

_, err = db.createCollection(ctx, txn, client.CollectionDescription{
Name: "test",
})
assert.Error(t, err)
}

func TestNewCollectionWithSchema(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
Expand Down Expand Up @@ -111,196 +98,6 @@ func TestNewCollectionReturnsErrorGivenDuplicateSchema(t *testing.T) {
assert.Errorf(t, err, "collection already exists")
}

func TestNewCollectionReturnsErrorGivenNoFields(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(
t,
err,
"invalid state, required property is uninitialized. Host: Collection, PropertyName: Fields",
)
}

func TestNewCollectionReturnsErrorGivenNoName(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(
t,
err,
"invalid state, required property is uninitialized. Host: Collection, PropertyName: Name",
)
}

func TestNewCollectionReturnsErrorGivenNoKeyField(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{
{
Name: "Name",
Kind: client.FieldKind_STRING,
Typ: client.LWW_REGISTER,
},
},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(t, err, "collection schema first field must be a DocKey")
}

func TestNewCollectionReturnsErrorGivenKeyFieldIsNotFirstField(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{
{
Name: "Name",
Kind: client.FieldKind_STRING,
Typ: client.LWW_REGISTER,
},
{
Name: "_key",
Kind: client.FieldKind_DocKey,
},
},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(t, err, "collection schema first field must be a DocKey")
}

func TestNewCollectionReturnsErrorGivenFieldWithNoName(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{
{
Name: "_key",
Kind: client.FieldKind_DocKey,
},
{
Name: "",
Kind: client.FieldKind_STRING,
Typ: client.LWW_REGISTER,
},
},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(
t,
err,
"invalid state, required property is uninitialized. Host: Collection.Schema, PropertyName: Name",
)
}

func TestNewCollectionReturnsErrorGivenFieldWithNoKind(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{
{
Name: "_key",
Kind: client.FieldKind_DocKey,
},
{
Name: "Name",
Typ: client.LWW_REGISTER,
},
},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(
t,
err,
"invalid state, required property is uninitialized. Host: Collection.Schema, PropertyName: FieldKind",
)
}

func TestNewCollectionReturnsErrorGivenFieldWithNoType(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
assert.NoError(t, err)
txn, err := db.NewTxn(ctx, false)
require.NoError(t, err)

desc := client.CollectionDescription{
Name: "users",
Schema: client.SchemaDescription{
Fields: []client.FieldDescription{
{
Name: "_key",
Kind: client.FieldKind_DocKey,
},
{
Name: "Name",
Kind: client.FieldKind_STRING,
},
},
},
}

_, err = db.createCollection(ctx, txn, desc)
assert.EqualError(
t,
err,
"invalid state, required property is uninitialized. Host: Collection.Schema, PropertyName: CRDT type",
)
}

func TestGetCollectionByName(t *testing.T) {
ctx := context.Background()
db, err := newMemoryDB(ctx)
Expand Down
1 change: 0 additions & 1 deletion db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ var (
ErrDocumentDeleted = errors.New(errDocumentDeleted)
ErrUnknownCRDTArgument = errors.New("invalid CRDT arguments")
ErrUnknownCRDT = errors.New("unknown crdt")
ErrSchemaFirstFieldDocKey = errors.New("collection schema first field must be a DocKey")
ErrCollectionAlreadyExists = errors.New("collection already exists")
ErrCollectionNameEmpty = errors.New("collection name can't be empty")
ErrSchemaIDEmpty = errors.New("schema ID can't be empty")
Expand Down

0 comments on commit 3d4b4a0

Please sign in to comment.