-
Notifications
You must be signed in to change notification settings - Fork 13
Создание слоя доступа к данным. #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0528eb6
3b4f1ac
40875eb
d6d5688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ TestResults | |
| TestResult.xml | ||
| /packages/ | ||
| /publish/ | ||
| _ReSharper* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System; | ||
| using System.Configuration; | ||
| using BuildRevisionCounter.Data; | ||
| using BuildRevisionCounter.Interfaces; | ||
|
|
||
| namespace BuildRevisionCounter.Tests | ||
| { | ||
| public static class DBStorageFactory | ||
| { | ||
| private static readonly Lazy<IUserDatabaseTestProvider> _defaultInstance = | ||
| new Lazy<IUserDatabaseTestProvider>(() => FromConfigurationConnectionString()); | ||
|
|
||
| public static IUserDatabaseTestProvider DefaultInstance { get { return _defaultInstance.Value; } } | ||
|
|
||
|
|
||
| public static IUserDatabaseTestProvider GetInstance<T>(string connectionStringName = "MongoDBStorage") where T : IUserDatabaseTestProvider | ||
| { | ||
| return GetDatabaseTestProvider<T>(connectionStringName); | ||
| } | ||
|
|
||
| public static IUserDatabaseTestProvider FromConfigurationConnectionString(string connectionStringName = "MongoDBStorage") | ||
| { | ||
| return GetDatabaseTestProvider(connectionStringName); | ||
| } | ||
|
|
||
| private static IUserDatabaseTestProvider GetDatabaseTestProvider(string connectionStringName) | ||
| { | ||
| var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; | ||
| connectionString = DBUtil.SetDatabaseNameRandom(typeof(MongoDBUserStorage), connectionString); | ||
| return new MongoDBUserStorage(connectionString); | ||
| } | ||
|
|
||
| private static IUserDatabaseTestProvider GetDatabaseTestProvider<T>(string connectionStringName) where T : IUserDatabaseTestProvider | ||
| { | ||
| var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString; | ||
| connectionString = DBUtil.SetDatabaseNameRandom(typeof (T), connectionString); | ||
| return (IUserDatabaseTestProvider)Activator.CreateInstance(typeof(T), connectionString); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| using System.Threading.Tasks; | ||
| using MongoDB.Driver; | ||
| using BuildRevisionCounter.Data; | ||
| using BuildRevisionCounter.Interfaces; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace BuildRevisionCounter.Tests | ||
| { | ||
| [TestFixture] | ||
| public class MongoDBStorageTest | ||
| public class DBStorageTest | ||
| { | ||
| private MongoDBStorage _storage; | ||
| private IUserDatabaseTestProvider _storage; | ||
|
|
||
| [TestFixtureSetUp] | ||
| public void SetUp() | ||
|
|
@@ -17,19 +18,17 @@ public void SetUp() | |
|
|
||
| public async Task SetUpAsync() | ||
| { | ||
| _storage = MongoDBStorageFactory.DefaultInstance; | ||
|
|
||
| await _storage.Revisions.Database.Client.DropDatabaseAsync( | ||
| _storage.Revisions.Database.DatabaseNamespace.DatabaseName); | ||
|
|
||
| _storage = DBStorageFactory.GetInstance<MongoDBUserStorage>(); | ||
| await _storage.SetUp(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task EnsureAdminUserCreated() | ||
| { | ||
| var user = await _storage.FindUser(MongoDBStorage.AdminName); | ||
| Assert.AreEqual(MongoDBStorage.AdminName, user.Name); | ||
| var adminName = _storage.GetAdminName(); | ||
| var user = await _storage.FindUser(adminName); | ||
| Assert.IsNotNull(user); | ||
| Assert.AreEqual(adminName, user.Name); | ||
| } | ||
|
|
||
| [Test] | ||
|
|
@@ -63,23 +62,24 @@ public async Task EnsureAdminUserMayBeInvokedMultipleTimes() | |
| [Test] | ||
| public async Task CreateUserMustThrowExceptionIfUserExists() | ||
| { | ||
| await _storage.CreateUser( | ||
| "CreateUserMustThrowExceptionIfUserExists", | ||
| "CreateUserMustThrowExceptionIfUserExists", | ||
| new[] {"testRole"}); | ||
|
|
||
| const string userName = "CreateUserMustThrowExceptionIfUserExists"; | ||
| const string userPass = "CreateUserMustThrowExceptionIfUserExists"; | ||
| var userRole = new[] {"testRole"}; | ||
| await _storage.CreateUser(userName, userPass, userRole); | ||
| try | ||
| { | ||
| await _storage.CreateUser( | ||
| "CreateUserMustThrowExceptionIfUserExists", | ||
| "CreateUserMustThrowExceptionIfUserExists", | ||
| new[] {"testRole"}); | ||
| await _storage.CreateUser(userName, userPass, userRole); | ||
| Assert.Fail(); | ||
| } | ||
| catch (MongoWriteException ex) | ||
| catch (DuplicateKeyException) | ||
| { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в NUnit есть более удобная конструкция ожидания исключения определенного типа.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. так было изначально: catch (MongoWriteException ex)
{
Assert.AreEqual(ServerErrorCategory.DuplicateKey, ex.WriteError.Category);
}а так написано в требованиях:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в NUnit можно пользоваться ExpectedExceptionAttribute или Asserts.Throws для более наглядного и простого способа декларации желаемого поведения. Я не понял ваш ответ. |
||
| Assert.AreEqual(ServerErrorCategory.DuplicateKey, ex.WriteError.Category); | ||
| } | ||
| } | ||
|
|
||
| [TestFixtureTearDown] | ||
| public void DropDatabaseAsync() | ||
| { | ||
| _storage.DropDatabaseAsync().Wait(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using BuildRevisionCounter.Data; | ||
| using BuildRevisionCounter.Interfaces; | ||
| using MongoDB.Driver; | ||
|
|
||
| namespace BuildRevisionCounter.Tests | ||
| { | ||
| internal static class DBUtil | ||
| { | ||
| internal static Task DropDatabaseAsync(this IUserDatabaseTestProvider dataProvider) | ||
| { | ||
| switch (GetDatabaseKind(dataProvider.GetType())) | ||
| { | ||
| case DatabaseKind.MongoDB: | ||
| return MangoDBDropDatabaseAsync(dataProvider.ConnectionString); | ||
| default: | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| internal static string SetDatabaseNameRandom(Type storageType, string connectionString) | ||
| { | ||
| switch (GetDatabaseKind(storageType)) | ||
| { | ||
| case DatabaseKind.MongoDB: | ||
| return MangoDBSetDatabaseNameRandom(connectionString); | ||
| default: | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
|
|
||
| private static string MangoDBSetDatabaseNameRandom(string connectionString) | ||
| { | ||
| var urlBuilder = new MongoUrlBuilder(connectionString) | ||
| { | ||
| DatabaseName = "brCounterTest_" + Guid.NewGuid().ToString("N") | ||
| }; | ||
| return urlBuilder.ToString(); | ||
| } | ||
|
|
||
| private static Task MangoDBDropDatabaseAsync(string connectionString) | ||
| { | ||
| var mongoUrl = MongoUrl.Create(connectionString); | ||
| return new MongoClient(mongoUrl).DropDatabaseAsync(mongoUrl.DatabaseName); | ||
| } | ||
|
|
||
|
|
||
| private enum DatabaseKind | ||
| { | ||
| Unknow, | ||
| MongoDB | ||
| } | ||
|
|
||
| private static DatabaseKind GetDatabaseKind(Type storageType) | ||
| { | ||
| if (storageType == typeof(MongoDBUserStorage)) | ||
| return DatabaseKind.MongoDB; | ||
| return DatabaseKind.Unknow; | ||
| } | ||
| } | ||
| } |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Все таки, избавимся от вызовов Activator.
Генерить случайное имя БД, не следует.
В случае сбоя тестов, будут накапливаться БД, которые надо будет удалять в ручную.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
добавлю абстрактный класс, чтобы получить требуемый конструктор.
Как раз ситуация с вопросом на собеседовании (и мой ответ про конструктор): интерфейсом тут конструктор не знаю как прикрутить.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Здесь ничего не надо абстрагироватью Просто
new MongoDBUserStorage(connectionString);достаточно.