forked from blogifierdotnet/Blogifier
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blogifier.Core added to the main project
- Loading branch information
Showing
87 changed files
with
8,472 additions
and
5 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
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
Binary file not shown.
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,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.0" /> | ||
<PackageReference Include="Moq" Version="4.13.1" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Blogifier.Core\Blogifier.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="appsettings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="Blog.db"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
26 changes: 26 additions & 0 deletions
26
src/Blogifier.Core.Tests/Extensions/StringExtensionsTests.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,26 @@ | ||
using Xunit; | ||
|
||
namespace Blogifier.Core.Tests.Extensions | ||
{ | ||
public class StringExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("tes't, #one", "test-one")] | ||
[InlineData("{test [two?", "test-two")] | ||
[InlineData("test$ ~three!", "test-three")] | ||
[InlineData("Тест* для& --Кирил/лицы", "тест-для-кириллицы")] | ||
public void ShouldRemoveIlligalChars(string title, string slug) | ||
{ | ||
Assert.Equal(title.ToSlug(), slug); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://foo/bar/img.jpg", "http://foo/bar/thumbs/img.jpg")] | ||
[InlineData("foo/bar//img-foo.jpg", "foo/bar//thumbs/img-foo.jpg")] | ||
[InlineData("foo/bar/img.one.png", "foo/bar/thumbs/img.one.png")] | ||
public void ShouldConvertImgPathToTumbPath(string img, string thumb) | ||
{ | ||
Assert.Equal(img.ToThumb(), thumb); | ||
} | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
src/Blogifier.Core.Tests/Repositories/AuthorRepositoryTests.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,159 @@ | ||
using Blogifier.Core.Data; | ||
using Blogifier.Core.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Blogifier.Core.Tests.Repositories | ||
{ | ||
public class AuthorRepositoryTests | ||
{ | ||
private readonly IEnumerable<Author> _authors = Enumerable.Range(1, 12) | ||
.Select(i => new Author | ||
{ | ||
Id = i, | ||
AppUserName = $"test{i}" | ||
}); | ||
|
||
[Fact] | ||
public async Task Can_Save_New_Author() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
|
||
var author = new Author | ||
{ | ||
Id = 25, | ||
AppUserName = "Test25" | ||
}; | ||
|
||
// act | ||
await sut.Save(author); | ||
var result = await sut.GetItem(a => a.AppUserName == "Test25"); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.True(result.Id == 25); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Remove_Author() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
var author = new Author { Id = 25, AppUserName = "Test25" }; | ||
|
||
// act | ||
await sut.Save(author); | ||
var result1 = await sut.GetItem(a => a.Id == 25); | ||
await sut.Remove(25); | ||
var result2 = await sut.GetItem(a => a.Id == 25); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.NotNull(result1); | ||
Assert.Null(result2); | ||
} | ||
|
||
[Fact] | ||
public async Task GetItem_By_Matching_Id_Returns_1_Result() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
|
||
// act | ||
var result = await sut.GetItem(x => x.AppUserName == "test1"); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
Assert.True(result.Id == 1); | ||
} | ||
|
||
[Fact] | ||
public async Task GetItems_By_NotMatching_Id_Returns_0_Results() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
var pager = new Pager(1); | ||
|
||
// act | ||
var result = await sut.GetList(x => x.Id == 123, pager); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.Empty(result); | ||
} | ||
|
||
[Fact] | ||
public async Task GetItems_By_Matching_Id_Returns_1_Result() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
var pager = new Pager(1); | ||
|
||
// act | ||
var result = await sut.GetList(x => x.Id == 1, pager); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.True(result.Count() == 1); | ||
} | ||
|
||
[Fact] | ||
public async Task Get_First_Page_Returns_10_Results() | ||
{ | ||
// arrange | ||
var dbName = Guid.NewGuid().ToString(); | ||
var db = GetMemoryDb(dbName); | ||
var sut = new AuthorRepository(db); | ||
var pager = new Pager(1); | ||
|
||
// act | ||
var result = await sut.GetList(x => x.Id > 0, pager); | ||
ClearMemoryDb(dbName); | ||
|
||
// assert | ||
Assert.True(result.Count() == 10); | ||
} | ||
|
||
private AppDbContext GetMemoryDb(string dbName) | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase(dbName).Options; | ||
|
||
var context = new AppDbContext(options); | ||
|
||
context.Authors.AddRange(_authors); | ||
context.SaveChanges(); | ||
|
||
return context; | ||
} | ||
|
||
private void ClearMemoryDb(string dbName) | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseInMemoryDatabase(dbName).Options; | ||
|
||
using (var context = new AppDbContext(options)) | ||
{ | ||
context.RemoveRange(_authors); | ||
context.SaveChanges(); | ||
} | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Blogifier.Core.Tests/Repositories/CustomFieldsRepositoryTests.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,67 @@ | ||
using Blogifier.Core.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Blogifier.Core.Tests.Repositories | ||
{ | ||
public class CustomFieldsRepositoryTests | ||
{ | ||
[Fact] | ||
public async Task CanSaveAndGetCustomField() | ||
{ | ||
var db = GetSut(); | ||
var sut = new CustomFieldRepository(db); | ||
|
||
sut.Add(new CustomField { AuthorId = 1, Name = "social|facebook|1", Content = "http://your.facebook.page.com" }); | ||
await db.SaveChangesAsync(); | ||
|
||
var result = sut.Single(f => f.Name.Contains("social|facebook")); | ||
Assert.NotNull(result); | ||
|
||
sut.Remove(result); | ||
await db.SaveChangesAsync(); | ||
|
||
result = sut.Single(f => f.Name.Contains("social|facebook")); | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public async Task CanSaveAndGetSocialField() | ||
{ | ||
var db = GetSut(); | ||
var sut = new CustomFieldRepository(db); | ||
|
||
await sut.SaveSocial(new SocialField { | ||
AuthorId = 0, | ||
Title = "Facebook", | ||
Icon = "fa-facebook", | ||
Name = "social|facebook|1", | ||
Rank = 1, | ||
Content = "http://your.facebook.page.com" | ||
}); | ||
|
||
var socials = await sut.GetSocial(); | ||
Assert.NotNull(socials); | ||
|
||
var result = sut.Single(f => f.Name.Contains("social|facebook")); | ||
Assert.NotNull(result); | ||
|
||
sut.Remove(result); | ||
await db.SaveChangesAsync(); | ||
|
||
result = sut.Single(f => f.Name.Contains("social|facebook")); | ||
Assert.Null(result); | ||
} | ||
|
||
private AppDbContext GetSut() | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseSqlite("DataSource=Blog.db").Options; | ||
|
||
var context = new AppDbContext(options); | ||
|
||
return context; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Blogifier.Core.Tests/Repositories/NewsletterRepositoryTests.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,44 @@ | ||
using Blogifier.Core.Data; | ||
using Blogifier.Core.Helpers; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Blogifier.Core.Tests.Repositories | ||
{ | ||
public class NewsletterRepositoryTests | ||
{ | ||
[Fact] | ||
public async Task CanGetAndRemoveNewsletterFromDb() | ||
{ | ||
var email = "test@test.com"; | ||
var db = GetSut(); | ||
var sut = new NewsletterRepository(db); | ||
|
||
sut.Add(new Newsletter { Email = email, Ip = "1.2.3", Created = SystemClock.Now() }); | ||
db.SaveChanges(); | ||
|
||
var result = await sut.GetList(x => x.Id > 0, new Pager(1)); | ||
Assert.NotNull(result); | ||
int count = result.Count(); | ||
|
||
var existing = sut.Single(x => x.Email == email); | ||
db.Newsletters.Remove(existing); | ||
db.SaveChanges(); | ||
|
||
result = await sut.GetList(x => x.Id > 0, new Pager(1)); | ||
Assert.True(result.Count() == count - 1); | ||
} | ||
|
||
private AppDbContext GetSut() | ||
{ | ||
var options = new DbContextOptionsBuilder<AppDbContext>() | ||
.UseSqlite("DataSource=Blog.db").Options; | ||
|
||
var context = new AppDbContext(options); | ||
|
||
return context; | ||
} | ||
} | ||
} |
Oops, something went wrong.