Browse Source

Added unit test. Added feature to generate ids for in memory database.

pull/96/head
Halil İbrahim Kalkan 9 years ago
parent
commit
b258738fef
  1. 2
      src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabase.cs
  2. 31
      src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/InMemoryIdGenerator.cs
  3. 10
      src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs
  4. 12
      src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  5. 23
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/PersonAppService_Tests.cs
  6. 1
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/IPersonAppService.cs
  7. 10
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs

2
src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabase.cs

@ -5,5 +5,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
public interface IMemoryDatabase
{
List<TEntity> Collection<TEntity>();
TPrimaryKey GenerateNextId<TEntity, TPrimaryKey>();
}
}

31
src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/InMemoryIdGenerator.cs

@ -0,0 +1,31 @@
using System;
using System.Threading;
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
internal class InMemoryIdGenerator
{
private int _lastInt;
private long _lastLong;
public TPrimaryKey GenerateNext<TPrimaryKey>()
{
if (typeof(TPrimaryKey) == typeof(Guid))
{
return (TPrimaryKey)(object)Guid.NewGuid();
}
if (typeof(TPrimaryKey) == typeof(int))
{
return (TPrimaryKey)(object)Interlocked.Increment(ref _lastInt);
}
if (typeof(TPrimaryKey) == typeof(long))
{
return (TPrimaryKey)(object)Interlocked.Increment(ref _lastLong);
}
throw new AbpException("Not supported PrimaryKey type: " + typeof(TPrimaryKey).FullName);
}
}
}

10
src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs

@ -8,16 +8,24 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
{
private readonly ConcurrentDictionary<Type, object> _sets;
private readonly object _syncObj = new object();
private readonly ConcurrentDictionary<Type, InMemoryIdGenerator> _idGenerators;
public MemoryDatabase()
{
_sets = new ConcurrentDictionary<Type, object>();
_idGenerators = new ConcurrentDictionary<Type, InMemoryIdGenerator>();
}
public List<TEntity> Collection<TEntity>()
{
return _sets.GetOrAdd(typeof(TEntity), _ => new List<TEntity>()) as List<TEntity>;
}
public TPrimaryKey GenerateNextId<TEntity, TPrimaryKey>()
{
return _idGenerators
.GetOrAdd(typeof(TEntity), () => new InMemoryIdGenerator())
.GenerateNext<TPrimaryKey>();
}
}
}

12
src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs

@ -33,10 +33,22 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
public override TEntity Insert(TEntity entity, bool autoSave = false)
{
SetIdIfNeeded(entity);
Collection.Add(entity);
return entity;
}
private void SetIdIfNeeded(TEntity entity)
{
if (typeof(TPrimaryKey) == typeof(int) || typeof(TPrimaryKey) == typeof(long) || typeof(TPrimaryKey) == typeof(Guid))
{
if (entity.IsTransient())
{
entity.Id = Database.GenerateNextId<TEntity, TPrimaryKey>();
}
}
}
public override TEntity Update(TEntity entity)
{
return entity;

23
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/PersonAppService_Tests.cs

@ -153,5 +153,28 @@ namespace Volo.Abp.AspNetCore.Mvc
personInDb.ShouldNotBeNull();
personInDb.Phones.Any(p => p.Number == phoneNumberToAdd).ShouldBeTrue();
}
[Fact]
public async Task GetPhones_Test()
{
var douglas = _personRepository.GetList().First(p => p.Name == "Douglas");
//Ideally should be [GET] /api/person/{id}/phones?type=office
var result = await GetResponseAsObjectAsync<ListResultDto<PhoneDto>>("/api/services/app/person/GetPhones?id=" + douglas.Id);
result.Items.Count.ShouldBe(douglas.Phones.Count);
}
[Fact]
public async Task DeletePhone_Test()
{
var douglas = _personRepository.GetList().First(p => p.Name == "Douglas");
var firstPhone = douglas.Phones.First();
//Ideally should be [DELETE] /api/app/person/{id}
await Client.DeleteAsync("/api/services/app/person/DeletePhone?id=" + douglas.Id + "&phoneId=" + firstPhone.Id);
douglas = _personRepository.GetList().First(p => p.Name == "Douglas");
douglas.Phones.Any(p => p.Id == firstPhone.Id).ShouldBeFalse();
}
}
}

1
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/IPersonAppService.cs

@ -7,7 +7,6 @@ namespace Volo.Abp.TestApp.Application
{
public interface IPersonAppService : IAsyncCrudAppService<PersonDto>
{
//URL: [GET] /api/people/{id}?type=office
Task<ListResultDto<PhoneDto>> GetPhones(Guid id, GetPersonPhonesFilter filter);
Task<PhoneDto> AddPhone(Guid id, PhoneDto phoneDto);

10
test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestDataBuilder.cs

@ -16,7 +16,15 @@ namespace Volo.Abp.TestApp
public void Build()
{
_personRepository.Insert(new Person(Guid.NewGuid(), "Douglas", 42));
AddPeople();
}
private void AddPeople()
{
var douglas = new Person(Guid.NewGuid(), "Douglas", 42);
douglas.Phones.Add(new Phone(douglas.Id, "123456789"));
douglas.Phones.Add(new Phone(douglas.Id, "123456780", PhoneType.Home));
_personRepository.Insert(douglas);
}
}
}
Loading…
Cancel
Save