Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy & Variants Implementation with test cases #50

Merged
merged 16 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/nuget-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Push generated package to GitHub registry
run: |
cd out
dotnet nuget push "contentstack.csharp.*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --skip-duplicate --no-symbols true --source https://api.nuget.org/v3/index.json
dotnet nuget push "contentstack.csharp.*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --skip-duplicate --no-symbols --source https://api.nuget.org/v3/index.json

publish-git:
runs-on: windows-latest
Expand All @@ -41,4 +41,4 @@ jobs:
- name: Push generated package to GitHub registry
run: |
cd out
dotnet nuget push "contentstack.csharp.*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --skip-duplicate --no-symbols true --source https://api.nuget.org/v3/index.json
dotnet nuget push "contentstack.csharp.*.nupkg" --api-key ${{ secrets.NUGET_API_KEY }} --skip-duplicate --no-symbols --source https://api.nuget.org/v3/index.json
6 changes: 5 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ fileignoreconfig:
- filename: Contentstack.Core/Models/Asset.cs
checksum: 98b819cb9b1e6a9a9e5394ac23c07bc642a41c0c7512d169afc63afe3baa6fb3
- filename: Contentstack.Core/Models/Query.cs
checksum: 9237bb4d3e862fad7f3c6d9bad47873758a18617dc9c90d28015dcea267fcd9e
checksum: ceea632e4ea870f35ad3bd313e9f8b4e5ec21aa86f006fca2e0a32945999ba67
- filename: Contentstack.Core/Models/Taxonomy.cs
checksum: db8bcefdc7aafde4286e7fb6d67348bec49f1ac27b54d84fddca8124135bd779
- filename: .github/workflows/nuget-publish.yml
checksum: 53ba4ce874c4d2362ad00deb23f5a6ec219318860352f997b945e9161a580651
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
### Version: 2.16.0
#### Date: Sep-3-2024

##### New Feature:
- Added Early Access Header Support

### Version: 2.15.0
#### Date: Jul-30-2024

##### New Feature:
- Taxonomy class added

### Version: 2.14.0
#### Date: May-28-2024

Expand Down
47 changes: 46 additions & 1 deletion Contentstack.Core.Tests/EntryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,59 @@ public async Task FetchEntryByUIDPublishFallback()
list.Add("ja-jp");
ContentType contenttype = client.ContentType(source);
string uid = await GetUID("source1");
Entry sourceEntry = await contenttype.Entry(uid)
Entry sourceEntry = contenttype.Entry(uid);
await sourceEntry
.SetLocale("ja-jp")
.IncludeFallback()
.Fetch<Entry>();

Assert.Contains((string)(sourceEntry.Get("publish_details") as JObject).GetValue("locale"), list);
}

[Fact]
public async Task FetchEntryByVariant()
{
ContentType contenttype = client.ContentType(source);
string uid = await GetUID("source1");
Entry sourceEntry = contenttype.Entry(uid);
await sourceEntry
.Variant("variant1")
.Fetch<Entry>().ContinueWith((t) =>
{
Entry result = t.Result;
if (result == null)
{
Assert.False(true, "Entry.Fetch is not match with expected result.");
}
else
{
Assert.True(result.Uid == sourceEntry.Uid);
}
});
}

[Fact]
public async Task FetchEntryByVariants()
{
ContentType contenttype = client.ContentType(source);
string uid = await GetUID("source1");
Entry sourceEntry = contenttype.Entry(uid);
await sourceEntry
.Variant(new List<string> { "variant1", "variant2" })
.Fetch<Entry>().ContinueWith((t) =>
{
Entry result = t.Result;
if (result == null)
{
Assert.False(true, "Entry.Fetch is not match with expected result.");
}
else
{
Assert.True(result.Uid == sourceEntry.Uid);
}
});
}

[Fact]
public async Task FetchEntryByUIDPublishWithoutFallback()
{
Expand Down
178 changes: 178 additions & 0 deletions Contentstack.Core.Tests/TaxonomyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
using System;
using Xunit;
using Contentstack.Core;
using Contentstack.Core.Configuration;
using Contentstack.Core.Models;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Contentstack.Core.Tests.Models;
using Newtonsoft.Json.Linq;
using System.Reflection.PortableExecutable;

namespace Contentstack.Core.Tests
{

public class TaxonomyTest
{
ContentstackClient client = StackConfig.GetStack();

private String numbersContentType = "numbers_content_type";
String source = "source";

public double EPSILON { get; private set; }

[Fact]

public async Task TaxonomyExists()
{
// Description: Taxonomy Exists - Get Entries With Any Taxonomy Terms ($exists)
Taxonomy query = client.Taxonomies();
query.Exists("taxonomies.one");
var result = await query.Find<Entry>();
if (result == null && result.Items.Count() == 0)
{
Assert.Fail("Query.Exec is not match with expected result.");
}
else if (result != null)
{
bool IsTrue = false;
foreach (Entry data in result.Items)
{
IsTrue = data.GetContentType() != null;
if (!IsTrue)
{
break;
}
}
Assert.True(IsTrue);
}
else
{
Assert.Fail("Result doesn't mathced the count.");
}
}

[Fact]
public async Task TaxonomyEqualAndBelow()
{
// Description: Taxonomy EqualAndBelow - Get Entries With Taxonomy Terms and Also Matching Its Children Term ($eq_below, level)
Taxonomy query = client.Taxonomies();
query.EqualAndBelow("taxonomies.one", "term_one");
var result = await query.Find<Entry>();
if (result == null && result.Items.Count() == 0)
{
Assert.Fail("Query.Exec is not match with expected result.");
}
else if (result != null)
{
bool IsTrue = false;
foreach (Entry data in result.Items)
{
IsTrue = data.GetContentType() != null;
if (!IsTrue)
{
break;
}
}
Assert.True(IsTrue);
}
else
{
Assert.Fail("Result doesn't mathced the count.");
}
}

[Fact]
public async Task TaxonomyBelow()
{
// Description: Taxonomy Below - Get Entries With Taxonomy Terms Children\'s and Excluding the term itself ($below, level)
Taxonomy query = client.Taxonomies();
query.Below("taxonomies.one", "term_one");
var result = await query.Find<Entry>();
if (result == null && result.Items.Count() == 0)
{
Assert.Fail("Query.Exec is not match with expected result.");
}
else if (result != null)
{
bool IsTrue = false;
foreach (Entry data in result.Items)
{
IsTrue = data.GetContentType() != null;
if (!IsTrue)
{
break;
}
}
Assert.True(IsTrue);
}
else
{
Assert.Fail("Result doesn't mathced the count.");
}
}

[Fact]
public async Task TaxonomyEqualAndAbove()
{
// Description: Taxonomy EqualAndAbove - Get Entries With Taxonomy Terms and Also Matching Its Parent Term ($eq_above, level)
Taxonomy query = client.Taxonomies();
query.EqualAndAbove("taxonomies.one", "term_one");
var result = await query.Find<Entry>();
if (result == null && result.Items.Count() == 0)
{
Assert.Fail("Query.Exec is not match with expected result.");
}
else if (result != null)
{
bool IsTrue = false;
foreach (Entry data in result.Items)
{
IsTrue = data.GetContentType() != null;
if (!IsTrue)
{
break;
}
}
Assert.True(IsTrue);
}
else
{
Assert.Fail("Result doesn't mathced the count.");
}
}

[Fact]
public async Task TaxonomyAbove()
{
// Description: Taxonomy Above - Get Entries With Taxonomy Terms Parent and Excluding the term itself ($above, level)
Taxonomy query = client.Taxonomies();
query.Above("taxonomies.one", "term_one");
var result = await query.Find<Entry>();
if (result == null && result.Items.Count() == 0)
{
Assert.Fail("Query.Exec is not match with expected result.");
}
else if (result != null)
{
bool IsTrue = false;
foreach (Entry data in result.Items)
{
IsTrue = data.GetContentType() != null;
if (!IsTrue)
{
break;
}
}
Assert.True(IsTrue);
}
else
{
Assert.Fail("Result doesn't mathced the count.");
}
}

}
}

7 changes: 6 additions & 1 deletion Contentstack.Core/Configuration/ContentstackOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ public ContentstackOptions()
{
Timeout = 30000; // Set default value
}
}

