public void PASS_Serialize() { CommonQuery query = new CommonQuery("the quick brown fox"); string json = JsonConvert.SerializeObject(query); Assert.IsNotNull(json); string expectedJson = "{\"common\":{\"body\":{\"query\":\"the quick brown fox\",\"cutoff_frequency\":0.001}}}"; Assert.AreEqual(expectedJson, json); }
public void FAIL_CreateQuery_Query() { try { CommonQuery query = new CommonQuery(null); Assert.Fail(); } catch (ArgumentNullException argEx) { Assert.AreEqual("query", argEx.ParamName); } }
public void FAIL_CreateQuery_CutOffFrequency() { try { CommonQuery query = new CommonQuery("the quick brown fox", 0); Assert.Fail(); } catch (ArgumentOutOfRangeException argEx) { Assert.AreEqual("cutOffFrequency", argEx.ParamName); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Dictionary<string, object> fieldDict = serializer.Deserialize<Dictionary<string, object>>(reader); while(!fieldDict.ContainsKey(_QUERY)) fieldDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict.First().Value.ToString()); CommonQuery query = new CommonQuery(); query.Query = fieldDict.GetString(_QUERY); query.CutOffFrequency = fieldDict.GetDouble(_CUTOFF_FREQUENCY, _CUTOFF_FREQUENCY_DEFAULT); query.HighFrequencyOperator = OperatorEnum.Find(fieldDict.GetString(_HIGH_FREQUENCY_OPERATOR, _HIGH_FREQUENCY_OPERATOR_DEFAULT.ToString())); query.LowFrequencyOperator = OperatorEnum.Find(fieldDict.GetString(_LOW_FREQUENCY_OPERATOR, _LOW_FREQUENCY_OPERATOR_DEFAULT.ToString())); query.QueryName = fieldDict.GetStringOrDefault(QuerySerializer._QUERY_NAME); if (fieldDict.ContainsKey(_MINIMUM_SHOULD_MATCH)) { Dictionary<string, object> minimumDict = null; try { minimumDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(fieldDict[_MINIMUM_SHOULD_MATCH].ToString()); } catch { } if (minimumDict != null) { MinimumShouldMatchBase highMin = null; MinimumShouldMatchBase lowMin = null; if (minimumDict.ContainsKey(_HIGH_FREQ_MINIMUM)) highMin = MinimumShouldMatchBase.BuildMinimumShouldMatch(minimumDict[_HIGH_FREQ_MINIMUM].ToString()); if (minimumDict.ContainsKey(_LOW_FREQ_MINIMUM)) lowMin = MinimumShouldMatchBase.BuildMinimumShouldMatch(minimumDict[_LOW_FREQ_MINIMUM].ToString()); query.MinimumShouldMatch = new MinimumShouldMatch(highMin, lowMin); } else { MinimumShouldMatchBase allMin = MinimumShouldMatchBase.BuildMinimumShouldMatch(minimumDict[_MINIMUM_SHOULD_MATCH].ToString()); query.MinimumShouldMatch = new MinimumShouldMatch(allMin); } } return query; }
public void PASS_CreateQuery() { CommonQuery query = new CommonQuery("the quick brown fox"); Assert.IsNotNull(query); }