/// <summary> /// Create a clause of in condition. /// </summary> /// <remarks> /// Query records matches with key-value specified by argument. /// More efficient than using combination of "equals" and "or" /// When querying the multiple records with specific key. /// </remarks> /// <returns> /// KiiClause instance /// </returns> /// <param name='key'> /// Key. /// </param> /// <param name='value'> /// Values to be compared. The length of values array should be less than or equals to 200. /// </param> /// <exception cref='ArgumentException'> /// Is thrown in the following cases: /// <list type="bullet"> /// <item> /// <term>key is null.</term> /// </item> /// <item> /// <term>value is null or length of value is 0.</term> /// </item> /// <item> /// <term>length of value is more than 200.</term> /// </item> /// </list> /// </exception> /// <exception cref='JsonException'> /// Is thrown when the unexpected invalid params given. /// </exception> public static KiiClause InWithStringValue(string key, params string[] value) { if (Utils.IsEmpty(key)) { throw new ArgumentException("key must not null or empty string"); } if (value == null || value.Length == 0) { throw new ArgumentException(ErrorInfo.KIICLAUSE_VALUE_NULL); } if (value.Length > 200) { throw new ArgumentException(ErrorInfo.KIICLAUSE_VALUE_EXCEED_MAX_LENGTH); } KiiClause clause = new KiiClause(); try { clause.mJson = new JsonObject(); clause.mJson.Put("type", "in"); clause.mJson.Put("field", key); JsonArray elements = new JsonArray(); foreach (string v in value) { if (v == null) { throw new ArgumentException(ErrorInfo.KIICLAUSE_VALUE_NULL); } elements.Put(v); } clause.mJson.Put("values", elements); return(clause); } catch (JsonException ex) { throw new SystemException("Invalid param given!", ex); } }
public void NotHasFieldTest() { KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket"); KiiObject obj1 = bucket.NewKiiObject(); obj1["name"] = "foo"; obj1["age"] = 20; obj1.Save(); KiiObject obj2 = bucket.NewKiiObject(); obj2["name"] = "bar"; obj2["age"] = 33; obj2.Save(); KiiObject obj3 = bucket.NewKiiObject(); obj3["name"] = "hoge"; obj3.Save(); KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.HasField("age", FieldType.INTEGER))); query.SortByAsc("name"); KiiQueryResult <KiiObject> results = bucket.Query(query); Assert.AreEqual(1, results.Count); Assert.AreEqual("hoge", results[0].GetString("name")); }
public void NotPrefixTest() { KiiBucket bucket = KiiUser.CurrentUser.Bucket("my_bucket"); KiiObject obj1 = bucket.NewKiiObject(); obj1["name"] = "foo"; obj1.Save(); KiiObject obj2 = bucket.NewKiiObject(); obj2["name"] = "fool"; obj2.Save(); KiiObject obj3 = bucket.NewKiiObject(); obj3["name"] = "hoge"; obj3.Save(); KiiQuery query = new KiiQuery(KiiClause.Not(KiiClause.StartsWith("name", "foo"))); query.SortByAsc("name"); KiiQueryResult <KiiObject> results = bucket.Query(query); Assert.AreEqual(1, results.Count); Assert.AreEqual("hoge", results[0].GetString("name")); }
public void Test_0404_In_double_array() { double[] values = { 1, 23.45, 3 }; KiiClause c = KiiClause.InWithDoubleValue("score", values); Assert.AreEqual("{\"type\":\"in\",\"field\":\"score\",\"values\":[1,23.45,3]}", c.ToJson().ToString()); }
public void Test_0504_In_string_array() { string[] values = { "kaa", "kii", "kuu" }; KiiClause c = KiiClause.InWithStringValue("name", values); Assert.AreEqual("{\"type\":\"in\",\"field\":\"name\",\"values\":[\"kaa\",\"kii\",\"kuu\"]}", c.ToJson().ToString()); }
public void Test_0304_In_long_array() { long[] values = { 1, 1234567890123, 3 }; KiiClause c = KiiClause.InWithLongValue("score", values); Assert.AreEqual("{\"type\":\"in\",\"field\":\"score\",\"values\":[1,1234567890123,3]}", c.ToJson().ToString()); }
public void Test_0001_GeoDistanceQuery() { KiiBucket bucket = testUser.Bucket("aBucket"); KiiObject obj = bucket.NewKiiObject(); KiiGeoPoint point = new KiiGeoPoint(35.667983, 139.739356); obj.SetGeoPoint("myLoc", point); obj.Save(); KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148); KiiClause clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc"); KiiQuery query = new KiiQuery(clause); KiiQueryResult <KiiObject> result = bucket.Query(query); KiiObject retObj = result [0]; KiiGeoPoint retPoint; retPoint = retObj.GetGeoPoint("myLoc"); Assert.AreEqual(point.Latitude, retPoint.Latitude); Assert.AreEqual(point.Longitude, retPoint.Longitude); JsonObject jObj = retObj.GetJsonObject("_calculated"); double retDistance = jObj.GetDouble("distanceToMyLoc"); double expectedDistance = distanceInMeter(point, center); Assert.IsTrue(approximateEqual(expectedDistance, retDistance, 0.00001)); }
public void Test_0204_In_int_array() { int[] values = { 1, 2, 3 }; KiiClause c = KiiClause.InWithIntValue("score", values); Assert.AreEqual("{\"type\":\"in\",\"field\":\"score\",\"values\":[1,2,3]}", c.ToJson().ToString()); }
public void Test_0002_GeoBox_null_key() { KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100); KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102); KiiClause.GeoBox(null, northEast, southWest); }
public void Test_0005_GeoBoxQuery() { KiiBucket bucket = testUser.Bucket("aBucket"); KiiObject obj = bucket.NewKiiObject(); KiiGeoPoint point = new KiiGeoPoint(35.667983, 139.739356); obj.SetGeoPoint("myloc", point); obj.Save(); //not in the box KiiObject obj2 = bucket.NewKiiObject(); KiiGeoPoint point2 = new KiiGeoPoint(); obj2.SetGeoPoint("myloc", point2); obj2.Save(); KiiGeoPoint sw = new KiiGeoPoint(35.52105, 139.699402); KiiGeoPoint ne = new KiiGeoPoint(36.069082, 140.07843); KiiClause clause = KiiClause.GeoBox("myloc", ne, sw); KiiQuery query = new KiiQuery(clause); KiiQueryResult <KiiObject> result = bucket.Query(query); Assert.AreEqual(result.Count, 1); KiiObject retObj = result [0]; KiiGeoPoint retPoint; retPoint = retObj.GetGeoPoint("myloc"); Assert.AreEqual(point.Latitude, retPoint.Latitude); Assert.AreEqual(point.Longitude, retPoint.Longitude); }
public void Test_0003_GeoBox_empty_key() { KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100); KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102); KiiClause.GeoBox("", northEast, southWest); }
private static KiiClause GeoDistanceInternal(string key, JsonObject geodistance, double radius, string putDistanceInto) { if (Utils.IsEmpty(key)) { throw new ArgumentException("key must not null or empty string"); } KiiClause clause = new KiiClause(); try { clause.mJson = new JsonObject(); clause.mJson.Put("type", "geodistance"); clause.mJson.Put("field", key); clause.mJson.Put("center", geodistance); clause.mJson.Put("radius", radius); if (!Utils.IsEmpty(putDistanceInto)) { clause.mJson.Put("putDistanceInto", putDistanceInto); } return(clause); } catch (JsonException ex) { throw new SystemException("Invalid param given!", ex); } }
public void Test_0001_And_1() { KiiClause c = KiiClause.And( KiiClause.Equals("name", "kii") ); Assert.AreEqual("{\"type\":\"eq\",\"field\":\"name\",\"value\":\"kii\"}", c.ToJson().ToString()); }
public void Test_0722_InWithLongValue_length_equal_200() { try{ KiiClause.InWithLongValue("100", new long[200]); }catch (Exception exception) { Assert.Fail("should not throw exception"); } }
public void Test_2_6_CountWithUnsupportedQuery() { MockHttpClientFactory factory = new MockHttpClientFactory(); Kii.HttpClientFactory = factory; // set response MockHttpClient client = factory.Client; string mockResponseBody = "{\"errorCode\" : \"QUERY_NOT_SUPPORTED\"}"; client.AddResponse(new CloudException(400, mockResponseBody)); string bucketName = "TestBucket"; KiiBucket bucket = Kii.Bucket(bucketName); KiiClause clause = KiiClause.Equals("key", "value"); KiiQuery query = new KiiQuery(clause); query.NextPaginationKey = "pkey"; CloudException exp = null; try { bucket.Count(query); Assert.Fail("Exception not thrown"); } catch (CloudException e) { exp = e; } Assert.IsNotNull(exp); Assert.AreEqual(400, exp.Status); Assert.AreEqual(mockResponseBody, exp.Body); // check request. string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query"); Assert.AreEqual(url, client.RequestUrl[0]); Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]); MockHttpHeaderList headerList = client.RequestHeader[0]; Assert.AreEqual("appId", headerList["X-Kii-AppID"]); Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]); String queryStr = "{ " + "\"bucketQuery\" : {" + "\"clause\" : {" + "\"type\" : \"eq\"," + "\"field\" : \"key\"," + "\"value\" : \"value\"" + "}," + "\"aggregations\" : [ {" + "\"type\" : \"COUNT\"," + "\"putAggregationInto\" : \"count_field\"" + "}]" + "}, " + "\"paginationKey\" : \"pkey\"" + "}"; JsonObject expectedBodyJson = new JsonObject(queryStr); JsonObject actualBodyJson = new JsonObject(client.RequestBody[0]); KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson); }
public void Test_0011_And_null() { KiiClause.And( KiiClause.Equals("name", "kii"), null, KiiClause.GreaterThan("score", 100), null ); }
public void Test_2_5_CountWithOrQuery() { MockHttpClientFactory factory = new MockHttpClientFactory(); Kii.HttpClientFactory = factory; // set response MockHttpClient client = factory.Client; client.AddResponse(200, "{\"aggregations\" : { \"count_field\" : 5 } }"); string bucketName = "TestBucket"; KiiBucket bucket = Kii.Bucket(bucketName); KiiClause clause1 = KiiClause.Equals("key1", "value1"); KiiClause clause2 = KiiClause.Equals("key2", "value2"); KiiClause clause = KiiClause.Or(clause1, clause2); KiiQuery query = new KiiQuery(clause); int count = bucket.Count(query); Assert.AreEqual(5, count); // check request. Console.WriteLine(client.RequestBody[0]); string url = Utils.Path(ConstantValues.DEFAULT_BASE_URL, "apps", "appId", "buckets", "TestBucket", "query"); Assert.AreEqual(url, client.RequestUrl[0]); Assert.AreEqual(KiiHttpMethod.POST, client.RequestMethod[0]); MockHttpHeaderList headerList = client.RequestHeader[0]; Assert.AreEqual("appId", headerList["X-Kii-AppID"]); Assert.AreEqual("appKey", headerList["X-Kii-AppKey"]); String queryStr = "{ " + "\"bucketQuery\" : {" + "\"clause\" : {" + "\"type\" : \"or\"," + "\"clauses\" :[ {" + "\"type\" : \"eq\"," + "\"field\" : \"key1\"," + "\"value\" : \"value1\"" + "}," + "{" + "\"type\" : \"eq\"," + "\"field\" : \"key2\"," + "\"value\" : \"value2\"" + "}]" + "}," + "\"aggregations\" : [ {" + "\"type\" : \"COUNT\"," + "\"putAggregationInto\" : \"count_field\"" + "}]" + "}" + "}"; JsonObject expectedBodyJson = new JsonObject(queryStr); JsonObject actualBodyJson = new JsonObject(client.RequestBody[0]); KiiAssertion.AssertJson(expectedBodyJson, actualBodyJson); }
public void Test_0733_InWithStringValue_empty_key() { string[] valueArray = new string[200]; for (int i = 0; i < valueArray.Length; i++) { valueArray[i] = i.ToString(); } KiiClause.InWithStringValue("", valueArray); }
public void Test_0008_GeoDistance_empty_key() { KiiGeoPoint center = new KiiGeoPoint(70.00, 100); double radius = 10.0; string key = ""; string putDistanceInto = "calculatedDistance"; KiiClause.GeoDistance(key, center, radius, putDistanceInto); }
public void Test_0010_GeoBox_Valid_key_Valid_Center_Invalid_radius() { KiiGeoPoint center = new KiiGeoPoint(70.00, 100); double radius = -1; string key = "currentLocation"; string putDistanceInto = "calculatedDistance"; KiiClause.GeoDistance(key, center, radius, putDistanceInto); }
public void Test_0100_Or() { KiiClause c = KiiClause.Or( KiiClause.Equals("name", "kii"), KiiClause.GreaterThan("score", 100) ); Assert.AreEqual("{\"type\":\"or\",\"clauses\":[" + "{\"type\":\"eq\",\"field\":\"name\",\"value\":\"kii\"}," + "{\"type\":\"range\",\"field\":\"score\",\"lowerLimit\":100,\"lowerIncluded\":false}]}", c.ToJson().ToString()); }
public void Test_0004_GeoDistanceQuery_sort_asc() { KiiBucket bucket = testUser.Bucket("aBucket"); KiiObject obj1 = bucket.NewKiiObject(); KiiGeoPoint point1 = new KiiGeoPoint(35.672568, 139.723606); obj1.SetGeoPoint("myLoc", point1); obj1.Save(); KiiObject obj2 = bucket.NewKiiObject(); KiiGeoPoint point2 = new KiiGeoPoint(35.667983, 139.739356); obj2.SetGeoPoint("myLoc", point2); obj2.Save(); // not in radius KiiObject obj3 = bucket.NewKiiObject(); KiiGeoPoint point3 = new KiiGeoPoint(); obj3.SetGeoPoint("myLoc", point3); obj3.Save(); KiiGeoPoint center = new KiiGeoPoint(35.677379, 139.702148); KiiClause clause = KiiClause.GeoDistance("myloc", center, 4000, "distanceToMyLoc"); KiiQuery query = new KiiQuery(clause); query.SortByAsc("_calculated.distanceToMyLoc"); KiiQueryResult <KiiObject> result = bucket.Query(query); Assert.AreEqual(result.Count, 2); KiiObject retObj1 = result [0]; KiiGeoPoint retPoint1 = retObj1.GetGeoPoint("myLoc"); Assert.AreEqual(point1.Latitude, retPoint1.Latitude); Assert.AreEqual(point1.Longitude, retPoint1.Longitude); JsonObject jObj1 = retObj1.GetJsonObject("_calculated"); KiiObject retObj2 = result [1]; KiiGeoPoint retPoint2 = retObj2.GetGeoPoint("myLoc"); Assert.AreEqual(point2.Latitude, retPoint2.Latitude); Assert.AreEqual(point2.Longitude, retPoint2.Longitude); JsonObject jObj2 = retObj2.GetJsonObject("_calculated"); double retDistance1 = jObj1.GetDouble("distanceToMyLoc"); double retDistance2 = jObj2.GetDouble("distanceToMyLoc"); double expectedDistance1 = distanceInMeter(point1, center); double expectedDistance2 = distanceInMeter(point2, center); Assert.IsTrue(approximateEqual(expectedDistance1, retDistance1, 0.00001)); Assert.IsTrue(approximateEqual(expectedDistance2, retDistance2, 0.00001)); }
public void Test_0010_And_array() { KiiClause[] arr = new KiiClause[] { KiiClause.Equals("name", "kii"), KiiClause.GreaterThan("score", 100) }; KiiClause c = KiiClause.And(arr); Assert.AreEqual("{\"type\":\"and\",\"clauses\":[" + "{\"type\":\"eq\",\"field\":\"name\",\"value\":\"kii\"}," + "{\"type\":\"range\",\"field\":\"score\",\"lowerLimit\":100,\"lowerIncluded\":false}]}", c.ToJson().ToString()); }
public void Test_0732_InWithStringValue_length_equal_200() { try{ string[] valueArray = new string[200]; for (int i = 0; i < valueArray.Length; i++) { valueArray[i] = i.ToString(); } KiiClause.InWithStringValue("100", valueArray); }catch (Exception exception) { Assert.Fail("should not throw exception"); } }
public void Test_2_5_CountWithQuery() { string bucketName = "bucket" + CurrentTimeMillis(); KiiBucket bucket = Kii.Bucket(bucketName); for (int i = 0; i < 15; i++) { KiiObject obj = bucket.NewKiiObject(); obj ["intField"] = i; obj.Save(); } KiiClause clause = KiiClause.GreaterThanOrEqual("intField", 10); int count = bucket.Count(new KiiQuery(clause)); Assert.AreEqual(5, count); }
/// <summary> /// Concatenate <see cref="KiiClause"/> with NOT operator. /// </summary> /// <remarks> /// Query performance will be worse as the number of objects in bucket increases, so we recommend you avoid the OR clause if possible. /// </remarks> /// <param name="clause">Clause.</param> /// <returns> /// KiiClanse instance. /// </returns> public static KiiClause Not(KiiClause clause) { if (clause == null) { throw new ArgumentException("clause can not be null"); } KiiClause cls = new KiiClause(); try { JsonObject json = new JsonObject(); json.Put("type", "not"); json.Put("clause", clause.ToJson()); cls.mJson = json; } catch (JsonException jse) { throw new ArgumentException("Invalid arugment.", jse); } return(cls); }
public void Test_0001_GeoBox_Valid_Parameters() { KiiGeoPoint northEast = new KiiGeoPoint(70.00, 100); KiiGeoPoint southWest = new KiiGeoPoint(71.00, 102); KiiClause c = KiiClause.GeoBox("box", northEast, southWest); JsonObject clause = c.ToJson(); JsonObject box = clause.GetJsonObject("box"); JsonObject ne = box.GetJsonObject("ne"); JsonObject sw = box.GetJsonObject("sw"); Assert.AreEqual(clause.GetString("type"), "geobox"); Assert.AreEqual(clause.GetString("field"), "box"); Assert.AreEqual(ne.GetDouble("lat"), northEast.Latitude); Assert.AreEqual(ne.GetDouble("lon"), northEast.Longitude); Assert.AreEqual(sw.GetDouble("lat"), southWest.Latitude); Assert.AreEqual(sw.GetDouble("lon"), southWest.Longitude); }
/// <summary> /// Create Query from <see cref="KiiClause"/> /// </summary> /// <remarks> /// If clause is null, matches all items in the bucket. /// </remarks> /// <param name='clause'> /// Clause. /// </param> public KiiQuery(KiiClause clause) { mJson = new JsonObject(); try { if (clause == null) { mJson.Put("clause", ALLCLAUSE); } else { mJson.Put("clause", clause.ToJson()); } } catch (JsonException e) { throw new SystemException("Query clause generate error", e); } }
/// <summary> /// Create a clause to return all entities that have a specified field. /// </summary> /// <remarks> /// The type of the content of the field must be provided, possible values are "STRING", "INTEGER", "DECIMAL" and "BOOLEAN". /// </remarks> /// <returns> /// KiiClause instance. /// </returns> /// <param name='key'> /// Name of the specified field. /// </param> /// <param name='fieldType'> /// The type of the content of the field. /// </param> public static KiiClause HasField(string key, FieldType fieldType) { if (Utils.IsEmpty(key)) { throw new ArgumentException("key must not null or empty string"); } KiiClause clause = new KiiClause(); try { clause.mJson = new JsonObject(); clause.mJson.Put("type", "hasField"); clause.mJson.Put("field", key); clause.mJson.Put("fieldType", fieldType.ToString()); return(clause); } catch (JsonException ex) { throw new SystemException("Invalid param given!", ex); } }
public void Test_0006_GeoDistance_Valid_Parameters() { KiiGeoPoint center = new KiiGeoPoint(70.00, 100); double radius = 10.0; string key = "currentLocation"; string putDistanceInto = "calculatedDistance"; KiiClause c = KiiClause.GeoDistance(key, center, radius, putDistanceInto); JsonObject clause = c.ToJson(); Assert.AreEqual(clause.GetString("type"), "geodistance"); Assert.AreEqual(clause.GetString("field"), key); Assert.AreEqual(clause.GetString("putDistanceInto"), putDistanceInto); Assert.AreEqual(clause.GetDouble("radius"), radius); JsonObject centerJson = clause.GetJsonObject("center"); Assert.AreEqual(centerJson.GetDouble("lat"), center.Latitude); Assert.AreEqual(centerJson.GetDouble("lon"), center.Longitude); }