Skip to content

Commit 63ba43d

Browse files
committed
Remove non-sensical testcases. Add caching in ObjectExtensions.
1 parent 1815dec commit 63ba43d

File tree

7 files changed

+22
-97
lines changed

7 files changed

+22
-97
lines changed
+22-6
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
1+
using System.Collections.Concurrent;
12
using System.Reflection;
23
using JsonApiDotNetCore.OpenApi.Client;
34

45
namespace OpenApiClientTests;
56

67
internal static class ObjectExtensions
78
{
9+
private static readonly ConcurrentDictionary<string, PropertyInfo> PropertyInfoCache = new();
10+
811
public static object? GetPropertyValue(this object source, string propertyName)
912
{
1013
ArgumentGuard.NotNull(source);
1114
ArgumentGuard.NotNull(propertyName);
1215

13-
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(attribute => attribute.Name == propertyName);
16+
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
17+
18+
return PropertyInfoCache[cacheKey].GetValue(source);
19+
}
20+
21+
private static string EnsurePropertyInfoIsCached(object source, string propertyName)
22+
{
23+
string cacheKey = $"{source.GetType().FullName}-{propertyName}";
24+
25+
if (!PropertyInfoCache.ContainsKey(cacheKey))
26+
{
27+
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(attribute => attribute.Name == propertyName);
28+
PropertyInfoCache[cacheKey] = propertyInfo;
29+
}
1430

15-
return propertyInfo.GetValue(source);
31+
return cacheKey;
1632
}
1733

1834
public static void SetPropertyValue(this object source, string propertyName, object? value)
1935
{
2036
ArgumentGuard.NotNull(source);
2137
ArgumentGuard.NotNull(propertyName);
2238

23-
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(attribute => attribute.Name == propertyName);
39+
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
2440

25-
propertyInfo.SetValue(source, value);
41+
PropertyInfoCache[cacheKey].SetValue(source, value);
2642
}
2743

2844
public static object? GetDefaultValueForProperty(this object source, string propertyName)
2945
{
3046
ArgumentGuard.NotNull(source);
3147
ArgumentGuard.NotNull(propertyName);
3248

33-
PropertyInfo propertyInfo = source.GetType().GetProperties().Single(attribute => attribute.Name == propertyName);
49+
string cacheKey = EnsurePropertyInfoIsCached(source, propertyName);
3450

35-
return Activator.CreateInstance(propertyInfo.PropertyType);
51+
return Activator.CreateInstance(PropertyInfoCache[cacheKey].PropertyType);
3652
}
3753
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesDisabled/ModelStateValidationDisabled/CreateResourceTests.cs

-83
Original file line numberDiff line numberDiff line change
@@ -442,87 +442,4 @@ public async Task Can_exclude_relationship_without_partial_attribute_serializati
442442
relationshipsObject.ShouldNotContainPath(jsonPropertyName);
443443
});
444444
}
445-
446-
[Theory]
447-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToOne), "requiredToOne", Skip = "Known limitation")]
448-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToMany), "requiredToMany", Skip = "Known limitation")]
449-
public async Task Cannot_exclude_relationship_with_partial_attribute_serialization(string relationshipPropertyName, string jsonPropertyName)
450-
{
451-
// Arrange
452-
var requestDocument = new ResourcePostRequestDocument
453-
{
454-
Data = new ResourceDataInPostRequest
455-
{
456-
Attributes = _fakers.PostAttributes.Generate(),
457-
Relationships = new ResourceRelationshipsInPostRequest
458-
{
459-
ToOne = _fakers.NullableToOne.Generate(),
460-
RequiredToOne = _fakers.NullableToOne.Generate(),
461-
ToMany = _fakers.ToMany.Generate(),
462-
RequiredToMany = _fakers.ToMany.Generate()
463-
}
464-
}
465-
};
466-
467-
ResourceRelationshipsInPostRequest emptyRelationshipsObject = new();
468-
object? defaultValue = emptyRelationshipsObject.GetPropertyValue(relationshipPropertyName);
469-
requestDocument.Data.Relationships.SetPropertyValue(relationshipPropertyName, defaultValue);
470-
471-
using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null);
472-
var apiClient = new NrtOffMsvOffClient(wrapper.HttpClient);
473-
474-
using (apiClient.WithPartialAttributeSerialization<ResourcePostRequestDocument, ResourceAttributesInPostRequest>(requestDocument))
475-
{
476-
// Act
477-
Func<Task<ResourcePrimaryResponseDocument?>> action = async () =>
478-
await ApiResponse.TranslateAsync(async () => await apiClient.PostResourceAsync(requestDocument));
479-
480-
// Assert
481-
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
482-
JsonSerializationException exception = assertion.Subject.Single();
483-
484-
exception.Message.Should()
485-
.Be($"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
486-
}
487-
}
488-
489-
[Theory]
490-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToOne), "requiredToOne", Skip = "Known limitation")]
491-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToMany), "requiredToMany", Skip = "Known limitation")]
492-
public async Task Cannot_exclude_relationship_without_partial_attribute_serialization(string relationshipPropertyName, string jsonPropertyName)
493-
{
494-
// Arrange
495-
var requestDocument = new ResourcePostRequestDocument
496-
{
497-
Data = new ResourceDataInPostRequest
498-
{
499-
Attributes = _fakers.PostAttributes.Generate(),
500-
Relationships = new ResourceRelationshipsInPostRequest
501-
{
502-
ToOne = _fakers.NullableToOne.Generate(),
503-
RequiredToOne = _fakers.NullableToOne.Generate(),
504-
ToMany = _fakers.ToMany.Generate(),
505-
RequiredToMany = _fakers.ToMany.Generate()
506-
}
507-
}
508-
};
509-
510-
ResourceRelationshipsInPostRequest emptyRelationshipsObject = new();
511-
object? defaultValue = emptyRelationshipsObject.GetPropertyValue(relationshipPropertyName);
512-
requestDocument.Data.Relationships.SetPropertyValue(relationshipPropertyName, defaultValue);
513-
514-
using var wrapper = FakeHttpClientWrapper.Create(HttpStatusCode.NoContent, null);
515-
var apiClient = new NrtOffMsvOffClient(wrapper.HttpClient);
516-
517-
// Act
518-
Func<Task<ResourcePrimaryResponseDocument?>> action = async () =>
519-
await ApiResponse.TranslateAsync(async () => await apiClient.PostResourceAsync(requestDocument));
520-
521-
// Assert
522-
ExceptionAssertions<JsonSerializationException> assertion = await action.Should().ThrowExactlyAsync<JsonSerializationException>();
523-
JsonSerializationException exception = assertion.Subject.Single();
524-
525-
exception.Message.Should()
526-
.Be($"Cannot write a null value for property 'id'. Property requires a value. Path 'data.relationships.{jsonPropertyName}.data'.");
527-
}
528445
}

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesDisabled/ModelStateValidationDisabled/ResourceFieldValidationFakers.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Bogus;
22
using OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesDisabled.ModelStateValidationDisabled.GeneratedCode;
3-
using TestBuildingBlocks;
43

