Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public async Task CreateManyAsync(IEnumerable<IEvent> entities)
var tableEvents = entities.Select(e => e as Core.Entities.Event ?? new Core.Entities.Event(e));
var entityEvents = Mapper.Map<List<Event>>(tableEvents);
entityEvents.ForEach(e => e.SetNewId());

// SQLite deployments can fail to resolve the linq2db bulk copy provider adapter
if (dbContext.Database.IsSqlite())
{
await dbContext.Events.AddRangeAsync(entityEvents);
await dbContext.SaveChangesAsync();
return;
}

await dbContext.BulkCopyAsync(entityEvents);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Xunit;
using Event = Bit.Core.Entities.Event;
using IEventRepository = Bit.Core.Repositories.IEventRepository;

namespace Bit.Infrastructure.IntegrationTest.Dirt.Repositories;

/// <summary>
/// Covers <c>CreateManyAsync</c> on every configured provider. <c>IEventRepository</c> is only
/// registered for self-hosted deployments, hence <c>SelfHosted = true</c>; that resolves to the
/// Dapper implementation on SQL Server and the EF implementation on the other providers.
/// </summary>
public class EventRepositoryCreateManyTests
{
[Theory, DatabaseData(SelfHosted = true)]
public async Task CreateManyAsync_MultipleEvents_PersistsEveryEvent(IEventRepository sut)
{
var organizationId = Guid.NewGuid();

await sut.CreateManyAsync(BuildEvents(organizationId, 3));

Assert.Equal(3, (await ReadEventsAsync(sut, organizationId)).Count);
}

[Theory, DatabaseData(SelfHosted = true)]
public async Task CreateManyAsync_SingleEvent_PersistsThatEvent(IEventRepository sut)
{
var organizationId = Guid.NewGuid();

await sut.CreateManyAsync(BuildEvents(organizationId, 1));

Assert.Single(await ReadEventsAsync(sut, organizationId));
}

[Theory, DatabaseData(SelfHosted = true)]
public async Task CreateManyAsync_NoEvents_DoesNotThrow(IEventRepository sut)
{
var organizationId = Guid.NewGuid();

await sut.CreateManyAsync([]);

Assert.Empty(await ReadEventsAsync(sut, organizationId));
}

private static List<Event> BuildEvents(Guid organizationId, int count) =>
Enumerable.Range(0, count)
.Select(i => new Event
{
Type = EventType.Organization_Updated,
OrganizationId = organizationId,
Date = DateTime.UtcNow.AddMinutes(-i),
})
.ToList();

private static async Task<IReadOnlyCollection<IEvent>> ReadEventsAsync(
IEventRepository sut, Guid organizationId)
{
var result = await sut.GetManyByOrganizationAsync(
organizationId, DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1),
new PageOptions { PageSize = 100 });
return result.Data;
}
}
Loading