tests(infra.tests): added builder classes for creating test entities
This commit is contained in:
@@ -81,6 +81,7 @@ dotnet_remove_unnecessary_suppression_exclusions = none
|
|||||||
|
|
||||||
# analyzer settings
|
# analyzer settings
|
||||||
dotnet_diagnostic.IDE0058.severity = none
|
dotnet_diagnostic.IDE0058.severity = none
|
||||||
|
dotnet_diagnostic.IDE0053.severity = when_on_single_line:suggestion
|
||||||
dotnet_diagnostic.IDE0100.severity = none
|
dotnet_diagnostic.IDE0100.severity = none
|
||||||
dotnet_diagnostic.CA1515.severity = none
|
dotnet_diagnostic.CA1515.severity = none
|
||||||
dotnet_diagnostic.CA1848.severity = none
|
dotnet_diagnostic.CA1848.severity = none
|
||||||
|
|||||||
@@ -15,19 +15,12 @@ public sealed class Institution : Entity
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Institution From(string? name, InstitutionMetadata metadata)
|
public static Institution From(string? name, InstitutionMetadata metadata)
|
||||||
{
|
|
||||||
return From(null, name, metadata);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Institution From(User? user, string? name, InstitutionMetadata metadata)
|
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(name, nameof(name));
|
ArgumentNullException.ThrowIfNull(name, nameof(name));
|
||||||
ArgumentNullException.ThrowIfNull(metadata, nameof(metadata));
|
ArgumentNullException.ThrowIfNull(metadata, nameof(metadata));
|
||||||
|
|
||||||
return new Institution()
|
return new Institution()
|
||||||
{
|
{
|
||||||
UserId = user?.Id ?? Guid.Empty,
|
|
||||||
User = user,
|
|
||||||
Name = name,
|
Name = name,
|
||||||
Metadata = metadata
|
Metadata = metadata
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ internal sealed class AccountBuilder
|
|||||||
{
|
{
|
||||||
private string _name = "accountName";
|
private string _name = "accountName";
|
||||||
private AccountMetadata? _metadata;
|
private AccountMetadata? _metadata;
|
||||||
|
private readonly List<Transaction> _transactions = [];
|
||||||
|
|
||||||
private AccountBuilder()
|
private AccountBuilder()
|
||||||
{
|
{
|
||||||
@@ -28,6 +29,14 @@ internal sealed class AccountBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AccountBuilder WithTransaction(Action<TransactionBuilder>? action = null)
|
||||||
|
{
|
||||||
|
var tb = TransactionBuilder.Create();
|
||||||
|
action?.Invoke(tb);
|
||||||
|
_transactions.Add(tb.Build());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Account Build()
|
public Account Build()
|
||||||
{
|
{
|
||||||
if (_metadata is null)
|
if (_metadata is null)
|
||||||
@@ -35,6 +44,13 @@ internal sealed class AccountBuilder
|
|||||||
throw new InvalidOperationException($"You must call {nameof(WithMetadata)} prior to building");
|
throw new InvalidOperationException($"You must call {nameof(WithMetadata)} prior to building");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Account.From(_name, _metadata);
|
var account = Account.From(_name, _metadata);
|
||||||
|
|
||||||
|
foreach (var transaction in _transactions)
|
||||||
|
{
|
||||||
|
account.AddTransaction(transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
return account;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@ namespace FiscalOS.Infra.Tests.Data;
|
|||||||
|
|
||||||
internal sealed class InstitutionBuilder
|
internal sealed class InstitutionBuilder
|
||||||
{
|
{
|
||||||
private User? _user;
|
|
||||||
private string _name = "institutionName";
|
private string _name = "institutionName";
|
||||||
private InstitutionMetadata? _metadata;
|
private InstitutionMetadata? _metadata;
|
||||||
private readonly List<Account> _accounts = [];
|
private readonly List<Account> _accounts = [];
|
||||||
@@ -16,12 +15,6 @@ internal sealed class InstitutionBuilder
|
|||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
public InstitutionBuilder WithUser(User user)
|
|
||||||
{
|
|
||||||
_user = user;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InstitutionBuilder WithName(string name)
|
public InstitutionBuilder WithName(string name)
|
||||||
{
|
{
|
||||||
_name = name;
|
_name = name;
|
||||||
@@ -52,7 +45,6 @@ internal sealed class InstitutionBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
var institution = Institution.From(
|
var institution = Institution.From(
|
||||||
_user,
|
|
||||||
_name,
|
_name,
|
||||||
_metadata
|
_metadata
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
namespace FiscalOS.Infra.Tests.Data;
|
||||||
|
|
||||||
|
internal sealed class TransactionBuilder
|
||||||
|
{
|
||||||
|
private string _merchantName = "merchantName";
|
||||||
|
private string _description = "description";
|
||||||
|
private decimal _amount = 100;
|
||||||
|
private DateTimeOffset _date = DateTimeOffset.UtcNow;
|
||||||
|
private TransactionMetadata? _metadata;
|
||||||
|
|
||||||
|
private TransactionBuilder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TransactionBuilder Create()
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder WithMerchantName(string merchantName)
|
||||||
|
{
|
||||||
|
_merchantName = merchantName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder WithDescription(string description)
|
||||||
|
{
|
||||||
|
_description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder WithAmount(decimal amount)
|
||||||
|
{
|
||||||
|
_amount = amount;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder WithDate(DateTimeOffset date)
|
||||||
|
{
|
||||||
|
_date = date;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionBuilder WithMetadata(Action<TransactionMetadataBuilder>? action = null)
|
||||||
|
{
|
||||||
|
var tmb = TransactionMetadataBuilder.Create();
|
||||||
|
action?.Invoke(tmb);
|
||||||
|
_metadata = tmb.Build();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Transaction Build()
|
||||||
|
{
|
||||||
|
if (_metadata is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"You must call {nameof(WithMetadata)} prior to building");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Transaction.From(
|
||||||
|
_merchantName,
|
||||||
|
_description,
|
||||||
|
_amount,
|
||||||
|
_date,
|
||||||
|
_metadata
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
namespace FiscalOS.Infra.Tests.Data;
|
||||||
|
|
||||||
|
internal sealed class TransactionMetadataBuilder
|
||||||
|
{
|
||||||
|
private string _plaidId = "plaidId";
|
||||||
|
|
||||||
|
private TransactionMetadataBuilder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TransactionMetadataBuilder Create()
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransactionMetadataBuilder WithPlaidId(string plaidId)
|
||||||
|
{
|
||||||
|
_plaidId = plaidId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlaidTransactionMetadata Build()
|
||||||
|
{
|
||||||
|
return PlaidTransactionMetadata.From(_plaidId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,43 +1,5 @@
|
|||||||
namespace FiscalOS.Infra.Tests.Data;
|
namespace FiscalOS.Infra.Tests.Data;
|
||||||
|
|
||||||
// protected async Task<User> CreateTestUserAsync()
|
|
||||||
// {
|
|
||||||
// var user = User.From(
|
|
||||||
// "username",
|
|
||||||
// "hashedPassword",
|
|
||||||
// EncryptedDataKey.From("keyUsed", "encryptedKey")
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// var institutionMetadata = PlaidInstitutionMetadata.From(
|
|
||||||
// "plaidId",
|
|
||||||
// "plaidName",
|
|
||||||
// "encryptedAccessToken",
|
|
||||||
// "itemId"
|
|
||||||
// );
|
|
||||||
// var institution = Institution.From("institutionName", institutionMetadata);
|
|
||||||
// user.AddInstitution(institution);
|
|
||||||
//
|
|
||||||
// var accountMetadata = PlaidAccountMetadata.From("plaidId", "plaidName");
|
|
||||||
// var account = Account.From("some account", accountMetadata);
|
|
||||||
// user.AddAccount(account);
|
|
||||||
// institution.AddAccount(account);
|
|
||||||
//
|
|
||||||
// var transactionMetadata = PlaidTransactionMetadata.From("plaidId");
|
|
||||||
// var transaction = Transaction.From(
|
|
||||||
// "merchantName",
|
|
||||||
// "description",
|
|
||||||
// 100,
|
|
||||||
// DateTimeOffset.UtcNow,
|
|
||||||
// transactionMetadata
|
|
||||||
// );
|
|
||||||
// user.AddTransaction(transaction);
|
|
||||||
// account.AddTransaction(transaction);
|
|
||||||
//
|
|
||||||
// await AppDbContext.AddAsync(user, TestContext.Current.CancellationToken);
|
|
||||||
// await AppDbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
|
|
||||||
//
|
|
||||||
// return user;
|
|
||||||
// }
|
|
||||||
internal sealed class UserBuilder
|
internal sealed class UserBuilder
|
||||||
{
|
{
|
||||||
private string _username = "username";
|
private string _username = "username";
|
||||||
@@ -46,7 +8,7 @@ internal sealed class UserBuilder
|
|||||||
"keyUsed",
|
"keyUsed",
|
||||||
"encryptedKey"
|
"encryptedKey"
|
||||||
);
|
);
|
||||||
private readonly List<InstitutionBuilder> _instituionBuilders = [];
|
private readonly List<Institution> _instituions = [];
|
||||||
|
|
||||||
private UserBuilder()
|
private UserBuilder()
|
||||||
{
|
{
|
||||||
@@ -79,7 +41,7 @@ internal sealed class UserBuilder
|
|||||||
{
|
{
|
||||||
var ib = InstitutionBuilder.Create();
|
var ib = InstitutionBuilder.Create();
|
||||||
action?.Invoke(ib);
|
action?.Invoke(ib);
|
||||||
_instituionBuilders.Add(ib);
|
_instituions.Add(ib.Build());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,10 +53,19 @@ internal sealed class UserBuilder
|
|||||||
_dataKey
|
_dataKey
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (var ib in _instituionBuilders)
|
foreach (var institution in _instituions)
|
||||||
{
|
{
|
||||||
var institution = ib.WithUser(user).Build();
|
|
||||||
user.AddInstitution(institution);
|
user.AddInstitution(institution);
|
||||||
|
|
||||||
|
foreach (var account in institution.Accounts)
|
||||||
|
{
|
||||||
|
user.AddAccount(account);
|
||||||
|
|
||||||
|
foreach (var transaction in account.Transactions)
|
||||||
|
{
|
||||||
|
user.AddTransaction(transaction);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
@@ -24,28 +24,17 @@ public abstract class IntegrationTest : IAsyncLifetime
|
|||||||
.WithInstitution(static ib =>
|
.WithInstitution(static ib =>
|
||||||
{
|
{
|
||||||
ib.WithMetadata();
|
ib.WithMetadata();
|
||||||
ib.WithAccount(static ab =>
|
ib.WithAccount(static ab =>
|
||||||
{
|
{
|
||||||
ab.WithMetadata();
|
ab.WithMetadata();
|
||||||
|
ab.WithTransaction(static tb =>
|
||||||
|
{
|
||||||
|
tb.WithMetadata();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var accountMetadata = PlaidAccountMetadata.From("plaidId", "plaidName");
|
|
||||||
var account = Account.From("some account", accountMetadata);
|
|
||||||
user.AddAccount(account);
|
|
||||||
|
|
||||||
var transactionMetadata = PlaidTransactionMetadata.From("plaidId");
|
|
||||||
var transaction = Transaction.From(
|
|
||||||
"merchantName",
|
|
||||||
"description",
|
|
||||||
100,
|
|
||||||
DateTimeOffset.UtcNow,
|
|
||||||
transactionMetadata
|
|
||||||
);
|
|
||||||
user.AddTransaction(transaction);
|
|
||||||
account.AddTransaction(transaction);
|
|
||||||
|
|
||||||
await AppDbContext.AddAsync(user, TestContext.Current.CancellationToken);
|
await AppDbContext.AddAsync(user, TestContext.Current.CancellationToken);
|
||||||
await AppDbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
|
await AppDbContext.SaveChangesAsync(TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user