public void FAIL_CreateQuery_Query()
 {
     try
     {
         HasParentQuery query = new HasParentQuery("type", null);
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("query", argEx.ParamName);
     }
 }
 public void FAIL_CreateQuery_ChildType()
 {
     try
     {
         HasParentQuery query = new HasParentQuery(null, new TermQuery("field", "value"));
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("parentType", argEx.ParamName);
     }
 }
        public void PASS_Serialize()
        {
            HasParentQuery query = new HasParentQuery("type", new TermQuery("field", "value"))
            {
                ScoreType = ScoreTypeEnum.Score
            };

            string json = JsonConvert.SerializeObject(query);

            Assert.IsNotNull(json);

            string expectedJson = "{\"has_parent\":{\"parent_type\":\"type\",\"score_type\":\"score\",\"query\":{\"term\":{\"field\":\"value\"}}}}";
            Assert.AreEqual(expectedJson, json);
        }
        public void PASS_CreateQuery()
        {
            HasParentQuery query = new HasParentQuery("type", new TermQuery("field", "value"))
            {
                ScoreType = ScoreTypeEnum.Score
            };

            Assert.IsNotNull(query);
            Assert.AreEqual("type", query.ParentType);
            Assert.AreEqual(ScoreTypeEnum.Score, query.ScoreType);
            Assert.IsTrue(query.Query is TermQuery);
            Assert.AreEqual("field", (query.Query as TermQuery).Field);
            Assert.AreEqual("value", (query.Query as TermQuery).Value);
        }
Ejemplo n.º 5
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> queryDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if (queryDict.ContainsKey(QueryTypeEnum.HasParent.ToString()))
                queryDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(queryDict.First().Value.ToString());

            if (!queryDict.ContainsKey(_PARENT_TYPE))
                throw new Exception("HasParent expects a type property.");
            if (!queryDict.ContainsKey(_QUERY))
                throw new Exception("HasParent expects a query property.");

            string parentType = queryDict.GetString(_PARENT_TYPE);
            IQuery query = JsonConvert.DeserializeObject<IQuery>(queryDict.GetString(_QUERY));

            HasParentQuery parentQuery = new HasParentQuery(parentType, query);
            ScoreTypeEnum scoreType = ScoreTypeEnum.None;
            parentQuery.ScoreType = ScoreTypeEnum.Find(queryDict.GetStringOrDefault(_SCORE_TYPE));
            if (scoreType == null)
                throw new Exception(queryDict[_SCORE_TYPE].ToString() + " is not an allowed score type for the has parent query.");

            parentQuery.QueryName = queryDict.GetStringOrDefault(QuerySerializer._QUERY_NAME);

            return parentQuery;
        }