From e201d821669ea3034e058d9aa94e299b165b746e Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Tue, 3 Oct 2017 16:01:29 +0300 Subject: [PATCH] Update test classes, add packs & statistic methods --- Retailcrm/Retailcrm.csproj | 1 + Retailcrm/Versions/V3/Packs.cs | 103 ++++++++++++ Retailcrm/Versions/V3/Statistic.cs | 13 ++ RetailcrmUnitTest/App.config.dist | 1 + RetailcrmUnitTest/RetailcrmUnitTest.csproj | 2 + RetailcrmUnitTest/V3/ClientTest.cs | 42 +++++ RetailcrmUnitTest/V3/CustomersTest.cs | 7 +- RetailcrmUnitTest/V3/PacksTest.cs | 178 +++++++++++++++++++++ 8 files changed, 345 insertions(+), 2 deletions(-) create mode 100644 Retailcrm/Versions/V3/Statistic.cs create mode 100644 RetailcrmUnitTest/V3/ClientTest.cs create mode 100644 RetailcrmUnitTest/V3/PacksTest.cs diff --git a/Retailcrm/Retailcrm.csproj b/Retailcrm/Retailcrm.csproj index c0b69b9..a6ae6f5 100644 --- a/Retailcrm/Retailcrm.csproj +++ b/Retailcrm/Retailcrm.csproj @@ -47,6 +47,7 @@ + diff --git a/Retailcrm/Versions/V3/Packs.cs b/Retailcrm/Versions/V3/Packs.cs index 1327e23..510ec7c 100644 --- a/Retailcrm/Versions/V3/Packs.cs +++ b/Retailcrm/Versions/V3/Packs.cs @@ -6,5 +6,108 @@ public partial class Client { + public Response PacksList(Dictionary filter = null, int page = 0, int limit = 0) + { + Dictionary parameters = new Dictionary(); + + if (filter != null && filter.Count > 0) + { + parameters.Add("filter", filter); + } + + if (page > 0) + { + parameters.Add("page", page); + } + + if (limit > 0) + { + parameters.Add("limit", limit); + } + + return _request.MakeRequest("/orders/packs", Request.MethodGet, parameters); + } + + public Response PacksCreate(Dictionary pack) + { + if (pack.Count < 1) + { + throw new ArgumentException("Parameter `pack` must contains a data"); + } + + return _request.MakeRequest( + "/orders/packs/create", + Request.MethodPost, + new Dictionary + { + { "pack", new JavaScriptSerializer().Serialize(pack) } + } + ); + } + + public Response PacksUpdate(Dictionary pack) + { + if (pack.Count < 1) + { + throw new ArgumentException("Parameter `pack` must contains a data"); + } + + if (!pack.ContainsKey("id")) + { + throw new ArgumentException("Parameter `pack` must contains an id"); + } + + return _request.MakeRequest( + $"/orders/packs/{pack["id"].ToString()}/edit", + Request.MethodPost, + new Dictionary + { + { "pack", new JavaScriptSerializer().Serialize(pack) } + } + ); + } + + public Response PacksDelete(string id) + { + if (id.Length < 1) + { + throw new ArgumentException("Parameter `id` must contains a data"); + } + + return _request.MakeRequest( + $"/orders/packs/{id}/delete", + Request.MethodPost + ); + } + + public Response PacksGet(string id) + { + return _request.MakeRequest( + $"/orders/packs/{id}", + Request.MethodGet + ); + } + + public Response PacksHistory(Dictionary filter = null, int page = 1, int limit = 20) + { + Dictionary parameters = new Dictionary(); + + if (filter != null && filter.Count > 0) + { + parameters.Add("filter", filter); + } + + if (page > 1) + { + parameters.Add("page", page); + } + + if (limit > 0) + { + parameters.Add("limit", limit); + } + + return _request.MakeRequest("/orders/packs/history", Request.MethodGet, parameters); + } } } diff --git a/Retailcrm/Versions/V3/Statistic.cs b/Retailcrm/Versions/V3/Statistic.cs new file mode 100644 index 0000000..bff3eb3 --- /dev/null +++ b/Retailcrm/Versions/V3/Statistic.cs @@ -0,0 +1,13 @@ +namespace Retailcrm.Versions.V3 +{ + public partial class Client + { + public Response StatisticUpdate() + { + return _request.MakeRequest( + "/statistic/update", + Request.MethodGet + ); + } + } +} diff --git a/RetailcrmUnitTest/App.config.dist b/RetailcrmUnitTest/App.config.dist index 07cb110..1a5450d 100644 --- a/RetailcrmUnitTest/App.config.dist +++ b/RetailcrmUnitTest/App.config.dist @@ -3,5 +3,6 @@ + \ No newline at end of file diff --git a/RetailcrmUnitTest/RetailcrmUnitTest.csproj b/RetailcrmUnitTest/RetailcrmUnitTest.csproj index 4b0a18e..4e9d20e 100644 --- a/RetailcrmUnitTest/RetailcrmUnitTest.csproj +++ b/RetailcrmUnitTest/RetailcrmUnitTest.csproj @@ -53,9 +53,11 @@ + + diff --git a/RetailcrmUnitTest/V3/ClientTest.cs b/RetailcrmUnitTest/V3/ClientTest.cs new file mode 100644 index 0000000..9814be0 --- /dev/null +++ b/RetailcrmUnitTest/V3/ClientTest.cs @@ -0,0 +1,42 @@ +namespace RetailcrmUnitTest.V3 +{ + using System.Collections.Specialized; + using System.Configuration; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Retailcrm; + using Retailcrm.Versions.V3; + + [TestClass] + public class ClientTest + { + private readonly Client _client; + + public ClientTest() + { + NameValueCollection appSettings = ConfigurationManager.AppSettings; + _client = new Client(appSettings["apiUrl"], appSettings["apiKey"], appSettings["site"]); + } + + [TestMethod] + public void InitTest() + { + Assert.IsInstanceOfType(_client, typeof(Client)); + + string siteCode = "default"; + + _client.SetSite(siteCode); + + Assert.AreEqual(_client.GetSite(), siteCode); + } + + [TestMethod] + public void StatisticUpdate() + { + Response response = _client.StatisticUpdate(); + + Assert.IsTrue(response.IsSuccessfull()); + Assert.IsInstanceOfType(response, typeof(Response)); + Assert.IsTrue(response.GetStatusCode() == 200); + } + } +} diff --git a/RetailcrmUnitTest/V3/CustomersTest.cs b/RetailcrmUnitTest/V3/CustomersTest.cs index f0311d9..6380d28 100644 --- a/RetailcrmUnitTest/V3/CustomersTest.cs +++ b/RetailcrmUnitTest/V3/CustomersTest.cs @@ -12,11 +12,12 @@ public class CustomersTest { private readonly Client _client; + private readonly NameValueCollection _appSettings; public CustomersTest() { - NameValueCollection appSettings = ConfigurationManager.AppSettings; - _client = new Client(appSettings["apiUrl"], appSettings["apiKey"]); + _appSettings = ConfigurationManager.AppSettings; + _client = new Client(_appSettings["apiUrl"], _appSettings["apiKey"]); } [TestMethod] @@ -101,6 +102,8 @@ [TestMethod] public void CustomersList() { + _client.SetSite(_appSettings["site"]); + Response response = _client.CustomersList(); Assert.IsTrue(response.IsSuccessfull()); diff --git a/RetailcrmUnitTest/V3/PacksTest.cs b/RetailcrmUnitTest/V3/PacksTest.cs new file mode 100644 index 0000000..8f3aa0d --- /dev/null +++ b/RetailcrmUnitTest/V3/PacksTest.cs @@ -0,0 +1,178 @@ +using System.Globalization; + +namespace RetailcrmUnitTest.V3 +{ + using System; + using System.Collections.Generic; + using System.Collections.Specialized; + using System.Configuration; + using System.Linq; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Retailcrm; + using Retailcrm.Versions.V3; + + [TestClass] + public class PacksTest + { + private readonly Client _client; + private readonly NameValueCollection _appSettings; + + public PacksTest() + { + _appSettings = ConfigurationManager.AppSettings; + _client = new Client(_appSettings["apiUrl"], _appSettings["apiKey"], _appSettings["site"]); + } + + #region Дополнительные атрибуты тестирования + // + // При написании тестов можно использовать следующие дополнительные атрибуты: + // + // ClassInitialize используется для выполнения кода до запуска первого теста в классе + // [ClassInitialize()] + // public static void MyClassInitialize(TestContext testContext) { } + // + // ClassCleanup используется для выполнения кода после завершения работы всех тестов в классе + // [ClassCleanup()] + // public static void MyClassCleanup() { } + // + // TestInitialize используется для выполнения кода перед запуском каждого теста + // [TestInitialize()] + // public void MyTestInitialize() { } + // + // TestCleanup используется для выполнения кода после завершения каждого теста + // [TestCleanup()] + // public void MyTestCleanup() { } + // + #endregion + + [TestMethod] + public void PacksCreateUpdateReadDelete() + { + string uid = Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 12); + + Dictionary order = new Dictionary + { + { "number", $"packs-test-{uid}" }, + { "firstName", $"John_{uid}" }, + { "lastName", $"Doe_{uid}"}, + { "email", $"{uid}@example.com"}, + { + "items", new List + { + new Dictionary + { + { "initialPrice", 500 }, + { "quantity", 2}, + { "productId", "_jAjMfjjgs6ukFxOiTE433"}, + { "productName", "Test"} + } + } + } + }; + + Response orderCreateResponse = _client.OrdersCreate(order); + + Assert.IsTrue(orderCreateResponse.IsSuccessfull()); + Assert.IsTrue(orderCreateResponse.GetStatusCode() == 201); + Assert.IsInstanceOfType(orderCreateResponse, typeof(Response)); + Assert.IsTrue(orderCreateResponse.GetResponse().ContainsKey("id")); + + Response orderGetResponse = _client.OrdersGet(orderCreateResponse.GetResponse()["id"].ToString(), "id"); + + Assert.IsTrue(orderGetResponse.IsSuccessfull()); + Assert.IsTrue(orderGetResponse.GetStatusCode() == 200); + Assert.IsInstanceOfType(orderGetResponse, typeof(Response)); + Assert.IsTrue(orderGetResponse.GetResponse().ContainsKey("order")); + + Dictionary orderFromResponse = + (Dictionary) orderGetResponse.GetResponse()["order"]; + + object[] arr = (object[])orderFromResponse["items"]; + int[] id = new int[1]; + + + foreach (Dictionary s in arr.OfType>()) + { + int itemId; + int.TryParse(s["id"].ToString(), NumberStyles.Any, null, out itemId); + id[0] = itemId; + } + + Dictionary pack = new Dictionary + { + { "purchasePrice", 100 }, + { "quantity", 1}, + { "store", _appSettings["store"]}, + { "itemId", id[0]} + }; + + Response packsCreateResponse = _client.PacksCreate(pack); + + Assert.IsTrue(packsCreateResponse.IsSuccessfull()); + Assert.IsTrue(packsCreateResponse.GetStatusCode() == 201); + Assert.IsInstanceOfType(packsCreateResponse, typeof(Response)); + Assert.IsTrue(packsCreateResponse.GetResponse().ContainsKey("id")); + + string packId = packsCreateResponse.GetResponse()["id"].ToString(); + + Dictionary packEdit = new Dictionary + { + { "id", packId }, + { "quantity", 2} + }; + + Response packsUpdateResponse = _client.PacksUpdate(packEdit); + + Assert.IsTrue(packsUpdateResponse.IsSuccessfull()); + Assert.IsTrue(packsUpdateResponse.GetStatusCode() == 200); + Assert.IsInstanceOfType(packsUpdateResponse, typeof(Response)); + Assert.IsTrue(packsUpdateResponse.GetResponse().ContainsKey("id")); + + Response packsGetResponse = _client.PacksGet(packId); + + Assert.IsTrue(packsGetResponse.IsSuccessfull()); + Assert.IsTrue(packsGetResponse.GetStatusCode() == 200); + Assert.IsInstanceOfType(packsGetResponse, typeof(Response)); + Assert.IsTrue(packsGetResponse.GetResponse().ContainsKey("pack")); + + Response packsDeleteResponse = _client.PacksGet(packId); + + Assert.IsTrue(packsDeleteResponse.IsSuccessfull()); + Assert.IsTrue(packsDeleteResponse.GetStatusCode() == 200); + Assert.IsInstanceOfType(packsDeleteResponse, typeof(Response)); + + } + + [TestMethod] + public void PacksList() + { + Dictionary filter = new Dictionary + { + { "store", _appSettings["store"]} + }; + + Response response = _client.PacksList(filter, 1, 100); + + Assert.IsTrue(response.IsSuccessfull()); + Assert.IsTrue(response.GetStatusCode() == 200); + Assert.IsInstanceOfType(response, typeof(Response)); + Assert.IsTrue(response.GetResponse().ContainsKey("packs")); + } + + [TestMethod] + public void PacksHistory() + { + Dictionary filter = new Dictionary + { + { "endDate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} + }; + + Response response = _client.PacksHistory(filter, 1, 100); + + Assert.IsTrue(response.IsSuccessfull()); + Assert.IsTrue(response.GetStatusCode() == 200); + Assert.IsInstanceOfType(response, typeof(Response)); + Assert.IsTrue(response.GetResponse().ContainsKey("history")); + } + } +}