public void PASS_Serialize_Filter()
        {
            HasChildFilter filter = new HasChildFilter("child", new TermFilter("field", "value"));
            string json = JsonConvert.SerializeObject(filter);
            Assert.IsNotNull(json);

            string expectedJson = "{\"has_child\":{\"type\":\"child\",\"filter\":{\"term\":{\"field\":\"value\"}}}}";
            Assert.AreEqual(expectedJson, json);
        }
 public void PASS_CreateFilter_Query()
 {
     HasChildFilter filter = new HasChildFilter("child", new TermQuery("field", "value"));
     Assert.IsNotNull("child", filter.ChildType);
     Assert.IsTrue(filter.Query is TermQuery);
     TermQuery termQuery = filter.Query as TermQuery;
     Assert.AreEqual("field", termQuery.Field);
     Assert.AreEqual("value", termQuery.Value);
 }
 public void PASS_CreateFilter_Filter()
 {
     HasChildFilter filter = new HasChildFilter("child", new TermFilter("field", "value"));
     Assert.IsNotNull("child", filter.ChildType);
     Assert.IsTrue(filter.Filter is TermFilter);
     TermFilter termFilter = filter.Filter as TermFilter;
     Assert.AreEqual("field", termFilter.Field);
     Assert.AreEqual("value", termFilter.Value);
 }
 public void FAIL_CreateFilter_Query()
 {
     try
     {
         HasChildFilter filter = new HasChildFilter("child", (IQuery)null);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("query", ex.ParamName);
     }
 }
 public void FAIL_CreateFilter_ChildType()
 {
     try
     {
         HasChildFilter filter = new HasChildFilter(null, (IFilter)null);
         Assert.Fail();
     }
     catch (ArgumentNullException ex)
     {
         Assert.AreEqual("childType", ex.ParamName);
     }
 }
Пример #6
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> fieldDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if (fieldDict.ContainsKey(FilterTypeEnum.HasChild.ToString()))
                fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.First().Value.ToString());

            HasChildFilter filter = null;
            if(fieldDict.ContainsKey(_FILTER))
            {
                filter = new HasChildFilter(
                                fieldDict.GetString(_TYPE),
                                JsonConvert.DeserializeObject<IFilter>(fieldDict.GetString(_FILTER)));
            }
            else
            {
                filter = new HasChildFilter(
                                fieldDict.GetString(_TYPE),
                                JsonConvert.DeserializeObject<IQuery>(fieldDict.GetString(_QUERY)));
            }

            filter.FilterName = fieldDict.GetStringOrDefault(FilterSerializer._FILTER_NAME);

            return filter;
        }