Visitar URL original
Modify query loader the way it generates parametrized SQL queries by YuriyIvon · Pull Request #163 · zzzprojects/GraphDiff · GitHub
Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Added a test for query parameter of the loading query
  • Loading branch information
YuriyIvon
YuriyIvon committed Aug 31, 2016
commit 2356e074db85fc5a51f9c333ccb3486b8779f094
63 changes: 63 additions & 0 deletions GraphDiff/GraphDiff.Tests/Tests/QueryLoaderBehaviours.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Data.Entity.Infrastructure;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RefactorThis.GraphDiff.Tests.Models;
using System;
using System.Data.Entity;
using System.Linq;

namespace RefactorThis.GraphDiff.Tests.Tests
{
Expand Down Expand Up @@ -95,5 +98,65 @@ public void ShouldPerformMutlipleQueriesWhenRequested()

// TODO how do I test number of queries..
}

[TestMethod]
public void ShouldPrametrizeLoadingQuery()
{
using (var context = new TestDbContext())
{
context.Set<OneToOneOneToOneAssociatedModel>().Add(_oneToOneAssociated);
context.Set<OneToOneOneToManyAssociatedModel>().Add(_oneToManyAssociated);
context.Nodes.Add(_node);
context.SaveChanges();
}

var x = new LocalDbConnectionFactory("v11.0");
var connection = x.CreateConnection("GraphDiff");

using (var context = new TestDbContext(connection))
{
using (var logCollector = new QueryLogCollector(context))
{
// Setup mapping
context.UpdateGraph(
entity: _node,
mapping: map => map.OwnedEntity(p => p.OneToOneOwned, with => with
.OwnedEntity(p => p.OneToOneOneToOneOwned)
.AssociatedEntity(p => p.OneToOneOneToOneAssociated)
.OwnedCollection(p => p.OneToOneOneToManyOwned)
.AssociatedCollection(p => p.OneToOneOneToManyAssociated)),
updateParams: new UpdateParams { QueryMode = QueryMode.SingleQuery });

Assert.IsTrue(logCollector.Logs.Any(l => l.Contains("@p__linq__0")),
"Can't find a parameter in the loading query");
}
}

// TODO how do I test number of queries..
}
}

internal class QueryLogCollector : IDisposable
{
private readonly DbContext _context;
private readonly Action<string> _originalLogger;
private readonly List<string> _logs = new List<string>();

public List<string> Logs
{
get { return _logs; }
}

public QueryLogCollector(DbContext context)
{
_context = context;
_originalLogger = _context.Database.Log;
_context.Database.Log = line => _logs.Add(line);
}

public void Dispose()
{
_context.Database.Log = _originalLogger;
}
}
}