Skip to content

Commit 13d91a7

Browse files
committed
Implementing test cases
1 parent cafdd15 commit 13d91a7

5 files changed

Lines changed: 370 additions & 30 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using SmartStore.Core.Data;
8+
using SmartStore.Core.Domain.Catalog;
9+
using SmartStore.Core.Domain.DataExchange;
10+
using SmartStore.Core.Domain.Orders;
11+
using SmartStore.Services.DataExchange;
12+
using SmartStore.Tests;
13+
14+
namespace SmartStore.Services.Tests.DataExchange
15+
{
16+
[TestFixture]
17+
public class SyncMappingServiceTests : ServiceTest
18+
{
19+
IRepository<SyncMapping> _rs;
20+
ISyncMappingService _service;
21+
22+
[SetUp]
23+
public new void SetUp()
24+
{
25+
_rs = new MemoryRepository<SyncMapping>();
26+
_service = new SyncMappingService(_rs);
27+
28+
_service.InsertSyncMappings(
29+
"App1", "Product",
30+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
31+
new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10" }
32+
);
33+
34+
_service.InsertSyncMappings(
35+
"App1", "Order",
36+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
37+
new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10" }
38+
);
39+
40+
_service.InsertSyncMappings(
41+
"App2", "Product",
42+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
43+
new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10" }
44+
);
45+
46+
_service.InsertSyncMappings(
47+
"App2", "Order",
48+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
49+
new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10"}
50+
);
51+
}
52+
53+
[Test]
54+
public void Throws_on_invalid_sequences()
55+
{
56+
Assert.Throws<InvalidOperationException>(() =>
57+
{
58+
_service.InsertSyncMappings(
59+
"App", "Entity",
60+
new int[] { 1, 2, 3, 4, 5, 6, 7, 8 },
61+
new string[] { "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10" }
62+
);
63+
});
64+
65+
Assert.Throws<InvalidOperationException>(() =>
66+
{
67+
_service.InsertSyncMappings(
68+
"App", "Entity",
69+
new int[] { },
70+
new string[] { }
71+
);
72+
});
73+
}
74+
75+
[Test]
76+
public void Can_load_all_mappings()
77+
{
78+
var mappings = _service.GetAllSyncMappings();
79+
mappings.Count.ShouldEqual(40);
80+
81+
var mappings2 = _service.GetAllSyncMappings("App1");
82+
mappings2.Count.ShouldEqual(20);
83+
84+
var mappings3 = _service.GetAllSyncMappings(null, "Product");
85+
mappings3.Count.ShouldEqual(20);
86+
87+
var mappings4 = _service.GetAllSyncMappings("App2", "Order");
88+
mappings4.Count.ShouldEqual(10);
89+
}
90+
91+
[Test]
92+
public void Can_load_mapping_by_entity()
93+
{
94+
var product = new Product { Id = 5 };
95+
96+
var mapping = _service.GetSyncMappingByEntity(product, "App2");
97+
Assert.NotNull(mapping);
98+
mapping.EntityName.ShouldEqual("Product");
99+
mapping.EntityId.ShouldEqual(5);
100+
mapping.SourceKey.ShouldEqual("e5");
101+
}
102+
103+
[Test]
104+
public void Can_load_mapping_by_source()
105+
{
106+
var mapping = _service.GetSyncMappingBySource("e3", "Order", "App1");
107+
Assert.NotNull(mapping);
108+
mapping.EntityName.ShouldEqual("Order");
109+
mapping.ContextName.ShouldEqual("App1");
110+
mapping.EntityId.ShouldEqual(3);
111+
mapping.SourceKey.ShouldEqual("e3");
112+
}
113+
114+
[Test]
115+
public void Can_delete_mappings()
116+
{
117+
_service.DeleteSyncMappings("App1");
118+
_rs.Table.Count().ShouldEqual(20);
119+
120+
_service.DeleteSyncMappings("App2", "Product");
121+
_rs.Table.Count().ShouldEqual(10);
122+
123+
_service.DeleteSyncMappings("App2", "Order");
124+
_rs.Table.Count().ShouldEqual(0);
125+
}
126+
127+
[Test]
128+
public void Can_delete_mappings_for_entity()
129+
{
130+
var product = new Product { Id = 5 };
131+
132+
_service.DeleteSyncMappingsFor(product);
133+
_rs.Table.Count().ShouldEqual(38);
134+
}
135+
136+
}
137+
}

src/Tests/SmartStore.Services.Tests/SmartStore.Services.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Configuration\ConfigurationProviderTests.cs" />
9696
<Compile Include="Customers\CustomerExtensionTests.cs" />
9797
<Compile Include="Customers\CustomerRegistrationServiceTests.cs" />
98+
<Compile Include="DataExchange\SyncMappingServiceTests.cs" />
9899
<Compile Include="Directory\CurrencyServiceTests.cs" />
99100
<Compile Include="Directory\TestExchangeRateProvider.cs" />
100101
<Compile Include="Directory\MeasureServiceTests.cs" />

