This class updates the database to have the necessary tables etc
Example #1
0
        public void CanCreate_should_throw_exception_if_nested_field_properties_null()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <UpsertSchemaField>
                {
                    new UpsertSchemaField
                    {
                        Name         = "array",
                        Properties   = new ArrayFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key,
                        Nested       = new List <UpsertSchemaNestedField>
                        {
                            new UpsertSchemaNestedField
                            {
                                Name       = "nested1",
                                Properties = null
                            }
                        }
                    }
                },
                Name = "new-schema"
            };

            ValidationAssert.Throws(() => GuardSchema.CanCreate(command),
                                    new ValidationError("Field properties is required.",
                                                        "Fields[1].Nested[1].Properties"));
        }
Example #2
0
        public void CanCreate_should_throw_exception_if_ui_field_is_invalid()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <UpsertSchemaField>
                {
                    new UpsertSchemaField
                    {
                        Name       = "field1",
                        Properties = new UIFieldProperties
                        {
                            IsListField      = true,
                            IsReferenceField = true
                        },
                        IsHidden     = true,
                        IsDisabled   = true,
                        Partitioning = Partitioning.Invariant.Key
                    }
                },
                Name = "new-schema"
            };

            ValidationAssert.Throws(() => GuardSchema.CanCreate(command),
                                    new ValidationError("UI field cannot be a list field.",
                                                        "Fields[1].Properties.IsListField"),
                                    new ValidationError("UI field cannot be a reference field.",
                                                        "Fields[1].Properties.IsReferenceField"),
                                    new ValidationError("UI field cannot be hidden.",
                                                        "Fields[1].IsHidden"),
                                    new ValidationError("UI field cannot be disabled.",
                                                        "Fields[1].IsDisabled"));
        }
Example #3
0
        private async Task <string?> CheckSchemaAsync(ISchemasCacheGrain cache, CreateSchema command)
        {
            var token = await cache.ReserveAsync(command.SchemaId, command.Name);

            if (token == null)
            {
                throw new ValidationException(T.Get("schemas.nameAlreadyExists"));
            }

            try
            {
                var existingId = await GetSchemaIdAsync(command.AppId.Id, command.Name);

                if (existingId != default)
                {
                    throw new ValidationException(T.Get("schemas.nameAlreadyExists"));
                }
            }
            catch
            {
                // Catch our own exception, just in case something went wrong before.
                await cache.RemoveReservationAsync(token);

                throw;
            }

            return(token);
        }
Example #4
0
        public Task CanCreate_should_throw_exception_if_fields_not_valid()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = null,
                        Properties   = null,
                        Partitioning = "invalid",
                    },
                    new CreateSchemaField
                    {
                        Name         = null,
                        Properties   = InvalidProperties(),
                        Partitioning = "invalid",
                    }
                },
                Name = "new-schema"
            };

            return(Assert.ThrowsAsync <ValidationException>(() => GuardSchema.CanCreate(command, schemas)));
        }
Example #5
0
        public static CreateSchema ToCommand(this CreateSchemaDto dto)
        {
            var command = new CreateSchema();

            SimpleMapper.Map(dto, command);

            if (dto.Properties != null)
            {
                command.Properties = new SchemaProperties();

                SimpleMapper.Map(dto.Properties, command.Properties);
            }

            if (dto.Fields != null)
            {
                command.Fields = new List <CreateSchemaField>();

                foreach (var fieldDto in dto.Fields)
                {
                    var fieldProperties = fieldDto?.Properties.ToProperties();
                    var fieldInstance   = SimpleMapper.Map(fieldDto, new CreateSchemaField {
                        Properties = fieldProperties
                    });

                    command.Fields.Add(fieldInstance);
                }
            }

            return(command);
        }
