Skip to content

Commit 2bc632e

Browse files
Add comprehensive unit tests for Query class, enhancing coverage with additional edge cases and validation scenarios.
1 parent d51c425 commit 2bc632e

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed

src/test/java/com/contentstack/sdk/TestQuery.java

Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,4 +1089,329 @@ void testNotExistsWithNewQueryValue() {
10891089
Query result = query.notExists("optional_field");
10901090
assertNotNull(result);
10911091
}
1092+
1093+
// ========== ADDITIONAL BRANCH COVERAGE TESTS ==========
1094+
1095+
@Test
1096+
void testLessThanOrEqualToWithNonEmptyQueryValue() {
1097+
query.queryValue = new JSONObject();
1098+
Query result = query.lessThanOrEqualTo("field", 100);
1099+
assertNotNull(result);
1100+
}
1101+
1102+
@Test
1103+
void testGreaterThanOrEqualToWithLengthCheck() {
1104+
query.queryValue = new JSONObject();
1105+
query.queryValue.put("test", "value");
1106+
Query result = query.greaterThanOrEqualTo("new_field", 50);
1107+
assertNotNull(result);
1108+
}
1109+
1110+
@Test
1111+
void testNotEqualToWithLengthCheck() {
1112+
query.queryValue = new JSONObject();
1113+
query.queryValue.put("test", "value");
1114+
Query result = query.notEqualTo("field", "value");
1115+
assertNotNull(result);
1116+
}
1117+
1118+
@Test
1119+
void testContainedInWithLengthCheck() {
1120+
query.queryValue = new JSONObject();
1121+
query.queryValue.put("test", "value");
1122+
Query result = query.containedIn("field", new Object[]{"val1", "val2"});
1123+
assertNotNull(result);
1124+
}
1125+
1126+
@Test
1127+
void testNotContainedInWithLengthCheck() {
1128+
query.queryValue = new JSONObject();
1129+
query.queryValue.put("test", "value");
1130+
Query result = query.notContainedIn("field", new Object[]{"val1", "val2"});
1131+
assertNotNull(result);
1132+
}
1133+
1134+
@Test
1135+
void testExistsWithLengthCheck() {
1136+
query.queryValue = new JSONObject();
1137+
query.queryValue.put("test", "value");
1138+
Query result = query.exists("field");
1139+
assertNotNull(result);
1140+
}
1141+
1142+
@Test
1143+
void testNotExistsWithLengthCheck() {
1144+
query.queryValue = new JSONObject();
1145+
query.queryValue.put("test", "value");
1146+
Query result = query.notExists("field");
1147+
assertNotNull(result);
1148+
}
1149+
1150+
@Test
1151+
void testRegexWithModifiersNewKey() {
1152+
query.queryValue = new JSONObject();
1153+
Query result = query.regex("field", "pattern", "i");
1154+
assertNotNull(result);
1155+
}
1156+
1157+
@Test
1158+
void testRegexWithModifiersExistingKeyNoModifiers() {
1159+
query.queryValueJSON.put("field", new JSONObject());
1160+
Query result = query.regex("field", "pattern", null);
1161+
assertNotNull(result);
1162+
}
1163+
1164+
@Test
1165+
void testRegexWithModifiersExistingKeyWithModifiers() {
1166+
query.queryValueJSON.put("field", new JSONObject());
1167+
Query result = query.regex("field", "pattern", "i");
1168+
assertNotNull(result);
1169+
}
1170+
1171+
// ========== MORE FIND/FINDONE EDGE CASES ==========
1172+
1173+
@Test
1174+
void testFindOneWithNullLimit() throws IllegalAccessException {
1175+
Stack stack = Contentstack.stack("api_key", "delivery_token", "env");
1176+
ContentType ct = stack.contentType("blog_post");
1177+
Query q = ct.query();
1178+
q.headers.put("environment", "production");
1179+
1180+
// Don't set limit, should handle -1 case
1181+
SingleQueryResultCallback callback = new SingleQueryResultCallback() {
1182+
@Override
1183+
public void onCompletion(ResponseType responseType, Entry entry, Error error) {
1184+
// Callback implementation
1185+
}
1186+
};
1187+
1188+
assertDoesNotThrow(() -> q.findOne(callback));
1189+
}
1190+
1191+
// ========== GET RESULT OBJECT WITH CALLBACKS ==========
1192+
1193+
@Test
1194+
void testGetResultObjectWithSingleEntryAndCallback() throws IllegalAccessException {
1195+
Stack stack = Contentstack.stack("api_key", "delivery_token", "env");
1196+
ContentType ct = stack.contentType("blog_post");
1197+
Query q = ct.query();
1198+
1199+
// Set callback
1200+
q.singleQueryResultCallback = new SingleQueryResultCallback() {
1201+
@Override
1202+
public void onCompletion(ResponseType responseType, Entry entry, Error error) {
1203+
// Callback implementation
1204+
}
1205+
};
1206+
1207+
// Create mock entry model
1208+
List<Object> objects = new ArrayList<>();
1209+
JSONObject entryJson = new JSONObject();
1210+
entryJson.put("uid", "entry_123");
1211+
entryJson.put("title", "Test Entry");
1212+
entryJson.put("tags", new JSONArray());
1213+
1214+
EntryModel model = new EntryModel(entryJson);
1215+
objects.add(model);
1216+
1217+
JSONObject resultJson = new JSONObject();
1218+
1219+
q.getResultObject(objects, resultJson, true);
1220+
assertNotNull(q);
1221+
}
1222+
1223+
@Test
1224+
void testGetResultObjectWithMultipleEntriesAndCallback() throws IllegalAccessException {
1225+
Stack stack = Contentstack.stack("api_key", "delivery_token", "env");
1226+
ContentType ct = stack.contentType("blog_post");
1227+
Query q = ct.query();
1228+
1229+
// Set callback
1230+
q.queryResultCallback = new QueryResultsCallBack() {
1231+
@Override
1232+
public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
1233+
// Callback implementation
1234+
}
1235+
};
1236+
1237+
// Create mock entry models
1238+
List<Object> objects = new ArrayList<>();
1239+
1240+
JSONObject entry1Json = new JSONObject();
1241+
entry1Json.put("uid", "entry_1");
1242+
entry1Json.put("title", "Entry 1");
1243+
entry1Json.put("tags", new JSONArray());
1244+
EntryModel model1 = new EntryModel(entry1Json);
1245+
objects.add(model1);
1246+
1247+
JSONObject entry2Json = new JSONObject();
1248+
entry2Json.put("uid", "entry_2");
1249+
entry2Json.put("title", "Entry 2");
1250+
entry2Json.put("tags", new JSONArray());
1251+
EntryModel model2 = new EntryModel(entry2Json);
1252+
objects.add(model2);
1253+
1254+
JSONObject resultJson = new JSONObject();
1255+
1256+
q.getResultObject(objects, resultJson, false);
1257+
assertNotNull(q);
1258+
}
1259+
1260+
// ========== QUERY VALUE MANIPULATION TESTS ==========
1261+
1262+
@Test
1263+
void testLessThanWithNonEmptyQueryValue() {
1264+
query.queryValue = new JSONObject();
1265+
Query result = query.lessThan("field", 100);
1266+
assertNotNull(result);
1267+
}
1268+
1269+
@Test
1270+
void testGreaterThanWithNonEmptyQueryValue() {
1271+
query.queryValue = new JSONObject();
1272+
Query result = query.greaterThan("field", 50);
1273+
assertNotNull(result);
1274+
}
1275+
1276+
@Test
1277+
void testRegexWithNonEmptyQueryValue() {
1278+
query.queryValue = new JSONObject();
1279+
Query result = query.regex("field", "pattern");
1280+
assertNotNull(result);
1281+
}
1282+
1283+
@Test
1284+
void testRegexWithModifiersAndLengthCheck() {
1285+
query.queryValue = new JSONObject();
1286+
query.queryValue.put("test", "value");
1287+
Query result = query.regex("field", "pattern", "i");
1288+
assertNotNull(result);
1289+
}
1290+
1291+
// ========== OR METHOD TESTS ==========
1292+
1293+
@Test
1294+
void testOrWithEmptyQueryObjects() {
1295+
List<Query> queries = new ArrayList<>();
1296+
Query result = query.or(queries);
1297+
assertNotNull(result);
1298+
}
1299+
1300+
@Test
1301+
void testOrWithMultipleQueries() {
1302+
List<Query> queries = new ArrayList<>();
1303+
1304+
Query q1 = new Query("test_ct");
1305+
q1.where("field1", "value1");
1306+
queries.add(q1);
1307+
1308+
Query q2 = new Query("test_ct");
1309+
q2.where("field2", "value2");
1310+
queries.add(q2);
1311+
1312+
Query result = query.or(queries);
1313+
assertNotNull(result);
1314+
}
1315+
1316+
// ========== VALIDATION TESTS ==========
1317+
1318+
@Test
1319+
void testWhereWithValidKeyAndValue() {
1320+
Query result = query.where("valid_key", "valid_value");
1321+
assertNotNull(result);
1322+
assertTrue(query.queryValueJSON.has("valid_key"));
1323+
}
1324+
1325+
@Test
1326+
void testLessThanWithValidKeyAndValue() {
1327+
Query result = query.lessThan("price", 100);
1328+
assertNotNull(result);
1329+
}
1330+
1331+
@Test
1332+
void testLessThanOrEqualToWithValidKeyAndValue() {
1333+
Query result = query.lessThanOrEqualTo("price", 100);
1334+
assertNotNull(result);
1335+
}
1336+
1337+
@Test
1338+
void testGreaterThanWithValidKeyAndValue() {
1339+
Query result = query.greaterThan("rating", 4.5);
1340+
assertNotNull(result);
1341+
}
1342+
1343+
@Test
1344+
void testGreaterThanOrEqualToWithValidKeyAndValue() {
1345+
Query result = query.greaterThanOrEqualTo("rating", 4.0);
1346+
assertNotNull(result);
1347+
}
1348+
1349+
@Test
1350+
void testNotEqualToWithValidKeyAndValue() {
1351+
Query result = query.notEqualTo("status", "draft");
1352+
assertNotNull(result);
1353+
}
1354+
1355+
@Test
1356+
void testContainedInWithValidKeyAndValues() {
1357+
Query result = query.containedIn("category", new Object[]{"tech", "science"});
1358+
assertNotNull(result);
1359+
}
1360+
1361+
@Test
1362+
void testNotContainedInWithValidKeyAndValues() {
1363+
Query result = query.notContainedIn("status", new Object[]{"archived", "deleted"});
1364+
assertNotNull(result);
1365+
}
1366+
1367+
@Test
1368+
void testExistsWithValidKey() {
1369+
Query result = query.exists("optional_field");
1370+
assertNotNull(result);
1371+
}
1372+
1373+
@Test
1374+
void testNotExistsWithValidKey() {
1375+
Query result = query.notExists("deprecated_field");
1376+
assertNotNull(result);
1377+
}
1378+
1379+
@Test
1380+
void testRegexWithValidKeyAndPattern() {
1381+
Query result = query.regex("email", "pattern");
1382+
assertNotNull(result);
1383+
}
1384+
1385+
@Test
1386+
void testRegexWithModifiersValidKeyAndPattern() {
1387+
Query result = query.regex("name", "pattern", "i");
1388+
assertNotNull(result);
1389+
}
1390+
1391+
// ========== COMPLEX QUERY COMBINATIONS ==========
1392+
1393+
@Test
1394+
void testComplexQueryWithMultipleConditions() {
1395+
query.where("type", "article")
1396+
.greaterThanOrEqualTo("rating", 4.0)
1397+
.lessThanOrEqualTo("price", 100)
1398+
.notEqualTo("status", "draft")
1399+
.containedIn("category", new Object[]{"tech", "science"})
1400+
.notContainedIn("tags", new Object[]{"deprecated"})
1401+
.exists("author")
1402+
.notExists("deleted_at")
1403+
.regex("title", "How");
1404+
1405+
assertNotNull(query.queryValueJSON);
1406+
assertTrue(query.queryValueJSON.length() > 0);
1407+
}
1408+
1409+
@Test
1410+
void testQueryWithMultipleOperatorsOnSameField() {
1411+
query.greaterThan("price", 10)
1412+
.lessThan("price", 100)
1413+
.notEqualTo("price", 50);
1414+
1415+
assertNotNull(query.queryValueJSON);
1416+
}
10921417
}

0 commit comments

Comments
 (0)