Skip to content

Commit

Permalink
Update namespaces, upgrade abstractions version, add tests for new Fi…
Browse files Browse the repository at this point in the history
…ndReflectedType method
  • Loading branch information
jordimontana82 committed Oct 13, 2023
1 parent cb0368c commit c300afa
Show file tree
Hide file tree
Showing 71 changed files with 168 additions and 78 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## [2.3.3]

### Added

- New method to retrieve early bound types based on EntityTypeCode.

### Changed

- Upgraded GitHub Actions to update Java major version to run SonarCloud analysis - https://github.com/DynamicsValue/fake-xrm-easy/issues/110
º
- Update namespaces in tests project for consistency
- Upgraded GitHub Actions to update Java major version to run SonarCloud analysis - https://github.com/DynamicsValue/fake-xrm-easy/issues/110
- Introduced new NewEntityRecord method to easily create instances of entity records based on the current use of early-bound or late-bound entities
- Resolves an issue with query evaluation and MultiOptionSets when using late bound entities or if type information is not present. - https://github.com/DynamicsValue/fake-xrm-easy/issues/66

Expand Down
12 changes: 6 additions & 6 deletions src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@
</ItemGroup>

<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2011" Version="[2.3.3-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2013" Version="[2.3.3-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2015'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2015" Version="[2.3.3-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_2016'">
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v2016" Version="[2.3.3-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_365'">
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v365" Version="[2.3.3-*,3.0)" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='FAKE_XRM_EASY_9'">
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.3.2-*,3.0)" />
<PackageReference Include="FakeXrmEasy.Abstractions.v9" Version="[2.3.3-*,3.0)" />
</ItemGroup>

<Target Name="PreparePackageReleaseNotesFromFile" BeforeTargets="GenerateNuspec">
Expand Down
78 changes: 77 additions & 1 deletion src/FakeXrmEasy.Core/XrmFakedContext.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace FakeXrmEasy
public partial class XrmFakedContext : IXrmFakedContext
{
/// <summary>
///
/// Finds the early-bound type based on an entity's logical name
/// </summary>
/// <param name="logicalName"></param>
/// <returns></returns>
Expand All @@ -45,6 +45,35 @@ public Type FindReflectedType(string logicalName)

return types.SingleOrDefault();
}

/// <summary>
/// Finds the early-bound type based on an entity's generated type code
/// </summary>
/// <param name="entityTypeCode"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public Type FindReflectedType(int entityTypeCode)
{
var types =
ProxyTypesAssemblies.Select(a => FindReflectedType(entityTypeCode, a))
.Where(t => t != null);

if (types.Count() > 1)
{
var errorMsg = $"Type { entityTypeCode } is defined in multiple assemblies: ";
foreach (var type in types)
{
errorMsg += type.Assembly
.GetName()
.Name + "; ";
}
var lastIndex = errorMsg.LastIndexOf("; ");
errorMsg = errorMsg.Substring(0, lastIndex) + ".";
throw new InvalidOperationException(errorMsg);
}

return types.SingleOrDefault();
}

/// <summary>
/// Finds reflected type for given entity from given assembly.
Expand Down Expand Up @@ -92,6 +121,53 @@ private static Type FindReflectedType(string logicalName,
throw new Exception("XrmFakedContext.FindReflectedType: " + s);
}
}

/// <summary>
/// Finds reflected type for given entity from given assembly.
/// </summary>
/// <param name="entityTypeCode">
/// Entity logical name which type is searched from given
/// <paramref name="assembly"/>.
/// </param>
/// <param name="assembly">
/// Assembly where early-bound type is searched for given
/// <paramref name="logicalName"/>.
/// </param>
/// <returns>
/// Early-bound type of <paramref name="entityTypeCode"/> if it's found
/// from <paramref name="assembly"/>. Otherwise null is returned.
/// </returns>
private static Type FindReflectedType(int entityTypeCode,
Assembly assembly)
{
try
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}

var subClassType = assembly.GetTypes()
.Where(t => typeof(Entity).IsAssignableFrom(t))
.Where(t => t.GetCustomAttributes(typeof(EntityLogicalNameAttribute), true).Length > 0)
.Where(t => t.GetField("EntityTypeCode").GetValue(null).Equals(entityTypeCode))
.FirstOrDefault();

return subClassType;
}
catch (ReflectionTypeLoadException exception)
{
// now look at ex.LoaderExceptions - this is an Exception[], so:
var s = "";
foreach (var innerException in exception.LoaderExceptions)
{
// write details of "inner", in particular inner.Message
s += innerException.Message + " ";
}

throw new Exception("XrmFakedContext.FindReflectedType: " + s);
}
}