/// <summary>
/// TheEarlyAccessHeader used to set service which the user has early access to.
/// </summary>
public string[] EarlyAccessHeader { get; set; }
}

internal class ContentstackRegionConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
Expand Down
20 changes: 20 additions & 0 deletions Contentstack.Core/ContentstackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public ContentstackClient(IOptions<ContentstackOptions> options)
{
this.SetHeader("access_token", _options.DeliveryToken);
}
if(_options.EarlyAccessHeader !=null)
{
this.SetHeader("x-header-ea", string.Join(",", _options.EarlyAccessHeader));
}
Config cnfig = new Config();
cnfig.Environment = _options.Environment;
if (_options.Host != null)
Expand Down Expand Up @@ -409,6 +413,22 @@ public AssetLibrary AssetLibrary()
return asset;
}

/// <summary>
/// Represents a Taxonomy. Creates Taxonomy Instance.
/// </summary>
/// <returns>Current instance of Taxonomy, this will be useful for a chaining calls.</returns>
/// <example>
/// <code>
/// ContentstackClient stack = new ContentstackClinet(&quot;api_key&quot;, &quot;delivery_token&quot;, &quot;environment&quot;);
/// Taxonomy taxonomy = stack.Taxonomy();
/// </code>
/// </example>
public Taxonomy Taxonomies()
{
Taxonomy tx = new Taxonomy(this);
return tx;
}

/// <summary>
/// Get version.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions Contentstack.Core/Internals/ContentstackRegion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
namespace Contentstack.Core.Internals
namespace Contentstack.Core.Internals
{
/// <summary>
/// Contentstack region.
Expand Down
50 changes: 50 additions & 0 deletions Contentstack.Core/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,56 @@ public void RemoveHeader(string key)

}



/// <summary>
/// To set variants header using Entry instance.
/// </summary>
/// <param name="variant_header">Entry instance</param>
/// <returns>Current instance of Entry, this will be useful for a chaining calls.</returns>
/// <example>
/// <code>
/// ContentstackClient stack = new ContentstackClinet(&quot;api_key&quot;, &quot;delivery_token&quot;, &quot;environment&quot;);
/// Entry csEntry = stack.ContentType(&quot;contentType_id&quot;).Entry(&quot;entry_uid&quot;);
///
/// csEntry.Variant("variant_entry_1");
/// csEntry.Fetch&lt;Product&gt;().ContinueWith((entryResult) =&gt; {
/// //Your callback code.
/// //var result = entryResult.Result.GetMetadata();
/// });
/// </code>
/// </example>
public Entry Variant(string variant_header)
{
this.SetHeader("x-cs-variant-uid", variant_header);
return this;
}



/// <summary>
/// To set multiple variants headers using Entry instance.
/// </summary>
/// <param name="variant_headers">Entry instance</param>
/// <returns>Current instance of Entry, this will be useful for a chaining calls.</returns>
/// <example>
/// <code>
/// ContentstackClient stack = new ContentstackClinet(&quot;api_key&quot;, &quot;delivery_token&quot;, &quot;environment&quot;);
/// Entry csEntry = stack.ContentType(&quot;contentType_id&quot;).Query();
///
/// csEntry.Variant(new List<string> { "variant_entry_1", "variant_entry_2", "variant_entry_3" });
/// csEntry.Fetch&lt;Product&gt;().ContinueWith((entryResult) =&gt; {
/// //Your callback code.
/// //var result = entryResult.Result.GetMetadata();
/// });
/// </code>
/// </example>
public Entry Variant(List<string> variant_headers)
{
this.SetHeader("x-cs-variant-uid", string.Join(",", variant_headers));
return this;
}

/// <summary>
/// Get metadata of entry.
/// </summary>
Expand Down
Loading
Loading