using System;
|
using System.Threading.Tasks;
|
using Mongo2Go;
|
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization.Conventions;
|
using MongoDB.Driver;
|
using Xunit;namespace IntegrationTest
|
{
|
public class MongoTestHelper : IDisposable
|
{
|
private readonly MongoDbRunner _runner;
|
private readonly IMongoDatabase _db; public MongoTestHelper(string databaseName, bool debug = false)
|
{
|
if (debug)
|
{
|
_runner = MongoDbRunner.StartForDebugging();
|
}
|
else
|
{
|
_runner = MongoDbRunner.Start();
|
} var mongo = new MongoClient(_runner.ConnectionString);
|
_db = mongo.GetDatabase(databaseName);
|
} public IMongoDatabase GetDatabase()
|
{
|
return _db;
|
} public void Dispose()
|
{
|
_runner.Dispose();
|
}
|
} public class AnimalTests : IDisposable
|
{
|
private readonly MongoTestHelper _helper; public AnimalTests()
|
{
|
var conventionPack = new ConventionPack
|
{
|
new IgnoreExtraElementsConvention(true)
|
};
|
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true); BsonClassMap.RegisterClassMap<Dog>();
|
BsonClassMap.RegisterClassMap<Cat>();
|
// var dogSerializer = BsonSerializer.LookupSerializer<Dog>();
|
// BsonSerializer.RegisterSerializer<IAnimal>(new ImpliedImplementationInterfaceSerializer<IAnimal, Dog>(dogSerializer));
|
//
|
// var catSerializer = BsonSerializer.LookupSerializer<Cat>();
|
// BsonSerializer.RegisterSerializer<IAnimal>(new ImpliedImplementationInterfaceSerializer<IAnimal, Cat>(catSerializer));
|
//
|
// because:
|
// MongoDB.Bson.BsonSerializationException : There is already a serializer registered for type IAnimal.
|
_helper = new MongoTestHelper("db_test", true);
|
} [Fact]
|
public async Task Some_Test()
|
{
|
var animals = new IAnimal[]
|
{
|
new Dog(),
|
new Cat()
|
}; var db = _helper.GetDatabase();
|
db.DropCollection("animal");
|
var collWrite = db.GetCollection<IAnimal>("animal");
|
|
collWrite.InsertMany(animals); var collRead = db.GetCollection<IAnimal>("animal"); // THIS WORKS
|
await (await collRead.FindAsync(x => true)).ToListAsync(); // THIS DOESN'T
|
await (await collRead.FindAsync(x => x.Type == "dog")).ToListAsync();
|
} public void Dispose()
|
{
|
_helper.Dispose();
|
} public interface IAnimal
|
{
|
string Type { get; set; }
|
string Speak { get; set; }
|
} public class Dog : IAnimal
|
{
|
public string Type { get; set; }
|
public string Speak { get; set; } public Dog()
|
{
|
Type = "dog";
|
Speak = "woof";
|
}
|
} public class Cat : IAnimal
|
{
|
public string Type { get; set; }
|
public string Speak { get; set; } public Cat()
|
{
|
Type = "cat";
|
Speak = "meow";
|
}
|
}
|
}
|
}
|