Skip to content

Commit 8a2ba50

Browse files
Merge pull request #1169 from dlcs/feature/bulkDeleteAdjunct
Implement bulk delete of adjuncts
2 parents 30eab1b + 325425d commit 8a2ba50

14 files changed

Lines changed: 1045 additions & 7 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using API.Exceptions;
4+
using API.Features.Customer.Infrastructure;
5+
using DLCS.Model;
6+
using Test.Helpers.Data;
7+
8+
namespace API.Tests.Features.Adjuncts.Infrastructure;
9+
10+
public class AdjunctIdentifierOnlyXTests
11+
{
12+
[Fact]
13+
public void ConvertToDictionary_ConvertsSingleAdjunctIdentifier()
14+
{
15+
// Arrange
16+
var assetId = AssetIdGenerator.GetAssetId();
17+
18+
List<AdjunctAssetIdentifier> adjunctIdentifiers =
19+
[
20+
new ()
21+
{
22+
Id = assetId.ToString(),
23+
Adjunct = [ "first", "second" ]
24+
}
25+
];
26+
27+
// Act
28+
var adjunctDictionary = adjunctIdentifiers.ConvertToDictionary(assetId.Customer);
29+
30+
// Assert
31+
adjunctDictionary.Keys.Should().Contain(assetId);
32+
adjunctDictionary.Keys.Count.Should().Be(adjunctIdentifiers.Count);
33+
adjunctDictionary.Values.Should().Contain(adjunctIdentifiers[0].Adjunct);
34+
}
35+
36+
[Fact]
37+
public void ConvertToDictionary_ConvertsMultipleAdjunctIdentifier()
38+
{
39+
// Arrange
40+
var firstAssetId = AssetIdGenerator.GetAssetId();
41+
var secondAssetId = AssetIdGenerator.GetAssetId(assetPostfix: "_1");
42+
43+
List<AdjunctAssetIdentifier> adjunctIdentifiers =
44+
[
45+
new ()
46+
{
47+
Id = firstAssetId.ToString(),
48+
Adjunct = [ "first", "second" ]
49+
},
50+
new ()
51+
{
52+
Id = secondAssetId.ToString(),
53+
Adjunct = [ "third", "fourth" ]
54+
}
55+
];
56+
57+
// Act
58+
var adjunctDictionary = adjunctIdentifiers.ConvertToDictionary(firstAssetId.Customer);
59+
60+
// Assert
61+
adjunctDictionary.Keys.Count.Should().Be(adjunctIdentifiers.Count);
62+
adjunctDictionary.Keys.Should().Contain(firstAssetId);
63+
adjunctDictionary.Values.Should().Contain(adjunctIdentifiers[0].Adjunct);
64+
adjunctDictionary.Keys.Should().Contain(secondAssetId);
65+
adjunctDictionary.Values.Should().Contain(adjunctIdentifiers[1].Adjunct);
66+
}
67+
68+
[Fact]
69+
public void ConvertToDictionary_ConcatenatesMultipleAdjunctIdentifier()
70+
{
71+
// Arrange
72+
var assetId = AssetIdGenerator.GetAssetId();
73+
74+
List<AdjunctAssetIdentifier> adjunctIdentifiers =
75+
[
76+
new ()
77+
{
78+
Id = assetId.ToString(),
79+
Adjunct = [ "first", "second" ]
80+
},
81+
new ()
82+
{
83+
Id = assetId.ToString(),
84+
Adjunct = [ "third", "fourth" ]
85+
}
86+
];
87+
88+
// Act
89+
var adjunctDictionary = adjunctIdentifiers.ConvertToDictionary(assetId.Customer);
90+
91+
// Assert
92+
var fullValues = adjunctIdentifiers[0].Adjunct;
93+
fullValues.AddRange(adjunctIdentifiers[1].Adjunct);
94+
95+
adjunctDictionary.Keys.Count.Should().Be(1);
96+
adjunctDictionary.Keys.Should().Contain(assetId);
97+
adjunctDictionary.Values.Should().Contain(fullValues);
98+
}
99+
100+
[Fact]
101+
public void ConvertToDictionary_ThrowsError_WhenAdjunctFailsToParse()
102+
{
103+
// Arrange
104+
List<AdjunctAssetIdentifier> adjunctIdentifiers =
105+
[
106+
new ()
107+
{
108+
Id = "notAnAssetId",
109+
Adjunct = [ "first", "second" ]
110+
}
111+
];
112+
113+
// Act
114+
Action action = () => adjunctIdentifiers.ConvertToDictionary(1);
115+
116+
// Assert
117+
action.Should().Throw<BadRequestException>().WithMessage("AssetId 'notAnAssetId' is invalid. Must be in format customer/space/asset");
118+
}
119+
120+
[Fact]
121+
public void ConvertToDictionary_ThrowsError_WhenDifferentCustomerId()
122+
{
123+
// Arrange
124+
var assetId = AssetIdGenerator.GetAssetId();
125+
126+
List<AdjunctAssetIdentifier> adjunctIdentifiers =
127+
[
128+
new ()
129+
{
130+
Id = assetId.ToString(),
131+
Adjunct = [ "first", "second" ]
132+
}
133+
];
134+
135+
// Act
136+
Action action = () => adjunctIdentifiers.ConvertToDictionary(1);
137+
138+
// Assert
139+
action.Should().Throw<BadRequestException>().WithMessage($"Asset id '{assetId}' cannot belong to a different customer");
140+
}
141+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using API.Features.Customer.Validation;
4+
using API.Settings;
5+
using DLCS.Core.Types;
6+
using FluentValidation.TestHelper;
7+
using Microsoft.Extensions.Options;
8+
using Test.Helpers.Data;
9+
10+
namespace API.Tests.Features.Adjuncts.Validation;
11+
12+
public class AdjunctIdListValidatorTests
13+
{
14+
private readonly AdjunctIdListValidator sut = new(Options.Create(
15+
new ApiSettings
16+
{
17+
MaxImageListSize = 4
18+
}));
19+
20+
[Fact]
21+
public void Valid_WhenSingleAdjunct()
22+
{
23+
var adjuncts = new Dictionary<AssetId, List<string>>
24+
{
25+
{AssetIdGenerator.GetAssetId(), ["first"]}
26+
};
27+
28+
var result = sut.TestValidate(adjuncts);
29+
result.ShouldNotHaveAnyValidationErrors();
30+
}
31+
32+
[Fact]
33+
public void Valid_WhenMultipleAdjunctSingleAsset()
34+
{
35+
var adjuncts = new Dictionary<AssetId, List<string>>
36+
{
37+
{AssetIdGenerator.GetAssetId(), ["first", "second"]}
38+
};
39+
40+
var result = sut.TestValidate(adjuncts);
41+
result.ShouldNotHaveAnyValidationErrors();
42+
}
43+
44+
[Fact]
45+
public void Valid_WhenSingleAdjunctMultipleAsset()
46+
{
47+
var adjuncts = new Dictionary<AssetId, List<string>>
48+
{
49+
{AssetIdGenerator.GetAssetId(), ["first"]},
50+
{AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["first"]}
51+
};
52+
53+
var result = sut.TestValidate(adjuncts);
54+
result.ShouldNotHaveAnyValidationErrors();
55+
}
56+
57+
[Fact]
58+
public void Invalid_WhenNullValue()
59+
{
60+
Dictionary<AssetId, List<string>> adjuncts = null;
61+
62+
var result = sut.TestValidate(adjuncts);
63+
result.ShouldHaveAnyValidationError();
64+
}
65+
66+
[Fact]
67+
public void Invalid_WhenEmptyValue()
68+
{
69+
Dictionary<AssetId, List<string>> adjuncts = [];
70+
71+
var result = sut.TestValidate(adjuncts);
72+
result.ShouldHaveAnyValidationError();
73+
}
74+
75+
[Fact]
76+
public void Invalid_WhenMultipleAdjunctRepeated()
77+
{
78+
var assetId = AssetIdGenerator.GetAssetId();
79+
var adjuncts = new Dictionary<AssetId, List<string>>
80+
{
81+
{assetId, ["first", "first"]}
82+
};
83+
84+
var result = sut.TestValidate(adjuncts);
85+
result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first");
86+
}
87+
88+
[Fact]
89+
public void Invalid_WhenMultipleAdjunctRepeatedWithAdditional()
90+
{
91+
var assetId = AssetIdGenerator.GetAssetId();
92+
var adjuncts = new Dictionary<AssetId, List<string>>
93+
{
94+
{assetId, ["first", "first", "second"]}
95+
};
96+
97+
var result = sut.TestValidate(adjuncts);
98+
result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first");
99+
}
100+
101+
[Fact]
102+
public void Invalid_WhenMultipleAdjunctRepeatedWithSecondAdjunct()
103+
{
104+
var assetId = AssetIdGenerator.GetAssetId();
105+
var adjuncts = new Dictionary<AssetId, List<string>>
106+
{
107+
{assetId, ["first", "first"]},
108+
{AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["second"]}
109+
};
110+
111+
var stuff3 = adjuncts.Any(kvp =>
112+
kvp.Value.SelectMany(a => a).Distinct().Count() == kvp.Value.SelectMany(a => a).Count());
113+
114+
var result = sut.TestValidate(adjuncts);
115+
result.ShouldHaveAnyValidationError().WithErrorMessage($"Members contains 1 duplicate Id(s): asset Id: {assetId} : adjunct id: first");
116+
}
117+
118+
[Fact]
119+
public void Invalid_WhenMoreAdjunctsThanBatchSize()
120+
{
121+
var assetId = AssetIdGenerator.GetAssetId();
122+
var adjuncts = new Dictionary<AssetId, List<string>>
123+
{
124+
{assetId, ["first", "second", "third", "fourth", "fifth"]}
125+
};
126+
127+
var result = sut.TestValidate(adjuncts);
128+
result.ShouldHaveAnyValidationError().WithErrorMessage("Maximum adjuncts in single batch is 4");
129+
}
130+
131+
[Fact]
132+
public void Invalid_WhenMoreSingleAdjunctAssetsThanBatchSize()
133+
{
134+
var adjuncts = new Dictionary<AssetId, List<string>>
135+
{
136+
{AssetIdGenerator.GetAssetId(), ["first"]},
137+
{AssetIdGenerator.GetAssetId(assetPostfix: "_1"), ["second"]},
138+
{AssetIdGenerator.GetAssetId(assetPostfix: "_2"), ["third"]},
139+
{AssetIdGenerator.GetAssetId(assetPostfix: "_3"), ["fourth"]},
140+
{AssetIdGenerator.GetAssetId(assetPostfix: "_4"), ["fifth"]},
141+
};
142+
143+
var result = sut.TestValidate(adjuncts);
144+
result.ShouldHaveAnyValidationError().WithErrorMessage("Maximum adjuncts in single batch is 4");
145+
}
146+
}

0 commit comments

Comments
 (0)