using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; using System.Globalization; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Retailcrm; using Retailcrm.Versions.V3; namespace RetailcrmUnitTest.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"]); } [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.PacksDelete(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, 2, 100); Assert.IsTrue(response.IsSuccessfull()); Assert.IsTrue(response.GetStatusCode() == 200); Assert.IsInstanceOfType(response, typeof(Response)); Assert.IsTrue(response.GetResponse().ContainsKey("history")); } [TestMethod] [ExpectedException(typeof(ArgumentException), "Parameter `pack` must contains a data")] public void PacksCreateArgumentExeption() { Dictionary pack = new Dictionary(); _client.PacksCreate(pack); } [TestMethod] [ExpectedException(typeof(ArgumentException), "Parameter `pack` must contains a data")] public void PacksUpdateArgumentExeption() { Dictionary pack = new Dictionary(); _client.PacksUpdate(pack); } [TestMethod] [ExpectedException(typeof(ArgumentException), "Parameter `pack` must contains an id")] public void PacksUpdateWithoutIdArgumentExeption() { Dictionary pack = new Dictionary { { "quantity", 2 } }; _client.PacksUpdate(pack); } } }