Example #1
0
        public void Should_return_null_from_references_field_when_removing_references_from_null_array()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            var result = sut.CleanReferences(null, null);

            Assert.Null(result);
        }
Example #2
0
        public void Should_get_default_value_from_references_field()
        {
            var field =
                new ReferencesField(1, "1", Partitioning.Invariant,
                                    new ReferencesFieldProperties());

            Assert.Equal(new JArray(), DefaultValueFactory.CreateDefaultValue(field, Now));
        }
Example #3
0
        public async Task Should_not_add_error_if_references_are_null_and_valid()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            await sut.ValidateAsync(CreateValue(null), errors);

            Assert.Empty(errors);
        }
Example #4
0
        public async Task Should_add_errors_if_value_is_not_valid()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            await sut.ValidateAsync("invalid", errors);

            errors.ShouldBeEquivalentTo(
                new[] { "<FIELD> is not a valid value." });
        }
Example #5
0
        public async Task Should_not_add_error_if_references_are_valid()
        {
            var referenceId = Guid.NewGuid();

            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            await sut.ValidateAsync(CreateValue(referenceId), errors, ValidationTestExtensions.ValidContext);

            Assert.Empty(errors);
        }
Example #6
0
        public async Task Should_add_errors_if_value_has_not_enough_items()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant, new ReferencesFieldProperties {
                SchemaId = schemaId, MinItems = 3
            });

            await sut.ValidateAsync(CreateValue(Guid.NewGuid(), Guid.NewGuid()), errors);

            errors.ShouldBeEquivalentTo(
                new[] { "<FIELD> must have at least 3 item(s)." });
        }
Example #7
0
        public async Task Should_add_errors_if_references_are_required_and_empty()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant, new ReferencesFieldProperties {
                SchemaId = schemaId, IsRequired = true
            });

            await sut.ValidateAsync(CreateValue(), errors);

            errors.ShouldBeEquivalentTo(
                new[] { "<FIELD> is required." });
        }
Example #8
0
        public void Should_return_list_from_references_field_with_schema_id_for_referenced_ids_when_other_type()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant,
                                          new ReferencesFieldProperties {
                SchemaId = schemaId
            });

            var result = sut.ExtractReferences("invalid").ToArray();

            Assert.Equal(new[] { schemaId }, result);
        }
Example #9
0
        public JsonProperty Visit(ReferencesField field)
        {
            return(CreateProperty(field, jsonProperty =>
            {
                var itemSchema = schemaResolver("ReferenceItem", new JsonSchema4 {
                    Type = JsonObjectType.String
                });

                jsonProperty.Type = JsonObjectType.Array;
                jsonProperty.Item = itemSchema;
            }));
        }
Example #10
0
        public void Should_return_same_token_from_references_field_when_removing_references_and_nothing_to_remove()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            var token  = CreateValue(id1, id2);
            var result = sut.CleanReferences(token, new HashSet <Guid>(new[] { Guid.NewGuid() }));

            Assert.Same(token, result);
        }
Example #11
0
        private static JToken Visit(ReferencesField field, JToken value, ICollection <Guid> oldReferences)
        {
            if (oldReferences.Contains(field.Properties.SchemaId))
            {
                return(new JArray());
            }

            var oldIds = GetIds(value);
            var newIds = oldIds.Except(oldReferences).ToList();

            return(oldIds.Count != newIds.Count ? JToken.FromObject(newIds) : value);
        }
Example #12
0
        public async Task Should_add_errors_if_reference_are_not_valid()
        {
            var referenceId = Guid.NewGuid();

            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant, new ReferencesFieldProperties {
                SchemaId = schemaId
            });

            await sut.ValidateAsync(CreateValue(referenceId), errors, ValidationTestExtensions.InvalidContext(referenceId));

            errors.ShouldBeEquivalentTo(
                new[] { $"<FIELD> contains invalid reference '{referenceId}'." });
        }
Example #13
0
        public void Should_remove_all_references_from_references_field_when_schema_is_removed()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant,
                                          new ReferencesFieldProperties {
                SchemaId = schemaId
            });

            var result = sut.CleanReferences(CreateValue(id1, id2), new HashSet <Guid>(new[] { schemaId }));

            Assert.Equal(CreateValue(), result);
        }
Example #14
0
        public void Should_return_ids_from_references_field()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant,
                                          new ReferencesFieldProperties {
                SchemaId = schemaId
            });

            var result = sut.ExtractReferences(CreateValue(id1, id2)).ToArray();

            Assert.Equal(new[] { id1, id2, schemaId }, result);
        }
Example #15
0
        private static IEnumerable <Guid> Visit(ReferencesField field, JToken value)
        {
            IEnumerable <Guid> result;

            try
            {
                result = value?.ToObject <List <Guid> >() ?? Enumerable.Empty <Guid>();
            }
            catch
            {
                result = Enumerable.Empty <Guid>();
            }

            return(result.Union(new[] { field.Properties.SchemaId }));
        }
Example #16
0
        public void Should_instantiate_field()
        {
            var sut = new ReferencesField(1, "my-refs", Partitioning.Invariant);

            Assert.Equal("my-refs", sut.Name);
        }
Example #17
0
 public IEdmTypeReference Visit(ReferencesField field)
 {
     return(CreatePrimitive(EdmPrimitiveTypeKind.String, field));
 }
Example #18
0
 public object Visit(ReferencesField field)
 {
     return(Value.ToObject <List <Guid> >());
 }