Skip to content

Commit 779d599

Browse files
committed
Add unit tests for logger and make logger optional
Added xunit and moq to run tests in VS and to better test logger.
1 parent 562064a commit 779d599

File tree

9 files changed

+164
-22
lines changed

9 files changed

+164
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes will be documented in this file, particularly any breaking
33
changes. This project adheres to [Semantic Versioning](http://semver.org).
44

55
## [x.x.x]
6+
- Added - Option to configure a `RavenMigration.ILogger` or use the `ConsoleLogger`.
67

78
## [2.0.0]
89
- Changed (breaking) - The way the MigrationDocument's Id is determined. Multiple underscores

RavenMigrations.Tests/AlterTests.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Raven.Client.Indexes;
77
using Raven.Json.Linq;
88
using Raven.Tests.Helpers;
9+
using Moq;
910
using Xunit;
1011

1112
namespace RavenMigrations.Tests
@@ -76,6 +77,48 @@ public void Can_add_additional_commands_as_part_of_migration()
7677
}
7778
}
7879

80+
[Fact]
81+
public void Logger_WriteInformation_is_called_when_altering_collection()
82+
{
83+
var loggerMock = new Mock<ILogger>();
84+
85+
using (var store = NewDocumentStore())
86+
{
87+
InitialiseWithPerson(store, "Sean Kearon");
88+
89+
var migration = new AlterCollectionMigration();
90+
migration.Setup(store, loggerMock.Object);
91+
92+
migration.Up();
93+
}
94+
95+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 1), "Informational message should indicate how many documents were updated.");
96+
}
97+
98+
[Fact]
99+
public void Logger_WriteInformation_is_called_per_batch_when_altering_collection()
100+
{
101+
var loggerMock = new Mock<ILogger>();
102+
103+
using (var store = NewDocumentStore())
104+
{
105+
InitialiseWithPeople(store, new List<Person1>() {
106+
new Person1 {Name = "Sean Kearon"},
107+
new Person1 {Name = "Jared M. Smith"},
108+
new Person1 {Name = "Michael Owen"},
109+
new Person1 {Name = "Jonathan Skelton"},
110+
new Person1 {Name = "Matt King"}
111+
});
112+
var migration = new AlterCollectionMigration();
113+
migration.Setup(store, loggerMock.Object);
114+
115+
migration.Up();
116+
}
117+
118+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 2), Times.Exactly(2), "Informational message should indicate how many documents were updated.");
119+
loggerMock.Verify(logger => logger.WriteInformation("Updated {0} documents", 1), Times.Once, "Informational message should indicate how many documents were updated.");
120+
}
121+
79122
private void InitialiseWithPerson(IDocumentStore store, string name)
80123
{
81124
new RavenDocumentsByEntityName().Execute(store); //https://groups.google.com/forum/#!topic/ravendb/QqZPrRUwEkE
@@ -86,6 +129,17 @@ private void InitialiseWithPerson(IDocumentStore store, string name)
86129
}
87130
WaitForIndexing(store);
88131
}
132+
133+
private void InitialiseWithPeople(IDocumentStore store, List<Person1> people)
134+
{
135+
new RavenDocumentsByEntityName().Execute(store); //https://groups.google.com/forum/#!topic/ravendb/QqZPrRUwEkE
136+
using (var session = store.OpenSession())
137+
{
138+
people.ForEach(session.Store);
139+
session.SaveChanges();
140+
}
141+
WaitForIndexing(store);
142+
}
89143
}
90144

91145
[Migration(1, "alter")]
@@ -98,7 +152,7 @@ public override void Down()
98152

99153
public override void Up()
100154
{
101-
Alter.CollectionWithAdditionalCommands("Person1s", MigratePerson1ToPerson2);
155+
Alter.CollectionWithAdditionalCommands("Person1s", MigratePerson1ToPerson2, 2);
102156
}
103157

104158
private void MigratePerson2ToPerson1(RavenJObject doc, RavenJObject metadata)

