Skip to content
Merged
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 @@ -4,7 +4,9 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using EdFi.Ods.AdminApi.Common.Constants;
using EdFi.Ods.AdminApi.Common.Infrastructure;
using EdFi.Ods.AdminApi.Common.Infrastructure.Models;
using EdFi.Ods.AdminApi.Infrastructure.Database.Queries;
Expand All @@ -16,6 +18,9 @@ namespace EdFi.Ods.AdminApi.DBTests.Database.QueryTests;
[TestFixture]
public class GetDbInstancesQueryTests : AdminApiDbContextTestBase
{
private static IEnumerable<string> AllStatuses =>
Enum.GetValues<DbInstanceStatus>().Select(status => status.ToString());

[Test]
public void ShouldRetrieveDbInstances()
{
Expand Down Expand Up @@ -150,4 +155,22 @@ public void ShouldRetrieveDbInstancesOrderedById()
ids.ShouldBe(ids.OrderBy(x => x).ToList());
});
}

[Test]
[TestCaseSource(nameof(AllStatuses))]
public void ShouldIncludeDbInstances_ForAllStatuses_WhenNoFiltersApplied(string status)
{
Save(
new DbInstance { Name = $"Status-{status}", OdsInstanceId = 111, Status = status, DatabaseTemplate = "Minimal", LastRefreshed = DateTime.UtcNow },
new DbInstance { Name = "Active Instance", OdsInstanceId = 222, Status = "Created", DatabaseTemplate = "Minimal", LastRefreshed = DateTime.UtcNow }
);

Transaction(context =>
{
var query = new GetDbInstancesQuery(context, Testing.GetAppSettings());
var results = query.Execute(new CommonQueryParams(0, null), null, null);
results.Count.ShouldBe(2);
results.ShouldContain(r => r.Name == $"Status-{status}" && r.Status == status);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using EdFi.Admin.DataAccess.Models;
using EdFi.Ods.AdminApi.Common.Constants;
Expand All @@ -22,6 +24,10 @@ namespace EdFi.Ods.AdminApi.UnitTests.Features.OdsInstances;
[TestFixture]
public class ReadEducationOrganizationsTests
{
private static IEnumerable<string> AllStatuses =>
Enum.GetValues<DbInstanceStatus>()
.Select(status => status.ToString());

private IGetEducationOrganizationsQuery _getEdOrgsQuery = null!;
private IGetDbInstancesQuery _getDbInstancesQuery = null!;
private IGetOdsInstanceQuery _getOdsInstanceQuery = null!;
Expand Down Expand Up @@ -110,6 +116,50 @@ public async Task GetEducationOrganizations_AppendsUnlinkedDbInstances_WithNegat
ok.Value.ShouldAllBe(i => i.EducationOrganizations.Count == 0);
}

[Test]
[TestCaseSource(nameof(AllStatuses))]
public async Task GetEducationOrganizations_AppendsOrphanedLinkedDbInstance_ForAllStatuses(string status)
{
A.CallTo(() => _getEdOrgsQuery.ExecuteAsync(_queryParams, null))
.Returns(new List<OdsInstanceWithEducationOrganizationsModel>());
A.CallTo(() => _getDbInstancesQuery.Execute(A<CommonQueryParams>._, null, null))
.Returns(new List<DbInstance>
{
new DbInstance { Id = 10, Name = $"Orphan-{status}", OdsInstanceId = 999, Status = status, DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_AnyStatus" }
});

var result = await ReadEducationOrganizations.GetEducationOrganizations(_getEdOrgsQuery, _getDbInstancesQuery, _queryParams);

var ok = result as Microsoft.AspNetCore.Http.HttpResults.Ok<List<OdsInstanceWithEducationOrganizationsModel>>;
ok.ShouldNotBeNull();
ok.Value!.Count.ShouldBe(1);
ok.Value[0].Id.ShouldBe(-1);
ok.Value[0].DbInstanceId.ShouldBe(10);
ok.Value[0].Status.ShouldBe(status);
}

[Test]
public async Task GetEducationOrganizations_AppendsLatestDbInstancePerMissingOdsInstanceId()
{
A.CallTo(() => _getEdOrgsQuery.ExecuteAsync(_queryParams, null))
.Returns(new List<OdsInstanceWithEducationOrganizationsModel>());
A.CallTo(() => _getDbInstancesQuery.Execute(A<CommonQueryParams>._, null, null))
.Returns(new List<DbInstance>
{
new DbInstance { Id = 20, Name = "Orphan-Older", OdsInstanceId = 999, Status = DbInstanceStatus.CreateFailed.ToString(), DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_Old", LastRefreshed = DateTime.UtcNow.AddMinutes(-10) },
new DbInstance { Id = 21, Name = "Orphan-Newer", OdsInstanceId = 999, Status = DbInstanceStatus.Deleted.ToString(), DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_New", LastModifiedDate = DateTime.UtcNow }
});

var result = await ReadEducationOrganizations.GetEducationOrganizations(_getEdOrgsQuery, _getDbInstancesQuery, _queryParams);

var ok = result as Microsoft.AspNetCore.Http.HttpResults.Ok<List<OdsInstanceWithEducationOrganizationsModel>>;
ok.ShouldNotBeNull();
ok.Value!.Count.ShouldBe(1);
ok.Value[0].DbInstanceId.ShouldBe(21);
ok.Value[0].Status.ShouldBe(DbInstanceStatus.Deleted.ToString());
ok.Value[0].Name.ShouldBe("Orphan-Newer");
}

[Test]
public async Task GetEducationOrganizationsByInstance_DoesNotAppendUnlinkedDbInstances()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using EdFi.Admin.DataAccess.Models;
Expand All @@ -29,6 +30,10 @@ namespace EdFi.Ods.AdminApi.UnitTests.Infrastructure.Services.Tenants;
[TestFixture]
internal class TenantServiceTests
{
private static IEnumerable<string> AllStatuses =>
Enum.GetValues<DbInstanceStatus>()
.Select(status => status.ToString());

private IOptionsSnapshot<AppSettingsFile> _options = null!;
private IMemoryCache _memoryCache = null!;
private AppSettingsFile _appSettings = null!;
Expand Down Expand Up @@ -372,4 +377,79 @@ public async Task GetTenantEdOrgsByInstancesAsync_MixedScenario_LinkedAndUnlinke
unlinkedInstance.Name.ShouldBe("Unlinked-C");
unlinkedInstance.Status.ShouldBe(DbInstanceStatus.PendingCreate.ToString());
}

[Test]
[TestCaseSource(nameof(AllStatuses))]
public async Task GetTenantEdOrgsByInstancesAsync_AddsDbInstance_WhenLinkedToMissingOdsInstance_ForAllStatuses(string status)
{
_appSettings.AppSettings.MultiTenancy = false;
var service = new TenantService(_options, _memoryCache);

A.CallTo(() => _getOdsInstancesQuery.Execute()).Returns([]);

var orphan = new DbInstance
{
Id = 42,
Name = $"Orphan-{status}",
OdsInstanceId = 9002,
Status = status,
DatabaseTemplate = "Minimal",
DatabaseName = "EdFi_ODS_9002",
LastRefreshed = System.DateTime.UtcNow
};
A.CallTo(() => _getDbInstancesQuery.Execute(A<CommonQueryParams>._, A<int?>._, A<string>.Ignored))
.Returns([orphan]);

var result = await service.GetTenantEdOrgsByInstancesAsync(
_getOdsInstancesQuery, _getEducationOrganizationQuery, _getDbInstancesQuery, Constants.DefaultTenantName);

result.ShouldNotBeNull();
result!.OdsInstances.Count.ShouldBe(1);
result.OdsInstances[0].OdsInstanceId.ShouldBe(-1);
result.OdsInstances[0].DbInstanceId.ShouldBe(42);
result.OdsInstances[0].Status.ShouldBe(status);
}

[Test]
public async Task GetTenantEdOrgsByInstancesAsync_AppendsLatestDbInstancePerMissingOdsInstanceId()
{
_appSettings.AppSettings.MultiTenancy = false;
var service = new TenantService(_options, _memoryCache);

A.CallTo(() => _getOdsInstancesQuery.Execute()).Returns([]);

var older = new DbInstance
{
Id = 50,
Name = "Orphan-Older",
OdsInstanceId = 9003,
Status = DbInstanceStatus.CreateFailed.ToString(),
DatabaseTemplate = "Minimal",
DatabaseName = "EdFi_ODS_9003_old",
LastRefreshed = System.DateTime.UtcNow.AddMinutes(-10)
};

var newer = new DbInstance
{
Id = 51,
Name = "Orphan-Newer",
OdsInstanceId = 9003,
Status = DbInstanceStatus.Deleted.ToString(),
DatabaseTemplate = "Minimal",
DatabaseName = "EdFi_ODS_9003_new",
LastModifiedDate = System.DateTime.UtcNow
};

A.CallTo(() => _getDbInstancesQuery.Execute(A<CommonQueryParams>._, A<int?>._, A<string>.Ignored))
.Returns([older, newer]);

var result = await service.GetTenantEdOrgsByInstancesAsync(
_getOdsInstancesQuery, _getEducationOrganizationQuery, _getDbInstancesQuery, Constants.DefaultTenantName);

result.ShouldNotBeNull();
result!.OdsInstances.Count.ShouldBe(1);
result.OdsInstances[0].DbInstanceId.ShouldBe(51);
result.OdsInstances[0].Status.ShouldBe(DbInstanceStatus.Deleted.ToString());
result.OdsInstances[0].Name.ShouldBe("Orphan-Newer");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using EdFi.Ods.AdminApi.Common.Constants;
using EdFi.Ods.AdminApi.Common.Infrastructure;
using EdFi.Ods.AdminApi.Common.Infrastructure.Models;
using EdFi.Ods.AdminApi.V3.Infrastructure.Database.Queries;
Expand All @@ -16,6 +18,9 @@ namespace EdFi.Ods.AdminApi.V3.DBTests.Database.QueryTests;
[TestFixture]
public class GetDbDataStoresQueryTests : AdminApiDbContextTestBase
{
private static IEnumerable<string> AllStatuses =>
Enum.GetValues<DbInstanceStatus>().Select(status => status.ToString());

[Test]
public void ShouldRetrieveDbInstances()
{
Expand Down Expand Up @@ -150,7 +155,23 @@ public void ShouldRetrieveDbInstancesOrderedById()
ids.ShouldBe(ids.OrderBy(x => x).ToList());
});
}
}

[Test]
[TestCaseSource(nameof(AllStatuses))]
public void ShouldIncludeDbDataStores_ForAllStatuses_WhenNoFiltersApplied(string status)
{
Save(
new DbInstance { Name = $"Status-{status}", OdsInstanceId = 111, Status = status, DatabaseTemplate = "Minimal", LastRefreshed = DateTime.UtcNow },
new DbInstance { Name = "Active DataStore", OdsInstanceId = 222, Status = "Created", DatabaseTemplate = "Minimal", LastRefreshed = DateTime.UtcNow }
);

Transaction(context =>
{
var query = new GetDbDataStoresQuery(context, Testing.GetAppSettings());
var results = query.Execute(new CommonQueryParams(0, null), null, null);
results.Count.ShouldBe(2);
results.ShouldContain(r => r.Name == $"Status-{status}" && r.Status == status);
});
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System.Collections.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
using EdFi.Ods.AdminApi.Common.Constants;
using EdFi.Ods.AdminApi.Common.Infrastructure;
Expand All @@ -20,6 +22,10 @@ namespace EdFi.Ods.AdminApi.V3.UnitTests.Features.DataStores;
[TestFixture]
public class ReadEducationOrganizationsTests
{
private static IEnumerable<string> AllStatuses =>
Enum.GetValues<DbInstanceStatus>()
.Select(status => status.ToString());

private IGetEducationOrganizationsQuery _getEdOrgsQuery = null!;
private IGetDbDataStoresQuery _getDbDataStoresQuery = null!;
private IGetDataStoreQuery _getDataStoreQuery = null!;
Expand Down Expand Up @@ -104,6 +110,48 @@ public async Task GetEducationOrganizations_AppendsUnlinkedDbDataStores_WithNega
ok.Value.ShouldAllBe(i => i.EducationOrganizations.Count == 0);
}

[Test]
[TestCaseSource(nameof(AllStatuses))]
public async Task GetEducationOrganizations_AppendsOrphanedLinkedDbDataStore_ForAllStatuses(string status)
{
A.CallTo(() => _getEdOrgsQuery.ExecuteAsync(_queryParams, null))
.Returns(new List<DataStoreWithEducationOrganizationsModel>());
A.CallTo(() => _getDbDataStoresQuery.Execute(A<CommonQueryParams>._, null, null))
.Returns(new List<DbInstance>
{
new DbInstance { Id = 10, Name = $"Orphan-{status}", OdsInstanceId = 999, Status = status, DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_AnyStatus" }
});

var result = await ReadEducationOrganizations.GetEducationOrganizations(_getEdOrgsQuery, _getDbDataStoresQuery, _queryParams);

var ok = result as Microsoft.AspNetCore.Http.HttpResults.Ok<List<DataStoreWithEducationOrganizationsModel>>;
ok.ShouldNotBeNull();
ok.Value!.Count.ShouldBe(1);
ok.Value[0].Id.ShouldBe(-1);
ok.Value[0].Status.ShouldBe(status);
}

[Test]
public async Task GetEducationOrganizations_AppendsLatestDbDataStorePerMissingDataStoreId()
{
A.CallTo(() => _getEdOrgsQuery.ExecuteAsync(_queryParams, null))
.Returns(new List<DataStoreWithEducationOrganizationsModel>());
A.CallTo(() => _getDbDataStoresQuery.Execute(A<CommonQueryParams>._, null, null))
.Returns(new List<DbInstance>
{
new DbInstance { Id = 20, Name = "Orphan-Older", OdsInstanceId = 999, Status = DbInstanceStatus.CreateFailed.ToString(), DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_Old", LastRefreshed = DateTime.UtcNow.AddMinutes(-10) },
new DbInstance { Id = 21, Name = "Orphan-Newer", OdsInstanceId = 999, Status = DbInstanceStatus.Deleted.ToString(), DatabaseTemplate = "Minimal", DatabaseName = "EdFi_Ods_New", LastModifiedDate = DateTime.UtcNow }
});

var result = await ReadEducationOrganizations.GetEducationOrganizations(_getEdOrgsQuery, _getDbDataStoresQuery, _queryParams);

var ok = result as Microsoft.AspNetCore.Http.HttpResults.Ok<List<DataStoreWithEducationOrganizationsModel>>;
ok.ShouldNotBeNull();
ok.Value!.Count.ShouldBe(1);
ok.Value[0].Status.ShouldBe(DbInstanceStatus.Deleted.ToString());
ok.Value[0].Name.ShouldBe("Orphan-Newer");
}

[Test]
public async Task GetEducationOrganizationsByDataStore_DoesNotAppendUnlinkedDbDataStores()
{
Expand Down
Loading
Loading