Skip to content

Commit

Permalink
Test: Use different database for FiltersInheritanceTests
Browse files Browse the repository at this point in the history
Since InheritanceTests modify the data causing flaky errors
Also add InheritanceTests for Sqlite
Execute test modifying data in a transaction
  • Loading branch information
smitpatel committed Jun 27, 2017
1 parent 1e2c6bf commit e6cd7e6
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 254 deletions.
1 change: 1 addition & 0 deletions src/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
// ReSharper disable InconsistentNaming

namespace Microsoft.EntityFrameworkCore
{
Expand Down
136 changes: 85 additions & 51 deletions src/EFCore.Specification.Tests/Query/FiltersInheritanceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,107 +19,141 @@ public abstract class FiltersInheritanceTestBase<TTestStore, TFixture> : IClassF
[Fact]
public virtual void Can_use_of_type_animal()
{
var animals = _context.Set<Animal>().OfType<Animal>().OrderBy(a => a.Species).ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, _context.ChangeTracker.Entries().Count());
using (var context = CreateContext())
{
var animals = context.Set<Animal>().OfType<Animal>().OrderBy(a => a.Species).ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public virtual void Can_use_is_kiwi()
{
var kiwis = _context.Set<Animal>().Where(a => a is Kiwi).ToList();
using (var context = CreateContext())
{
var kiwis = context.Set<Animal>().Where(a => a is Kiwi).ToList();

Assert.Equal(1, kiwis.Count);
Assert.Equal(1, kiwis.Count);
}
}

[Fact]
public virtual void Can_use_is_kiwi_with_other_predicate()
{
var animals = _context.Set<Animal>().Where(a => a is Kiwi && a.CountryId == 1).ToList();
using (var context = CreateContext())
{
var animals = context.Set<Animal>().Where(a => a is Kiwi && a.CountryId == 1).ToList();

Assert.Equal(1, animals.Count);
Assert.Equal(1, animals.Count);
}
}

[Fact]
public virtual void Can_use_is_kiwi_in_projection()
{
var animals = _context.Set<Animal>().Select(a => a is Kiwi).ToList();

Assert.Equal(1, animals.Count);
Assert.Equal(1, animals.Count(a => a));
Assert.Equal(0, animals.Count(a => !a));
using (var context = CreateContext())
{
var animals = context.Set<Animal>().Select(a => a is Kiwi).ToList();

Assert.Equal(1, animals.Count);
Assert.Equal(1, animals.Count(a => a));
Assert.Equal(0, animals.Count(a => !a));
}
}

[Fact]
public virtual void Can_use_of_type_bird()
{
var animals = _context.Set<Animal>().OfType<Bird>().OrderBy(a => a.Species).ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, _context.ChangeTracker.Entries().Count());
using (var context = CreateContext())
{
var animals = context.Set<Animal>().OfType<Bird>().OrderBy(a => a.Species).ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public virtual void Can_use_of_type_bird_predicate()
{
var animals
= _context.Set<Animal>()
.Where(a => a.CountryId == 1)
.OfType<Bird>()
.OrderBy(a => a.Species)
.ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, _context.ChangeTracker.Entries().Count());
using (var context = CreateContext())
{
var animals
= context.Set<Animal>()
.Where(a => a.CountryId == 1)
.OfType<Bird>()
.OrderBy(a => a.Species)
.ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public virtual void Can_use_of_type_bird_with_projection()
{
var animals
= _context.Set<Animal>()
.OfType<Bird>()
.Select(b => new { b.EagleId })
.ToList();

Assert.Equal(1, animals.Count);
Assert.Equal(0, _context.ChangeTracker.Entries().Count());
using (var context = CreateContext())
{
var animals
= context.Set<Animal>()
.OfType<Bird>()
.Select(b => new { b.EagleId })
.ToList();

Assert.Equal(1, animals.Count);
Assert.Equal(0, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public virtual void Can_use_of_type_bird_first()
{
var bird = _context.Set<Animal>().OfType<Bird>().OrderBy(a => a.Species).First();

Assert.NotNull(bird);
Assert.IsType<Kiwi>(bird);
Assert.Equal(1, _context.ChangeTracker.Entries().Count());
using (var context = CreateContext())
{
var bird = context.Set<Animal>().OfType<Bird>().OrderBy(a => a.Species).First();

Assert.NotNull(bird);
Assert.IsType<Kiwi>(bird);
Assert.Equal(1, context.ChangeTracker.Entries().Count());
}
}

[Fact]
public virtual void Can_use_of_type_kiwi()
{
var animals = _context.Set<Animal>().OfType<Kiwi>().ToList();
using (var context = CreateContext())
{
var animals = context.Set<Animal>().OfType<Kiwi>().ToList();

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, context.ChangeTracker.Entries().Count());
}
}

protected InheritanceContext CreateContext() => Fixture.CreateContext(TestStore);

protected FiltersInheritanceTestBase(TFixture fixture)
{
Fixture = fixture;

Assert.Equal(1, animals.Count);
Assert.IsType<Kiwi>(animals[0]);
Assert.Equal(1, _context.ChangeTracker.Entries().Count());
TestStore = Fixture.CreateTestStore();
}

protected TFixture Fixture { get; }

private readonly InheritanceContext _context;
protected TTestStore TestStore { get; }

protected FiltersInheritanceTestBase(TFixture fixture)
protected virtual void ClearLog()
{
Fixture = fixture;
_context = fixture.CreateContext();
}

public void Dispose() => _context.Dispose();
public void Dispose() => TestStore.Dispose();
}
}
79 changes: 2 additions & 77 deletions src/EFCore.Specification.Tests/Query/InheritanceFixtureBase.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.TestModels.Inheritance;

namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class InheritanceFixtureBase<TTestStore> : IDisposable
public abstract class InheritanceFixtureBase<TTestStore>
where TTestStore : TestStore
{
private DbContextOptions _options;
protected TTestStore TestStore { get; }

protected InheritanceFixtureBase()
{
TestStore = CreateTestStore();
}

public abstract DbContextOptions BuildOptions();

public abstract TTestStore CreateTestStore();

public virtual InheritanceContext CreateContext()
=> new InheritanceContext(_options ?? (_options = BuildOptions()));

protected virtual void ClearLog()
{
}
public abstract InheritanceContext CreateContext(TTestStore testStore);

protected virtual bool EnableFilters => false;

Expand All @@ -51,64 +35,5 @@ public virtual void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Animal>().HasQueryFilter(a => a.CountryId == 1);
}
}

protected void SeedData(InheritanceContext context)
{
var kiwi = new Kiwi
{
Species = "Apteryx haastii",
Name = "Great spotted kiwi",
IsFlightless = true,
FoundOn = Island.South
};

var eagle = new Eagle
{
Species = "Aquila chrysaetos canadensis",
Name = "American golden eagle",
Group = EagleGroup.Booted
};

eagle.Prey.Add(kiwi);

var rose = new Rose
{
Species = "Rosa canina",
Name = "Dog-rose",
HasThorns = true
};

var daisy = new Daisy
{
Species = "Bellis perennis",
Name = "Common daisy"
};

var nz = new Country { Id = 1, Name = "New Zealand" };

nz.Animals.Add(kiwi);

var usa = new Country { Id = 2, Name = "USA" };

usa.Animals.Add(eagle);

context.Set<Animal>().Add(kiwi);
context.Set<Bird>().Add(eagle);
context.Set<Country>().Add(nz);
context.Set<Country>().Add(usa);
context.Set<Rose>().Add(rose);
context.Set<Daisy>().Add(daisy);

context.AddRange(
new Tea { HasMilk = true, CaffeineGrams = 1 },
new Lilt { SugarGrams = 4, Carbination = 7 },
new Coke { SugarGrams = 6, CaffeineGrams = 4, Carbination = 5 });

context.SaveChanges();

ClearLog();
}

public void Dispose() => TestStore.Dispose();
}
}
Loading

0 comments on commit e6cd7e6

Please sign in to comment.