Esempio n. 1
0
        public void Should_provide_master_language_from_invariant()
        {
            var expected =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("en", 3));

            var input =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("iv", 3));

            var actual = input.ToApiModel(schema, languages, masterLanguage);

            Assert.Equal(expected, actual);
        }
Esempio n. 2
0
        public ContentData ToIdModel(Schema schema)
        {
            Guard.NotNull(schema, nameof(schema));

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out Field field))
                {
                    continue;
                }

                result[field.Id.ToString()] = fieldValue.Value;
            }

            return(result);
        }
Esempio n. 3
0
        public ContentData ToNameModel(Schema schema)
        {
            Guard.NotNull(schema, nameof(schema));

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!long.TryParse(fieldValue.Key, out long fieldId) || !schema.Fields.TryGetValue(fieldId, out Field field))
                {
                    continue;
                }

                result[field.Name] = fieldValue.Value;
            }

            return(result);
        }
Esempio n. 4
0
        public void Should_provide_invariant_from_first_language()
        {
            var expected =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("iv", 2));

            var input =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("de", 2)
                          .AddValue("it", 3));

            var actual = input.ToApiModel(schema, languagesConfig);

            Assert.Equal(expected, actual);
        }
Esempio n. 5
0
        public ContentData ToIdModel(Schema schema, bool encodeJsonField)
        {
            Guard.NotNull(schema, nameof(schema));

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out Field field))
                {
                    continue;
                }

                var fieldId = field.Id.ToString();

                if (encodeJsonField && field is JsonField)
                {
                    var encodedValue = new ContentFieldData();

                    foreach (var partitionValue in fieldValue.Value)
                    {
                        if (partitionValue.Value == null || partitionValue.Value.Type == JTokenType.Null)
                        {
                            encodedValue[partitionValue.Key] = null;
                        }
                        else
                        {
                            var value = Convert.ToBase64String(Encoding.UTF8.GetBytes(partitionValue.Value.ToString()));

                            encodedValue[partitionValue.Key] = value;
                        }
                    }

                    result[fieldId] = encodedValue;
                }
                else
                {
                    result[fieldId] = fieldValue.Value;
                }
            }

            return(result);
        }
Esempio n. 6
0
        public void Should_remove_null_values_when_cleaning()
        {
            var expected =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("en", 2));

            var input =
                new ContentData()
                .AddField("field1", null)
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("en", 2)
                          .AddValue("it", null));

            var actual = input.ToCleaned();

            Assert.Equal(expected, actual);
        }
Esempio n. 7
0
        public ContentData ToNameModel(Schema schema, bool decodeJsonField)
        {
            Guard.NotNull(schema, nameof(schema));

            var result = new ContentData();

            foreach (var fieldValue in this)
            {
                if (!long.TryParse(fieldValue.Key, out long fieldId) || !schema.FieldsById.TryGetValue(fieldId, out Field field))
                {
                    continue;
                }

                if (decodeJsonField && field is JsonField)
                {
                    var encodedValue = new ContentFieldData();

                    foreach (var partitionValue in fieldValue.Value)
                    {
                        if (partitionValue.Value == null || partitionValue.Value.Type == JTokenType.Null)
                        {
                            encodedValue[partitionValue.Key] = null;
                        }
                        else
                        {
                            var value = Encoding.UTF8.GetString(Convert.FromBase64String(partitionValue.Value.ToString()));

                            encodedValue[partitionValue.Key] = JToken.Parse(value);
                        }
                    }

                    result[field.Name] = encodedValue;
                }
                else
                {
                    result[field.Name] = fieldValue.Value;
                }
            }

            return(result);
        }
Esempio n. 8
0
        public void Should_not_include_hidden_field()
        {
            var expected =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("iv", 5));

            var input =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("iv", 5))
                .AddField("field3",
                          new ContentFieldData()
                          .AddValue("iv", 2));

            var actual = input.ToApiModel(schema, languages, masterLanguage);

            Assert.Equal(expected, actual);
        }