54
namespace OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesDisabled.ModelStateValidationDisabled;
65

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesDisabled/ModelStateValidationEnabled/ResourceFieldValidationFakers.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Bogus;
22
using OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesDisabled.ModelStateValidationEnabled.GeneratedCode;
3-
using TestBuildingBlocks;
43

54
namespace OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesDisabled.ModelStateValidationEnabled;
65

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesEnabled/ModelStateValidationDisabled/CreateResourceTests.cs

-4
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,6 @@ public async Task Can_exclude_relationship_without_partial_attribute_serializati
524524

525525
[Theory]
526526
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredNonNullableToOne), "requiredNonNullableToOne")]
527-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredNullableToOne), "requiredNullableToOne", Skip = "Known limitation")]
528-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToMany), "requiredToMany", Skip = "Known limitation")]
529527
public async Task Cannot_exclude_relationship_with_partial_attribute_serialization(string relationshipPropertyName, string jsonPropertyName)
530528
{
531529
// Arrange
@@ -570,8 +568,6 @@ public async Task Cannot_exclude_relationship_with_partial_attribute_serializati
570568

571569
[Theory]
572570
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredNonNullableToOne), "requiredNonNullableToOne")]
573-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredNullableToOne), "requiredNullableToOne", Skip = "Known limitation")]
574-
[InlineData(nameof(ResourceRelationshipsInPostRequest.RequiredToMany), "requiredToMany", Skip = "Known limitation")]
575571
public async Task Cannot_exclude_relationship_without_partial_attribute_serialization(string relationshipPropertyName, string jsonPropertyName)
576572
{
577573
// Arrange

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesEnabled/ModelStateValidationDisabled/ResourceFieldValidationFakers.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Bogus;
22
using OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesEnabled.ModelStateValidationDisabled.GeneratedCode;
3-
using TestBuildingBlocks;
43

54
namespace OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesEnabled.ModelStateValidationDisabled;
65

test/OpenApiClientTests/ResourceFieldValidation/NullableReferenceTypesEnabled/ModelStateValidationEnabled/ResourceFieldValidationFakers.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Bogus;
22
using OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesEnabled.ModelStateValidationEnabled.GeneratedCode;
3-
using TestBuildingBlocks;
43

54
namespace OpenApiClientTests.ResourceFieldValidation.NullableReferenceTypesEnabled.ModelStateValidationEnabled;
65

0 commit comments

Comments
 (0)