Example #6
0
        public Task CanCreate_should_throw_exception_if_nested_field_properties_not_valid()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = "array",
                        Properties   = new ArrayFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key,
                        Nested       = new List <CreateSchemaNestedField>
                        {
                            new CreateSchemaNestedField
                            {
                                Name       = "nested1",
                                Properties = new StringFieldProperties {
                                    MinLength = 10, MaxLength = 5
                                }
                            }
                        }
                    }
                },
                Name = "new-schema"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("Max length must be greater than min length.",
                                                                    "Fields[1].Nested[1].Properties.MinLength",
                                                                    "Fields[1].Nested[1].Properties.MaxLength")));
        }
Example #7
0
        public Task CanCreate_should_throw_exception_if_nested_field_is_array()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = "array",
                        Properties   = new ArrayFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key,
                        Nested       = new List <CreateSchemaNestedField>
                        {
                            new CreateSchemaNestedField
                            {
                                Name       = "nested1",
                                Properties = new ArrayFieldProperties()
                            }
                        }
                    }
                },
                Name = "new-schema"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("Nested field cannot be array fields.",
                                                                    "Fields[1].Nested[1].Properties")));
        }
        public async Task Create_should_create_schema_with_initial_fields()
        {
            var properties = new SchemaProperties();

            var fields = new List <CreateSchemaField>
            {
                new CreateSchemaField {
                    Name = "field1", Properties = ValidProperties()
                },
                new CreateSchemaField {
                    Name = "field2", Properties = ValidProperties()
                }
            };

            var command = new CreateSchema {
                Name = SchemaName, SchemaId = SchemaId, Properties = properties, Fields = fields
            };

            var result = await sut.ExecuteAsync(CreateCommand(command));

            result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 0));

            var @event = (SchemaCreated)LastEvents.Single().Payload;

            Assert.Equal(AppId, sut.Snapshot.AppId.Id);
            Assert.Equal(SchemaName, sut.Snapshot.Name);
            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);

            Assert.Equal(2, @event.Fields.Count);
        }
        public async Task Create_should_create_events_and_set_intitial_state()
        {
            var properties = new SchemaProperties();

            var command = new CreateSchema {
                Name = SchemaName, SchemaId = SchemaId, Properties = properties, IsSingleton = true
            };

            var result = await PublishAsync(command);

            result.ShouldBeEquivalent(sut.Snapshot);

            Assert.Equal(AppId, sut.Snapshot.AppId.Id);

            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);
            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);
            Assert.True(sut.Snapshot.SchemaDef.IsSingleton);

            LastEvents
            .ShouldHaveSameEvents(
                CreateEvent(new SchemaCreated {
                Schema = new Schema(command.Name, command.Properties, command.IsSingleton)
            })
                );
        }
Example #10
0
        public Task CanCreate_should_throw_exception_if_fields_contains_duplicate_name()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = "field1",
                        Properties   = new StringFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key
                    },
                    new CreateSchemaField
                    {
                        Name         = "field1",
                        Properties   = new StringFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key
                    }
                },
                Name = "new-schema"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("Fields cannot have duplicate names.",
                                                                    "Fields")));
        }
Example #11
0
        public Task CanCreate_should_throw_exception_if_fields_contain_duplicate_names()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = "field1",
                        Properties   = ValidProperties(),
                        Partitioning = "invariant"
                    },
                    new CreateSchemaField
                    {
                        Name         = "field1",
                        Properties   = ValidProperties(),
                        Partitioning = "invariant"
                    }
                },
                Name = "new-schema"
            };

            return(Assert.ThrowsAsync <ValidationException>(() => GuardSchema.CanCreate(command, appProvider)));
        }