RavenMigrations.Tests/RavenMigrations.Tests.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -32,6 +33,10 @@
3233
<WarningLevel>4</WarningLevel>
3334
</PropertyGroup>
3435
<ItemGroup>
36+
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
3540
<Reference Include="FluentAssertions">
3641
<HintPath>..\packages\FluentAssertions.2.1.0.0\lib\net45\FluentAssertions.dll</HintPath>
3742
</Reference>
@@ -49,6 +54,10 @@
4954
<Reference Include="Microsoft.WindowsAzure.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
5055
<HintPath>..\packages\WindowsAzure.Storage.2.0.6.1\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath>
5156
</Reference>
57+
<Reference Include="Moq, Version=4.5.9.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
58+
<HintPath>..\packages\Moq.4.5.9\lib\net45\Moq.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
5261
<Reference Include="Raven.Abstractions">
5362
<HintPath>..\packages\RavenDB.Client.2.5.2700\lib\net45\Raven.Abstractions.dll</HintPath>
5463
</Reference>
@@ -101,6 +110,12 @@
101110
</ItemGroup>
102111
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
103112
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
113+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
114+
<PropertyGroup>
115+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
116+
</PropertyGroup>
117+
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.1.0\build\net20\xunit.runner.visualstudio.props'))" />
118+
</Target>
104119
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
105120
Other similar extension points exist, see Microsoft.Common.targets.
106121
<Target Name="BeforeBuild">

RavenMigrations.Tests/RunnerTests.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Linq;
22
using FluentAssertions;
3+
using Moq;
34
using Raven.Abstractions.Data;
5+
using Raven.Client;
46
using Raven.Client.Indexes;
57
using Raven.Tests.Helpers;
68
using Xunit;
@@ -284,6 +286,52 @@ public void Can_call_migrations_with_custom_attributes()
284286
}
285287
}
286288
}
289+
290+
public class Logger
291+
{
292+
public class WhenMigrationRunUp : RavenTestBase
293+
{
294+
[Fact]
295+
public void Logger_WriteInformation_should_be_called_before_and_after_migration()
296+
{
297+
var mockLogger = new Mock<ILogger>();
298+
299+
using (var store = NewDocumentStore())
300+
{
301+
Runner.Run(store, new MigrationOptions { Logger = mockLogger.Object });
302+
}
303+
304+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Up migration started", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
305+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Up migration completed", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
306+
}
307+
}
308+
309+
public class WhenMigrationRunDown : RavenTestBase
310+
{
311+
[Fact]
312+
public void Logger_WriteInformation_should_be_called_before_and_after_migration()
313+
{
314+
var mockLogger = new Mock<ILogger>();
315+
316+
using (var store = NewDocumentStore())
317+
{
318+
new TestDocumentIndex().Execute(store);
319+
320+
Runner.Run(store);
321+
WaitForIndexing(store);
322+
323+
Runner.Run(store, new MigrationOptions
324+
{
325+
Direction = Directions.Down,
326+
Logger = mockLogger.Object
327+
});
328+
}
329+
330+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Down migration started", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
331+
mockLogger.Verify(logger => logger.WriteInformation("{0}: Down migration completed", typeof(First_Migration).Name), Times.AtLeastOnce, "Informational message should indicate Up migration started.");
332+
}
333+
}
334+
}
287335
}
288336

289337
public class TestDocument
@@ -388,7 +436,9 @@ public override void Up()
388436
session.SaveChanges();
389437
}
390438
}
391-
}
439+
}
440+
441+
392442

