forked from ThoughtWorksZA/bookworm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryTest.cs
144 lines (118 loc) · 6.92 KB
/
RepositoryTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Linq;
using BirdBrain;
using BookWorm.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Linq;
namespace BookWorm.Tests.Models
{
[TestClass]
public class RepositoryTest
{
[TestMethod]
public void ShouldKnowThatCreatedModelIsAssignedAnId()
{
var testModel = new StaticPage();
var documentSession = new Mock<IDocumentSession>();
documentSession.Setup(session => session.Store(testModel)).Callback<object>(arg => testModel.Id = 1);
var repository = new Repository(documentSession.Object);
repository.Create(testModel);
documentSession.Verify(session => session.Store(testModel), Times.Once());
Assert.AreNotEqual(0, testModel.Id);
}
[TestMethod]
public void ShouldKnowHowToGetModelFromTheirId()
{
var persistedModel = new StaticPage {Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it."};
var documentSession = new Mock<IDocumentSession>();
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
var repository = new Repository(documentSession.Object);
var retrievedModel = repository.Get<StaticPage>(persistedModel.Id);
documentSession.Verify(session=>session.Load<StaticPage>(persistedModel.Id), Times.Once());
Assert.AreEqual(persistedModel.Id , retrievedModel.Id);
}
[TestMethod]
[ExpectedException(typeof(Raven.Client.Exceptions.NonUniqueObjectException))]
public void ShouldRethrowRavenNonUniqueObjectException()
{
var testModel = new StaticPage();
var documentSession = new Mock<IDocumentSession>();
documentSession.Setup(session => session.Store(testModel))
.Throws(new Raven.Client.Exceptions.NonUniqueObjectException());
var repository = new Repository(documentSession.Object);
repository.Create(testModel);
}
[TestMethod]
public void ShouldKnowHowToDeleteAModelByItsIdWhenItExists()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it." };
var documentSession = new Mock<IDocumentSession>();
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
var repository = new Repository(documentSession.Object);
repository.Delete<StaticPage>(persistedModel.Id);
documentSession.Verify(session => session.Delete(persistedModel), Times.Once());
}
[TestMethod]
[ExpectedException(typeof(System.InvalidOperationException))]
public void DeletingNonExistentDocumentShouldRethrowInvalidOperationException()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it." };
var documentSession = new Mock<IDocumentSession>();
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
documentSession.Setup(session => session.Delete(persistedModel))
.Throws(new System.InvalidOperationException());
var repository = new Repository(documentSession.Object);
repository.Delete<StaticPage>(persistedModel.Id);
}
[TestMethod]
public void ShouldKnowHowToEditAModelByItsIdWhenItExists()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it." };
var documentSession = new Mock<IDocumentSession>();
documentSession.SetupGet(x => x.Advanced).Returns(new Mock<ISyncAdvancedSessionOperation>().Object);
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
var repository = new Repository(documentSession.Object);
repository.Edit<StaticPage>(persistedModel);
documentSession.Verify(session => session.Store(persistedModel), Times.Once());
}
[TestMethod]
[ExpectedException(typeof(System.InvalidOperationException))]
public void EditingNonExistentDocumentShouldRethrowInvalidOperationException()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes veggie burger. You're going to love it." };
var documentSession = new Mock<IDocumentSession>();
documentSession.SetupGet(x => x.Advanced).Returns(new Mock<ISyncAdvancedSessionOperation>().Object);
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
documentSession.Setup(session => session.Store(persistedModel))
.Throws(new System.InvalidOperationException());
var repository = new Repository(documentSession.Object);
repository.Edit<StaticPage>(persistedModel);
}
[TestMethod]
public void ShouldKnowHowToDetatchDocumentFromSession()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it." };
var documentSession = new Mock<IDocumentSession>();
var _syncAdvancedSessionOperation = new Mock<ISyncAdvancedSessionOperation>();
documentSession.SetupGet(session => session.Advanced).Returns(_syncAdvancedSessionOperation.Object);
var repository = new Repository(documentSession.Object);
repository.Detach(persistedModel);
_syncAdvancedSessionOperation.Verify(session => session.Evict(persistedModel), Times.Once());
}
[TestMethod]
public void ShouldSetCreatedAtFromExistingValueOnEdit()
{
var persistedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it.", CreatedAt = DateTime.Now.AddMinutes(-1)};
var documentSession = new Mock<IDocumentSession>();
documentSession.SetupGet(x => x.Advanced).Returns(new Mock<ISyncAdvancedSessionOperation>().Object);
documentSession.Setup(session => session.Load<StaticPage>(persistedModel.Id)).Returns(persistedModel);
var repository = new Repository(documentSession.Object);
var editedModel = new StaticPage { Title = "Nandos Rocks", Id = 1337, Content = "Nandos makes chicken. You're going to love it.", CreatedAt = DateTime.Now };
repository.Edit<StaticPage>(editedModel);
Assert.AreEqual(persistedModel.CreatedAt, editedModel.CreatedAt);
}
}
}