Skip to content

Commit e1677a0

Browse files
committed
added unit tests for GlobalField functionality
1 parent 827d25d commit e1677a0

File tree

3 files changed

+183
-10
lines changed

3 files changed

+183
-10
lines changed

Contentstack/GlobalField.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#import "CSIOAPIURLs.h"
1414
#import "NSObject+Extensions.h"
1515
#import "Stack.h"
16-
#import "Query.h"
1716

1817
@interface GlobalField ()
1918
@property (nonatomic, strong, getter=stack) Stack *csStack;

ContentstackTest/ContentstackTest.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ -(void)testKVOEntryProperties {
403403
if (error) {
404404
XCTFail(@"~ ERR: %@, Message = %@", error.userInfo, error.description);
405405
}else {
406-
NSLog(@"entry : %@", _kvoEntry);
406+
// NSLog(@"entry : %@", _kvoEntry);
407407
}
408408
[expectation fulfill];
409409
}];
@@ -572,7 +572,7 @@ - (void)testFetchMarkDownString {
572572
}
573573

574574
}
575-
NSLog(@"MarkDown Values = %@", markdownString);
575+
// NSLog(@"MarkDown Values = %@", markdownString);
576576
}
577577
[expectation fulfill];
578578
}];
@@ -593,7 +593,7 @@ - (void)testFetchMarkDownArray {
593593
}else {
594594
[self checkLanguageStatus:entry];
595595

596-
NSLog(@"result %@", entry.locale);
596+
// NSLog(@"result %@", entry.locale);
597597

598598
NSArray *markdownArray = [entry HTMLArrayForMarkdownKey:@"markdown_multiple"];
599599
[markdownArray enumerateObjectsUsingBlock:^(NSString *markdownString, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -610,7 +610,7 @@ - (void)testFetchMarkDownArray {
610610
}
611611
}];
612612

613-
NSLog(@"MarkDown Array = %@, Total Objects = %lu",markdownArray, (unsigned long)[markdownArray count]);
613+
// NSLog(@"MarkDown Array = %@, Total Objects = %lu",markdownArray, (unsigned long)[markdownArray count]);
614614
}
615615
[expectation fulfill];
616616
}];
@@ -689,7 +689,7 @@ - (void)testDownloadAsset {
689689
[expectation fulfill];
690690

691691
}else{
692-
NSLog(@"%@",connectionError);
692+
// NSLog(@"%@",connectionError);
693693
[expectation fulfill];
694694
}
695695
}];
@@ -1076,7 +1076,7 @@ - (void)testFetchEntryEqualToField {
10761076
if (error) {
10771077
XCTFail(@"~ ERR: %@", error.userInfo);
10781078
}else {
1079-
NSLog(@"result %@", [result getResult]);
1079+
// NSLog(@"result %@", [result getResult]);
10801080
[self testProductCount:[result getResult]];
10811081

10821082
[[result getResult] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -1760,7 +1760,7 @@ - (void)testSearch {
17601760
if (error) {
17611761
XCTFail(@"~ ERR: %@", error.userInfo);
17621762
}else {
1763-
NSLog(@"result %@", [result getResult]);
1763+
// NSLog(@"result %@", [result getResult]);
17641764
[self testProductCount:[result getResult]];
17651765

17661766
[[result getResult] enumerateObjectsUsingBlock:^(Entry *entry, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -1806,7 +1806,7 @@ - (void)testMatchRgex {
18061806
if (error) {
18071807
XCTFail(@"~ ERR: %@", error.userInfo);
18081808
}else {
1809-
NSLog(@"result %@", [result getResult]);
1809+
// NSLog(@"result %@", [result getResult]);
18101810
// [self testProductCount:[result getResult]];
18111811

18121812
[[result getResult] enumerateObjectsUsingBlock:^(Entry *entry, NSUInteger idx, BOOL * _Nonnull stop) {
@@ -2367,7 +2367,7 @@ - (void)testaddParamForAsset {
23672367
}else {
23682368
if ([assetObj.url length] > 0) {
23692369
if ( [assetObj.properties objectForKey:@"dimension"]){
2370-
NSLog(@"%@",assetObj.properties);
2370+
// NSLog(@"%@",assetObj.properties);
23712371
XCTAssert(YES, @"Pass");
23722372
}else{
23732373
XCTFail(@"wrong asset object");

ContentstackTest/GlobalFieldTest.m

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//
2+
// GlobalFieldTest.m
3+
// Contentstack
4+
//
5+
// Created by Reeshika Hosmani on 02/06/25.
6+
// Copyright © 2025 Contentstack. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import <Contentstack/Contentstack.h>
11+
#import "CSIOInternalHeaders.h"
12+
#import "GlobalField.h"
13+
#import "ContentstackDefinitions.h"
14+
15+
@interface GlobalFieldTest : XCTestCase{
16+
Stack *csStack;
17+
Config *config;
18+
}
19+
20+
@property (nonatomic, strong) Stack *stack;
21+
@property (nonatomic, strong) GlobalField *globalField;
22+
23+
@end
24+
25+
@implementation GlobalFieldTest
26+
27+
- (void)setUp {
28+
[super setUp];
29+
NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"config" ofType:@"json"];
30+
NSData *data = [NSData dataWithContentsOfFile:path];
31+
NSDictionary *configdict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
32+
self.stack = [Contentstack stackWithAPIKey:configdict[@"api_key"]
33+
accessToken:configdict[@"delivery_token"]
34+
environmentName:configdict[@"environment"]];
35+
// Initialize global field
36+
self.globalField = [self.stack globalFieldWithName:@"global_field_uid"];
37+
}
38+
39+
- (void)tearDown {
40+
self.stack = nil;
41+
self.globalField = nil;
42+
[super tearDown];
43+
}
44+
45+
- (void)testFetchGlobalField {
46+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField"];
47+
48+
[self.globalField includeBranch];
49+
50+
// Call fetch method
51+
[self.globalField fetch:^(ResponseType type, NSError * _Nullable error) {
52+
if (error) {
53+
XCTFail(@"Error fetching global field: %@", error);
54+
}
55+
56+
// Verify no error
57+
XCTAssertNil(error, @"Error should be nil");
58+
59+
// Verify globalField properties
60+
XCTAssertNotNil(self.globalField.title, @"Title should not be nil");
61+
XCTAssertNotNil(self.globalField.uid, @"UID should not be nil");
62+
XCTAssertNotNil(self.globalField.description, @"Description should not be nil");
63+
64+
// Verify data types
65+
XCTAssertTrue([self.globalField.title isKindOfClass:[NSString class]], @"Title should be NSString");
66+
XCTAssertTrue([self.globalField.uid isKindOfClass:[NSString class]], @"UID should be NSString");
67+
XCTAssertTrue([self.globalField.description isKindOfClass:[NSString class]], @"Description should be NSString");
68+
69+
[expectation fulfill];
70+
}];
71+
72+
// Wait for expectation
73+
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
74+
if (error) {
75+
NSLog(@"Test timeout error: %@", error);
76+
XCTFail(@"Expectation failed with error: %@", error);
77+
}
78+
}];
79+
}
80+
81+
- (void)testFetchGlobalFieldWithInvalidName {
82+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Invalid GlobalField"];
83+
84+
// Create globalField with invalid name
85+
GlobalField *invalidGlobalField = [self.stack globalFieldWithName:@"invalid_global_field"];
86+
87+
// Call fetch method
88+
[invalidGlobalField fetch:^(ResponseType type, NSError * _Nullable error) {
89+
// Verify error is not nil
90+
XCTAssertNotNil(error, @"Error should not be nil");
91+
92+
[expectation fulfill];
93+
}];
94+
95+
// Wait for expectation
96+
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
97+
if (error) {
98+
XCTFail(@"Expectation failed with error: %@", error);
99+
}
100+
}];
101+
}
102+
103+
- (void)testFetchGlobalFieldWithIncludeBranch {
104+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField with Branch"];
105+
106+
// Include branch
107+
[self.globalField includeBranch];
108+
109+
// Call fetch method
110+
[self.globalField fetch:^(ResponseType type, NSError * _Nullable error) {
111+
// Verify no error
112+
XCTAssertNil(error, @"Error should be nil");
113+
114+
// Verify branch is included
115+
XCTAssertNotNil(self.globalField.branch, @"Branch should not be nil");
116+
117+
[expectation fulfill];
118+
}];
119+
120+
// Wait for expectation
121+
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
122+
if (error) {
123+
XCTFail(@"Expectation failed with error: %@", error);
124+
}
125+
}];
126+
}
127+
128+
- (void)testFetchGlobalFieldWithIncludeSchema {
129+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField with Schema"];
130+
131+
// Include schema
132+
[self.globalField includeGlobalFieldSchema];
133+
134+
// Call fetch method
135+
[self.globalField fetch:^(ResponseType type, NSError * _Nullable error) {
136+
// Verify no error
137+
XCTAssertNil(error, @"Error should be nil");
138+
139+
// Verify schema is included in properties
140+
XCTAssertNotNil(self.globalField.schema, @"Schema should not be nil");
141+
142+
[expectation fulfill];
143+
}];
144+
145+
// Wait for expectation
146+
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
147+
if (error) {
148+
XCTFail(@"Expectation failed with error: %@", error);
149+
}
150+
}];
151+
}
152+
153+
- (void)testFetchAllGlobalFields {
154+
XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch all GlobalFields"];
155+
156+
// Call fetch method
157+
[self.globalField fetchAll:^(ResponseType type, NSArray<GlobalField *> * _Nullable result, NSError * _Nullable error) {
158+
159+
// Verify no error
160+
XCTAssertNil(error, @"Error should be nil");
161+
XCTAssertNotNil(result, @"Result is not null");
162+
[expectation fulfill];
163+
164+
}];
165+
166+
// Wait for expectation
167+
[self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) {
168+
if (error) {
169+
XCTFail(@"Expectation failed with error: %@", error);
170+
}
171+
}];
172+
}
173+
174+
@end

0 commit comments

Comments
 (0)