forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KrzysztofPajak
committed
Jan 8, 2019
1 parent
c24ccb3
commit 1bf0172
Showing
4 changed files
with
180 additions
and
0 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
132 changes: 132 additions & 0 deletions
132
Tests/Grand.Api.Tests/ControllerTests/CategoryControllerTest.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,132 @@ | ||
using Grand.Api.DTOs.Catalog; | ||
using Grand.Api.Services; | ||
using Grand.Api.Tests.Helpers; | ||
using Grand.Core.Data; | ||
using Grand.Core.Domain.Catalog; | ||
using Grand.Services.Security; | ||
using Grand.Services.Tests; | ||
using Grand.Web.Areas.Api.Controllers.OData; | ||
using Microsoft.AspNet.OData.Results; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using MongoDB.Driver.Linq; | ||
using Moq; | ||
using System.Linq; | ||
|
||
namespace Grand.Api.Tests.ControllerTests | ||
{ | ||
[TestClass] | ||
public class CategoryControllerTest | ||
{ | ||
private string _id1 = "5c349ef4d595601e04da9dfc"; | ||
private string _id2 = "5c349ef4d595601e04da9dfd"; | ||
private string _id3 = "5c349ef4d595601e04da9dfe"; | ||
private CategoryDto modelInsertOrUpdate = new CategoryDto() | ||
{ | ||
Id = "", | ||
Name = "sample category 4" | ||
}; | ||
private IRepository<Category> _categoryRepo; | ||
|
||
private CategoryController _categoryController; | ||
private ICategoryApiService _categoryApiService; | ||
private IPermissionService _permissionService; | ||
private IMongoCollection<CategoryDto> _categoryDto; | ||
|
||
[TestInitialize()] | ||
public void TestInitialize() | ||
{ | ||
Mongodb.IgnoreExtraElements(); | ||
InitCategoryRepo(); | ||
var tempCategoryApiService = new Mock<ICategoryApiService>(); | ||
{ | ||
tempCategoryApiService.Setup(instance => instance.GetCategories()).Returns(_categoryDto.AsQueryable()); | ||
tempCategoryApiService.Setup(instance => instance.GetById(_id1)).Returns(_categoryDto.AsQueryable().FirstOrDefault(x => x.Id == _id1)); | ||
tempCategoryApiService.Setup(instance => instance.InsertOrUpdateCategory(modelInsertOrUpdate)).Returns(InsertOrUpdate(modelInsertOrUpdate)); | ||
|
||
_categoryApiService = tempCategoryApiService.Object; | ||
} | ||
var tempPermissionService = new Mock<IPermissionService>(); | ||
{ | ||
tempPermissionService.Setup(instance => instance.Authorize(PermissionSystemName.Categories)).Returns(true); | ||
_permissionService = tempPermissionService.Object; | ||
} | ||
_categoryController = new CategoryController(_categoryApiService, _permissionService); | ||
|
||
} | ||
|
||
private void InitCategoryRepo() | ||
{ | ||
_categoryRepo = new MongoDBRepositoryTest<Category>(); | ||
_categoryRepo.Collection.DeleteMany(new BsonDocument()); | ||
_categoryRepo.Insert(new Category() | ||
{ | ||
Id = _id1, | ||
Name = "sample category 1" | ||
}); | ||
_categoryRepo.Insert(new Category() | ||
{ | ||
Id = _id2, | ||
Name = "sample category 2" | ||
}); | ||
_categoryRepo.Insert(new Category() | ||
{ | ||
Id = _id3, | ||
Name = "sample category 3" | ||
}); | ||
_categoryDto = _categoryRepo.Database.GetCollection<CategoryDto>(typeof(Core.Domain.Catalog.Category).Name); | ||
} | ||
private CategoryDto InsertOrUpdate(CategoryDto model) | ||
{ | ||
return model; | ||
} | ||
|
||
|
||
[TestMethod()] | ||
public void Can_get_category() | ||
{ | ||
IActionResult result = _categoryController.Get(_id1); | ||
|
||
// Assert | ||
var okObjectResult = result as OkObjectResult; | ||
Assert.IsNotNull(okObjectResult); | ||
var presentations = okObjectResult.Value as CategoryDto; | ||
Assert.IsNotNull(presentations); | ||
Assert.AreEqual("sample category 1", presentations.Name); | ||
} | ||
|
||
[TestMethod()] | ||
public void Can_get_categories() | ||
{ | ||
IActionResult result = _categoryController.Get(); | ||
|
||
// Assert | ||
var okObjectResult = result as OkObjectResult; | ||
Assert.IsNotNull(okObjectResult); | ||
var presentations = okObjectResult.Value as IMongoQueryable<CategoryDto>; | ||
Assert.IsNotNull(presentations); | ||
var count = presentations.Count(); | ||
Assert.AreEqual(3, count); | ||
} | ||
|
||
[TestMethod()] | ||
public void Can_Delete() | ||
{ | ||
IActionResult result = _categoryController.Delete(_id1); | ||
// Assert | ||
var okResult = result as OkResult; | ||
Assert.AreEqual(200, okResult.StatusCode); | ||
} | ||
|
||
[TestMethod()] | ||
public void Can_Post() | ||
{ | ||
var response = _categoryController.Post(modelInsertOrUpdate); | ||
// Assert | ||
var createResult = response as CreatedODataResult<CategoryDto>; | ||
Assert.IsNotNull(createResult); | ||
} | ||
} | ||
} |
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<Copyright>Copyright © UNIT-SOFT Sp. z o.o.</Copyright> | ||
<Company>UNIT-SOFT Sp. z o.o.</Company> | ||
<Authors>UNIT-SOFT Sp. z o.o.</Authors> | ||
<PackageProjectUrl>https://grandnode.com/</PackageProjectUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | ||
<PackageReference Include="Moq" Version="4.10.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | ||
<PackageReference Include="NUnit" Version="3.11.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\Grand.Core\Grand.Core.csproj" /> | ||
<ProjectReference Include="..\..\Grand.Framework\Grand.Framework.csproj" /> | ||
<ProjectReference Include="..\..\Grand.Services\Grand.Services.csproj" /> | ||
<ProjectReference Include="..\..\Grand.Api\Grand.Api.csproj" /> | ||
<ProjectReference Include="..\..\Grand.Web\Grand.Web.csproj" /> | ||
<ProjectReference Include="..\Grand.Services.Tests\Grand.Services.Tests.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,14 @@ | ||
using MongoDB.Bson.Serialization.Conventions; | ||
|
||
namespace Grand.Api.Tests.Helpers | ||
{ | ||
public static class Mongodb | ||
{ | ||
public static void IgnoreExtraElements() | ||
{ | ||
var cp = new ConventionPack(); | ||
cp.Add(new IgnoreExtraElementsConvention(true)); | ||
ConventionRegistry.Register("ApplicationConventions", cp, t => true); | ||
} | ||
} | ||
} |