Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
e72cc784
Commit
e72cc784
authored
Jul 18, 2017
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add unit tests.
parent
c1db4267
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
143 additions
and
94 deletions
+143
-94
CapMessageStoreTest.cs
test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs
+0
-11
ConnectionUtil.cs
test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs
+1
-1
DatabaseTestHost.cs
test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs
+51
-51
TestDbContext.cs
test/DotNetCore.CAP.SqlServer.Test/TestDbContext.cs
+13
-0
TestHost.cs
test/DotNetCore.CAP.SqlServer.Test/TestHost.cs
+4
-3
CAP.BuilderTest.cs
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs
+15
-1
ConsistencyOptionsTest.cs
test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs
+0
-6
NoopMessageStore.cs
test/DotNetCore.CAP.Test/NoopMessageStore.cs
+0
-21
StateChangerTest.cs
test/DotNetCore.CAP.Test/StateChangerTest.cs
+59
-0
No files found.
test/DotNetCore.CAP.SqlServer.Test/CapMessageStoreTest.cs
deleted
100644 → 0
View file @
c1db4267
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
using
Xunit
;
namespace
DotNetCore.CAP.EntityFrameworkCore.Test
{
public
class
CapMessageStoreTest
{
}
}
\ No newline at end of file
test/DotNetCore.CAP.SqlServer.Test/ConnectionUtil.cs
View file @
e72cc784
using
System
;
using
System.Data.SqlClient
;
namespace
DotNetCore.CAP.
EntityFrameworkCore
.Test
namespace
DotNetCore.CAP.
SqlServer
.Test
{
public
static
class
ConnectionUtil
{
...
...
test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs
View file @
e72cc784
using
System.Data
;
using
System.Threading
.Tasks
;
using
System.Threading
;
using
Dapper
;
using
Microsoft.EntityFrameworkCore
;
namespace
DotNetCore.CAP.
EntityFrameworkCore
.Test
namespace
DotNetCore.CAP.
SqlServer
.Test
{
//
public abstract class DatabaseTestHost : TestHost
//
{
//
private static bool _sqlObjectInstalled;
public
abstract
class
DatabaseTestHost
:
TestHost
{
private
static
bool
_sqlObjectInstalled
;
//
protected override void PostBuildServices()
//
{
//
base.PostBuildServices();
//
InitializeDatabase();
//
}
protected
override
void
PostBuildServices
()
{
base
.
PostBuildServices
();
InitializeDatabase
();
}
//
public override void Dispose()
//
{
//
DeleteAllData();
//
base.Dispose();
//
}
public
override
void
Dispose
()
{
DeleteAllData
();
base
.
Dispose
();
}
//
private void InitializeDatabase()
//
{
//
if (!_sqlObjectInstalled)
//
{
//
using (CreateScope())
//
{
// var context = GetService<TestDbContext
>();
// context.Database.EnsureDeleted()
;
// context.Database.Migrate
();
//
_sqlObjectInstalled = true;
//
}
//
}
//
}
private
void
InitializeDatabase
()
{
if
(!
_sqlObjectInstalled
)
{
using
(
CreateScope
())
{
var
storage
=
GetService
<
SqlServerStorage
>();
var
token
=
new
CancellationTokenSource
().
Token
;
storage
.
InitializeAsync
(
token
).
Wait
();
_sqlObjectInstalled
=
true
;
}
}
}
//
private void DeleteAllData()
//
{
//
using (CreateScope())
//
{
//
var context = GetService<TestDbContext>();
private
void
DeleteAllData
()
{
using
(
CreateScope
())
{
var
context
=
GetService
<
TestDbContext
>();
//
var commands = new[]
//
{
//
"DISABLE TRIGGER ALL ON ?",
//
"ALTER TABLE ? NOCHECK CONSTRAINT ALL",
//
"DELETE FROM ?",
//
"ALTER TABLE ? CHECK CONSTRAINT ALL",
//
"ENABLE TRIGGER ALL ON ?"
//
};
//
foreach (var command in commands)
//
{
//
context.Database.GetDbConnection().Execute(
//
"sp_MSforeachtable",
// new {command1 = command
},
//
commandType: CommandType.StoredProcedure);
//
}
//
}
//
}
//
}
var
commands
=
new
[]
{
"DISABLE TRIGGER ALL ON ?"
,
"ALTER TABLE ? NOCHECK CONSTRAINT ALL"
,
"DELETE FROM ?"
,
"ALTER TABLE ? CHECK CONSTRAINT ALL"
,
"ENABLE TRIGGER ALL ON ?"
};
foreach
(
var
command
in
commands
)
{
context
.
Database
.
GetDbConnection
().
Execute
(
"sp_MSforeachtable"
,
new
{
command1
=
command
},
commandType
:
CommandType
.
StoredProcedure
);
}
}
}
}
}
\ No newline at end of file
test/DotNetCore.CAP.SqlServer.Test/TestDbContext.cs
0 → 100644
View file @
e72cc784
using
Microsoft.EntityFrameworkCore
;
namespace
DotNetCore.CAP.SqlServer.Test
{
public
class
TestDbContext
:
DbContext
{
protected
override
void
OnConfiguring
(
DbContextOptionsBuilder
optionsBuilder
)
{
var
connectionString
=
ConnectionUtil
.
GetConnectionString
();
optionsBuilder
.
UseSqlServer
(
connectionString
);
}
}
}
\ No newline at end of file
test/DotNetCore.CAP.SqlServer.Test/TestHost.cs
View file @
e72cc784
...
...
@@ -2,7 +2,7 @@ using System;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.DependencyInjection
;
namespace
DotNetCore.CAP.
EntityFrameworkCore
.Test
namespace
DotNetCore.CAP.
SqlServer
.Test
{
public
abstract
class
TestHost
:
IDisposable
{
...
...
@@ -28,8 +28,9 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test
services
.
AddLogging
();
var
connectionString
=
ConnectionUtil
.
GetConnectionString
();
//services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString });
//services.AddDbContext<TestDbContext>(options => options.UseSqlServer(connectionString));
services
.
AddSingleton
(
new
SqlServerOptions
{
ConnectionString
=
connectionString
});
services
.
AddSingleton
<
SqlServerStorage
>();
services
.
AddDbContext
<
TestDbContext
>(
options
=>
options
.
UseSqlServer
(
connectionString
));
_services
=
services
;
}
...
...
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs
View file @
e72cc784
...
...
@@ -8,17 +8,21 @@ namespace DotNetCore.CAP.Test
{
public
class
CapBuilderTest
{
[
Fact
]
public
void
CanCreateInstanceAndGetService
()
{
var
services
=
new
ServiceCollection
();
services
.
AddSingleton
<
ICapPublisher
,
MyProducerService
>();
var
builder
=
new
CapBuilder
(
services
);
Assert
.
NotNull
(
builder
);
var
count
=
builder
.
Services
.
Count
;
Assert
.
Equal
(
1
,
count
);
var
provider
=
services
.
BuildServiceProvider
();
var
capPublisher
=
provider
.
GetService
<
ICapPublisher
>();
Assert
.
NotNull
(
capPublisher
);
}
[
Fact
]
...
...
@@ -45,6 +49,16 @@ namespace DotNetCore.CAP.Test
Assert
.
NotNull
(
thingy
);
}
[
Fact
]
public
void
CanResolveCapOptions
()
{
var
services
=
new
ServiceCollection
();
services
.
AddCap
(
x
=>
{
});
var
builder
=
services
.
BuildServiceProvider
();
var
capOptions
=
builder
.
GetService
<
CapOptions
>();
Assert
.
NotNull
(
capOptions
);
}
private
class
MyProducerService
:
ICapPublisher
{
public
Task
PublishAsync
(
string
topic
,
string
content
)
...
...
test/DotNetCore.CAP.Test/ConsistencyOptionsTest.cs
deleted
100644 → 0
View file @
c1db4267
namespace
CDotNetCore.CAPTest
{
public
class
ConsistencyOptionsTest
{
}
}
\ No newline at end of file
test/DotNetCore.CAP.Test/NoopMessageStore.cs
deleted
100644 → 0
View file @
c1db4267
using
System
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
DotNetCore.CAP.Models
;
namespace
DotNetCore.CAP.Test
{
//public class NoopMessageStore : ICapMessageStore
//{
// public Task<OperateResult> ChangeReceivedMessageStateAsync(CapReceivedMessage message, string statusName,
// bool autoSaveChanges = true)
// {
// throw new NotImplementedException();
// }
// public Task<OperateResult> StoreSentMessageAsync(CapSentMessage message)
// {
// throw new NotImplementedException();
// }
//}
}
\ No newline at end of file
test/DotNetCore.CAP.Test/StateChangerTest.cs
0 → 100644
View file @
e72cc784
using
System
;
using
DotNetCore.CAP.Infrastructure
;
using
DotNetCore.CAP.Models
;
using
DotNetCore.CAP.Processor.States
;
using
Moq
;
using
Xunit
;
namespace
DotNetCore.CAP.Test
{
public
class
StateChangerTest
{
[
Fact
]
public
void
ChangeState
()
{
// Arrange
var
fixture
=
Create
();
var
message
=
new
CapPublishedMessage
{
StatusName
=
StatusName
.
Enqueued
};
var
state
=
Mock
.
Of
<
IState
>(
s
=>
s
.
Name
==
"s"
&&
s
.
ExpiresAfter
==
null
);
var
mockTransaction
=
new
Mock
<
IStorageTransaction
>();
// Act
fixture
.
ChangeState
(
message
,
state
,
mockTransaction
.
Object
);
// Assert
Assert
.
Equal
(
message
.
StatusName
,
"s"
);
Assert
.
Null
(
message
.
ExpiresAt
);
Mock
.
Get
(
state
).
Verify
(
s
=>
s
.
Apply
(
message
,
mockTransaction
.
Object
),
Times
.
Once
);
mockTransaction
.
Verify
(
t
=>
t
.
UpdateMessage
(
message
),
Times
.
Once
);
mockTransaction
.
Verify
(
t
=>
t
.
CommitAsync
(),
Times
.
Never
);
}
[
Fact
]
public
void
ChangeState_ExpiresAfter
()
{
// Arrange
var
fixture
=
Create
();
var
message
=
new
CapPublishedMessage
{
StatusName
=
StatusName
.
Enqueued
};
var
state
=
Mock
.
Of
<
IState
>(
s
=>
s
.
Name
==
"s"
&&
s
.
ExpiresAfter
==
TimeSpan
.
FromHours
(
1
));
var
mockTransaction
=
new
Mock
<
IStorageTransaction
>();
// Act
fixture
.
ChangeState
(
message
,
state
,
mockTransaction
.
Object
);
// Assert
Assert
.
Equal
(
message
.
StatusName
,
"s"
);
Assert
.
NotNull
(
message
.
ExpiresAt
);
mockTransaction
.
Verify
(
t
=>
t
.
UpdateMessage
(
message
),
Times
.
Once
);
mockTransaction
.
Verify
(
t
=>
t
.
CommitAsync
(),
Times
.
Never
);
}
private
StateChanger
Create
()
=>
new
StateChanger
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment