Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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,12 @@
using System.Data.Entity;

namespace Highway.Data.ReadonlyTests
{
internal class GetStudentsWithEvents : QueryWithEvents<Student>
{
public GetStudentsWithEvents()
{
ContextQuery = source => source.AsQueryable<Student>().Include(x => x.Grade);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Highway.Data.Interceptors.Events;

namespace Highway.Data.ReadonlyTests
{
internal abstract class QueryWithEvents<T> : Query<T>
{
protected QueryWithEvents()
{
AfterQuery += OnAfterQuery;
BeforeQuery += OnBeforeQuery;
}

private void OnBeforeQuery(object sender, BeforeQuery e)
{
BeforeQueryFired = true;
}

public bool BeforeQueryFired { get; set; }

private void OnAfterQuery(object sender, AfterQuery e)
{
AfterQueryFired = true;
}

public bool AfterQueryFired { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Highway.Data.ReadonlyTests/SchoolDomain/SchoolDomain.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using System.Collections.Generic;
using System.Linq;

using Highway.Data.EventManagement.Interfaces;

namespace Highway.Data.ReadonlyTests
{
public class SchoolDomain : IDomain
{
public SchoolDomain(IEnumerable<IInterceptor> events = null)
{
Events = events?.ToList();
}

public string ConnectionString { get; } = Configuration.Instance.TestDatabaseConnectionString;

public IContextConfiguration Context { get; } = new DefaultContextConfiguration();

public List<IInterceptor> Events { get; } = new();
public List<IInterceptor> Events { get; }

public IMappingConfiguration Mappings { get; } = new SchoolMapping();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Highway.Data.EventManagement.Interfaces;
using Highway.Data.Interceptors;
using Highway.Data.Interceptors.Events;

namespace Highway.Data.ReadonlyTests
{
public class TypeInspectionAfterQueryInterceptor : TypeInspectionInterceptor, IEventInterceptor<AfterQuery>
{
public bool AppliesTo(AfterQuery eventArgs)
{
return true;
}

public InterceptorResult Apply(IDataSource context, AfterQuery eventArgs)
{
InspectedType = eventArgs.Result.GetType();

return InterceptorResult.Succeeded();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Highway.Data.EventManagement.Interfaces;
using Highway.Data.Interceptors;
using Highway.Data.Interceptors.Events;

namespace Highway.Data.ReadonlyTests
{
public class TypeInspectionBeforeQueryInterceptor : TypeInspectionInterceptor, IEventInterceptor<BeforeQuery>
{
public bool AppliesTo(BeforeQuery eventArgs)
{
return true;
}

public InterceptorResult Apply(IDataSource context, BeforeQuery eventArgs)
{
InspectedType = eventArgs.Query.GetType();

return InterceptorResult.Succeeded();
}
}
}
14 changes: 14 additions & 0 deletions src/Highway.Data.ReadonlyTests/TypeInspectionInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

using Highway.Data.EventManagement.Interfaces;

namespace Highway.Data.ReadonlyTests
{
public abstract class TypeInspectionInterceptor : IInterceptor
{
public int Priority => -1;


public Type InspectedType { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;

using FluentAssertions;

using Highway.Data.EventManagement.Interfaces;
using Highway.Data.Repositories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Highway.Data.ReadonlyTests
{
[TestClass]
public class WhenADomainRepositoryWithQueryInterceptorsRunsAQueryWithInterceptors
{
private const string ConnectionString = "Data Source=(localDb);Initial Catalog=Highway.Data.Test.Db;Integrated Security=True;Connection Timeout=1";

private readonly TypeInspectionAfterQueryInterceptor _afterQueryInterceptor = new TypeInspectionAfterQueryInterceptor();

private readonly TypeInspectionBeforeQueryInterceptor _beforeQueryInterceptor = new TypeInspectionBeforeQueryInterceptor();

private IEnumerable<Student> _results;

private GetStudentsWithEvents _testQuery;

[TestInitialize]
public void Setup()
{
// Arrange
// The following code uses a writeable domain context to insert test data.
var domain = new SchoolDomain();
var domainContext = new DomainContext<SchoolDomain>(domain);

var firstGrade = new Grade
{
Name = "first",
Section = "section one"
};

var bill = new Student
{
DoB = DateTime.Now.Subtract(TimeSpan.FromDays(365)),
Height = 60,
Weight = 180,
Name = "Bill"
};

firstGrade.AddStudent(bill);

domainContext.Add(bill);
domainContext.Commit();

// Act
// The following code uses a readonly domain context to query the test data.
var readonlyDomain = new SchoolDomain(new List<IInterceptor> { _afterQueryInterceptor, _beforeQueryInterceptor });
var readonlyDomainContext = new ReadonlyDomainContext<SchoolDomain>(readonlyDomain);
var readonlyRepository = new ReadonlyDomainRepository<SchoolDomain>(readonlyDomainContext, readonlyDomain);
_testQuery = new GetStudentsWithEvents();
_results = readonlyRepository.Find(_testQuery);
}

[TestMethod]
public void The_Query_After_Query_Interceptor_Should_Fire()
{
// FAILS
_testQuery.AfterQueryFired.Should().BeTrue();
}

[TestMethod]
public void The_Query_Before_Query_Interceptor_Should_Fire()
{
// FAILS
_testQuery.BeforeQueryFired.Should().BeTrue();
}

[TestMethod]
public void The_Repository_After_Query_Interceptor_Should_Fire()
{
// PASSES
_afterQueryInterceptor.InspectedType.Should().Be(typeof(DbQuery<Student>));
}

[TestMethod]
public void The_Repository_Before_Query_Interceptor_Should_Fire()
{
// PASSES
_beforeQueryInterceptor.InspectedType.Should().Be(typeof(GetStudentsWithEvents));
}

[TestMethod]
public void The_Results_Should_Be_Populated()
{
// PASSES
_results.Should().HaveCountGreaterThan(0);
}
}
}