feat(dal): ShortcodesService добавлена обработка динамичесих составляющих "%СВЯЗИ%", "%ТНК-КРАТКО%"
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.DomainServices.Implementations;
|
||||
// Предположим, что у вас есть реализации IJobService и IUnitService
|
||||
// Если нет, можно создать моки, но лучше использовать реальные, если они просты
|
||||
using PARR.DAL.Services.Implementations.Job; // Пример: JobService
|
||||
using PARR.DAL.Services.Implementations.Unit; // Пример: UnitService
|
||||
using PARR.DAL.Services.Interfaces.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace PARR.DAL.IntegrationTests.Integration
|
||||
{
|
||||
public class UnitFilterServiceIntegrationTests : IAsyncLifetime
|
||||
{
|
||||
private DbContextOptions<DataContext> _options;
|
||||
private DataContext _context;
|
||||
private UnitFilterService _service;
|
||||
|
||||
public UnitFilterServiceIntegrationTests()
|
||||
{
|
||||
_options = new DbContextOptionsBuilder<DataContext>()
|
||||
.UseInMemoryDatabase($"IntegrationTestDb_{Guid.NewGuid()}")
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
// Создаём контекст
|
||||
_context = new DataContext(_options);
|
||||
await _context.Database.EnsureCreatedAsync();
|
||||
|
||||
// Заполняем тестовые данные (убедитесь, что TestDataGenerator доступен)
|
||||
TestDataGenerator.Seed(_context); // или async, если вы его переделаете
|
||||
|
||||
// Создаём ServiceProvider с реальными сервисами
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection.AddLogging(builder => builder.AddConsole()); // или Mock.Of<ILogger>
|
||||
|
||||
// Регистрируем контекст
|
||||
serviceCollection.AddScoped<DataContext>(provider => _context);
|
||||
|
||||
serviceCollection.AddScoped<IJobService, JobService>();
|
||||
serviceCollection.AddScoped<IUnitService, UnitService>();
|
||||
|
||||
// Добавляем новые сервисы
|
||||
serviceCollection.AddScoped<IUnitInUnitService, UnitInUnitService>();
|
||||
serviceCollection.AddScoped<IUnitInValueService, UnitInValueService>();
|
||||
|
||||
// Регистрируем тестируемый сервис
|
||||
serviceCollection.AddScoped<UnitFilterService>();
|
||||
|
||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
|
||||
// Получаем сервис
|
||||
_service = serviceProvider.GetRequiredService<UnitFilterService>();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (_context != null)
|
||||
{
|
||||
await _context.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRelatedUnitNamesAsync_WithChildRelationshipFilter_ShouldReturnExpectedNames()
|
||||
{
|
||||
// Arrange
|
||||
var jobId = _context.Jobs.First(j => j.Name == "TestJob_2").Id;
|
||||
var unitId = TestDataGenerator.UnitWId;
|
||||
|
||||
// Act
|
||||
var result = await _service.GetRelatedUnitNamesAsync(jobId, unitId);
|
||||
|
||||
// Assert
|
||||
result.Should().Contain(new[] { "СХД-КМТ-CISCO-MDS9148-1-ДВС", "СХД-КМТ-CISCO-MDS9148-3-ДВС" });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetUnitsIdByJobFilterAsync_WithExistingJobId_ShouldReturnExpectedUnits()
|
||||
{
|
||||
// Arrange
|
||||
var jobId = _context.Jobs.First(j => j.Name == "TestJob_1").Id;
|
||||
var expectedUnitIds = new[] { TestDataGenerator.UnitAId, TestDataGenerator.UnitBId };
|
||||
|
||||
// Act
|
||||
var result = await _service.GetUnitsIdByJobFilterAsync(jobId, takeCount: 10);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull()
|
||||
.And.HaveCount(2)
|
||||
.And.Contain(expectedUnitIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
PARR.DAL.IntegrationTests/PARR.DAL.IntegrationTests.csproj
Normal file
33
PARR.DAL.IntegrationTests/PARR.DAL.IntegrationTests.csproj
Normal file
@@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.20" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.20" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
|
||||
<PackageReference Include="Npgsql" Version="7.0.4" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
254
PARR.DAL.IntegrationTests/TestDataGenerator.cs
Normal file
254
PARR.DAL.IntegrationTests/TestDataGenerator.cs
Normal file
@@ -0,0 +1,254 @@
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Models.Job;
|
||||
using PARR.DAL.Models.Unit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace PARR.DAL.IntegrationTests
|
||||
{
|
||||
public static class TestDataGenerator
|
||||
{
|
||||
// --- Unit IDs ---
|
||||
public static readonly Guid UnitAId = Guid.Parse("11111111-1111-1111-1111-111111111111");
|
||||
public static readonly Guid UnitBId = Guid.Parse("00000000-0000-1111-1111-111111111111");
|
||||
public static readonly Guid UnitCId = Guid.Parse("22222222-2222-2222-2222-222222222222");
|
||||
public static readonly Guid UnitDId = Guid.Parse("33333333-3333-3333-3333-333333333333");
|
||||
public static readonly Guid UnitWId = Guid.Parse("44444444-4444-4444-4444-444444444444");
|
||||
public static readonly Guid UnitXId = Guid.Parse("55555555-5555-5555-5555-555555555555");
|
||||
public static readonly Guid UnitYId = Guid.Parse("66666666-6666-6666-6666-666666666666");
|
||||
public static readonly Guid UnitZId = Guid.Parse("77777777-7777-7777-7777-777777777777");
|
||||
|
||||
// --- Field IDs (для обхода составного ключа UnitFilterId+FieldId) ---
|
||||
public static readonly Guid FieldId1 = Guid.Parse("08b42860-09a7-4a1f-aa4f-0290e86df883");//ТИП_ЭК
|
||||
public static readonly Guid FieldId2 = Guid.Parse("5b2d3f5b-a9f3-4a6b-9142-70ef4357ac37"); //неуникальный
|
||||
public static readonly Guid FieldId3 = Guid.Parse("5f3c7b8b-583d-411b-b9bd-1e045ddc5a8e");//ПОДКАТЕГОРИЯ_ЭК
|
||||
public static readonly Guid FieldId4 = Guid.Parse("3e9ac5aa-531b-4ffc-8f93-694d00aa92a0");//активный
|
||||
|
||||
// --- Field Value IDs ---
|
||||
public static readonly Guid FieldValueId1 = Guid.Parse("fe18edcd-ab6a-4f3e-b7bc-e54aad9ecc0d"); // система хранения данных (схд) field3
|
||||
public static readonly Guid FieldValueId2 = Guid.Parse("f385b22c-42c3-4db9-bd41-b829603cc2d9"); // 1 field2+field4
|
||||
public static readonly Guid FieldValueId3 = Guid.Parse("2badc139-69cb-4d52-830a-1b3c6a9faf9d"); // 0 field2+field4
|
||||
public static readonly Guid FieldValueId4 = Guid.Parse("fde68ef7-a07e-4c10-872c-cd98c3d1c30b"); // коммутатор
|
||||
public static readonly Guid FieldValueId5 = Guid.Parse("9f169862-3f78-49f2-b967-13b5b2f56231"); // серверное оборудование
|
||||
public static readonly Guid FieldValueId6 = Guid.Parse("075b6fad-d24f-40f4-aa0e-83a4cfc46bbf"); // стойка цвк
|
||||
public static readonly Guid FieldValueId7 = Guid.Parse("9679afd8-8004-4ce2-bd6f-9005b037ef96"); // схд
|
||||
|
||||
internal static void Seed(DataContext context)
|
||||
{
|
||||
if (context.Units.Any()) return;
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
// 1. Значения полей (остаются те же)
|
||||
var fieldValues = new[]
|
||||
{
|
||||
new UnitFieldValue { Id = FieldValueId1, Value = "система хранения данных (схд)", DateCreated = now },
|
||||
new UnitFieldValue { Id = FieldValueId2, Value = "1", DateCreated = now },
|
||||
new UnitFieldValue { Id = FieldValueId3, Value = "0", DateCreated = now },
|
||||
new UnitFieldValue { Id = FieldValueId4, Value = "коммутатор", DateCreated = now },
|
||||
};
|
||||
context.UnitFieldValues.AddRange(fieldValues);
|
||||
|
||||
// 2. Unit'ы
|
||||
var units = new[]
|
||||
{
|
||||
new Unit { Id = UnitAId, Name = "СХД-КМТ-DVGD-MDS9148-1-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitBId, Name = "СХД-КМТ-DVGD-MDS9148-2-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitCId, Name = "СРВ-СТОЙКА-СТОЙКА-ХИВЦ-4.0-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitDId, Name = "СХД-VSP-E590-01-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitWId, Name = "СХД-КМТ-SAN1_FAB_1-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitXId, Name = "СХД-КМТ-CISCO-MDS9148-1-ДВС", DateCreated = now },
|
||||
new Unit { Id = UnitYId, Name = "СХД-КМТ-CISCO-MDS9148-3-ДВС", DateCreated = now },
|
||||
};
|
||||
context.Units.AddRange(units);
|
||||
|
||||
// 3. UnitInUnit (связи)
|
||||
var unitInUnits = new[]
|
||||
{
|
||||
new UnitInUnit { ParentUnitId = UnitAId, ChildUnitId = UnitCId, DateCreated = now },
|
||||
new UnitInUnit { ParentUnitId = UnitBId, ChildUnitId = UnitCId, DateCreated = now },
|
||||
new UnitInUnit { ParentUnitId = UnitDId, ChildUnitId = UnitAId, DateCreated = now },
|
||||
new UnitInUnit { ParentUnitId = UnitDId, ChildUnitId = UnitBId, DateCreated = now },
|
||||
new UnitInUnit { ParentUnitId = UnitWId, ChildUnitId = UnitXId, DateCreated = now },
|
||||
new UnitInUnit { ParentUnitId = UnitWId, ChildUnitId = UnitYId, DateCreated = now }
|
||||
};
|
||||
context.UnitInUnits.AddRange(unitInUnits);
|
||||
|
||||
// 4. UnitInValue
|
||||
var unitInValues = new List<UnitInValue>
|
||||
{
|
||||
// Основные (FieldId)
|
||||
new UnitInValue { UnitId = UnitAId, FieldId = FieldId1, ValueId = FieldValueId4, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitAId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitAId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitAId, FieldId = FieldId2, ValueId = FieldValueId3, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitBId, FieldId = FieldId2, ValueId = FieldValueId3, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitBId, FieldId = FieldId1, ValueId = FieldValueId4, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitBId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitBId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitCId, FieldId = FieldId3, ValueId = FieldValueId5, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitCId, FieldId = FieldId1, ValueId = FieldValueId6, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitCId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitDId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitDId, FieldId = FieldId1, ValueId = FieldValueId7, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitDId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitWId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitWId, FieldId = FieldId1, ValueId = FieldValueId4, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitWId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitWId, FieldId = FieldId2, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitXId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitXId, FieldId = FieldId1, ValueId = FieldValueId4, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitXId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitYId, FieldId = FieldId3, ValueId = FieldValueId1, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitYId, FieldId = FieldId1, ValueId = FieldValueId4, DateCreated = now },
|
||||
new UnitInValue { UnitId = UnitYId, FieldId = FieldId4, ValueId = FieldValueId2, DateCreated = now },
|
||||
|
||||
};
|
||||
context.UnitInValues.AddRange(unitInValues);
|
||||
|
||||
// 5. JobGroupType и Group
|
||||
var groupType = context.JobGroupTypes
|
||||
.FirstOrDefault(t => t.Code == JobGroupTypesEnum.Simple)
|
||||
?? throw new InvalidOperationException("JobGroupType 'Simple' не найден в контексте.");
|
||||
|
||||
var group = new JobGroup
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
GroupTypeId = groupType.Id,
|
||||
GroupType = groupType,
|
||||
GroupName = "Группа",
|
||||
ShortDescription = "%ТНК-КРАТКО% %ЭК%",
|
||||
FullDescription = "Подробно:\n%СВЯЗИ%",
|
||||
Solution = "Решение",
|
||||
TemplateDuration = "540 00:00:00",
|
||||
ReferenceDate = now,
|
||||
IsAutoDistributionEnabled = false,
|
||||
IsAgent = false,
|
||||
DateCreated = now
|
||||
};
|
||||
context.JobGroups.Add(group);
|
||||
|
||||
// 6. Jobs
|
||||
var jobs = new List<Job>();
|
||||
|
||||
// --- TestJob ---
|
||||
var job1 = new Job
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "TestJob_1",
|
||||
WorkName = "Работа",
|
||||
TemplateNameMask = "%ЭК%",
|
||||
WorkGroupMask = "%ГРУППА_РАБОТ%",
|
||||
GroupId = group.Id,
|
||||
TnkId = Guid.NewGuid(),
|
||||
Group = group,
|
||||
DateCreated = now
|
||||
};
|
||||
var unitFilter1Id = Guid.NewGuid();
|
||||
job1.UnitFilters = new List<JobUnitFilter>
|
||||
{
|
||||
new JobUnitFilter
|
||||
{
|
||||
Id = unitFilter1Id,
|
||||
UnitFilter = "СХД-%",
|
||||
JobId = job1.Id,
|
||||
Job = job1,
|
||||
DateCreated = now,
|
||||
FieldFilters = new List<JobFieldFilter>
|
||||
{
|
||||
new JobFieldFilter
|
||||
{
|
||||
UnitFilterId = unitFilter1Id,
|
||||
FieldId = FieldId1,
|
||||
ValueMask="коммутатор"
|
||||
},
|
||||
new JobFieldFilter
|
||||
{
|
||||
UnitFilterId = unitFilter1Id,
|
||||
FieldId = FieldId4,
|
||||
ValueMask="1"
|
||||
},
|
||||
new JobFieldFilter
|
||||
{
|
||||
UnitFilterId = unitFilter1Id,
|
||||
FieldId = FieldId2,
|
||||
ValueMask="0"
|
||||
},
|
||||
},
|
||||
RelationshipFilters = new List<JobRelationshipFilter>
|
||||
{
|
||||
new JobRelationshipFilter
|
||||
{
|
||||
IsParent = true,
|
||||
IsInverse = true,
|
||||
IsFullMatch = true,
|
||||
FieldId = FieldId1,
|
||||
ValueMask = "коммутатор",
|
||||
UnitFilterId = unitFilter1Id
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
jobs.Add(job1);
|
||||
|
||||
// --- TestJob_2 (с IsParent = false для дочерних связей) ---
|
||||
var job2 = new Job
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "TestJob_2",
|
||||
WorkName = "Работа2",
|
||||
TemplateNameMask = "%ЭК%",
|
||||
WorkGroupMask = "%ГРУППА_РАБОТ%",
|
||||
GroupId = group.Id,
|
||||
TnkId = Guid.NewGuid(),
|
||||
Group = group,
|
||||
DateCreated = now
|
||||
};
|
||||
var unitFilter2Id = Guid.NewGuid();
|
||||
job2.UnitFilters = new List<JobUnitFilter>
|
||||
{
|
||||
new JobUnitFilter
|
||||
{
|
||||
Id = unitFilter2Id,
|
||||
UnitFilter = "СХД-%",
|
||||
JobId = job2.Id,
|
||||
Job = job2,
|
||||
DateCreated = now,
|
||||
FieldFilters = new List<JobFieldFilter>
|
||||
{
|
||||
new JobFieldFilter
|
||||
{
|
||||
UnitFilterId = unitFilter2Id,
|
||||
FieldId = FieldId1,
|
||||
ValueMask="коммутатор"
|
||||
},
|
||||
new JobFieldFilter
|
||||
{
|
||||
UnitFilterId = unitFilter2Id,
|
||||
FieldId = FieldId4,
|
||||
ValueMask="1"
|
||||
},
|
||||
},
|
||||
RelationshipFilters = new List<JobRelationshipFilter>
|
||||
{
|
||||
new JobRelationshipFilter
|
||||
{
|
||||
IsParent = false, // <-- Новый фильтр: ищем дочерние юниты
|
||||
IsInverse = false, // <-- Простой фильтр
|
||||
IsFullMatch = false, // <-- Простой фильтр
|
||||
FieldId = FieldId1,
|
||||
ValueMask = "коммутатор",
|
||||
UnitFilterId = unitFilter2Id
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
jobs.Add(job2);
|
||||
|
||||
context.Jobs.AddRange(jobs);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user