Example #12
0
        public async Task Create_should_create_schema_with_initial_fields()
        {
            var properties = new SchemaProperties();

            var fields = new List<UpsertSchemaField>
            {
                new UpsertSchemaField { Name = "field1", Properties = ValidProperties() },
                new UpsertSchemaField { Name = "field2", Properties = ValidProperties() },
                new UpsertSchemaField
                {
                    Name = "field3",
                    Partitioning = Partitioning.Language.Key,
                    Properties = new ArrayFieldProperties(),
                    Nested = new List<UpsertSchemaNestedField>
                    {
                        new UpsertSchemaNestedField { Name = "nested1", Properties = ValidProperties() },
                        new UpsertSchemaNestedField { Name = "nested2", Properties = ValidProperties() }
                    }
                }
            };

            var command = new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties, Fields = fields };

            var result = await sut.ExecuteAsync(CreateCommand(command));

            result.ShouldBeEquivalent(sut.Snapshot);

            var @event = (SchemaCreated)LastEvents.Single().Payload;

            Assert.Equal(AppId, sut.Snapshot.AppId.Id);
            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);
            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);

            Assert.Equal(3, @event.Schema.Fields.Count);
        }
Example #13
0
        public SchemaDomainObject Create(CreateSchema command)
        {
            Guard.Valid(command, nameof(command), () => "Cannot create schema");

            VerifyNotCreated();

            var @event = SimpleMapper.Map(command, new SchemaCreated {
                SchemaId = new NamedId <Guid>(Id, command.Name)
            });

            if (command.Fields != null)
            {
                @event.Fields = new List <SchemaCreatedField>();

                foreach (var commandField in command.Fields)
                {
                    var eventField = SimpleMapper.Map(commandField, new SchemaCreatedField());

                    @event.Fields.Add(eventField);
                }
            }

            RaiseEvent(@event);

            return(this);
        }
Example #14
0
        public async Task Create_should_create_schema_and_create_events()
        {
            var properties = new SchemaProperties();

            var command = new CreateSchema {
                Name = SchemaName, SchemaId = SchemaId, Properties = properties, Singleton = true
            };

            var result = await sut.ExecuteAsync(CreateCommand(command));

            result.ShouldBeEquivalent(EntityCreatedResult.Create(Id, 0));

            Assert.Equal(AppId, sut.Snapshot.AppId.Id);

            Assert.Equal(SchemaName, sut.Snapshot.Name);
            Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name);
            Assert.True(sut.Snapshot.IsSingleton);

            LastEvents
            .ShouldHaveSameEvents(
                CreateEvent(new SchemaCreated {
                Name = SchemaName, Properties = properties, Singleton = true
            })
                );
        }
Example #15
0
        /// <summary>
        /// Provides a general-purpose way to save QueryTraces. Use this in <see cref="Query{TResult}.TraceCallback"/> or
        /// <see cref="SqlDb{TKey}.TraceCallback"/> handlers
        /// </summary>
        public static void SaveTrace(IDbConnection connection, QueryTrace trace, SqlDb <int> db)
        {
            if (!db.Syntax.TableExists(connection, typeof(QueryTrace)))
            {
                var tableInfo = db.Syntax.GetTableInfoFromType(typeof(QueryTrace));
                if (!db.Syntax.SchemaExists(connection, tableInfo.Schema))
                {
                    CreateSchema cs = new CreateSchema(db.Syntax, tableInfo.Schema);
                    foreach (var cmd in cs.SqlCommands(connection))
                    {
                        connection.Execute(cmd);
                    }
                }
                CreateTable ct = new CreateTable(db.Syntax, db.Syntax.GetTableInfoFromType(typeof(QueryTrace)));
                foreach (var cmd in ct.SqlCommands(connection))
                {
                    connection.Execute(cmd);
                }
            }

            var callback = db.TraceCallback;

            // prevent infinite loop
            db.TraceCallback = null;

            db.Save(connection, trace);

            // restore original callback
            db.TraceCallback = callback;
        }
