Skip to content

Commit 9a4edc5

Browse files
author
Bilal Fazlani
committed
Added unit tests for testing metadata feature
1 parent 41c1de6 commit 9a4edc5

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using TrackerEnabledDbContext.Common.Configuration;
5+
using TrackerEnabledDbContext.Common.Testing;
6+
using TrackerEnabledDbContext.Common.Testing.Extensions;
7+
using TrackerEnabledDbContext.Common.Testing.Models;
8+
9+
namespace TrackerEnabledDbContext.Identity.IntegrationTests
10+
{
11+
[TestClass]
12+
public class MetadataTests : PersistanceTests<TestTrackerIdentityContext>
13+
{
14+
[TestMethod]
15+
public void ShouldAddSingleMetadata_WhenSingleMetadataIsProvided()
16+
{
17+
Db.ConfigureMetadata(m =>
18+
{
19+
m.IpAddress = "192.168.2.23";
20+
});
21+
22+
EntityTracker.TrackAllProperties<POCO>();
23+
POCO entity = ObjectFactory.Create<POCO>();
24+
25+
Db.POCOs.Add(entity);
26+
Db.SaveChanges("bilal");
27+
28+
entity.AssertAuditForAddition(Db, entity.Id, "bilal",
29+
x => x.Color, x => x.Height, x => x.StartTime, x => x.Id);
30+
31+
entity.AssertMetadata(Db, entity.Id, new Dictionary<string, string>
32+
{
33+
["IpAddress"] = "192.168.2.23"
34+
});
35+
}
36+
37+
[TestMethod]
38+
public async Task ShouldAddSingleMetadata_WhenSingleMetadataIsProvided_Async()
39+
{
40+
Db.ConfigureMetadata(m =>
41+
{
42+
m.IpAddress = "192.168.2.23";
43+
});
44+
45+
EntityTracker.TrackAllProperties<POCO>();
46+
POCO entity = ObjectFactory.Create<POCO>();
47+
48+
Db.POCOs.Add(entity);
49+
await Db.SaveChangesAsync("bilal");
50+
51+
entity.AssertAuditForAddition(Db, entity.Id, "bilal",
52+
x => x.Color, x => x.Height, x => x.StartTime, x => x.Id);
53+
54+
entity.AssertMetadata(Db, entity.Id, new Dictionary<string, string>
55+
{
56+
["IpAddress"] = "192.168.2.23"
57+
});
58+
}
59+
60+
[TestMethod]
61+
public void ShouldNotAddMetadata_WhenValueIsNull()
62+
{
63+
Db.ConfigureMetadata(m =>
64+
{
65+
m.IpAddress = "192.168.2.23";
66+
m.Country = null;
67+
m.Device = string.Empty;
68+
});
69+
70+
EntityTracker.TrackAllProperties<POCO>();
71+
POCO entity = ObjectFactory.Create<POCO>();
72+
73+
Db.POCOs.Add(entity);
74+
Db.SaveChanges();
75+
76+
entity.AssertAuditForAddition(Db, entity.Id, null,
77+
x => x.Color, x => x.Height, x => x.StartTime, x => x.Id);
78+
79+
entity.AssertMetadata(Db, entity.Id, new Dictionary<string, string>
80+
{
81+
["IpAddress"] = "192.168.2.23",
82+
["Device"] = string.Empty
83+
});
84+
}
85+
}
86+
}

TrackerEnabledDbContext.Identity.IntegrationTests/TrackerEnabledDbContext.Identity.IntegrationTests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<HintPath>..\packages\Microsoft.AspNet.Identity.EntityFramework.2.2.1\lib\net45\Microsoft.AspNet.Identity.EntityFramework.dll</HintPath>
5858
<Private>True</Private>
5959
</Reference>
60+
<Reference Include="Microsoft.CSharp" />
6061
<Reference Include="System" />
6162
<Reference Include="System.ComponentModel.DataAnnotations" />
6263
</ItemGroup>
@@ -75,6 +76,7 @@
7576
<ItemGroup>
7677
<Compile Include="EventTestsForIdentity.cs" />
7778
<Compile Include="FluentConfigurationTestsForIdentity.cs" />
79+
<Compile Include="MetadataTests.cs" />
7880
<Compile Include="Migrations\Configuration.cs" />
7981
<Compile Include="Properties\AssemblyInfo.cs" />
8082
<Compile Include="TestTrackerIdentityContext .cs" />

TrackerEnabledDbContext.IntegrationTests/MetadataTests.cs

+27-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void ShouldAddSingleMetadata_WhenSingleMetadataIsProvided()
2626
Db.SaveChanges("bilal");
2727

2828
entity.AssertAuditForAddition(Db, entity.Id, "bilal",
29-
x => x.Color, x => x.Height, x => x.StartTime, x=>x.Id);
29+
x => x.Color, x => x.Height, x => x.StartTime, x => x.Id);
3030

3131
entity.AssertMetadata(Db, entity.Id, new Dictionary<string, string>
3232
{
@@ -56,5 +56,31 @@ public async Task ShouldAddSingleMetadata_WhenSingleMetadataIsProvided_Async()
5656
["IpAddress"] = "192.168.2.23"
5757
});
5858
}
59+
60+
[TestMethod]
61+
public void ShouldNotAddMetadata_WhenValueIsNull()
62+
{
63+
Db.ConfigureMetadata(m =>
64+
{
65+
m.IpAddress = "192.168.2.23";
66+
m.Country = null;
67+
m.Device = string.Empty;
68+
});
69+
70+
EntityTracker.TrackAllProperties<POCO>();
71+
POCO entity = ObjectFactory.Create<POCO>();
72+
73+
Db.POCOs.Add(entity);
74+
Db.SaveChanges();
75+
76+
entity.AssertAuditForAddition(Db, entity.Id, null,
77+
x => x.Color, x => x.Height, x => x.StartTime, x => x.Id);
78+
79+
entity.AssertMetadata(Db, entity.Id, new Dictionary<string, string>
80+
{
81+
["IpAddress"] = "192.168.2.23",
82+
["Device"] = string.Empty
83+
});
84+
}
5985
}
6086
}

0 commit comments

Comments
 (0)