Skip to content

Commit

Permalink
Use using declarations in EFCore.Specifications.Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Nov 8, 2019
1 parent 0345459 commit 37eff6c
Show file tree
Hide file tree
Showing 51 changed files with 13,322 additions and 15,526 deletions.
22 changes: 10 additions & 12 deletions test/EFCore.Specification.Tests/ConcurrencyDetectorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,20 @@ public virtual Task ToList_logs_concurrent_access_async()

protected virtual async Task ConcurrencyDetectorTest(Func<NorthwindContext, Task> test)
{
using (var context = CreateContext())
{
context.Products.Add(
new Product { ProductID = 10001 });
using var context = CreateContext();
context.Products.Add(
new Product { ProductID = 10001 });

var concurrencyDetector = context.GetService<IConcurrencyDetector>();
IDisposable disposer = null;
var concurrencyDetector = context.GetService<IConcurrencyDetector>();
IDisposable disposer = null;

Task.Run(() => disposer = concurrencyDetector.EnterCriticalSection()).Wait();
Task.Run(() => disposer = concurrencyDetector.EnterCriticalSection()).Wait();

using (disposer)
{
Exception ex = await Assert.ThrowsAsync<InvalidOperationException>(() => test(context));
using (disposer)
{
Exception ex = await Assert.ThrowsAsync<InvalidOperationException>(() => test(context));

Assert.Equal(CoreStrings.ConcurrentMethodInvocation, ex.Message);
}
Assert.Equal(CoreStrings.ConcurrentMethodInvocation, ex.Message);
}
}

Expand Down
126 changes: 59 additions & 67 deletions test/EFCore.Specification.Tests/CustomConvertersTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,90 +402,82 @@ public class SimpleCounter
[ConditionalFact]
public virtual void Field_on_derived_type_retrieved_via_cast_applies_value_converter()
{
using (var context = CreateContext())
{
var query = context.Set<Blog>()
.Where(b => b.BlogId == 2)
.Select(
x => new
{
x.BlogId,
x.Url,
RssUrl = x is RssBlog ? ((RssBlog)x).RssUrl : null
}).ToList();
using var context = CreateContext();
var query = context.Set<Blog>()
.Where(b => b.BlogId == 2)
.Select(
x => new
{
x.BlogId,
x.Url,
RssUrl = x is RssBlog ? ((RssBlog)x).RssUrl : null
}).ToList();

var result = Assert.Single(query);
Assert.Equal("http://rssblog.com/rss", result.RssUrl);
}
var result = Assert.Single(query);
Assert.Equal("http://rssblog.com/rss", result.RssUrl);
}

[ConditionalFact]
public virtual void Value_conversion_is_appropriately_used_for_join_condition()
{
using (var context = CreateContext())
{
var blogId = 1;
var query = (from b in context.Set<Blog>()
join p in context.Set<Post>()
on new
{
BlogId = (int?)b.BlogId,
b.IsVisible,
AnotherId = b.BlogId
}
equals new
{
p.BlogId,
IsVisible = true,
AnotherId = blogId
}
where b.IsVisible
select b.Url).ToList();

var result = Assert.Single(query);
Assert.Equal("http://blog.com", result);
}
using var context = CreateContext();
var blogId = 1;
var query = (from b in context.Set<Blog>()
join p in context.Set<Post>()
on new
{
BlogId = (int?)b.BlogId,
b.IsVisible,
AnotherId = b.BlogId
}
equals new
{
p.BlogId,
IsVisible = true,
AnotherId = blogId
}
where b.IsVisible
select b.Url).ToList();

var result = Assert.Single(query);
Assert.Equal("http://blog.com", result);
}

[ConditionalFact]
public virtual void Value_conversion_is_appropriately_used_for_left_join_condition()
{
using (var context = CreateContext())
{
var blogId = 1;
var query = (from b in context.Set<Blog>()
join p in context.Set<Post>()
on new
{
BlogId = (int?)b.BlogId,
b.IsVisible,
AnotherId = b.BlogId
}
equals new
{
p.BlogId,
IsVisible = true,
AnotherId = blogId
} into g
from p in g.DefaultIfEmpty()
where b.IsVisible
select b.Url).ToList();

var result = Assert.Single(query);
Assert.Equal("http://blog.com", result);
}
using var context = CreateContext();
var blogId = 1;
var query = (from b in context.Set<Blog>()
join p in context.Set<Post>()
on new
{
BlogId = (int?)b.BlogId,
b.IsVisible,
AnotherId = b.BlogId
}
equals new
{
p.BlogId,
IsVisible = true,
AnotherId = blogId
} into g
from p in g.DefaultIfEmpty()
where b.IsVisible
select b.Url).ToList();

var result = Assert.Single(query);
Assert.Equal("http://blog.com", result);
}

[ConditionalFact]
public virtual void Where_bool_gets_converted_to_equality_when_value_conversion_is_used()
{
using (var context = CreateContext())
{
var query = context.Set<Blog>().Where(b => b.IsVisible).ToList();
using var context = CreateContext();
var query = context.Set<Blog>().Where(b => b.IsVisible).ToList();

var result = Assert.Single(query);
Assert.Equal("http://blog.com", result.Url);
}
var result = Assert.Single(query);
Assert.Equal("http://blog.com", result.Url);
}

[ConditionalFact]
Expand Down
30 changes: 13 additions & 17 deletions test/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,17 +1432,15 @@ public virtual void ConcurrencyCheckAttribute_throws_if_value_in_database_change
clientRow.RowVersion = new Guid("00000000-0000-0000-0002-000000000001");
clientRow.RequiredColumn = "ChangedData";
using (var innerContext = CreateContext())
{
UseTransaction(innerContext.Database, context.Database.CurrentTransaction);
var storeRow = innerContext.Set<One>().First(r => r.UniqueNo == 1);
storeRow.RowVersion = new Guid("00000000-0000-0000-0003-000000000001");
storeRow.RequiredColumn = "ModifiedData";
using var innerContext = CreateContext();
UseTransaction(innerContext.Database, context.Database.CurrentTransaction);
var storeRow = innerContext.Set<One>().First(r => r.UniqueNo == 1);
storeRow.RowVersion = new Guid("00000000-0000-0000-0003-000000000001");
storeRow.RequiredColumn = "ModifiedData";
innerContext.SaveChanges();
innerContext.SaveChanges();
Assert.Throws<DbUpdateConcurrencyException>(() => context.SaveChanges());
}
Assert.Throws<DbUpdateConcurrencyException>(() => context.SaveChanges());
});
}

Expand Down Expand Up @@ -2249,16 +2247,14 @@ public virtual void TimestampAttribute_throws_if_value_in_database_changed()
var clientRow = context.Set<Two>().First(r => r.Id == 1);
clientRow.Data = "ChangedData";
using (var innerContext = CreateContext())
{
UseTransaction(innerContext.Database, context.Database.CurrentTransaction);
var storeRow = innerContext.Set<Two>().First(r => r.Id == 1);
storeRow.Data = "ModifiedData";
using var innerContext = CreateContext();
UseTransaction(innerContext.Database, context.Database.CurrentTransaction);
var storeRow = innerContext.Set<Two>().First(r => r.Id == 1);
storeRow.Data = "ModifiedData";
innerContext.SaveChanges();
innerContext.SaveChanges();
Assert.Throws<DbUpdateConcurrencyException>(() => context.SaveChanges());
}
Assert.Throws<DbUpdateConcurrencyException>(() => context.SaveChanges());
});
}

Expand Down
Loading

0 comments on commit 37eff6c

Please sign in to comment.