33using Contentstack . Core . Models ;
44using System . Threading . Tasks ;
55using System . Collections . Generic ;
6+ using Newtonsoft . Json . Linq ;
67
78namespace Contentstack . Core . Tests
89{
@@ -79,5 +80,130 @@ public async Task GetContentTypesIncludeGlobalFields()
7980
8081 }
8182 }
83+
84+ [ Fact ]
85+ public async Task FetchGlobalFieldSchema ( )
86+ {
87+ string globalFieldUid = "global_field_uid" ;
88+ GlobalField globalField = client . GlobalField ( globalFieldUid ) ;
89+
90+ var result = await globalField . Fetch ( ) ;
91+ Assert . NotNull ( result ) ;
92+ Assert . True ( result . HasValues , "GlobalField.Fetch() did not return expected schema." ) ;
93+ }
94+
95+ [ Fact ]
96+ public async Task FetchGlobalFieldSchema_InvalidUid_ThrowsOrReturnsNull ( )
97+ {
98+ string invalidUid = "invalid_uid" ;
99+ GlobalField globalField = client . GlobalField ( invalidUid ) ;
100+ await Assert . ThrowsAnyAsync < Exception > ( async ( ) => await globalField . Fetch ( ) ) ;
101+ }
102+
103+ [ Fact ]
104+ public async Task FetchGlobalFieldSchema_WithParameters_ReturnsSchema ( )
105+ {
106+ string globalFieldUid = "global_field_uid" ;
107+ GlobalField globalField = client . GlobalField ( globalFieldUid ) ;
108+ var param = new Dictionary < string , object > { { "include_global_field_schema" , true } } ;
109+ var result = await globalField . Fetch ( param ) ;
110+ Assert . NotNull ( result ) ;
111+ Assert . True ( result . HasValues , "GlobalField.Fetch() with params did not return expected schema." ) ;
112+ }
113+
114+ [ Fact ]
115+ public void SetAndRemoveHeader_WorksCorrectly ( )
116+ {
117+ string globalFieldUid = "global_field_uid" ;
118+ GlobalField globalField = client . GlobalField ( globalFieldUid ) ;
119+ globalField . SetHeader ( "custom_key" , "custom_value" ) ;
120+ globalField . RemoveHeader ( "custom_key" ) ;
121+ Assert . True ( true ) ;
122+ }
123+
124+ [ Fact ]
125+ public async Task FetchGlobalFieldSchema_WithCustomHeader ( )
126+ {
127+ string globalFieldUid = "global_field_uid" ;
128+ GlobalField globalField = client . GlobalField ( globalFieldUid ) ;
129+ globalField . SetHeader ( "custom_key" , "custom_value" ) ;
130+ var result = await globalField . Fetch ( ) ;
131+ Assert . NotNull ( result ) ;
132+ }
133+
134+ [ Fact ]
135+ public async Task FetchGlobalFieldSchema_NullParameters_Succeeds ( )
136+ {
137+ string globalFieldUid = "global_field_uid" ;
138+ GlobalField globalField = client . GlobalField ( globalFieldUid ) ;
139+ var result = await globalField . Fetch ( null ) ;
140+ Assert . NotNull ( result ) ;
141+ }
142+
143+ [ Fact ]
144+ public void GlobalField_EmptyUid_Throws ( )
145+ {
146+ Assert . Throws < ArgumentNullException > ( ( ) => {
147+ GlobalField globalField = client . GlobalField ( "" ) ;
148+ } ) ;
149+ }
150+
151+ [ Fact ]
152+ public async Task GlobalFieldQuery_Find_ReturnsArray ( )
153+ {
154+ var query = client . GlobalFieldQuery ( ) ;
155+ var result = await query . Find ( ) ;
156+
157+ Assert . NotNull ( result ) ;
158+ }
159+
160+ [ Fact ]
161+ public async Task GlobalFieldQuery_Find_WithParameters_ReturnsArray ( )
162+ {
163+ var query = client . GlobalFieldQuery ( ) ;
164+ var param = new Dictionary < string , object > { { "include_global_field_schema" , true } } ;
165+ var result = await query . Find ( param ) ;
166+ Assert . NotNull ( result ) ;
167+ }
168+
169+ [ Fact ]
170+ public async Task GlobalFieldQuery_Find_WithSkipAndLimit_ReturnsArray ( )
171+ {
172+ var query = client . GlobalFieldQuery ( ) ;
173+ var param = new Dictionary < string , object > { { "skip" , 1 } , { "limit" , 2 } } ;
174+ var result = await query . Find ( param ) ;
175+ Assert . Empty ( result [ "global_fields" ] ) ;
176+ }
177+
178+ [ Fact ]
179+ public void GlobalFieldQuery_IncludeBranch_SetsQueryParam ( )
180+ {
181+ var query = client . GlobalFieldQuery ( ) ;
182+ var result = query . IncludeBranch ( ) ;
183+ Assert . NotNull ( result ) ;
184+ Assert . Equal ( query , result ) ;
185+ }
186+
187+ [ Fact ]
188+ public void GlobalFieldQuery_IncludeGlobalFieldSchema_SetsQueryParam ( )
189+ {
190+ var query = client . GlobalFieldQuery ( ) ;
191+ var result = query . IncludeGlobalFieldSchema ( ) ;
192+ Assert . NotNull ( result ) ;
193+ }
194+
195+ [ Fact ]
196+ public async Task GlobalFieldQuery_Find_InvalidParams_ThrowsOrReturnsEmpty ( )
197+ {
198+ var query = client . GlobalFieldQuery ( ) ;
199+ var invalidParams = new Dictionary < string , object > { { "invalid_param" , true } } ;
200+
201+ var result = await query . Find ( invalidParams ) ;
202+
203+ Assert . NotNull ( result ) ;
204+ Assert . IsType < JObject > ( result ) ;
205+ var globalFields = result [ "global_fields" ] as JArray ;
206+ Assert . NotNull ( globalFields ) ;
207+ }
82208 }
83209}
0 commit comments