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+ }
0 commit comments