Skip to content

Commit b062d79

Browse files
author
Ben Lei
committed
Add fetch record by id APIs
1 parent 812e525 commit b062d79

10 files changed

+701
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package io.skygear.skygear;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
import org.junit.AfterClass;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import static org.junit.Assert.*;
15+
16+
@RunWith(AndroidJUnit4.class)
17+
public class MultiRecordFetchResponseHandlerUnitTest {
18+
static Context instrumentationContext;
19+
static Container instrumentationContainer;
20+
static Database instrumentationPublicDatabase;
21+
22+
@BeforeClass
23+
public static void setUpClass() throws Exception {
24+
instrumentationContext = InstrumentationRegistry.getContext().getApplicationContext();
25+
instrumentationContainer = new Container(instrumentationContext, Configuration.testConfiguration());
26+
instrumentationPublicDatabase = Database.Factory.publicDatabase(instrumentationContainer);
27+
}
28+
29+
@AfterClass
30+
public static void tearDownClass() throws Exception {
31+
instrumentationContext = null;
32+
instrumentationContainer = null;
33+
instrumentationPublicDatabase = null;
34+
}
35+
36+
@Test
37+
public void testMultiRecordFetchResponseHandlerNormalFlow() throws Exception {
38+
JSONObject jsonObject1 = new JSONObject();
39+
jsonObject1.put("_id", "Note/48092492-0791-4120-B314-022202AD3970");
40+
jsonObject1.put("_created_at", "2016-06-15T07:55:32.342Z");
41+
jsonObject1.put("_created_by", "5a497b0b-cf93-4720-bea4-14637478cfc0");
42+
jsonObject1.put("_ownerID", "5a497b0b-cf93-4720-bea4-14637478cfc1");
43+
jsonObject1.put("_updated_at", "2016-06-15T07:55:33.342Z");
44+
jsonObject1.put("_updated_by", "5a497b0b-cf93-4720-bea4-14637478cfc2");
45+
jsonObject1.put("_type", "record");
46+
jsonObject1.put("_access", null);
47+
jsonObject1.put("hello", "world1");
48+
jsonObject1.put("foobar", 3);
49+
jsonObject1.put("abc", 12.345);
50+
51+
JSONObject jsonObject2 = new JSONObject();
52+
jsonObject2.put("_id", "Note/48092492-0791-4120-B314-022202AD3971");
53+
jsonObject2.put("_created_at", "2016-06-15T07:55:32.342Z");
54+
jsonObject2.put("_created_by", "5a497b0b-cf93-4720-bea4-14637478cfc0");
55+
jsonObject2.put("_ownerID", "5a497b0b-cf93-4720-bea4-14637478cfc1");
56+
jsonObject2.put("_updated_at", "2016-06-15T07:55:33.342Z");
57+
jsonObject2.put("_updated_by", "5a497b0b-cf93-4720-bea4-14637478cfc2");
58+
jsonObject2.put("_type", "record");
59+
jsonObject2.put("_access", null);
60+
jsonObject2.put("hello", "world2");
61+
jsonObject2.put("foobar", 4);
62+
jsonObject2.put("abc", 22.345);
63+
64+
JSONArray result = new JSONArray();
65+
result.put(jsonObject1);
66+
result.put(jsonObject2);
67+
68+
JSONObject responseObject = new JSONObject();
69+
responseObject.put("result", result);
70+
71+
final boolean[] checkpoints = new boolean[]{ false };
72+
RecordFetchRequest request = new RecordFetchRequest(
73+
"Note",
74+
new String[]{
75+
"48092492-0791-4120-B314-022202AD3971",
76+
"48092492-0791-4120-B314-022202AD3970"
77+
},
78+
instrumentationPublicDatabase
79+
);
80+
request.setResponseHandler(new MultiRecordFetchResponseHandler() {
81+
@Override
82+
public void onFetchSuccess(RecordResult<Record>[] result) {
83+
assertEquals(2, result.length);
84+
85+
assertFalse(result[0].isError());
86+
assertFalse(result[1].isError());
87+
88+
Record record1 = result[0].value;
89+
assertEquals("world2", record1.get("hello"));
90+
assertEquals(4, record1.get("foobar"));
91+
assertEquals(22.345, record1.get("abc"));
92+
93+
Record record2 = result[1].value;
94+
assertEquals("world1", record2.get("hello"));
95+
assertEquals(3, record2.get("foobar"));
96+
assertEquals(12.345, record2.get("abc"));
97+
98+
checkpoints[0] = true;
99+
}
100+
101+
@Override
102+
public void onFetchError(Error error) {
103+
fail("Should not get error callback");
104+
}
105+
});
106+
107+
request.getResponseHandler().onSuccess(responseObject);
108+
assertTrue(checkpoints[0]);
109+
}
110+
111+
@Test
112+
public void testMultiRecordFetchResponseHandlerSomeMissingFlow() throws Exception {
113+
JSONObject jsonObject1 = new JSONObject();
114+
jsonObject1.put("_id", "Note/48092492-0791-4120-B314-022202AD3970");
115+
jsonObject1.put("_created_at", "2016-06-15T07:55:32.342Z");
116+
jsonObject1.put("_created_by", "5a497b0b-cf93-4720-bea4-14637478cfc0");
117+
jsonObject1.put("_ownerID", "5a497b0b-cf93-4720-bea4-14637478cfc1");
118+
jsonObject1.put("_updated_at", "2016-06-15T07:55:33.342Z");
119+
jsonObject1.put("_updated_by", "5a497b0b-cf93-4720-bea4-14637478cfc2");
120+
jsonObject1.put("_type", "record");
121+
jsonObject1.put("_access", null);
122+
jsonObject1.put("hello", "world1");
123+
jsonObject1.put("foobar", 3);
124+
jsonObject1.put("abc", 12.345);
125+
126+
JSONObject jsonObject2 = new JSONObject();
127+
jsonObject2.put("_id", "Note/48092492-0791-4120-B314-022202AD3971");
128+
jsonObject2.put("_created_at", "2016-06-15T07:55:32.342Z");
129+
jsonObject2.put("_created_by", "5a497b0b-cf93-4720-bea4-14637478cfc0");
130+
jsonObject2.put("_ownerID", "5a497b0b-cf93-4720-bea4-14637478cfc1");
131+
jsonObject2.put("_updated_at", "2016-06-15T07:55:33.342Z");
132+
jsonObject2.put("_updated_by", "5a497b0b-cf93-4720-bea4-14637478cfc2");
133+
jsonObject2.put("_type", "record");
134+
jsonObject2.put("_access", null);
135+
jsonObject2.put("hello", "world2");
136+
jsonObject2.put("foobar", 4);
137+
jsonObject2.put("abc", 22.345);
138+
139+
JSONArray result = new JSONArray();
140+
result.put(jsonObject1);
141+
result.put(jsonObject2);
142+
143+
JSONObject responseObject = new JSONObject();
144+
responseObject.put("result", result);
145+
146+
final boolean[] checkpoints = new boolean[]{ false };
147+
RecordFetchRequest request = new RecordFetchRequest(
148+
"Note",
149+
new String[]{
150+
"48092492-0791-4120-B314-022202AD3971",
151+
"missing-id",
152+
"48092492-0791-4120-B314-022202AD3970"
153+
},
154+
instrumentationPublicDatabase
155+
);
156+
request.setResponseHandler(new MultiRecordFetchResponseHandler() {
157+
@Override
158+
public void onFetchSuccess(RecordResult<Record>[] result) {
159+
assertEquals(3, result.length);
160+
161+
assertFalse(result[0].isError());
162+
assertTrue(result[1].isError());
163+
assertFalse(result[2].isError());
164+
165+
Record record1 = result[0].value;
166+
assertEquals("world2", record1.get("hello"));
167+
assertEquals(4, record1.get("foobar"));
168+
assertEquals(22.345, record1.get("abc"));
169+
170+
Record record3 = result[2].value;
171+
assertEquals("world1", record3.get("hello"));
172+
assertEquals(3, record3.get("foobar"));
173+
assertEquals(12.345, record3.get("abc"));
174+
175+
Error error2 = result[1].error;
176+
assertEquals(Error.Code.RESOURCE_NOT_FOUND, error2.getCode());
177+
assertEquals(
178+
"Cannot find Note record with ID missing-id",
179+
error2.getDetailMessage()
180+
);
181+
182+
checkpoints[0] = true;
183+
}
184+
185+
@Override
186+
public void onFetchError(Error error) {
187+
fail("Should not get error callback");
188+
}
189+
});
190+
191+
request.getResponseHandler().onSuccess(responseObject);
192+
assertTrue(checkpoints[0]);
193+
}
194+
195+
@Test
196+
public void testMultiRecordFetchResponseHandlerOperationErrorFlow() throws Exception {
197+
final boolean[] checkpoints = new boolean[]{ false };
198+
RecordFetchRequest request = new RecordFetchRequest(
199+
"Note",
200+
new String[]{
201+
"48092492-0791-4120-B314-022202AD3970",
202+
"48092492-0791-4120-B314-022202AD3971"
203+
},
204+
instrumentationPublicDatabase
205+
);
206+
request.setResponseHandler(new MultiRecordFetchResponseHandler() {
207+
@Override
208+
public void onFetchSuccess(RecordResult<Record>[] result) {
209+
fail("Should not get success callback");
210+
}
211+
212+
@Override
213+
public void onFetchError(Error error) {
214+
assertEquals("Some Error", error.getDetailMessage());
215+
checkpoints[0] = true;
216+
}
217+
});
218+
219+
request.getResponseHandler().onFailure(new Error("Some Error"));
220+
assertTrue(checkpoints[0]);
221+
}
222+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.skygear.skygear;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
import org.junit.AfterClass;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import java.util.Map;
15+
16+
import static junit.framework.Assert.assertEquals;
17+
18+
@RunWith(AndroidJUnit4.class)
19+
public class RecordFetchRequestUnitTest {
20+
static Context instrumentationContext;
21+
static Container instrumentationContainer;
22+
static Database instrumentationPublicDatabase;
23+
24+
@BeforeClass
25+
public static void setUpClass() throws Exception {
26+
instrumentationContext = InstrumentationRegistry.getContext().getApplicationContext();
27+
instrumentationContainer = new Container(instrumentationContext, Configuration.testConfiguration());
28+
instrumentationPublicDatabase = Database.Factory.publicDatabase(instrumentationContainer);
29+
}
30+
31+
@AfterClass
32+
public static void tearDownClass() throws Exception {
33+
instrumentationContext = null;
34+
instrumentationContainer = null;
35+
instrumentationPublicDatabase = null;
36+
}
37+
38+
@Test
39+
public void testRecordFetchRequestNormalFlow() throws Exception {
40+
RecordFetchRequest request = new RecordFetchRequest(
41+
"Note",
42+
new String[]{"1", "2"},
43+
instrumentationPublicDatabase
44+
);
45+
46+
String[] fetchingRecordIDs = request.fetchingRecordIDs;
47+
assertEquals(2, fetchingRecordIDs.length);
48+
assertEquals("1", fetchingRecordIDs[0]);
49+
assertEquals("2", fetchingRecordIDs[1]);
50+
51+
Map<String, Object> data = request.data;
52+
53+
assertEquals("_public", data.get("database_id"));
54+
assertEquals("Note", data.get("record_type"));
55+
56+
JSONArray predicate = (JSONArray) data.get("predicate");
57+
assertEquals("in", predicate.getString(0));
58+
59+
JSONObject predicateKeypath = predicate.getJSONObject(1);
60+
assertEquals("keypath", predicateKeypath.getString("$type"));
61+
assertEquals("_id", predicateKeypath.getString("$val"));
62+
63+
JSONArray predicateLookups = predicate.getJSONArray(2);
64+
65+
assertEquals("1", predicateLookups.getString(0));
66+
assertEquals("2", predicateLookups.getString(1));
67+
}
68+
69+
@Test
70+
public void testRecordFetchRequestSingleIDFlow() throws Exception {
71+
RecordFetchRequest request = new RecordFetchRequest(
72+
"Note",
73+
"123",
74+
instrumentationPublicDatabase
75+
);
76+
77+
String[] fetchingRecordIDs = request.fetchingRecordIDs;
78+
assertEquals(1, fetchingRecordIDs.length);
79+
assertEquals("123", fetchingRecordIDs[0]);
80+
81+
Map<String, Object> data = request.data;
82+
83+
assertEquals("_public", data.get("database_id"));
84+
assertEquals("Note", data.get("record_type"));
85+
86+
JSONArray predicate = (JSONArray) data.get("predicate");
87+
assertEquals("in", predicate.getString(0));
88+
89+
JSONObject predicateKeypath = predicate.getJSONObject(1);
90+
assertEquals("keypath", predicateKeypath.getString("$type"));
91+
assertEquals("_id", predicateKeypath.getString("$val"));
92+
93+
JSONArray predicateLookups = predicate.getJSONArray(2);
94+
95+
assertEquals("123", predicateLookups.getString(0));
96+
}
97+
}

0 commit comments

Comments
 (0)