Skip to content

Commit 7b1a518

Browse files
committed
More testing
1 parent 01f526d commit 7b1a518

27 files changed

Lines changed: 492 additions & 124 deletions
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using UnitTestingDemo.Data.Adapters;
4+
using UnitTestingDemo.Domain;
5+
6+
namespace UnitTestingDemo.Data.Tests.Adapters
7+
{
8+
[ExcludeFromCodeCoverage]
9+
[TestClass]
10+
public class BookDomainEntityAdapterTests
11+
{
12+
private BookDomainEntityAdapter _bookDomainEntityAdapter;
13+
14+
[TestInitialize]
15+
public void TestInitialize()
16+
{
17+
_bookDomainEntityAdapter = new BookDomainEntityAdapter();
18+
}
19+
20+
[TestMethod]
21+
public void Adapt_NullItem_ReturnsNull()
22+
{
23+
//Arrange
24+
Book book = null;
25+
26+
//Act
27+
var bookEntity = _bookDomainEntityAdapter.Adapt(book);
28+
29+
//Assert
30+
Assert.IsNull(bookEntity);
31+
}
32+
33+
[TestMethod]
34+
public void Adapt_Id_IsEqual()
35+
{
36+
//Arrange
37+
var book = new Book
38+
{
39+
Id = 1,
40+
Title = "Test Title",
41+
};
42+
43+
//Act
44+
var bookEntity = _bookDomainEntityAdapter.Adapt(book);
45+
46+
//Assert
47+
Assert.AreEqual(book.Id, bookEntity.Id);
48+
}
49+
50+
[TestMethod]
51+
public void Adapt_Title_IsEqual()
52+
{
53+
//Arrange
54+
var book = new Book
55+
{
56+
Id = 1,
57+
Title = "Test Title"
58+
};
59+
60+
//Act
61+
var bookEntity = _bookDomainEntityAdapter.Adapt(book);
62+
63+
//Assert
64+
Assert.AreEqual(book.Title, bookEntity.Title);
65+
}
66+
67+
[TestMethod]
68+
public void Adapt_AuthorId_IsEqual()
69+
{
70+
//Arrange
71+
var author= new Author{Id = 1, FirstName = "Chris", LastName = "Ayers"};
72+
var book= new Book
73+
{
74+
Id = 1,
75+
Title = "Test Title",
76+
AuthorId = 1,
77+
Author = author
78+
};
79+
80+
//Act
81+
var bookEntity = _bookDomainEntityAdapter.Adapt(book);
82+
83+
//Assert
84+
Assert.AreEqual(book.AuthorId, bookEntity.AuthorId);
85+
}
86+
87+
[TestMethod]
88+
public void Adapt_AuthorName_IsEqual()
89+
{
90+
//Arrange
91+
var author = new Author { Id = 1, FirstName = "Chris", LastName = "Ayers" };
92+
var book = new Book
93+
{
94+
Id = 1,
95+
Title = "Test Title",
96+
AuthorId = 1,
97+
Author = author
98+
};
99+
100+
//Act
101+
var bookEntity = _bookDomainEntityAdapter.Adapt(book);
102+
103+
//Assert
104+
Assert.AreEqual(book.Author.FirstName, bookEntity.Author.FirstName);
105+
Assert.AreEqual(book.Author.LastName, bookEntity.Author.LastName);
106+
}
107+
}
108+
}

UnitTestingDemo/UnitTestingDemo.Data.Tests/Adapters/BookEntityDomainAdapterTests.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System.Diagnostics.CodeAnalysis;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using UnitTestingDemo.Data.Adapters;
34
using UnitTestingDemo.Data.EntityModels;
45

56
namespace UnitTestingDemo.Data.Tests.Adapters
67
{
8+
[ExcludeFromCodeCoverage]
79
[TestClass]
810
public class BookEntityDomainAdapterTests
911
{
@@ -36,8 +38,6 @@ public void Adapt_Id_IsEqual()
3638
{
3739
Id = 1,
3840
Title = "Test Title",
39-
Author = "Chris Ayers"
40-
4141
};
4242

4343
//Act
@@ -54,9 +54,7 @@ public void Adapt_Title_IsEqual()
5454
var bookEntity = new BookEntity
5555
{
5656
Id = 1,
57-
Title = "Test Title",
58-
Author = "Chris Ayers"
59-
57+
Title = "Test Title"
6058
};
6159

6260
//Act
@@ -67,22 +65,44 @@ public void Adapt_Title_IsEqual()
6765
}
6866

6967
[TestMethod]
70-
public void Adapt_Author_IsEqual()
68+
public void Adapt_AuthorId_IsEqual()
7169
{
7270
//Arrange
71+
var authorEntity = new AuthorEntity {Id = 1, FirstName = "Chris", LastName = "Ayers"};
7372
var bookEntity = new BookEntity
7473
{
7574
Id = 1,
7675
Title = "Test Title",
77-
Author = "Chris Ayers"
76+
AuthorId = 1,
77+
Author = authorEntity
78+
};
7879

80+
//Act
81+
var book = _bookEntityDomainAdapter.Adapt(bookEntity);
82+
83+
//Assert
84+
Assert.AreEqual(bookEntity.AuthorId, book.AuthorId);
85+
}
86+
87+
[TestMethod]
88+
public void Adapt_AuthorName_IsEqual()
89+
{
90+
//Arrange
91+
var authorEntity = new AuthorEntity { Id = 1, FirstName = "Chris", LastName = "Ayers" };
92+
var bookEntity = new BookEntity
93+
{
94+
Id = 1,
95+
Title = "Test Title",
96+
AuthorId = 1,
97+
Author = authorEntity
7998
};
8099

81100
//Act
82101
var book = _bookEntityDomainAdapter.Adapt(bookEntity);
83102

84103
//Assert
85-
Assert.AreEqual(bookEntity.Author, book.Author);
104+
Assert.AreEqual(bookEntity.Author.FirstName, book.Author.FirstName);
105+
Assert.AreEqual(bookEntity.Author.LastName, book.Author.LastName);
86106
}
87107
}
88108
}