Esempio n. 9
0
        public void Should_cleanup_old_languages()
        {
            var expected =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("en", "en_string")
                          .AddValue("de", "de_string"));

            var input =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("en", "en_string")
                          .AddValue("de", "de_string")
                          .AddValue("it", "it_string"));

            var actual = input.ToApiModel(schema, languages, masterLanguage);

            Assert.Equal(expected, actual);
        }
Esempio n. 10
0
        public ContentData ToCleaned()
        {
            var result = new ContentData();

            foreach (var fieldValue in this.Where(x => x.Value != null))
            {
                var resultValue = new ContentFieldData();

                foreach (var languageValue in fieldValue.Value.Where(x => x.Value != null && x.Value.Type != JTokenType.Null))
                {
                    resultValue[languageValue.Key] = languageValue.Value;
                }

                if (resultValue.Count > 0)
                {
                    result[fieldValue.Key] = resultValue;
                }
            }

            return(result);
        }
Esempio n. 11
0
        public void Should_convert_to_encoded_id_model()
        {
            var input =
                new ContentData()
                .AddField("json",
                          new ContentFieldData()
                          .AddValue("en", new JObject())
                          .AddValue("de", null)
                          .AddValue("it", JValue.CreateNull()));

            var actual = input.ToIdModel(schema, true);

            var expected =
                new ContentData()
                .AddField("4",
                          new ContentFieldData()
                          .AddValue("en", "e30=")
                          .AddValue("de", null)
                          .AddValue("it", null));

            Assert.Equal(expected, actual);
        }
Esempio n. 12
0
        public ContentData MergeInto(ContentData other)
        {
            Guard.NotNull(other, nameof(other));

            var result = new ContentData(this);

            if (ReferenceEquals(other, this))
            {
                return(result);
            }

            foreach (var otherValue in other)
            {
                var fieldValue = result.GetOrAdd(otherValue.Key, x => new ContentFieldData());

                foreach (var value in otherValue.Value)
                {
                    fieldValue[value.Key] = value.Value;
                }
            }

            return(result);
        }
Esempio n. 13
0
        public void Should_merge_two_data()
        {
            var lhs =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("iv", 1))
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("de", 2));

            var rhs =
                new ContentData()
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("en", 3))
                .AddField("field3",
                          new ContentFieldData()
                          .AddValue("iv", 4));

            var expected =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("iv", 1))
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("de", 2)
                          .AddValue("en", 3))
                .AddField("field3",
                          new ContentFieldData()
                          .AddValue("iv", 4));

            var actual = lhs.MergeInto(rhs);

            Assert.Equal(expected, actual);
        }
Esempio n. 14
0
        public void Should_not_be_equal_when_data_have_not_same_structure()
        {
            var lhs =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("iv", 2))
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("iv", 2));

            var rhs =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("en", 2))
                .AddField("field3",
                          new ContentFieldData()
                          .AddValue("iv", 2));

            Assert.False(lhs.Equals(rhs));
            Assert.False(lhs.Equals((object)rhs));
            Assert.NotEqual(lhs.GetHashCode(), rhs.GetHashCode());
        }
Esempio n. 15
0
        public void Should_return_flat_list_when_single_languages_specified()
        {
            var data =
                new ContentData()
                .AddField("field1",
                          new ContentFieldData()
                          .AddValue("de", 1)
                          .AddValue("en", 2))
                .AddField("field2",
                          new ContentFieldData()
                          .AddValue("de", null)
                          .AddValue("en", 4))
                .AddField("field3",
                          new ContentFieldData()
                          .AddValue("en", 6))
                .AddField("field4",
                          new ContentFieldData()
                          .AddValue("it", 7));

            var fallbackConfig =
                LanguagesConfig.Create(Language.DE).Add(Language.EN)
                .Update(Language.DE, false, false, new[] { Language.EN });

            var output = (Dictionary <string, JToken>)data.ToLanguageModel(fallbackConfig, new List <Language> {
                Language.DE
            });

            var expected = new Dictionary <string, JToken>
            {
                { "field1", 1 },
                { "field2", 4 },
                { "field3", 6 }
            };

            Assert.True(expected.EqualsDictionary(output));
        }
Esempio n. 16
0
 public bool Equals(ContentData other)
 {
     return(other != null && (ReferenceEquals(this, other) || this.EqualsDictionary(other)));
 }