393443
public abstract class BaseMigration : Migration
394444
{
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Castle.Core" version="3.3.3" targetFramework="net45" />
34
<package id="FluentAssertions" version="2.1.0.0" targetFramework="net45" />
45
<package id="Microsoft.Data.Edm" version="5.2.0" targetFramework="net45" />
56
<package id="Microsoft.Data.OData" version="5.2.0" targetFramework="net45" />
67
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="1.8.0.0" targetFramework="net45" />
8+
<package id="Moq" version="4.5.9" targetFramework="net45" />
79
<package id="RavenDB.Client" version="2.5.2700" targetFramework="net45" />
810
<package id="RavenDB.Database" version="2.5.2700" targetFramework="net45" />
911
<package id="RavenDB.Embedded" version="2.5.2700" targetFramework="net45" />
1012
<package id="RavenDB.Tests.Helpers" version="2.5.2700" targetFramework="net45" />
1113
<package id="System.Spatial" version="5.2.0" targetFramework="net45" />
1214
<package id="WindowsAzure.Storage" version="2.0.6.1" targetFramework="net45" />
1315
<package id="xunit" version="1.9.2" targetFramework="net45" />
16+
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net45" />
1417
</packages>

RavenMigrations/Migration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public virtual void Down()
1010
{
1111
}
1212

13-
public virtual void Setup(IDocumentStore documentStore, ILogger logger)
13+
public virtual void Setup(IDocumentStore documentStore, ILogger logger = null)
1414
{
1515
DocumentStore = documentStore;
16-
Logger = logger;
16+
Logger = logger ?? new NullLogger();
1717
Alter = new Alter(documentStore, logger);
1818
}
1919

RavenMigrations/Runner.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static void Run(IDocumentStore documentStore, MigrationOptions options =
3535
switch (options.Direction)
3636
{
3737
case Directions.Down:
38+
if (migrationDoc == null)
39+
continue;
40+
3841
options.Logger.WriteInformation("{0}: Down migration started", migration.GetType().Name);
3942
migration.Down();
4043
session.Delete(migrationDoc);

RavenMigrations/Verbs/Alter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace RavenMigrations.Verbs
1111
{
1212
public class Alter
1313
{
14-
public Alter(IDocumentStore documentStore, ILogger logger)
14+
public Alter(IDocumentStore documentStore, ILogger logger = null)
1515
{
16-
Logger = logger;
16+
Logger = logger ?? new NullLogger();
1717
DocumentStore = documentStore;
1818
}
1919

readme.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,26 @@ After each migration is executed, a document of type **MigrationDocument** is in
6868
You can modify the runner options by declaring a **MigrationOptions** instance and passing it to the runner.
6969

7070
```
71-
public class MigrationOptions
71+
public class MigrationOptions
72+
{
73+
public MigrationOptions()
7274
{
73-
public MigrationOptions()
74-
{
75-
Direction = Directions.Up;
76-
Assemblies = new List<Assembly>();
77-
Profiles = new List<string>();
78-
MigrationResolver = new DefaultMigrationResolver();
79-
Assemblies = new List<Assembly>();
80-
ToVersion = 0;
81-
}
82-
83-
public Directions Direction { get; set; }
84-
public IList<Assembly> Assemblies { get; set; }
85-
public IList<string> Profiles { get; set; }
86-
public IMigrationResolver MigrationResolver { get; set; }
87-
public long ToVersion { get; set; }
75+
Direction = Directions.Up;
76+
Assemblies = new List<Assembly>();
77+
Profiles = new List<string>();
78+
MigrationResolver = new DefaultMigrationResolver();
79+
Assemblies = new List<Assembly>();
80+
ToVersion = 0;
81+
Logger = new ConsoleLogger();
8882
}
83+
84+
public Directions Direction { get; set; }
85+
public IList<Assembly> Assemblies { get; set; }
86+
public IList<string> Profiles { get; set; }
87+
public IMigrationResolver MigrationResolver { get; set; }
88+
public long ToVersion { get; set; }
89+
public ILogger Logger { get; set; }
90+
}
8991
```
9092

9193
### Profiles
@@ -266,6 +268,20 @@ Example usage in a migration:
266268
}
267269
```
268270

271+
## Logging
272+
273+
Specify a logger when configuring the runner.
274+
275+
By default the `ConsoleLogger` is used.
276+
277+
Implement the `RavenMigrations.ILogger` interface to create your own logger:
278+
279+
```
280+
void WriteInformation(string format, params object[] args);
281+
void WriteError(string format, params object[] args);
282+
void WriteWarning(string format, params object[] args);
283+
```
284+
269285
## Integration
270286

271287
We suggest you run the migrations at the start of your application to ensure that any new changes you have made apply to your application before you application starts. If you do not want to do it here, you can choose to do it out of band using a seperate application.

0 commit comments

Comments
 (0)