Skip to content

Commit

Permalink
Filter NQ results by batch
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldgray committed Jan 10, 2025
1 parent 5556ea5 commit 4bfc054
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace DLCS.Model.Assets.NamedQueries;
public class ParsedNamedQuery
{
/// <summary>
/// Collection of OrderBy clauses to apply to assets.
/// Collection of OrderBy clauses to apply to assets
/// </summary>
public List<QueryOrder> AssetOrdering { get; set; } = new() { new QueryOrder(QueryMapping.Unset) };

Expand Down Expand Up @@ -105,7 +105,6 @@ public enum QueryMapping
Number1,
Number2,
Number3,
Batch,
}

public enum OrderDirection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using DLCS.Core.Caching;
using DLCS.Core.Caching;
using DLCS.Core.Types;
using DLCS.Model.Assets;
using DLCS.Model.Assets.NamedQueries;
using DLCS.Repository.Assets;
using LazyCache.Mocks;
Expand All @@ -14,12 +14,11 @@ namespace DLCS.Repository.Tests.NamedQueries;
[Collection(DatabaseCollection.CollectionName)]
public class NamedQueryRepositoryTests
{
private readonly DlcsContext dbContext;
private readonly NamedQueryRepository sut;

public NamedQueryRepositoryTests(DlcsDatabaseFixture dbFixture)
{
dbContext = dbFixture.DbContext;
var dbContext = dbFixture.DbContext;
sut = new NamedQueryRepository(dbFixture.DbContext, new MockCachingService(),
Options.Create(new CacheSettings()));

Expand All @@ -38,6 +37,26 @@ public NamedQueryRepositoryTests(DlcsDatabaseFixture dbFixture)
dbContext.Images.AddTestAsset(AssetId.FromString("99/1/7"), space: 2);
dbContext.Images.AddTestAsset(AssetId.FromString("99/1/8"), ref1: "foo", ref2: "bar", ref3: "baz", num1: 5,
num2: 10, num3: 20);

// Batch records - first with 3 and second with 2. 1 asset is in both
var batchedAsset1 = AssetId.FromString("99/2/1");
var batchedAsset2 = AssetId.FromString("99/2/2");
var batchedAsset3 = AssetId.FromString("99/2/3");
var batchedAsset4 = AssetId.FromString("99/2/4");
const int batchId1 = 101010;
const int batchId2 = 101011;
dbContext.Images.AddTestAsset(batchedAsset1, space: 2);
dbContext.Images.AddTestAsset(batchedAsset2, space: 2, num3: 1);
dbContext.Images.AddTestAsset(batchedAsset3, space: 2, num3: 1);
dbContext.Images.AddTestAsset(batchedAsset4, space: 2, num3: 1);
var batch = dbContext.Batches.AddTestBatch(batchId1).Result;
batch.Entity
.AddBatchAsset(batchedAsset1)
.AddBatchAsset(batchedAsset2, BatchAssetStatus.Completed)
.AddBatchAsset(batchedAsset3, BatchAssetStatus.Error);

var batch2 = dbContext.Batches.AddTestBatch(batchId2).Result;
batch2.Entity.AddBatchAsset(batchedAsset1).AddBatchAsset(batchedAsset4);
dbContext.SaveChanges();
}

Expand Down Expand Up @@ -119,7 +138,7 @@ public async Task GetNamedQueryResults_ReturnsAllForCustomer_IfNoOtherCriteria()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Count().Should().Be(8);
result.Should().HaveCount(12);
}

[Fact]
Expand All @@ -135,7 +154,7 @@ public async Task GetNamedQueryResults_FilterByString1()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/1"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/1"));
}

[Fact]
Expand All @@ -151,7 +170,7 @@ public async Task GetNamedQueryResults_FilterByString2()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/2"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/2"));
}

[Fact]
Expand All @@ -167,7 +186,7 @@ public async Task GetNamedQueryResults_FilterByString3()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/3"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/3"));
}

[Fact]
Expand All @@ -183,7 +202,7 @@ public async Task GetNamedQueryResults_FilterByNumber1()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/4"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/4"));
}

[Fact]
Expand All @@ -199,7 +218,7 @@ public async Task GetNamedQueryResults_FilterByNumber2()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/5"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/5"));
}

[Fact]
Expand All @@ -215,7 +234,7 @@ public async Task GetNamedQueryResults_FilterByNumber3()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/6"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/6"));
}

[Fact]
Expand All @@ -231,7 +250,7 @@ public async Task GetNamedQueryResults_FilterBySpace()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Count().Should().Be(7);
result.Should().HaveCount(7);
}

[Fact]
Expand All @@ -247,7 +266,7 @@ public async Task GetNamedQueryResults_FilterBySpaceName()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Count().Should().Be(7);
result.Should().HaveCount(7);
}

[Fact]
Expand All @@ -263,7 +282,7 @@ public async Task GetNamedQueryResults_FilterBySpaceAndSpaceName_SpaceTakesPrior
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Count().Should().Be(7);
result.Should().HaveCount(7);
}

[Fact]
Expand All @@ -280,6 +299,54 @@ public async Task GetNamedQueryResults_FilterByMultipleCriteria()
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Single().Id.Should().Be(AssetId.FromString("99/1/8"));
result.Should().ContainSingle(r => r.Id == AssetId.FromString("99/1/8"));
}

[Fact]
public async Task GetNamedQueryResults_FilterBySingleBatch()
{
// Arrange
var query = new ParsedNamedQuery(99)
{
Batches = new[] { 101010 }
};

// Act
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Should().HaveCount(3);
}

[Fact]
public async Task GetNamedQueryResults_FilterByMultipleBatch()
{
// Arrange
var query = new ParsedNamedQuery(99)
{
Batches = new[] { 101010, 101011 }
};

// Act
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Should().HaveCount(4);
}

[Fact]
public async Task GetNamedQueryResults_FilterByMultipleBatch_AndOtherCriteria()
{
// Arrange
var query = new ParsedNamedQuery(99)
{
Batches = new[] { 101010, 101011 }, Number3 = 1
};

// Act
var result = await sut.GetNamedQueryResults(query).ToListAsync();

// Assert
result.Should().HaveCount(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using DLCS.Core.Caching;
using DLCS.Core.Collections;
using DLCS.Core.Strings;
using DLCS.Model.Assets;
using DLCS.Model.Assets.NamedQueries;
Expand Down Expand Up @@ -101,6 +102,11 @@ public IQueryable<Asset> GetNamedQueryResults(ParsedNamedQuery query)
.Select(arg => arg.Image);
}

if (!query.Batches.IsNullOrEmpty())
{
imageFilter = imageFilter.Where(i => i.BatchAssets.Any(ba => query.Batches.Contains(ba.BatchId)));
}

return imageFilter;
}
}

0 comments on commit 4bfc054

Please sign in to comment.