Skip to content

Commit

Permalink
Merge pull request #8 from nozzlegear/ShopifyScriptTagService
Browse files Browse the repository at this point in the history
ShopifyScriptTagService: Create, get, list, count, update and delete script tags.
  • Loading branch information
nozzlegear committed Oct 3, 2015
2 parents 738ae5e + 7c7f9e4 commit f0f934e
Show file tree
Hide file tree
Showing 15 changed files with 602 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_counting_script_tags
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
FilteredSrc = string.Format("https://nozzlegear.com/{0}/even.js", Guid.NewGuid().ToString());
for (int i = 0; i < 5; i++)
{
string src = i % 2 == 0 ? FilteredSrc : "https://nozzlegear.com/odd.js";
Tags.Add(Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = src
}).Await().AsTask.Result);
}
};

Because of = () =>
{
Count = Service.CountAsync().Await().AsTask.Result;
FilteredCount = Service.CountAsync(FilteredSrc).Await().AsTask.Result;
};

It should_count_script_tags = () =>
{
Count.ShouldBeGreaterThanOrEqualTo(3);
FilteredCount.ShouldEqual(3);
};

Cleanup after = () =>
{
foreach (var tag in Tags)
{
Service.DeleteAsync(tag.Id.Value).Await();
}
};

static string FilteredSrc;

static int Count;

static int FilteredCount;

static ShopifyScriptTagService Service;

static List<ShopifyScriptTag> Tags = new List<ShopifyScriptTag>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_creating_a_script_tag
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
};

Because of = () =>
{
Tag = Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = "https://nozzlegear.com/test.js"
}).Await().AsTask.Result;
};

It should_create_a_script_tag = () =>
{
Tag.ShouldNotBeNull();
Tag.Src.ShouldEqual("https://nozzlegear.com/test.js");
};

Cleanup after = () =>
{
Service.DeleteAsync(Tag.Id.Value).Await();
};

static ShopifyScriptTagService Service;

static ShopifyScriptTag Tag;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_deleting_a_script_tag
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
Tag = Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = "https://nozzlegear.com/test.js"
}).Await().AsTask.Result;
};

Because of = () =>
{
try
{
Service.DeleteAsync(Tag.Id.Value).Await();
}
catch(Exception e)
{
Ex = e;
}
};

It should_delete_a_script_tag = () =>
{
Ex.ShouldBeNull();
};

Cleanup after = () =>
{
};

static ShopifyScriptTagService Service;

static ShopifyScriptTag Tag;

static Exception Ex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_getting_a_script_tag
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
TagId = Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = "https://nozzlegear.com/test.js"
}).Await().AsTask.Result.Id.Value;
};

Because of = () =>
{
Tag = Service.GetAsync(TagId).Await().AsTask.Result;
};

It should_get_a_script_tag = () =>
{
Tag.ShouldNotBeNull();
Tag.Id.ShouldEqual(TagId);
};

Cleanup after = () =>
{
Service.DeleteAsync(TagId).Await();
};

static ShopifyScriptTagService Service;

static ShopifyScriptTag Tag;

static long TagId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_listing_script_tags
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
for (int i = 0; i < 5; i++)
{
string src = i % 2 == 0 ? FilteredSrc : "https://nozzlegear.com/odd.js";
Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = src
}).Await();
}
};

Because of = () =>
{
Tags = Service.ListAsync().Await().AsTask.Result;
FilteredTags = Service.ListAsync(new ShopifyScriptTagListOptions() {
Src = FilteredSrc
}).Await().AsTask.Result;
};

It should_list_script_tags = () =>
{
Tags.Count().ShouldBeGreaterThanOrEqualTo(5);
FilteredTags.All(x => x.Src == FilteredSrc).ShouldBeTrue();
};

Cleanup after = () =>
{
foreach (var tag in Tags)
{
Service.DeleteAsync(tag.Id.Value).Await();
}
};

