Skip to content

Commit

Permalink
Merge pull request #15 from nozzlegear/ShopifyThemeService
Browse files Browse the repository at this point in the history
ShopifyThemeService: Create, retrieve, list, update and delete a store's themes.
  • Loading branch information
nozzlegear committed Nov 11, 2015
2 parents 7bdf7be + bf4776b commit 98de835
Show file tree
Hide file tree
Showing 19 changed files with 674 additions and 43 deletions.
1 change: 1 addition & 0 deletions ShopifySharp.Tests/Playlists/Themes.playlist
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>
7 changes: 7 additions & 0 deletions ShopifySharp.Tests/ShopifySharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@
<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="ShopifyThemeService Tests\Test_Data\ThemeCreation.cs" />
<Compile Include="ShopifyThemeService Tests\When_creating_a_theme.cs" />
<Compile Include="ShopifyThemeService Tests\When_deleting_a_theme.cs" />
<Compile Include="ShopifyThemeService Tests\When_deserializing_a_theme.cs" />
<Compile Include="ShopifyThemeService Tests\When_getting_a_theme.cs" />
<Compile Include="ShopifyThemeService Tests\When_listing_themes.cs" />
<Compile Include="ShopifyThemeService Tests\When_updating_a_theme.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 Down
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
};
}
}
}
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;
}
}
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;
}
}
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; }
}
}
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; }
}
}
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;
}
}
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.";
}
}
30 changes: 30 additions & 0 deletions ShopifySharp/Converters/NullableEnumConverter.cs
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;
}
}
}
3 changes: 3 additions & 0 deletions ShopifySharp/Entities/ShopifyProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace ShopifySharp
{
/// <summary>
/// An entity representing a Shopify product.
/// </summary>
public class ShopifyProduct : ShopifyObject
{
/// <summary>
Expand Down
Loading

0 comments on commit 98de835

Please sign in to comment.