Example #16
0
        public void Create(CreateSchema command)
        {
            var @event = SimpleMapper.Map(command, new SchemaCreated {
                SchemaId = NamedId.Of(command.SchemaId, command.Name)
            });

            if (command.Fields != null)
            {
                @event.Fields = new List <SchemaCreatedField>();

                foreach (var commandField in command.Fields)
                {
                    var eventField = SimpleMapper.Map(commandField, new SchemaCreatedField());

                    @event.Fields.Add(eventField);

                    if (commandField.Nested != null)
                    {
                        eventField.Nested = new List <SchemaCreatedNestedField>();

                        foreach (var nestedField in commandField.Nested)
                        {
                            var eventNestedField = SimpleMapper.Map(nestedField, new SchemaCreatedNestedField());

                            eventField.Nested.Add(eventNestedField);
                        }
                    }
                }
            }

            RaiseEvent(@event);
        }
Example #17
0
        public Task CanCreate_should_throw_exception_if_nested_field_name_invalid()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <CreateSchemaField>
                {
                    new CreateSchemaField
                    {
                        Name         = "array",
                        Properties   = new ArrayFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key,
                        Nested       = new List <CreateSchemaNestedField>
                        {
                            new CreateSchemaNestedField
                            {
                                Name       = "invalid name",
                                Properties = new StringFieldProperties()
                            }
                        }
                    }
                },
                Name = "new-schema"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("Field name must be a valid javascript property name.",
                                                                    "Fields[1].Nested[1].Name")));
        }
Example #18
0
        public void CanCreate_should_throw_exception_if_nested_field_have_duplicate_names()
        {
            var command = new CreateSchema
            {
                AppId  = appId,
                Fields = new List <UpsertSchemaField>
                {
                    new UpsertSchemaField
                    {
                        Name         = "array",
                        Properties   = new ArrayFieldProperties(),
                        Partitioning = Partitioning.Invariant.Key,
                        Nested       = new List <UpsertSchemaNestedField>
                        {
                            new UpsertSchemaNestedField
                            {
                                Name       = "nested1",
                                Properties = new StringFieldProperties()
                            },
                            new UpsertSchemaNestedField
                            {
                                Name       = "nested1",
                                Properties = new StringFieldProperties()
                            }
                        }
                    }
                },
                Name = "new-schema"
            };

            ValidationAssert.Throws(() => GuardSchema.CanCreate(command),
                                    new ValidationError("Fields cannot have duplicate names.",
                                                        "Fields[1].Nested"));
        }
Example #19
0
        public Task CanCreate_should_throw_exception_if_name_not_valid()
        {
            var command = new CreateSchema {
                AppId = appId, Name = "INVALID NAME"
            };

            return(Assert.ThrowsAsync <ValidationException>(() => GuardSchema.CanCreate(command, appProvider)));
        }
Example #20
0
        public Task CanCreate_should_not_throw_exception_if_command_is_valid()
        {
            var command = new CreateSchema {
                AppId = appId, Name = "new-schema"
            };

            return(GuardSchema.CanCreate(command, appProvider));
        }
Example #21
0
        public void CanCreate_should_throw_exception_if_name_not_valid()
        {
            var command = new CreateSchema {
                AppId = appId, Name = "INVALID NAME"
            };

            ValidationAssert.Throws(() => GuardSchema.CanCreate(command),
                                    new ValidationError("Name is not a valid slug.", "Name"));
        }
Example #22
0
        public Task CanCreate_should_throw_exception_if_name_not_valid()
        {
            var command = new CreateSchema {
                AppId = appId, Name = "INVALID NAME"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("Name is not a valid slug.", "Name")));
        }
