Browse Source

Rfining unit test.

master
yangxiaodong 7 years ago
parent
commit
71016d4ef3
4 changed files with 184 additions and 64 deletions
  1. +0
    -0
      src/DotNetCore.CAP.EntityFrameworkCore/CapDbContext.cs
  2. +12
    -0
      test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs
  3. +172
    -0
      test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs
  4. +0
    -64
      test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs

src/DotNetCore.CAP.EntityFrameworkCore/ConsistencyDbContext.cs → src/DotNetCore.CAP.EntityFrameworkCore/CapDbContext.cs View File


+ 12
- 0
test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public class CapMessageStoreTest
{

}
}

+ 172
- 0
test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs View File

@@ -0,0 +1,172 @@
using System;
using System.Linq;
using DotNetCore.CAP.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public class EFMessageStoreTest : DatabaseTestHost
{
[Fact]
public void CanCreateSentMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapSentMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();
Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
}
}

[Fact]
public void CanUpdateSentMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapSentMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();

var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.NotNull(selectedMessage);

selectedMessage.StatusName = StatusName.Succeeded;
selectedMessage.Content = "Test";
db.SaveChanges();

selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
Assert.NotNull(selectedMessage);
Assert.True(selectedMessage.Content == "Test");
}
}

[Fact]
public void CanRemoveSentMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapSentMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();

var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.NotNull(selectedMessage);

db.CapSentMessages.Remove(selectedMessage);
selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.Null(selectedMessage);
}
}

[Fact]
public void CanCreateReceivedMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapReceivedMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();

Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
}
}

[Fact]
public void CanUpdateReceivedMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapReceivedMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();

var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.NotNull(selectedMessage);

selectedMessage.StatusName = StatusName.Succeeded;
selectedMessage.Content = "Test";
db.SaveChanges();

selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
Assert.NotNull(selectedMessage);
Assert.True(selectedMessage.Content == "Test");
}
}

[Fact]
public void CanRemoveReceivedMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapReceivedMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();

var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.NotNull(selectedMessage);

db.CapSentMessages.Remove(selectedMessage);
selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
Assert.Null(selectedMessage);
}
}

public TestDbContext CreateContext(bool delete = false)
{
var db = Provider.GetRequiredService<TestDbContext>();
if (delete)
{
db.Database.EnsureDeleted();
}
db.Database.EnsureCreated();
return db;
}
}
}

+ 0
- 64
test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs View File

@@ -1,64 +0,0 @@
using System;
using System.Linq;
using DotNetCore.CAP.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public class MessageStoreTest : DatabaseTestHost
{
[Fact]
public void CanCreateSentMessageUsingEF()
{
using (var db = CreateContext())
{
var guid = Guid.NewGuid().ToString();
var message = new CapSentMessage
{
Id = guid,
Content = "this is message body",
StatusName = StatusName.Enqueued
};
db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;

db.SaveChanges();
Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
}
}

//[Fact]
//public async Task CanCreateUsingManager()
//{
// var manager = CreateManager();
// var guid = Guid.NewGuid().ToString();
// var message = new CapSentMessage
// {
// Id = guid,
// Content = "this is message body",
// StateName = StateName.Enqueued,
// };

// var result = await manager.StoreSentMessageAsync(message);
// Assert.NotNull(result);
// Assert.True(result.Succeeded);

// result = await manager.RemoveSentMessageAsync(message);
// Assert.NotNull(result);
// Assert.True(result.Succeeded);
//}

public TestDbContext CreateContext(bool delete = false)
{
var db = Provider.GetRequiredService<TestDbContext>();
if (delete)
{
db.Database.EnsureDeleted();
}
db.Database.EnsureCreated();
return db;
}
}
}

Loading…
Cancel
Save