Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 82a7ecc

Browse files
author
Morten Turn Pedersen
committed
Begun adding unit tests and image
1 parent 00164f5 commit 82a7ecc

File tree

12 files changed

+179
-7
lines changed

12 files changed

+179
-7
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
- name: projects
3+
type: string
4+
default: '**/*[Tt]ests/*.csproj'
5+
6+
- name: buildConfiguration
7+
type: string
8+
default: 'Release'
9+
10+
steps:
11+
- task: DotNetCoreCLI@2
12+
displayName: 'Run unit tests'
13+
inputs:
14+
command: 'test'
15+
projects: '${{parameters.projects}}'
16+
arguments: '--configuration ${{parameters.buildConfiguration}}'

Build Configurations/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ steps:
2222
projects: '**/NetCoreEntityFramework.csproj'
2323
buildConfiguration: '$(buildConfiguration)'
2424
skipBuild: true
25-
skipInstall: true
25+
skipInstall: true
26+
27+
- template: 'Templates/Test Net Core.yml'
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Nodes.NetCore.EntityFramework.Tests.Mocks;
3+
using NUnit.Framework;
4+
using System;
5+
using System.Threading.Tasks;
6+
using TestContext = Nodes.NetCore.EntityFramework.Tests.Mocks.TestContext;
7+
8+
namespace Nodes.NetCore.EntityFramework.Tests
9+
{
10+
public class EntityRepositoryTests
11+
{
12+
private TestEntityRepository _repository;
13+
private TestContext _context;
14+
private TestEntity _entity;
15+
16+
[SetUp]
17+
public void Setup()
18+
{
19+
var options = new DbContextOptionsBuilder<TestContext>()
20+
.UseInMemoryDatabase(Guid.NewGuid().ToString())
21+
.Options;
22+
23+
_context = new TestContext(options);
24+
25+
_repository = new TestEntityRepository(_context);
26+
27+
DateTime now = DateTime.UtcNow;
28+
29+
_entity = new TestEntity
30+
{
31+
Created = now,
32+
Deleted = false,
33+
Id = Guid.NewGuid(),
34+
Updated = now,
35+
Property = string.Empty
36+
};
37+
38+
_context.Table.Add(_entity);
39+
40+
_context.SaveChanges();
41+
42+
_repository = new TestEntityRepository(_context);
43+
}
44+
45+
[Test]
46+
public async Task AddAddsEntityAndSetsAttributes()
47+
{
48+
int startSize = await _context.Table.CountAsync();
49+
int expectedSize = startSize + 1;
50+
var entity = new TestEntity();
51+
52+
using(_repository)
53+
{
54+
await _repository.Add(entity);
55+
}
56+
57+
Assert.NotNull(entity.Id);
58+
Assert.AreNotEqual(default(DateTime), entity.Created);
59+
Assert.AreNotEqual(default(DateTime), entity.Updated);
60+
Assert.IsFalse(entity.Deleted);
61+
Assert.AreEqual(expectedSize, await _context.Table.CountAsync());
62+
}
63+
64+
[Test]
65+
public async Task AddEntityWithIdKeepsId()
66+
{
67+
Guid id = Guid.NewGuid();
68+
var entity = new TestEntity
69+
{
70+
Id = id
71+
};
72+
73+
using (_repository)
74+
{
75+
await _repository.Add(entity);
76+
}
77+
78+
Assert.AreEqual(id, entity.Id);
79+
}
80+
}
81+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace Nodes.NetCore.EntityFramework.Tests.Mocks
4+
{
5+
public class TestContext : DbContext
6+
{
7+
public TestContext(DbContextOptions options) : base(options) { }
8+
9+
public DbSet<TestEntity> Table { get; set; }
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Nodes.NetCore.EntityFramework.Models;
2+
3+
namespace Nodes.NetCore.EntityFramework.Tests.Mocks
4+
{
5+
public class TestEntity : EntityBase
6+
{
7+
public string Property { get; set; }
8+
}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Nodes.NetCore.EntityFramework.Repositories;
2+
3+
namespace Nodes.NetCore.EntityFramework.Tests.Mocks
4+
{
5+
public class TestEntityRepository : EntityRepository<TestEntity, TestContext>
6+
{
7+
public TestEntityRepository(TestContext context) : base(context, context.Table)
8+
{
9+
}
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
8+
<AssemblyName>Nodes.NetCore.EntityFramework.Tests</AssemblyName>
9+
10+
<RootNamespace>Nodes.NetCore.EntityFramework.Tests</RootNamespace>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
15+
<PackageReference Include="nunit" Version="3.12.0" />
16+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\NetCoreEntityFramework\NetCoreEntityFramework.csproj" />
25+
</ItemGroup>
26+
27+
</Project>

NetCoreEntityFramework.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.29905.134
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreEntityFramework", "NetCoreEntityFramework\NetCoreEntityFramework.csproj", "{30F23EBE-4571-4215-8D70-F776A71BA51E}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreEntityFramework", "NetCoreEntityFramework\NetCoreEntityFramework.csproj", "{30F23EBE-4571-4215-8D70-F776A71BA51E}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCoreEntityFramework.Tests", "NetCoreEntityFramework.Tests\NetCoreEntityFramework.Tests.csproj", "{170EDF01-1CAD-49CF-9837-D0076667247B}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +17,10 @@ Global
1517
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{30F23EBE-4571-4215-8D70-F776A71BA51E}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{170EDF01-1CAD-49CF-9837-D0076667247B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{170EDF01-1CAD-49CF-9837-D0076667247B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{170EDF01-1CAD-49CF-9837-D0076667247B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{170EDF01-1CAD-49CF-9837-D0076667247B}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

NetCoreEntityFramework/Models/EntityBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using System;
1+
using System;
32
using System.ComponentModel.DataAnnotations;
43
using System.ComponentModel.DataAnnotations.Schema;
54

NetCoreEntityFramework/NetCoreEntityFramework.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<PackageId>Nodes.NetCore.EntityFramework.Helpers</PackageId>
6-
<Version>0.2.0</Version>
6+
<Version>1.0.0</Version>
77
<Authors>Nodes</Authors>
88
<Company>Nodes</Company>
99
<Product>.NET Core Entity Framework Helpers</Product>
1010
<Description>The package contains base classes for entities and repositories</Description>
1111
<AssemblyName>Nodes.NetCore.EntityFramework</AssemblyName>
1212
<RootNamespace>Nodes.NetCore.EntityFramework</RootNamespace>
13+
<PackageIcon>Nodes logo.png</PackageIcon>
14+
<RepositoryUrl>https://github.com/nodes-dotnet/net-core-entity-framework</RepositoryUrl>
15+
<RepositoryType>Github</RepositoryType>
1316
</PropertyGroup>
1417

1518
<ItemGroup>
1619
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
1720
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
1821
</ItemGroup>
1922

23+
<ItemGroup>
24+
<None Include="Nodes logo.png">
25+
<Pack>True</Pack>
26+
<PackagePath></PackagePath>
27+
</None>
28+
</ItemGroup>
29+
2030
</Project>

0 commit comments

Comments
 (0)