-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from nozzlegear/ShopifyThemeService
ShopifyThemeService: Create, retrieve, list, update and delete a store's themes.
- Loading branch information
Showing
19 changed files
with
674 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<Playlist Version="1.0"><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_creating_a_themes::should_create_a_theme" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_listing_themes::should_list_themes" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_getting_a_theme::should_get_a_theme" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_counting_themes::should_count_themes" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_updating_a_themes::should_update_a_themes" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_deleting_a_themes::should_delete_a_theme" /><Add Test="ShopifySharp.Tests.ShopifyThemeService_Tests.When_deserializing_a_theme::should_deserialize_a_theme" /></Playlist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
ShopifySharp.Tests/ShopifyThemeService Tests/Test_Data/ThemeCreation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests.Test_Data | ||
{ | ||
public static class ThemeCreation | ||
{ | ||
/// <summary> | ||
/// A URL pointing to a zipped up Shopify theme. Can be used with ShopifyThemeService.CreateAsync. | ||
/// </summary> | ||
public static string ZipUrl = "https://ironstorage.blob.core.windows.net/public-downloads/ShopifySharp/shopify_test_theme_for_shopifysharp.zip"; | ||
|
||
public static ShopifyTheme CreateValidTheme() | ||
{ | ||
return new ShopifyTheme() | ||
{ | ||
Name = "My new theme.", | ||
Role = Enums.ShopifyThemeRole.Unpublished | ||
}; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
ShopifySharp.Tests/ShopifyThemeService Tests/When_creating_a_theme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Machine.Specifications; | ||
using ShopifySharp.Tests.ShopifyThemeService_Tests.Test_Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_creating_a_themes | ||
{ | ||
Establish context = () => | ||
{ | ||
Service = new ShopifyThemeService(Utils.MyShopifyUrl, Utils.AccessToken); | ||
}; | ||
|
||
Because of = () => | ||
{ | ||
Theme = Service.CreateAsync(ThemeCreation.CreateValidTheme(), "").Await().AsTask.Result; | ||
}; | ||
|
||
It should_create_a_theme = () => | ||
{ | ||
Theme.Id.HasValue.ShouldBeTrue(); | ||
Theme.Role.ShouldEqual(Enums.ShopifyThemeRole.Unpublished); | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
Service.DeleteAsync(Theme.Id.Value).Await(); | ||
}; | ||
|
||
static ShopifyThemeService Service; | ||
|
||
static ShopifyTheme Theme; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
ShopifySharp.Tests/ShopifyThemeService Tests/When_deleting_a_theme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Machine.Specifications; | ||
using ShopifySharp.Tests.ShopifyThemeService_Tests.Test_Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_deleting_a_themes | ||
{ | ||
Establish context = () => | ||
{ | ||
Service = new ShopifyThemeService(Utils.MyShopifyUrl, Utils.AccessToken); | ||
ThemeId = Service.CreateAsync(ThemeCreation.CreateValidTheme(), "").Await().AsTask.Result.Id.Value; | ||
}; | ||
|
||
Because of = () => | ||
{ | ||
Service.DeleteAsync(ThemeId).Await(); | ||
}; | ||
|
||
It should_delete_a_theme = () => | ||
{ | ||
//Passes if no exception was thrown. | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
|
||
}; | ||
|
||
static ShopifyThemeService Service; | ||
|
||
static long ThemeId; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
ShopifySharp.Tests/ShopifyThemeService Tests/When_deserializing_a_theme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Machine.Specifications; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_deserializing_a_theme | ||
{ | ||
Establish context = () => | ||
{ | ||
|
||
}; | ||
|
||
Because of = () => | ||
{ | ||
Theme1 = JsonConvert.DeserializeObject<ShopifyTheme>(Theme1Json); | ||
|
||
Theme2 = JsonConvert.DeserializeObject<ShopifyTheme>(Theme2Json); | ||
|
||
Theme3 = JsonConvert.DeserializeObject<ShopifyTheme>(Theme3Json); | ||
}; | ||
|
||
It should_deserialize_a_theme = () => | ||
{ | ||
Theme1.Role.ShouldBeNull(); | ||
|
||
Theme2.Role.ShouldBeNull(); | ||
|
||
Theme3.Role.ShouldEqual(Enums.ShopifyThemeRole.Main); | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
|
||
}; | ||
|
||
static string Theme1Json { get; } = "{\"id\":10556555,\"name\":\"launchpad-star\",\"created_at\":\"2014-09-03T11:20:41-05:00\",\"updated_at\":\"2015-11-11T11:28:59-06:00\",\"role\":\"\",\"theme_store_id\":null,\"previewable\":true,\"processing\":false}"; | ||
|
||
static string Theme2Json { get; } = "{\"id\":10556555,\"name\":\"launchpad-star\",\"created_at\":\"2014-09-03T11:20:41-05:00\",\"updated_at\":\"2015-11-11T11:28:59-06:00\",\"role\":\"unknown_value\",\"theme_store_id\":null,\"previewable\":true,\"processing\":false}"; | ||
|
||
static string Theme3Json { get; } = "{\"id\":10556555,\"name\":\"launchpad-star\",\"created_at\":\"2014-09-03T11:20:41-05:00\",\"updated_at\":\"2015-11-11T11:28:59-06:00\",\"role\":\"main\",\"theme_store_id\":null,\"previewable\":true,\"processing\":false}"; | ||
|
||
static ShopifyTheme Theme1 { get; set; } | ||
|
||
static ShopifyTheme Theme2 { get; set; } | ||
|
||
static ShopifyTheme Theme3 { get; set; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
ShopifySharp.Tests/ShopifyThemeService Tests/When_getting_a_theme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_getting_a_theme | ||
{ | ||
Establish context = () => | ||
{ | ||
Service = new ShopifyThemeService(Utils.MyShopifyUrl, Utils.AccessToken); | ||
|
||
ThemeId = Service.ListAsync().Await().AsTask.Result.First().Id.Value; | ||
}; | ||
|
||
Because of = () => | ||
{ | ||
Theme = Service.GetAsync(ThemeId).Await().AsTask.Result; | ||
}; | ||
|
||
It should_get_a_theme = () => | ||
{ | ||
Theme.ShouldNotBeNull(); | ||
Theme.Role.ShouldNotBeNull(); | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
|
||
}; | ||
|
||
static ShopifyThemeService Service { get; set; } | ||
|
||
static long ThemeId { get; set; } | ||
|
||
static ShopifyTheme Theme { get; set; } | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
ShopifySharp.Tests/ShopifyThemeService Tests/When_listing_themes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Machine.Specifications; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_listing_themes | ||
{ | ||
Establish context = () => | ||
{ | ||
Service = new ShopifyThemeService(Utils.MyShopifyUrl, Utils.AccessToken); | ||
}; | ||
|
||
Because of = () => | ||
{ | ||
Result = Service.ListAsync().Await().AsTask.Result; | ||
}; | ||
|
||
It should_list_themes = () => | ||
{ | ||
Result.Count().ShouldBeGreaterThanOrEqualTo(1); | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
|
||
}; | ||
|
||
static ShopifyThemeService Service; | ||
|
||
static IEnumerable<ShopifyTheme> Result; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
ShopifySharp.Tests/ShopifyThemeService Tests/When_updating_a_theme.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Machine.Specifications; | ||
using ShopifySharp.Tests.ShopifyThemeService_Tests.Test_Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShopifySharp.Tests.ShopifyThemeService_Tests | ||
{ | ||
[Subject(typeof(ShopifyThemeService))] | ||
class When_updating_a_themes | ||
{ | ||
Establish context = () => | ||
{ | ||
Service = new ShopifyThemeService(Utils.MyShopifyUrl, Utils.AccessToken); | ||
Theme = Service.CreateAsync(ThemeCreation.CreateValidTheme(), ThemeCreation.ZipUrl).Await().AsTask.Result; | ||
|
||
int counter = 0; | ||
|
||
//Wait 30 seconds at max for the theme to finish processing. Its role cannot be updated before it's finished processing. | ||
while(Theme.Processing && counter < 6) | ||
{ | ||
Theme = Service.GetAsync(Theme.Id.Value).Await().AsTask.Result; | ||
counter += 1; | ||
|
||
if (!Theme.Processing) | ||
{ | ||
continue; | ||
} | ||
|
||
Thread.Sleep(5000); | ||
} | ||
|
||
Theme.Name = UpdatedThemeName; | ||
Theme.Role = Enums.ShopifyThemeRole.Main; | ||
}; | ||
|
||
Because of = () => | ||
{ | ||
Theme = Service.UpdateAsync(Theme).Await().AsTask.Result; | ||
}; | ||
|
||
It should_update_a_themes = () => | ||
{ | ||
Theme.Name.ShouldEqual(UpdatedThemeName); | ||
Theme.Role.ShouldEqual(Enums.ShopifyThemeRole.Main); | ||
}; | ||
|
||
Cleanup after = () => | ||
{ | ||
Service.DeleteAsync(Theme.Id.Value).Await(); | ||
}; | ||
|
||
static ShopifyThemeService Service; | ||
|
||
static ShopifyTheme Theme; | ||
|
||
static string UpdatedThemeName = "Updated theme name."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Newtonsoft.Json.Converters; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using ShopifySharp.Enums; | ||
|
||
namespace ShopifySharp.Converters | ||
{ | ||
/// <summary> | ||
/// A custom enum converter for all enums which returns the value | ||
/// as null when the value is null or does not exist. | ||
/// </summary> | ||
public class NullableEnumConverter<T> : StringEnumConverter where T : struct | ||
{ | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
T parsed; | ||
|
||
if (!Enum.TryParse(reader.Value?.ToString() ?? "", true, out parsed)) | ||
{ | ||
return null; | ||
} | ||
|
||
return parsed; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.