-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
MultiLingual_Entity_Tests.cs
227 lines (189 loc) · 7.42 KB
/
MultiLingual_Entity_Tests.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.ZeroCore.SampleApp.Application.Shop;
using Abp.ZeroCore.SampleApp.Core.Shop;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
namespace Abp.Zero.MultiLingual
{
public class MultiLingual_Entity_Tests : AbpZeroTestBase
{
private readonly IProductAppService _productAppService;
private readonly IRepository<Product> _productRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public MultiLingual_Entity_Tests()
{
_productAppService = Resolve<IProductAppService>();
_productRepository = Resolve<IRepository<Product>>();
_unitOfWorkManager = Resolve<IUnitOfWorkManager>();
}
[Fact]
public async Task Create_MultiLingualEntity_With_Single_Translation_Test()
{
await _productAppService.CreateProduct(new ProductCreateDto
{
Price = 99,
Stock = 1000,
Translations = new List<ProductTranslationDto>
{
new ProductTranslationDto
{
Language = "en",
Name = "Mobile Phone"
}
}
});
using (var uow = _unitOfWorkManager.Begin())
{
var products = await _productRepository.GetAllIncluding(p => p.Translations).ToListAsync();
products.SelectMany(p => p.Translations).Count(pt => pt.Name == "Mobile Phone" && pt.Language == "en").ShouldBe(1);
await uow.CompleteAsync();
}
}
[Fact]
public async Task Create_MultiLingualEntity_With_Multiple_Translation_Test()
{
await _productAppService.CreateProduct(new ProductCreateDto
{
Price = 99,
Stock = 1000,
Translations = new List<ProductTranslationDto>
{
new ProductTranslationDto
{
Language = "en",
Name = "Mobile Phone"
},
new ProductTranslationDto
{
Language = "tr",
Name = "Cep telefonu"
}
}
});
using (var uow = _unitOfWorkManager.Begin())
{
var products = await _productRepository.GetAllIncluding(p => p.Translations).ToListAsync();
products.SelectMany(p => p.Translations).Count(pt => pt.Name == "Mobile Phone" && pt.Language == "en").ShouldBe(1);
products.SelectMany(p => p.Translations).Count(pt => pt.Name == "Cep telefonu" && pt.Language == "tr").ShouldBe(1);
await uow.CompleteAsync();
}
}
[Fact]
public async Task Update_MultiLingualEntity_With_Single_Translation_Test()
{
var product = await GetProduct("it", "Giornale");
product.ShouldNotBeNull();
product.Translations.Count.ShouldBe(1);
await _productAppService.UpdateProduct(new ProductUpdateDto
{
Id = product.Id,
Price = product.Price,
Stock = product.Stock,
Translations = new List<ProductTranslationDto>
{
new ProductTranslationDto
{
Name = "Newspaper",
Language = "en"
}
}
});
using (var uow = _unitOfWorkManager.Begin())
{
// Old Translation
product = await GetProduct("it", "Giornale");
product.ShouldBe(null);
product = await GetProduct("en", "Newspaper");
product.ShouldNotBe(null);
product.Translations.Count.ShouldBe(1);
var translation = product.Translations.First();
translation.Language.ShouldBe("en");
translation.Name.ShouldBe("Newspaper");
await uow.CompleteAsync();
}
}
[Fact]
public async Task Update_MultiLingualEntity_With_Multiple_Translation_Test()
{
var product = await GetProduct("en", "Bike");
product.ShouldNotBeNull();
product.Translations.Count.ShouldBe(2);
await _productAppService.UpdateProduct(new ProductUpdateDto
{
Id = product.Id,
Price = product.Price,
Stock = product.Stock,
Translations = new List<ProductTranslationDto>
{
new ProductTranslationDto
{
Name = "Bicycle",
Language = "en"
},
new ProductTranslationDto
{
Name = "Bicicleta",
Language = "es"
}
}
});
using (var uow = _unitOfWorkManager.Begin())
{
// Old Translation
product = await GetProduct("en", "Bike");
product.ShouldBe(null);
product = await GetProduct("en", "Bicycle");
product.ShouldNotBe(null);
product.Translations.Count.ShouldBe(2);
product.Translations.Count(pt => pt.Language == "fr").ShouldBe(0);
product.Translations.Count(pt => pt.Language == "es" && pt.Name == "Bicicleta").ShouldBe(1);
await uow.CompleteAsync();
}
}
[Fact]
public async Task Translate_MultiLingualEntity_Insert_Test()
{
var product = await GetProduct("it", "Giornale");
await _productAppService.Translate(product.Id, new ProductTranslationDto
{
Name = "Bicycle",
Language = "en"
});
product = await GetProduct("en", "Bicycle");
product.ShouldNotBeNull();
product.Translations.Count.ShouldBe(2);
product.Translations.Count(pt => pt.Language == "en" && pt.Name == "Bicycle").ShouldNotBeNull();
}
[Fact]
public async Task Translate_MultiLingualEntity_Update_Test()
{
var product = await GetProduct("it", "Giornale");
await _productAppService.Translate(product.Id, new ProductTranslationDto
{
Name = "il Giornale",
Language = "it"
});
product = await GetProduct("it", "il Giornale");
product.ShouldNotBeNull();
product.Translations.Count.ShouldBe(1);
}
private async Task<Product> GetProduct(string culture, string productName)
{
Product product;
using (var uow = _unitOfWorkManager.Begin())
{
product = await _productRepository.GetAll()
.Include(p => p.Translations)
.Where(p => p.Translations.Any(pt => pt.Language == culture && pt.Name == productName))
.FirstOrDefaultAsync();
await uow.CompleteAsync();
}
return product;
}
}
}