UnitTestingDemo/UnitTestingDemo.Data.Tests/UnitTestingDemo.Data.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
12-
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
11+
<PackageReference Include="Moq" Version="4.10.1" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
13+
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

UnitTestingDemo/UnitTestingDemo.Data/Adapters/BookDomainEntityAdapter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,21 @@ public BookEntity Adapt(Book book)
1515
{
1616
Id = book.Id,
1717
Title = book.Title,
18-
Author = book.Title
19-
};
18+
AuthorId = book.AuthorId,
19+
Author = AdaptAuthor(book.Author)
20+
};
21+
}
22+
23+
private AuthorEntity AdaptAuthor(Author bookAuthor)
24+
{
25+
return bookAuthor == null
26+
? null
27+
: new AuthorEntity
28+
{
29+
Id = bookAuthor.Id,
30+
FirstName = bookAuthor.FirstName,
31+
LastName = bookAuthor.LastName
32+
};
2033
}
2134
}
2235
}

UnitTestingDemo/UnitTestingDemo.Data/Adapters/BookEntityDomainAdapter.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ public Book Adapt(BookEntity bookEntity)
1515
{
1616
Id = bookEntity.Id,
1717
Title = bookEntity.Title,
18-
Author = bookEntity.Author
18+
AuthorId = bookEntity.AuthorId,
19+
Author = AdaptAuthor(bookEntity.Author)
20+
};
21+
}
22+
23+
private Author AdaptAuthor(AuthorEntity bookEntityAuthor)
24+
{
25+
return bookEntityAuthor == null
26+
? null
27+
: new Author
28+
{
29+
Id = bookEntityAuthor.Id,
30+
FirstName = bookEntityAuthor.FirstName,
31+
LastName = bookEntityAuthor.LastName
1932
};
2033
}
2134
}

UnitTestingDemo/UnitTestingDemo.Data/BookRepository.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
4+
using Microsoft.EntityFrameworkCore;
55
using UnitTestingDemo.Data.Contexts;
66
using UnitTestingDemo.Data.EntityModels;
77
using UnitTestingDemo.Data.Interfaces;
8-
using UnitTestingDemo.Domain;
98

109
namespace UnitTestingDemo.Data
1110
{
@@ -20,12 +19,18 @@ public BookRepository(BookContext bookContext)
2019

2120
public IList<BookEntity> GetAll()
2221
{
23-
return _bookContext.Books.ToList();
22+
return _bookContext
23+
.Books
24+
.Include(b=>b.Author)
25+
.ToList();
2426
}
2527

2628
public BookEntity GetById(int id)
2729
{
28-
return _bookContext.Books.FirstOrDefault(x => x.Id == id);
30+
return _bookContext
31+
.Books
32+
.Include(b => b.Author)
33+
.FirstOrDefault(x => x.Id == id);
2934
}
3035

3136
public void Save(BookEntity saveThis)

UnitTestingDemo/UnitTestingDemo.Data/Contexts/BookContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public BookContext(DbContextOptions<BookContext> options)
1010
{ }
1111

1212
public DbSet<BookEntity> Books { get; set; }
13+
public DbSet<AuthorEntity> Authors { get; set; }
1314
}
1415
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
3+
using UnitTestingDemo.Data.Interfaces;
4+
5+
namespace UnitTestingDemo.Data.EntityModels
6+
{
7+
[ExcludeFromCodeCoverage]
8+
public class AuthorEntity : IIntId
9+
{
10+
public int Id { get; set; }
11+
public string FirstName { get; set; }
12+
public string LastName { get; set; }
13+
public virtual IList<BookEntity> Books { get; set; } = new List<BookEntity>();
14+
}
15+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using UnitTestingDemo.Data.Interfaces;
1+
using System.Diagnostics.CodeAnalysis;
2+
using UnitTestingDemo.Data.Interfaces;
23

34
namespace UnitTestingDemo.Data.EntityModels
45
{
6+
[ExcludeFromCodeCoverage]
57
public class BookEntity : IIntId
68
{
79
public int Id { get; set; }
810
public string Title { get; set; }
9-
public string Author { get; set; }
11+
public int AuthorId { get; set; }
12+
public virtual AuthorEntity Author { get; set; }
1013
}
1114
}

UnitTestingDemo/UnitTestingDemo.Data/Migrations/20190218234220_InitialCreate.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)