static string FilteredSrc = "https://nozzlegear.com/even.js";

static ShopifyScriptTagService Service;

static IEnumerable<ShopifyScriptTag> Tags;

static IEnumerable<ShopifyScriptTag> FilteredTags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Machine.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp.Tests.ShopifyScriptTagService_Tests
{
[Subject(typeof(ShopifyScriptTagService))]
class When_updating_a_script_tag
{
Establish context = () =>
{
Service = new ShopifyScriptTagService(Utils.MyShopifyUrl, Utils.AccessToken);
OriginalTag = Service.CreateAsync(new ShopifyScriptTag()
{
Event = Enums.ShopifyScriptTagEvent.Onload,
Src = "https://nozzlegear.com/test.js"
}).Await().AsTask.Result;
OriginalTag.Src = "https://nozzlegear.com/updated.js";
};

Because of = () =>
{
UpdatedTag = Service.UpdateAsync(OriginalTag).Await().AsTask.Result;
};

It should_update_a_script_tag = () =>
{
UpdatedTag.ShouldNotBeNull();
UpdatedTag.Src.ShouldEqual("https://nozzlegear.com/updated.js");
};

Cleanup after = () =>
{
Service.DeleteAsync(OriginalTag.Id.Value).Await();
};

static ShopifyScriptTagService Service;

static ShopifyScriptTag OriginalTag;

static ShopifyScriptTag UpdatedTag;
}
}
13 changes: 13 additions & 0 deletions ShopifySharp.Tests/ShopifySharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
<Compile Include="ShopifyOrderService Tests\When_opening_an_order.cs" />
<Compile Include="ShopifyOrderService Tests\When_updating_an_order.cs" />
<Compile Include="ShopifyAuthorizationService Tests\When_validating_shop_url.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_counting_script_tags.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_deleting_a_script_tag.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_creating_a_script_tag.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_getting_a_script_tag.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_listing_script_tags.cs" />
<Compile Include="ShopifyScriptTagService Tests\When_updating_a_script_tag.cs" />
<Compile Include="ShopifyWebhookService Tests\Test_Data\WebhookCreation.cs" />
<Compile Include="ShopifyWebhookService Tests\When_counting_webhooks.cs" />
<Compile Include="ShopifyWebhookService Tests\When_counting_webhooks_with_a_filter.cs" />
Expand All @@ -127,6 +133,13 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
<None Include="Playlists\Authorization.playlist" />
<None Include="Playlists\Charges.playlist" />
<None Include="Playlists\Customers.playlist" />
<None Include="Playlists\FalseToNullConverter.playlist" />
<None Include="Playlists\Orders.playlist" />
<None Include="Playlists\RecurringCharges.playlist" />
<None Include="Playlists\Webhooks.playlist" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShopifySharp\ShopifySharp.csproj">
Expand Down
41 changes: 41 additions & 0 deletions ShopifySharp/Entities/ShopifyScriptTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ShopifySharp.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShopifySharp
{
/// <summary>
/// An object representing remote javascript tags that are loaded into the pages of a shop's storefront.
/// </summary>
public class ShopifyScriptTag: ShopifyObject
{
/// <summary>
/// The date and time when the <see cref="ShopifyScriptTag"/> was created.
/// </summary>
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }

/// <summary>
/// DOM event which triggers the loading of the script. Valid values are: <see cref="ShopifyScriptTagEvent.Onload"/>.
/// </summary>
[JsonProperty("event"), JsonConverter(typeof(StringEnumConverter))]
public ShopifyScriptTagEvent Event { get; set; }

/// <summary>
/// Specifies the location of the ScriptTag.
/// </summary>
[JsonProperty("src")]
public string Src { get; set; }

/// <summary>
/// The date and time when the <see cref="ShopifyScriptTag"/> was updated.
/// </summary>
[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }
}
}
Loading

0 comments on commit f0f934e

Please sign in to comment.