src/Tests/SmartStore.Tests/MemoryRepository.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace SmartStore.Tests
1414
{
1515
public class MemoryRepository<T> : IRepository<T> where T : BaseEntity, new()
1616
{
17-
private readonly IDictionary<int, T> _data = new Dictionary<int, T>();
17+
private readonly TestDbSet<T> _dbSet = new TestDbSet<T>();
1818
private IDbContext _dbContext;
1919

2020
public IQueryable<T> Table
2121
{
22-
get { return _data.Values.AsQueryable(); }
22+
get { return _dbSet; }
2323
}
2424

2525
public IQueryable<T> TableUntracked
@@ -29,54 +29,51 @@ public IQueryable<T> TableUntracked
2929

3030
public ICollection<T> Local
3131
{
32-
get { return _data.Values; }
32+
get { return _dbSet.Local; }
3333
}
3434

3535
public T Create()
3636
{
37-
return Activator.CreateInstance<T>();
37+
return _dbSet.Create<T>();
3838
}
3939

4040
public T GetById(object id)
4141
{
42-
int id2 = (int)id;
43-
T entity;
44-
if (_data.TryGetValue(id2, out entity))
45-
{
46-
return entity;
47-
}
48-
49-
return null;
42+
return _dbSet.Find(id);
5043
}
5144

5245
public void Insert(T entity)
5346
{
54-
_data[entity.Id] = entity;
47+
if (entity.Id <= 0)
48+
{
49+
entity.Id = Local.Count == 0 ? 1 : Local.Max(x => x.Id) + 1;
50+
}
51+
_dbSet.Add(entity);
5552
}
5653

5754
public void InsertRange(IEnumerable<T> entities, int batchSize = 100)
5855
{
59-
entities.Each(x => _data[x.Id] = x);
56+
entities.Each(x => Insert(x));
6057
}
6158

6259
public void Update(T entity)
6360
{
64-
_data[entity.Id] = entity;
61+
// Noop
6562
}
6663

6764
public void UpdateRange(IEnumerable<T> entities)
6865
{
69-
entities.Each(x => _data[x.Id] = x);
66+
entities.Each(x => Update(x));
7067
}
7168

7269
public void Delete(T entity)
7370
{
74-
_data.Remove(entity.Id);
71+
_dbSet.Remove(entity);
7572
}
7673

7774
public void DeleteRange(IEnumerable<T> entities)
7875
{
79-
entities.Each(x => Delete(x));
76+
_dbSet.RemoveRange(entities);
8077
}
8178

8279
public IQueryable<T> Expand(IQueryable<T> query, string path)
@@ -105,18 +102,14 @@ public IDbContext Context
105102
{
106103
if (_dbContext == null)
107104
{
108-
var dbSet = MockRepository.GenerateMock<DbSet<T>>();
109-
dbSet.Expect(x => ((IQueryable<T>)x).Provider).Return(_data.Values.AsQueryable().Provider);
110-
dbSet.Expect(x => ((IQueryable<T>)x).Expression).Return(_data.Values.AsQueryable().Expression);
111-
dbSet.Expect(x => ((IQueryable<T>)x).ElementType).Return(_data.Values.AsQueryable().ElementType);
112-
dbSet.Expect(x => ((IQueryable<T>)x).GetEnumerator()).Return(_data.Values.AsQueryable().GetEnumerator());
113-
114-
_dbContext = MockRepository.GenerateMock<IDbContext>();
115-
_dbContext.Expect(x => x.Set<T>()).Return(dbSet);
116-
_dbContext.Expect(x => x.SaveChangesAsync()).Return(Task.FromResult<int>(0));
117-
_dbContext.Expect(x => x.ExecuteStoredProcedureList<T>(Arg<string>.Is.Anything)).Return(new List<T>());
118-
_dbContext.Expect(x => x.GetModifiedProperties(Arg<BaseEntity>.Is.Anything)).Return(new Dictionary<string, object>());
119-
_dbContext.Expect(x => x.BeginTransaction(Arg<IsolationLevel>.Is.Anything)).Return(MockRepository.GenerateMock<ITransaction>());
105+
var ctx = MockRepository.GenerateMock<IDbContext>();
106+
ctx.Stub(x => x.Set<T>()).Return(_dbSet);
107+
ctx.Stub(x => x.SaveChangesAsync()).Return(Task.FromResult<int>(0));
108+
ctx.Stub(x => x.ExecuteStoredProcedureList<T>(Arg<string>.Is.Anything, Arg<object[]>.Is.Anything)).Return(new List<T>());
109+
ctx.Stub(x => x.GetModifiedProperties(Arg<BaseEntity>.Is.Anything)).Return(new Dictionary<string, object>());
110+
ctx.Stub(x => x.BeginTransaction(Arg<IsolationLevel>.Is.Anything)).Return(MockRepository.GenerateMock<ITransaction>());
111+
112+
_dbContext = ctx;
120113
}
121114

122115
return _dbContext;

src/Tests/SmartStore.Tests/SmartStore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="ExceptionAssert.cs" />
9090
<Compile Include="MemoryRepository.cs" />
9191
<Compile Include="Properties\AssemblyInfo.cs" />
92+
<Compile Include="TestDbSet.cs" />
9293
<Compile Include="TestExtensions.cs" />
9394
<Compile Include="TestsBase.cs" />
9495
<Compile Include="TypeAssert.cs" />

0 commit comments

Comments
 (0)