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

chore(graphql): Add model property names test #1434

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,70 @@
using Attachment = Digdir.Domain.Dialogporten.GraphQL.EndUser.DialogById.Attachment;
using DomainAttachmentUrl = Digdir.Domain.Dialogporten.Domain.Attachments.AttachmentUrl;

namespace Digdir.Domain.Dialogporten.GraphQl.Unit.Tests.ObjectTypes;

public class AttachmentTests
{
[Fact]
public void Attachment_Object_Type_Should_Match_Property_Names_On_AttachmentEntity()
{
// Arrange
var ignoreList = new List<string>
{
nameof(Domain.Attachments.Attachment.CreatedAt),
nameof(Domain.Attachments.Attachment.UpdatedAt)
};

var attachmentProperties = typeof(Attachment)
.GetProperties()
.Select(p => p.Name)
.ToList();

var domainAttachmentProperties = typeof(Domain.Attachments.Attachment)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();

var missingProperties = domainAttachmentProperties
.Except(attachmentProperties, StringComparer.OrdinalIgnoreCase)
.ToList();

// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql Attachment: {string.Join(", ", missingProperties)}");
}
Comment on lines +8 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage and documentation.

While the test validates domain properties exist in GraphQL type, consider these improvements:

  1. Add XML documentation explaining the test's purpose and excluded properties
  2. Add bidirectional validation to ensure GraphQL type doesn't have extra properties
  3. Add test cases for inheritance scenarios
+    /// <summary>
+    /// Validates that all domain model properties (except audit fields) are present in the GraphQL type.
+    /// Excluded properties:
+    /// - CreatedAt: Audit field not exposed in GraphQL
+    /// - UpdatedAt: Audit field not exposed in GraphQL
+    /// </summary>
     [Fact]
     public void Attachment_Object_Type_Should_Match_Property_Names_On_AttachmentEntity()
     {
         // Arrange
         var ignoreList = new List<string>
         {
             nameof(Domain.Attachments.Attachment.CreatedAt),
             nameof(Domain.Attachments.Attachment.UpdatedAt)
         };

         var attachmentProperties = typeof(Attachment)
             .GetProperties()
             .Select(p => p.Name)
             .ToList();

         var domainAttachmentProperties = typeof(Domain.Attachments.Attachment)
             .GetProperties()
             .Select(p => p.Name)
             .Where(name => !ignoreList.Contains(name))
             .ToList();

+        // Check domain properties exist in GraphQL type
         var missingProperties = domainAttachmentProperties
             .Except(attachmentProperties, StringComparer.OrdinalIgnoreCase)
             .ToList();

+        // Check no extra properties in GraphQL type
+        var extraProperties = attachmentProperties
+            .Except(domainAttachmentProperties, StringComparer.OrdinalIgnoreCase)
+            .ToList();

         // Assert
         Assert.True(missingProperties.Count == 0,
             $"Properties missing in graphql Attachment: {string.Join(", ", missingProperties)}");
+        Assert.True(extraProperties.Count == 0,
+            $"Extra properties in graphql Attachment: {string.Join(", ", extraProperties)}");
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[Fact]
public void Attachment_Object_Type_Should_Match_Property_Names_On_AttachmentEntity()
{
// Arrange
var ignoreList = new List<string>
{
nameof(Domain.Attachments.Attachment.CreatedAt),
nameof(Domain.Attachments.Attachment.UpdatedAt)
};
var attachmentProperties = typeof(Attachment)
.GetProperties()
.Select(p => p.Name)
.ToList();
var domainAttachmentProperties = typeof(Domain.Attachments.Attachment)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();
var missingProperties = domainAttachmentProperties
.Except(attachmentProperties, StringComparer.OrdinalIgnoreCase)
.ToList();
// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql Attachment: {string.Join(", ", missingProperties)}");
}
/// <summary>
/// Validates that all domain model properties (except audit fields) are present in the GraphQL type.
/// Excluded properties:
/// - CreatedAt: Audit field not exposed in GraphQL
/// - UpdatedAt: Audit field not exposed in GraphQL
/// </summary>
[Fact]
public void Attachment_Object_Type_Should_Match_Property_Names_On_AttachmentEntity()
{
// Arrange
var ignoreList = new List<string>
{
nameof(Domain.Attachments.Attachment.CreatedAt),
nameof(Domain.Attachments.Attachment.UpdatedAt)
};
var attachmentProperties = typeof(Attachment)
.GetProperties()
.Select(p => p.Name)
.ToList();
var domainAttachmentProperties = typeof(Domain.Attachments.Attachment)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();
// Check domain properties exist in GraphQL type
var missingProperties = domainAttachmentProperties
.Except(attachmentProperties, StringComparer.OrdinalIgnoreCase)
.ToList();
// Check no extra properties in GraphQL type
var extraProperties = attachmentProperties
.Except(domainAttachmentProperties, StringComparer.OrdinalIgnoreCase)
.ToList();
// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql Attachment: {string.Join(", ", missingProperties)}");
Assert.True(extraProperties.Count == 0,
$"Extra properties in graphql Attachment: {string.Join(", ", extraProperties)}");
}


[Fact]
public void AttachmentUrl_Object_Type_Should_Match_Property_Names_On_DialogAttachmentUrl()
{
// Arrange
var ignoreList = new List<string>
{
nameof(DomainAttachmentUrl.CreatedAt),
nameof(DomainAttachmentUrl.UpdatedAt),
nameof(DomainAttachmentUrl.ConsumerTypeId),
nameof(DomainAttachmentUrl.AttachmentId),
nameof(DomainAttachmentUrl.Attachment)
};

var attachmentUrlProperties = typeof(GraphQL.EndUser.DialogById.AttachmentUrl)
.GetProperties()
.Select(p => p.Name)
.ToList();

var domainAttachmentUrlProperties = typeof(DomainAttachmentUrl)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();

var missingProperties = domainAttachmentUrlProperties
.Except(attachmentUrlProperties, StringComparer.OrdinalIgnoreCase)
.ToList();

// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql AttachmentUrl: {string.Join(", ", missingProperties)}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities;
using Digdir.Domain.Dialogporten.GraphQL.EndUser.DialogById;
using DialogStatus = Digdir.Domain.Dialogporten.GraphQL.EndUser.Common.DialogStatus;
using DialogStatusValues = Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.DialogStatus.Values;

namespace Digdir.Domain.Dialogporten.GraphQl.Unit.Tests.ObjectTypes;

public class DialogEntityTests
{
[Fact]
public void Dialog_Object_Type_Should_Match_Property_Names_On_DialogEntity()
{
// Arrange
var ignoreList = new List<string>
{
nameof(DialogEntity.Deleted),
nameof(DialogEntity.DeletedAt),
nameof(DialogEntity.StatusId),
nameof(DialogEntity.SearchTags),
nameof(DialogEntity.SeenLog),
nameof(DialogEntity.DialogEndUserContext)
};

var dialogProperties = typeof(DialogEntity)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();

var domainDialogProperties = typeof(Dialog)
.GetProperties()
.Select(p => p.Name)
.ToList();

var missingProperties = dialogProperties.Except(domainDialogProperties, StringComparer.OrdinalIgnoreCase).ToList();

// Assert
Assert.True(missingProperties.Count == 0, $"Properties missing in graphql dialog: {string.Join(", ", missingProperties)}");
}

[Fact]
public void DialogStatus_Object_Type_Should_Match_Property_Names_On_DialogStatusValues()
{
// Arrange
var dialogStatusValues = typeof(DialogStatus)
.GetProperties()
.Select(p => p.Name)
.ToList();

var domainDialogStatusValues = typeof(DialogStatusValues)
.GetProperties()
.Select(p => p.Name)
.ToList();

var missingProperties = domainDialogStatusValues.Except(dialogStatusValues, StringComparer.OrdinalIgnoreCase).ToList();

// Assert
Assert.True(missingProperties.Count == 0, $"Properties missing in graphql dialog status: {string.Join(", ", missingProperties)}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Actions;
using Digdir.Domain.Dialogporten.GraphQL.EndUser.DialogById;

namespace Digdir.Domain.Dialogporten.GraphQl.Unit.Tests.ObjectTypes;

public class GuiActionTests
{
[Fact]
public void DialogGuiAction_Object_Type_Should_Match_Property_Names_On_GuiAction()
{
// Arrange
var ignoreList = new List<string>
{
nameof(DialogGuiAction.CreatedAt),
nameof(DialogGuiAction.UpdatedAt),
nameof(DialogGuiAction.PriorityId),
nameof(DialogGuiAction.HttpMethodId),
nameof(DialogGuiAction.DialogId),
nameof(DialogGuiAction.Dialog)
};

var domainGuiActionProperties = typeof(DialogGuiAction)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();

var guiActionProperties = typeof(GuiAction)
.GetProperties()
.Select(p => p.Name)
.ToList();

var missingProperties = domainGuiActionProperties
.Except(guiActionProperties, StringComparer.OrdinalIgnoreCase)
.ToList();

// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql GuiAction: {string.Join(", ", missingProperties)}");
}

[Fact]
public void GuiActionPriority_Object_Type_Should_Match_Property_Names_On_DialogGuiActionPriorityValues()
{
// Arrange
var guiActionPriorities = typeof(GuiActionPriority)
.GetProperties()
.Select(p => p.Name)
.ToList();

var domainGuiActionPriorities = typeof(DialogGuiActionPriority.Values)
.GetProperties()
.Select(p => p.Name)
.ToList();

var missingProperties = domainGuiActionPriorities
.Except(guiActionPriorities, StringComparer.OrdinalIgnoreCase)
.ToList();

// Assert
Assert.True(missingProperties.Count == 0,
$"Properties missing in graphql GuiActionPriority: {string.Join(", ", missingProperties)}");
}
Comment on lines +42 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance enum value validation.

The current test only checks property names but for enums, we should also validate that the actual values match between domain and GraphQL types.

Consider adding value comparison:

     [Fact]
     public void GuiActionPriority_Object_Type_Should_Match_Property_Names_On_DialogGuiActionPriorityValues()
     {
-        // Arrange
-        var guiActionPriorities = typeof(GuiActionPriority)
-            .GetProperties()
-            .Select(p => p.Name)
-            .ToList();
-
-        var domainGuiActionPriorities = typeof(DialogGuiActionPriority.Values)
-            .GetProperties()
-            .Select(p => p.Name)
-            .ToList();
-
-        var missingProperties = domainGuiActionPriorities
-            .Except(guiActionPriorities, StringComparer.OrdinalIgnoreCase)
-            .ToList();
-
-        // Assert
-        Assert.True(missingProperties.Count == 0,
-            $"Properties missing in graphql GuiActionPriority: {string.Join(", ", missingProperties)}");
+        // Check property names
+        AssertPropertyNamesMatch<DialogGuiActionPriority.Values, GuiActionPriority>();
+
+        // Check enum values match
+        var domainValues = typeof(DialogGuiActionPriority.Values)
+            .GetProperties()
+            .ToDictionary(p => p.Name, p => p.GetValue(null));
+
+        var graphqlValues = typeof(GuiActionPriority)
+            .GetProperties()
+            .ToDictionary(p => p.Name, p => p.GetValue(null));
+
+        foreach (var (name, domainValue) in domainValues)
+        {
+            Assert.True(
+                graphqlValues.TryGetValue(name, out var graphqlValue) && 
+                domainValue?.ToString() == graphqlValue?.ToString(),
+                $"Value mismatch for {name}: Domain={domainValue}, GraphQL={graphqlValue}");
+        }
     }

Committable suggestion skipped: line range outside the PR's diff.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Transmissions;
using Digdir.Domain.Dialogporten.GraphQL.EndUser.DialogById;

namespace Digdir.Domain.Dialogporten.GraphQl.Unit.Tests.ObjectTypes;

public class DialogTransmissionTests
{
[Fact]
public void Transmission_Object_Type_Should_Match_Property_Names_On_DialogTransmission()
{
// Arrange
var ignoreList = new List<string>
{
nameof(DialogTransmission.RelatedTransmission),
nameof(DialogTransmission.RelatedTransmissions),
nameof(DialogTransmission.Activities),
nameof(DialogTransmission.Dialog),
nameof(DialogTransmission.DialogId),
nameof(DialogTransmission.TypeId)
};

var domainTransmissionProperties = typeof(DialogTransmission)
.GetProperties()
.Select(p => p.Name)
.Where(name => !ignoreList.Contains(name))
.ToList();

var transmissionProperties = typeof(Transmission)
.GetProperties()
.Select(p => p.Name)
.ToList();

var missingProperties = domainTransmissionProperties.Except(transmissionProperties, StringComparer.OrdinalIgnoreCase).ToList();

// Assert
Assert.True(missingProperties.Count == 0, $"Properties missing in graphql transmission: {string.Join(", ", missingProperties)}");
}

[Fact]
public void TransmissionType_Object_Type_Should_Match_Property_Names_On_DialogTransmissionTypeValues()
{
// Arrange
var transmissionTypes = typeof(TransmissionType)
.GetFields()
.Select(f => f.Name)
.ToList();

var domainTransmissionTypes = typeof(DialogTransmissionType.Values)
.GetFields()
.Select(f => f.Name)
.ToList();

var missingProperties = domainTransmissionTypes.Except(transmissionTypes, StringComparer.OrdinalIgnoreCase).ToList();

// Assert
Assert.True(missingProperties.Count == 0, $"Properties missing in graphql TransmissionType: {string.Join(", ", missingProperties)}");
}
Comment on lines +39 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance enum validation and add documentation.

The test would benefit from documentation and more thorough validation.

Apply these improvements:

+    /// <summary>
+    /// Validates that all enum values from the domain DialogTransmissionType are represented in the GraphQL TransmissionType.
+    /// </summary>
     [Fact]
     public void TransmissionType_Object_Type_Should_Match_Property_Names_On_DialogTransmissionTypeValues()
     {
         // Arrange
-        var transmissionTypes = typeof(TransmissionType)
+        var graphqlEnumValues = typeof(TransmissionType)
             .GetFields()
-            .Select(f => f.Name)
+            .Where(f => f.IsLiteral && !f.IsSpecialName)
+            .ToDictionary(f => f.Name, f => f.GetRawConstantValue())
             .ToList();
 
-        var domainTransmissionTypes = typeof(DialogTransmissionType.Values)
+        var domainEnumValues = typeof(DialogTransmissionType.Values)
             .GetFields()
-            .Select(f => f.Name)
+            .Where(f => f.IsLiteral && !f.IsSpecialName)
+            .ToDictionary(f => f.Name, f => f.GetRawConstantValue())
             .ToList();
 
-        var missingProperties = domainTransmissionTypes.Except(transmissionTypes, StringComparer.OrdinalIgnoreCase).ToList();
+        var missingInGraphQL = domainEnumValues.Keys
+            .Except(graphqlEnumValues.Keys)
+            .ToList();
+
+        var extraInGraphQL = graphqlEnumValues.Keys
+            .Except(domainEnumValues.Keys)
+            .ToList();
+
+        var valueMismatches = domainEnumValues
+            .Where(dv => graphqlEnumValues.ContainsKey(dv.Key) && 
+                  !Equals(dv.Value, graphqlEnumValues[dv.Key]))
+            .Select(dv => dv.Key)
+            .ToList();
 
         // Assert
-        Assert.True(missingProperties.Count == 0, $"Properties missing in graphql TransmissionType: {string.Join(", ", missingProperties)}");
+        Assert.Empty(missingInGraphQL, 
+            $"Enum values missing in GraphQL: {string.Join(", ", missingInGraphQL)}");
+        Assert.Empty(extraInGraphQL, 
+            $"Extra enum values in GraphQL: {string.Join(", ", extraInGraphQL)}");
+        Assert.Empty(valueMismatches, 
+            $"Enum values with mismatched values: {string.Join(", ", valueMismatches)}");
     }

This enhancement:

  1. Adds documentation explaining the test's purpose
  2. Validates both enum names and their underlying values
  3. Checks for missing and extra values in both directions
  4. Provides clearer error messages
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
[Fact]
public void TransmissionType_Object_Type_Should_Match_Property_Names_On_DialogTransmissionTypeValues()
{
// Arrange
var transmissionTypes = typeof(TransmissionType)
.GetFields()
.Select(f => f.Name)
.ToList();
var domainTransmissionTypes = typeof(DialogTransmissionType.Values)
.GetFields()
.Select(f => f.Name)
.ToList();
var missingProperties = domainTransmissionTypes.Except(transmissionTypes, StringComparer.OrdinalIgnoreCase).ToList();
// Assert
Assert.True(missingProperties.Count == 0, $"Properties missing in graphql TransmissionType: {string.Join(", ", missingProperties)}");
}
/// <summary>
/// Validates that all enum values from the domain DialogTransmissionType are represented in the GraphQL TransmissionType.
/// </summary>
[Fact]
public void TransmissionType_Object_Type_Should_Match_Property_Names_On_DialogTransmissionTypeValues()
{
// Arrange
var graphqlEnumValues = typeof(TransmissionType)
.GetFields()
.Where(f => f.IsLiteral && !f.IsSpecialName)
.ToDictionary(f => f.Name, f => f.GetRawConstantValue())
.ToList();
var domainEnumValues = typeof(DialogTransmissionType.Values)
.GetFields()
.Where(f => f.IsLiteral && !f.IsSpecialName)
.ToDictionary(f => f.Name, f => f.GetRawConstantValue())
.ToList();
var missingInGraphQL = domainEnumValues.Keys
.Except(graphqlEnumValues.Keys)
.ToList();
var extraInGraphQL = graphqlEnumValues.Keys
.Except(domainEnumValues.Keys)
.ToList();
var valueMismatches = domainEnumValues
.Where(dv => graphqlEnumValues.ContainsKey(dv.Key) &&
!Equals(dv.Value, graphqlEnumValues[dv.Key]))
.Select(dv => dv.Key)
.ToList();
// Assert
Assert.Empty(missingInGraphQL,
$"Enum values missing in GraphQL: {string.Join(", ", missingInGraphQL)}");
Assert.Empty(extraInGraphQL,
$"Extra enum values in GraphQL: {string.Join(", ", extraInGraphQL)}");
Assert.Empty(valueMismatches,
$"Enum values with mismatched values: {string.Join(", ", valueMismatches)}");
}

}