Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Version: 2.22.0
#### Date: March-03-2025

##### Feat:
- Added Support for Global Fields

### Version: 2.21.0
#### Date: March-03-2025

Expand Down
127 changes: 127 additions & 0 deletions Contentstack.Core.Tests/ContentTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Contentstack.Core.Models;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Contentstack.Core.Tests
{
Expand Down Expand Up @@ -79,5 +80,131 @@ public async Task GetContentTypesIncludeGlobalFields()

}
}

[Fact]
public async Task FetchGlobalFieldSchema()
{
string globalFieldUid = "global_field_uid";
GlobalField globalField = client.GlobalField(globalFieldUid);

var result = await globalField.Fetch();
Assert.NotNull(result);
Assert.True(result.HasValues, "GlobalField.Fetch() did not return expected schema.");
}

[Fact]
public async Task FetchGlobalFieldSchema_InvalidUid_ThrowsOrReturnsNull()
{
string invalidUid = "invalid_uid";
GlobalField globalField = client.GlobalField(invalidUid);
await Assert.ThrowsAnyAsync<Exception>(async () => await globalField.Fetch());
}

[Fact]
public async Task FetchGlobalFieldSchema_WithParameters_ReturnsSchema()
{
string globalFieldUid = "global_field_uid";
GlobalField globalField = client.GlobalField(globalFieldUid);
var param = new Dictionary<string, object> { { "include_global_field_schema", true } };
var result = await globalField.Fetch(param);
Assert.NotNull(result);
Assert.True(result.HasValues, "GlobalField.Fetch() with params did not return expected schema.");
}

[Fact]
public void SetAndRemoveHeader_WorksCorrectly()
{
string globalFieldUid = "global_field_uid";
GlobalField globalField = client.GlobalField(globalFieldUid);
globalField.SetHeader("custom_key", "custom_value");
globalField.RemoveHeader("custom_key");
Assert.True(true);
}

[Fact]
public async Task FetchGlobalFieldSchema_WithCustomHeader()
{
string globalFieldUid = "global_field_uid";
GlobalField globalField = client.GlobalField(globalFieldUid);
globalField.SetHeader("custom_key", "custom_value");
var result = await globalField.Fetch();
Assert.NotNull(result);
}

[Fact]
public async Task FetchGlobalFieldSchema_NullParameters_Succeeds()
{
string globalFieldUid = "global_field_uid";
GlobalField globalField = client.GlobalField(globalFieldUid);
var result = await globalField.Fetch(null);
Assert.NotNull(result);
}

[Fact]
public void GlobalField_EmptyUid_Throws()
{
Assert.Throws<ArgumentNullException>(() => {
GlobalField globalField = client.GlobalField("");
});
}

[Fact]
public async Task GlobalFieldQuery_Find_ReturnsArray()
{
var query = client.GlobalFieldQuery();
var result = await query.Find();

Assert.NotNull(result);
}

[Fact]
public async Task GlobalFieldQuery_Find_WithParameters_ReturnsArray()
{
var query = client.GlobalFieldQuery();
var param = new Dictionary<string, object> { { "include_global_field_schema", true } };
var result = await query.Find(param);
Assert.NotNull(result);
}

[Fact]
public async Task GlobalFieldQuery_Find_WithSkipAndLimit_ReturnsArray()
{
var query = client.GlobalFieldQuery();
var param = new Dictionary<string, object> { { "skip", 1 }, { "limit", 2 } };
var result = await query.Find(param);
Assert.Empty(result["global_fields"]);
}

[Fact]
public void GlobalFieldQuery_IncludeBranch_SetsQueryParam()
{
var query = client.GlobalFieldQuery();
var result = query.IncludeBranch();
Assert.NotNull(result);
Assert.Equal(query, result);
}

[Fact]
public void GlobalFieldQuery_IncludeGlobalFieldSchema_SetsQueryParam()
{
var query = client.GlobalFieldQuery();
var result = query.IncludeGlobalFieldSchema();
Assert.NotNull(result);
Assert.Equal(query, result);
Comment thread
cs-raj marked this conversation as resolved.
Outdated
}

[Fact]
public async Task GlobalFieldQuery_Find_InvalidParams_ThrowsOrReturnsEmpty()
{
var query = client.GlobalFieldQuery();
var invalidParams = new Dictionary<string, object> { { "invalid_param", true } };

var result = await query.Find(invalidParams);

Assert.NotNull(result);
Assert.IsType<JObject>(result);
var globalFields = result["global_fields"] as JArray;
Assert.NotNull(globalFields);
}
}
}
Loading
Loading