/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.Extensions
namespace FakeXrmEasy.Core.Tests.Extensions
{
public class EntityExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Xunit;
using System.Collections.Generic;

namespace FakeXrmEasy.Tests.Extensions
namespace FakeXrmEasy.Core.Tests.Extensions
{
public class EntityMetadataExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System;
using Xunit;

namespace FakeXrmEasy.Tests.Extensions
namespace FakeXrmEasy.Core.Tests.Extensions
{
public class OrganizationRequestExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Xrm.Sdk.Query;
using FakeXrmEasy.Query;

namespace FakeXrmEasy.Tests.Extensions
namespace FakeXrmEasy.Core.Tests.Extensions
{
public class QueryExpressionExtensionsTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Xunit;
using FakeXrmEasy.Extensions;

namespace FakeXrmEasy.Tests.FakeContextTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests
{
public class DateTimeBehaviourTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Reflection;
using Xunit;

namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeContextTestCreateQuery: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Xunit;


namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeXrmEasyTestsDelete : FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests
{
public class FakeContextTestMetadata : FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Crm;
using Xunit;

namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeXrmEasyTestsAddEntity : FakeXrmEasyTestsBase
{
Expand Down
14 changes: 12 additions & 2 deletions tests/FakeXrmEasy.Core.Tests/FakeContextTests/FakeContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Crm;
using System.Reflection;

namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeContextCoreTests : FakeXrmEasyTestsBase
{
Expand Down Expand Up @@ -300,7 +300,7 @@ public void Should_return_entity_by_id()
}

[Fact]
public void Should_return_error_if_entity_lofical_name_doesnt_exists()
public void Should_return_error_if_entity_logical_name_doesnt_exists()
{
Assert.Throws<InvalidOperationException>(() =>_context.GetEntityById("doesNotExist", Guid.NewGuid()));
}
Expand All @@ -318,5 +318,15 @@ public void Should_return_error_if_entity_id_does_not_exists()

Assert.Throws<InvalidOperationException>(() =>_context.GetEntityById("contact", Guid.NewGuid()));
}

[Fact]
public void Should_find_reflected_type_by_entity_type_code() {

var assembly = typeof(Crm.Account).Assembly;
_context.EnableProxyTypes(assembly);

var type = _context.FindReflectedType(1);
Assert.Equal(typeof(Account), type);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Xunit;
using System;

namespace FakeXrmEasy.Tests.FakeContextTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests
{
public class FakeTracingServiceTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests
{
public class ConditionExpressionTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests
{
public class FormattedValuesTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Query;

namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeContextTestTranslateQueryExpression: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests
{
public class FilterExpressionTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Xunit;
using FakeXrmEasy.Query;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.DateTimes
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.DateTimes
{
public class DateTimeOperatorsTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Xrm.Sdk.Query;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.MultiSelectOptionSet
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.MultiSelectOptionSet
{
public class MultiSelectOptionSetTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Xunit;
using FakeXrmEasy.Query;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.Strings
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests.OperatorTests.Strings
{
public class StringOperatorsTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Linq;
using Xunit;

namespace FakeXrmEasy.Tests.FakeContextTests.TranslateQueryExpressionTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests.TranslateQueryExpressionTests
{
public class OrderByTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using System.Collections.Generic;


namespace FakeXrmEasy.Tests.FakeContextTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests
{
public class ValidateReferencesTests: FakeXrmEasyTestsBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Xunit;
using FakeXrmEasy.Abstractions;

namespace FakeXrmEasy.Tests.FakeContextTests
namespace FakeXrmEasy.Core.Tests.FakeContextTests
{
public class XrmFakedRelationshipTests: FakeXrmEasyTestsBase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FakeXrmEasy.Core.Tests/FakeXrmEasyTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using FakeXrmEasy.Middleware;
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Tests
namespace FakeXrmEasy.Core.Tests
{
public class FakeXrmEasyTestsBase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FakeXrmEasy.Core.Tests/Issue116.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using Xunit;

namespace FakeXrmEasy.Tests.Issues
namespace FakeXrmEasy.Core.Tests.Issues
{
public class Issue116 : FakeXrmEasyTestsBase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FakeXrmEasy.Core.Tests/Issues/Issue125.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Xunit;
using FakeXrmEasy.Abstractions;

namespace FakeXrmEasy.Tests.Issues
namespace FakeXrmEasy.Core.Tests.Issues
{
public class Issue125 : FakeXrmEasyTestsBase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/FakeXrmEasy.Core.Tests/Issues/Issue156.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Reflection;
using Xunit;

namespace FakeXrmEasy.Tests.Issues
namespace FakeXrmEasy.Core.Tests.Issues
{
public class Issue156: FakeXrmEasyTestsBase
{
Expand Down
Loading

0 comments on commit c300afa

Please sign in to comment.