Skip to content

Commit

Permalink
Merge pull request #54 from contentstack/feat/DX-1222-Fetch-asset-by-…
Browse files Browse the repository at this point in the history
…query

Feat/dx 1222 fetch asset by query
  • Loading branch information
reeshika-h authored Oct 3, 2024
2 parents 94fc027 + 34c1823 commit 636e6a0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Contentstack/AssetLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ typedef NS_ENUM(NSUInteger, OrderBy) {
*/

- (void)fetchAll:(void (^) (ResponseType type,NSArray<Asset *> * BUILT_NULLABLE_P result,NSError * BUILT_NULLABLE_P error))completionBlock;

/**
This method fetches assets using other fields than UID..
//Obj-C
[assetLib where:(NSString *)field equalTo:(NSObject *)value];
//Swift
assetLib.where("fieldName","value");
This allows filtering assets by specifying the field name and the value to match.
@param field The name of the field to filter by.
@param value The value that the field should match.
*/
- (void)where:(NSString *)field equalTo:(NSObject *)value ;

- (NSDictionary*)getPostParamDictionary;

@end

BUILT_ASSUME_NONNULL_END
19 changes: 19 additions & 0 deletions Contentstack/AssetLibrary.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ - (void)cancelRequest {
[self.requestOperation cancel];
}
}
// MARK: - Where Query -
- (NSDictionary*) getPostParamDictionary {
return [self.postParamDictionary copy];
}

- (void)where:(NSString *)field equalTo:(NSObject *)value {
if (field.length == 0 || !value) {
NSLog(@"Field or value cannot be empty");
return;
}
NSMutableDictionary *queryDict = [NSMutableDictionary dictionary];
NSDictionary *existingQuery = self.postParamDictionary[@"query"];
// If an existing query exists, merge it
if (existingQuery) {
[queryDict addEntriesFromDictionary:existingQuery];
}
queryDict[field] = value;
[self.postParamDictionary setObject:queryDict forKey:@"query"];
}


@end
82 changes: 82 additions & 0 deletions ContentstackTest/ContentstackTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,88 @@ - (void)test04FetchAssets {
[self waitForRequest];
}

- (void)testFetchAssetByQuery01{
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By where method"];
AssetLibrary* assets = [csStack assetLibrary];
[assets where:@"title" equalTo:@"image1"];
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
if (error) {
XCTFail(@"~ ERR: %@, Message = %@", error.userInfo, error.description);
} else {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssert(result.count > 0, @"Expected results, but got none.");
}
[expectation fulfill];
}];
[self waitForRequest];
}

- (void)testFetchAssetsByValidFileSize02 {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By valid file size"];
AssetLibrary *assets = [csStack assetLibrary];
[assets where:@"file_size" equalTo:@(53986)]; // Valid file size
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssert(result.count > 0, @"Expected results, but got none.");
[expectation fulfill];
}];
[self waitForRequest];
}

- (void)testFetchAssetsByNonExistentFileSize03 {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By non-existent file size"];
AssetLibrary *assets = [csStack assetLibrary];
[assets where:@"file_size" equalTo:@(9999999)]; // Non-existent file size
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssertEqual(result.count, 0, @"Expected no results, but got some.");
[expectation fulfill];
}];
[self waitForRequest];
}

- (void)testFetchAssetsByNonExistentTitle04 {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By non-existent title"];
AssetLibrary *assets = [csStack assetLibrary];
[assets where:@"title" equalTo:@"non-existent-title.png"]; // Non-existent title
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssertEqual(result.count, 0, @"Expected no results, but got some.");
[expectation fulfill];
}];
[self waitForRequest];
}

- (void)testFetchAssetsByMultipleConditions05 {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By multiple conditions"];
AssetLibrary *assets = [csStack assetLibrary];
[assets where:@"file_size" equalTo:@(6884)]; // Valid file size
[assets where:@"title" equalTo:@"image4"]; // Valid title
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssert(result.count > 0, @"Expected results, but got none.");
[expectation fulfill];
}];
[self waitForRequest];
}

- (void)testFetchAssetsByInvalidFieldName06 {
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Asset By invalid field name"];
AssetLibrary *assets = [csStack assetLibrary];
[assets where:@"invalid_field" equalTo:@"value"]; // Invalid field name
[assets fetchAll:^(ResponseType type, NSArray *result, NSError *error) {
XCTAssert(type == NETWORK, @"Pass");
XCTAssertNil(error, @"Expected no error, but got: %@", error.userInfo);
XCTAssertEqual(result.count, 0, @"Expected no results.");
[expectation fulfill];
}];
[self waitForRequest];
}

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

0 comments on commit 636e6a0

Please sign in to comment.