Skip to content

Commit 636e6a0

Browse files
authored
Merge pull request #54 from contentstack/feat/DX-1222-Fetch-asset-by-query
Feat/dx 1222 fetch asset by query
2 parents 94fc027 + 34c1823 commit 636e6a0

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Contentstack/AssetLibrary.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ typedef NS_ENUM(NSUInteger, OrderBy) {
230230
*/
231231

232232
- (void)fetchAll:(void (^) (ResponseType type,NSArray<Asset *> * BUILT_NULLABLE_P result,NSError * BUILT_NULLABLE_P error))completionBlock;
233+
234+
/**
235+
This method fetches assets using other fields than UID..
236+
237+
//Obj-C
238+
[assetLib where:(NSString *)field equalTo:(NSObject *)value];
239+
240+
//Swift
241+
assetLib.where("fieldName","value");
242+
243+
This allows filtering assets by specifying the field name and the value to match.
244+
@param field The name of the field to filter by.
245+
@param value The value that the field should match.
246+
*/
247+
- (void)where:(NSString *)field equalTo:(NSObject *)value ;
248+
249+
- (NSDictionary*)getPostParamDictionary;
250+
233251
@end
234252

235253
BUILT_ASSUME_NONNULL_END

Contentstack/AssetLibrary.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ - (void)cancelRequest {
156156
[self.requestOperation cancel];
157157
}
158158
}
159+
// MARK: - Where Query -
160+
- (NSDictionary*) getPostParamDictionary {
161+
return [self.postParamDictionary copy];
162+
}
163+
164+
- (void)where:(NSString *)field equalTo:(NSObject *)value {
165+
if (field.length == 0 || !value) {
166+
NSLog(@"Field or value cannot be empty");
167+
return;
168+
}
169+
NSMutableDictionary *queryDict = [NSMutableDictionary dictionary];
170+
NSDictionary *existingQuery = self.postParamDictionary[@"query"];
171+
// If an existing query exists, merge it
172+
if (existingQuery) {
173+
[queryDict addEntriesFromDictionary:existingQuery];
174+
}
175+
queryDict[field] = value;
176+
[self.postParamDictionary setObject:queryDict forKey:@"query"];
177+
}
159178

160179

161180
@end

ContentstackTest/ContentstackTest.m

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,88 @@ - (void)test04FetchAssets {
258258
[self waitForRequest];
259259
}
260260

261+
- (void)testFetchAssetByQuery01{
262+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By where method"];
263+
AssetLibrary* assets = [csStack assetLibrary];
264+
[assets where:@"title" equalTo:@"image1"];
265+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
266+
if (error) {
267+
XCTFail(@"~ ERR: %@, Message = %@", error.userInfo, error.description);
268+
} else {
269+
XCTAssert(type == NETWORK, @"Pass");
270+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
271+
XCTAssert(result.count > 0, @"Expected results, but got none.");
272+
}
273+
[expectation fulfill];
274+
}];
275+
[self waitForRequest];
276+
}
277+
278+
- (void)testFetchAssetsByValidFileSize02 {
279+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By valid file size"];
280+
AssetLibrary *assets = [csStack assetLibrary];
281+
[assets where:@"file_size" equalTo:@(53986)]; // Valid file size
282+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
283+
XCTAssert(type == NETWORK, @"Pass");
284+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
285+
XCTAssert(result.count > 0, @"Expected results, but got none.");
286+
[expectation fulfill];
287+
}];
288+
[self waitForRequest];
289+
}
290+
291+
- (void)testFetchAssetsByNonExistentFileSize03 {
292+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By non-existent file size"];
293+
AssetLibrary *assets = [csStack assetLibrary];
294+
[assets where:@"file_size" equalTo:@(9999999)]; // Non-existent file size
295+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
296+
XCTAssert(type == NETWORK, @"Pass");
297+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
298+
XCTAssertEqual(result.count, 0, @"Expected no results, but got some.");
299+
[expectation fulfill];
300+
}];
301+
[self waitForRequest];
302+
}
303+
304+
- (void)testFetchAssetsByNonExistentTitle04 {
305+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By non-existent title"];
306+
AssetLibrary *assets = [csStack assetLibrary];
307+
[assets where:@"title" equalTo:@"non-existent-title.png"]; // Non-existent title
308+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
309+
XCTAssert(type == NETWORK, @"Pass");
310+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
311+
XCTAssertEqual(result.count, 0, @"Expected no results, but got some.");
312+
[expectation fulfill];
313+
}];
314+
[self waitForRequest];
315+
}
316+
317+
- (void)testFetchAssetsByMultipleConditions05 {
318+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By multiple conditions"];
319+
AssetLibrary *assets = [csStack assetLibrary];
320+
[assets where:@"file_size" equalTo:@(6884)]; // Valid file size
321+
[assets where:@"title" equalTo:@"image4"]; // Valid title
322+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
323+
XCTAssert(type == NETWORK, @"Pass");
324+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
325+
XCTAssert(result.count > 0, @"Expected results, but got none.");
326+
[expectation fulfill];
327+
}];
328+
[self waitForRequest];
329+
}
330+
331+
- (void)testFetchAssetsByInvalidFieldName06 {
332+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By invalid field name"];
333+
AssetLibrary *assets = [csStack assetLibrary];
334+
[assets where:@"invalid_field" equalTo:@"value"]; // Invalid field name
335+
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
336+
XCTAssert(type == NETWORK, @"Pass");
337+
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
338+
XCTAssertEqual(result.count, 0, @"Expected no results.");
339+
[expectation fulfill];
340+
}];
341+
[self waitForRequest];
342+
}
261343

262344
- (void)testGetHeader {
263345
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Set Header"];

0 commit comments

Comments
 (0)