public void Create() { using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql); if (existing != null) { this.Delete(); } using (RESTClient restClient = new RESTClient(BaseUrl)) { ProductRowApiO newItem = new ProductRowApiO() { ProductName = TestProductName, CategoryId = 2, QuantityPerUnit = "4 in a box", UnitPrice = (decimal)12.37, SupplierId = 1 }; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PUT, jsonBody: newItem, headerParameters: Headers); Assert.True(apiResult.StatusCode == 201, apiResult.Content); ProductRowApiO sqlResult = sqlClient.Fill <ProductRowApiO>(GetRecordSql); Assert.True(sqlResult.ProductName == apiResult.Result.ProductName && sqlResult.CategoryId == apiResult.Result.CategoryId && sqlResult.QuantityPerUnit == apiResult.Result.QuantityPerUnit && sqlResult.ProductId == apiResult.Result.ProductId); } } }
public void Delete() { using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql); if (existingCategory == null) { this.Create(); existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql); } Assert.NotNull(existingCategory); using (RESTClient restClient = new RESTClient(BaseUrl)) { List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("key", existingCategory.ProductId.ToString()) }; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product/{key}", RestSharp.Method.DELETE, routeParameters: routeParams, headerParameters: Headers); Assert.True(apiResult.StatusCode == 301, apiResult.Content); } ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql); Assert.Null(dbValue); } }
public void UpdatePartialBadly() { using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql); if (existingCategory == null) { this.Create(); existingCategory = sqlClient.Fill <ProductRowApiO>(GetRecordSql); } Assert.NotNull(existingCategory); using (RESTClient restClient = new RESTClient(BaseUrl)) { string partialJson = "{ \"ProductId\": " + existingCategory.ProductId + " }"; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers); Assert.False(apiResult.StatusCode == 200, apiResult.Content); } ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql); Assert.False(dbValue.ProductName == string.Empty, "Incorrect text was saved."); } }
public void UpdatePartial() { using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql); if (existing == null) { this.Create(); existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql); } Assert.NotNull(existing); using (RESTClient restClient = new RESTClient(BaseUrl)) { string partialJson = "{ \"ProductId\": " + existing.ProductId + ", \"CategoryId\": 2, \"SupplierId\": 1, \"UnitPrice\": 32.1 }"; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBodyPartial: partialJson, headerParameters: Headers); Assert.True(apiResult.StatusCode == 200, apiResult.Content); ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql); Assert.True(dbValue.UnitPrice == (decimal)32.1, "Incorrect text was saved."); } } }
public void Update() { using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql); if (existing == null) { this.Create(); existing = sqlClient.Fill <ProductRowApiO>(GetRecordSql); } Assert.NotNull(existing); using (RESTClient restClient = new RESTClient(BaseUrl)) { ProductRowApiO updated = existing; updated.UnitPrice = 123; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product", RestSharp.Method.PATCH, jsonBody: updated, headerParameters: Headers); Assert.True(apiResult.StatusCode == 200, apiResult.Content); Assert.True(apiResult.Result.UnitPrice == 123, "The updated value was not returned"); } ProductRowApiO dbValue = sqlClient.Fill <ProductRowApiO>(GetRecordSql); Assert.True(dbValue.UnitPrice == 123, "The updated value was not returned"); } }
public void TestFillOne() { using (MSSQLDbClient client = new MSSQLDbClient(ConnectionString)) { ProductRowApiO productRowApiOs = client.Fill <ProductRowApiO>("SELECT TOP 1 * FROM PRODUCTS"); Assert.True(productRowApiOs.ProductId == 1, "No products were found."); } }
public void CreateInvalid() { using (RESTClient restClient = new RESTClient(BaseUrl)) { ProductRowApiO newItem = new ProductRowApiO() { ProductName = string.Empty, UnitPrice = 0, SupplierId = -3, CategoryId = -4 }; RestResult <CategoryRowApiO> apiResult = restClient.Execute <CategoryRowApiO>("Product", RestSharp.Method.PUT, jsonBody: newItem, headerParameters: Headers); Assert.True(apiResult.StatusCode == 406 || apiResult.StatusCode == 400, apiResult.Content); } }
public void GetOne() { using (RESTClient restClient = new RESTClient(BaseUrl)) { List <KeyValuePair <string, string> > routeParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("key", "2") }; RestResult <ProductRowApiO> apiResult = restClient.Execute <ProductRowApiO>("Product/{key}", RestSharp.Method.POST, routeParameters: routeParams, headerParameters: Headers); Assert.True(apiResult.Success, apiResult.Content); Assert.NotNull(apiResult); using (MSSQLDbClient sqlClient = new MSSQLDbClient(ConnectionString)) { ProductRowApiO sqlResult = sqlClient.Fill <ProductRowApiO>("SELECT ProductID as ProductId, ProductName, SupplierID as SupplierId, CategoryID as CategoryId, QuantityPerUnit, " + "UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products WHERE ProductId = 2 ORDER BY ProductID"); Assert.NotNull(sqlResult); Assert.True(apiResult.Result.ProductId == sqlResult.ProductId && apiResult.Result.ProductName == sqlResult.ProductName, "The records do not match."); } } }