Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Expand generic repository functionalities #1346

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public IEnumerable<T> GetAll()
.ToList<T>();
}

public async Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default)
{
IQueryable<T> query = this.context.Set<T>();
if (expression != null) query = query.Where(expression);

return await query.ToDynamicListAsync<T>(cancellationToken: cancellationToken);
}

public virtual async Task<T?> GetByIdAsync(object id)
{
var t = await this.context.Set<T>().FindAsync(id);
Expand Down Expand Up @@ -80,5 +88,20 @@ public async Task<PaginatedResult<T>> GetPaginatedListAsync(

return new PaginatedResult<T>(items, count, pageNumber, pageSize);
}

public async Task<int> CountAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default)
{
IQueryable<T> query = this.context.Set<T>();
if (expression != null) query = query.Where(expression);

return await query
.AsNoTracking()
.CountAsync(cancellationToken: cancellationToken);
}

public async Task<bool> ExistsAsync(Expression<Func<T, bool>> expression, CancellationToken cancellationToken = default)
{
return await this.context.Set<T>().AnyAsync(expression, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,17 @@ namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Repositories
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Infrastructure.Repositories;
using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases;
using UnitTests.Bases;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;

[TestFixture]
public class GenericRepositoryTests : RepositoryTestBase
{
private MockRepository mockRepository;

[SetUp]
public void SetUp()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);

var context = SetupDbContext();

_ = context.Database.EnsureDeleted();
Expand Down Expand Up @@ -154,8 +149,6 @@ public async Task DeleteTest()
Assert.IsNull(context.Set<DeviceModelProperty>().Find(entityId));
}



[Test]
public async Task GetPaginatedListAsync_CustomFilter_ExpectedPageReturned()
{
Expand Down Expand Up @@ -232,5 +225,209 @@ await context.AddRangeAsync(new List<DeviceTag>
_ = secondDeviceTag.Required.Should().BeFalse();
_ = secondDeviceTag.Searchable.Should().BeTrue();
}

[Test]
public async Task CountAsync_WithoutFilter_ExpectedCountReturned()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
},
new()
{
Id = "device_tag_02",
Label = "Location 2",
Required = true,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Act
var result = await instance.CountAsync();

// Assert
_ = result.Should().Be(2);
}

[Test]
public async Task CountAsync_CustomFilter_ExpectedCountReturned()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
},
new()
{
Id = "device_tag_02",
Label = "Location 2",
Required = true,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Filter only tags with labels containing "location 1"
var deviceTagPredicate = PredicateBuilder.True<DeviceTag>()
.And(tag => tag.Label.ToLowerInvariant().Contains("location 1"));

// Act
var result = await instance.CountAsync(deviceTagPredicate);

// Assert
_ = result.Should().Be(1);
}

[Test]
public async Task ExistsAsync_ExistingData_ReturnsTrue()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Act
var result = await instance.ExistsAsync(tag => tag.Id.Equals("device_tag_01"));

// Assert
_ = result.Should().BeTrue();
}

[Test]
public async Task ExistsAsync_NonExistingData_ReturnsFalse()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Act
var result = await instance.ExistsAsync(tag => tag.Id.Equals("device_tag_03"));

// Assert
_ = result.Should().BeFalse();
}

[Test]
public async Task GetAllAsync_WithFilter_AllItemsReturned()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
},
new()
{
Id = "device_tag_02",
Label = "Location 2",
Required = true,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Act
var result = await instance.GetAllAsync();

// Assert
_ = result.Count().Should().Be(2);
}

[Test]
public async Task GetAllAsync_CustomFilter_ExpectedItemsReturned()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
},
new()
{
Id = "device_tag_02",
Label = "Location 2",
Required = true,
Searchable = true
}
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Filter only tags with labels required
var deviceTagPredicate = PredicateBuilder.True<DeviceTag>()
.And(tag => tag.Required);

// Act
var result = await instance.GetAllAsync(deviceTagPredicate);

// Assert
_ = result.Count().Should().Be(1);
}
}
}
6 changes: 6 additions & 0 deletions src/AzureIoTHubPortal.Domain/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface IRepository<T> where T : EntityBase
{
IEnumerable<T> GetAll();

Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default);

Task<T?> GetByIdAsync(object id);

Task InsertAsync(T obj);
Expand All @@ -22,5 +24,9 @@ public interface IRepository<T> where T : EntityBase
void Delete(object id);

Task<PaginatedResult<T>> GetPaginatedListAsync(int pageNumber, int pageSize, string[]? orderBy = null, Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default);

Task<int> CountAsync(Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default);

Task<bool> ExistsAsync(Expression<Func<T, bool>> expression, CancellationToken cancellationToken = default);
}
}