Example #23
0
        public Task CanCreate_should_throw_exception_if_name_already_in_use()
        {
            var command = new CreateSchema {
                AppId = appId, Name = "existing"
            };

            return(ValidationAssert.ThrowsAsync(() => GuardSchema.CanCreate(command, appProvider),
                                                new ValidationError("A schema with the same name already exists.")));
        }
        protected Task On(CreateSchema command, CommandContext context)
        {
            return(handler.CreateSyncedAsync <SchemaDomainObject>(context, async s =>
            {
                await GuardSchema.CanCreate(command, appProvider);

                s.Create(command);

                context.Complete(EntityCreatedResult.Create(command.SchemaId, s.Version));
            }));
        }
        public async Task Should_not_override_schema_id()
        {
            var command = new CreateSchema {
                SchemaId = Guid.NewGuid()
            };
            var context = new CommandContext(command, commandBus);

            await sut.HandleAsync(context);

            Assert.NotEqual(schemaId.Id, command.SchemaId);
        }
Example #26
0
        public Task CanCreate_should_throw_exception_if_name_already_in_use()
        {
            A.CallTo(() => appProvider.GetSchemaAsync(A <Guid> .Ignored, "new-schema"))
            .Returns(Task.FromResult(A.Fake <ISchemaEntity>()));

            var command = new CreateSchema {
                AppId = appId, Name = "new-schema"
            };

            return(Assert.ThrowsAsync <ValidationException>(() => GuardSchema.CanCreate(command, appProvider)));
        }
Example #27
0
        public static Task CanCreate(CreateSchema command, IAppProvider appProvider)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot create schema.", async error =>
            {
                if (!command.Name.IsSlug())
                {
                    error(new ValidationError("Name must be a valid slug.", nameof(command.Name)));
                }

                if (await appProvider.GetSchemaAsync(command.AppId.Id, command.Name) != null)
                {
                    error(new ValidationError($"A schema with name '{command.Name}' already exists", nameof(command.Name)));
                }

                if (command.Fields != null && command.Fields.Any())
                {
                    var index = 0;

                    foreach (var field in command.Fields)
                    {
                        var prefix = $"Fields.{index}";

                        if (!field.Partitioning.IsValidPartitioning())
                        {
                            error(new ValidationError("Partitioning is not valid.", $"{prefix}.{nameof(field.Partitioning)}"));
                        }

                        if (!field.Name.IsPropertyName())
                        {
                            error(new ValidationError("Name must be a valid property name.", $"{prefix}.{nameof(field.Name)}"));
                        }

                        if (field.Properties == null)
                        {
                            error(new ValidationError("Properties is required.", $"{prefix}.{nameof(field.Properties)}"));
                        }

                        var propertyErrors = FieldPropertiesValidator.Validate(field.Properties);

                        foreach (var propertyError in propertyErrors)
                        {
                            error(propertyError);
                        }
                    }

                    if (command.Fields.Select(x => x.Name).Distinct().Count() != command.Fields.Count)
                    {
                        error(new ValidationError("Fields cannot have duplicate names.", nameof(command.Fields)));
                    }
                }
            }));
        }
Example #28
0
        public SchemaDomainObject Create(CreateSchema command)
        {
            Guard.Valid(command, nameof(command), () => "Cannot create schema");

            VerifyNotCreated();

            RaiseEvent(SimpleMapper.Map(command, new SchemaCreated {
                SchemaId = new NamedId <Guid>(Id, command.Name)
            }));

            return(this);
        }
        public async Task Should_not_override_schema_id()
        {
            httpContext.Features.Set <ISchemaFeature>(new SchemaFeature(schemaId));

            var command = new CreateSchema {
                SchemaId = DomainId.NewGuid()
            };
            var context = Ctx(command);

            await sut.HandleAsync(context);

            Assert.NotEqual(schemaId.Id, command.SchemaId);
        }
Example #30
0
        public async Task Should_not_create_content_if_non_singleton_schema_is_created()
        {
            var command = new CreateSchema();

            var context =
                new CommandContext(command, commandBus)
                .Complete();

            await sut.HandleAsync(context);

            A.CallTo(() => commandBus.PublishAsync(A <ICommand> ._))
            .MustNotHaveHappened();
        }
Example #31
0
 public string visit(CreateSchema ddl)
 {
     return "";
 }