feat(dal): ShortcodesService добавлена обработка динамичесих составляющих "%СВЯЗИ%", "%ТНК-КРАТКО%"
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.DomainServices.Implementations;
|
||||
using PARR.DAL.Services.Interfaces.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
using Xunit;
|
||||
|
||||
namespace PARR.DAL.Tests
|
||||
{
|
||||
public class UnitFilterServiceTests : IDisposable
|
||||
{
|
||||
private readonly DbContextOptions<DataContext> _options;
|
||||
private readonly DataContext _context;
|
||||
private readonly UnitFilterService _service;
|
||||
|
||||
public UnitFilterServiceTests()
|
||||
{
|
||||
_options = new DbContextOptionsBuilder<DataContext>()
|
||||
.UseInMemoryDatabase($"TestDb_{Guid.NewGuid()}")
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
|
||||
_context = new DataContext(_options);
|
||||
_context.Database.EnsureCreated();
|
||||
TestDataGenerator.Seed(_context);
|
||||
|
||||
var logger = Mock.Of<ILogger<UnitFilterService>>();
|
||||
|
||||
// Используем реальные сервисы, работающие с _context
|
||||
var jobServiceMock = new Mock<IJobService>();
|
||||
jobServiceMock.Setup(x => x.Get()).Returns(() => _context.Jobs);
|
||||
|
||||
var unitServiceMock = new Mock<IUnitService>();
|
||||
unitServiceMock.Setup(x => x.Get()).Returns(() => _context.Units);
|
||||
|
||||
_service = new UnitFilterService(logger, jobServiceMock.Object, unitServiceMock.Object);
|
||||
}
|
||||
|
||||
public void Dispose() => _context.Dispose();
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetRelatedUnitNamesAsync_WithChildRelationshipFilter_ShouldReturnExpectedNames()
|
||||
{
|
||||
// Arrange
|
||||
var jobId = _context.Jobs.First(j => j.Name == "TestJob_2").Id;
|
||||
var unitId = TestDataGenerator.UnitWId; // Родитель
|
||||
|
||||
// rf.IsParent = false
|
||||
// Ищем юниты, у которых ParentUnitId = UnitWId (т.е. это дети UnitWId)
|
||||
// и у этих юнитов (детей) есть FieldId1 = "коммутатор"
|
||||
// Это UnitXId и UnitYId -> имена: "СХД-КМТ-CISCO-MDS9148-1-ДВС", "СХД-КМТ-CISCO-MDS9148-3-ДВС"
|
||||
|
||||
// Act
|
||||
var result = await _service.GetRelatedUnitNamesAsync(jobId, unitId);
|
||||
|
||||
// Assert
|
||||
result.Should().Contain(new[] { "СХД-КМТ-CISCO-MDS9148-1-ДВС", "СХД-КМТ-CISCO-MDS9148-3-ДВС" });
|
||||
}
|
||||
//[Fact]
|
||||
//public async Task GetRelatedUnitNamesAsync_WithChildRelationshipFilter_ShouldReturnExpectedNames()
|
||||
//{
|
||||
// // Arrange
|
||||
// var jobId = _context.Jobs.First(j => j.Name == "TestJob_2").Id;
|
||||
// var unitId = TestDataGenerator.UnitWId;
|
||||
|
||||
// // Проверим связи вручную
|
||||
// var childLinks = await _context.UnitInUnits
|
||||
// .Where(u => u.ParentUnitId == unitId)
|
||||
// .ToListAsync();
|
||||
|
||||
// var childIds = childLinks.Select(l => l.ChildUnitId).ToList();
|
||||
// var childUnits = await _context.Units
|
||||
// .Where(u => childIds.Contains(u.Id))
|
||||
// .ToListAsync();
|
||||
|
||||
// Console.WriteLine($"Child units of {unitId}:");
|
||||
// foreach (var u in childUnits)
|
||||
// {
|
||||
// Console.WriteLine($" - {u.Id} ({u.Name})");
|
||||
|
||||
// var values = await _context.UnitInValues
|
||||
// .Where(v => v.UnitId == u.Id && v.FieldId == TestDataGenerator.FieldId1)
|
||||
// .ToListAsync();
|
||||
|
||||
// foreach (var v in values)
|
||||
// {
|
||||
// var val = await _context.UnitFieldValues.FindAsync(v.ValueId);
|
||||
// Console.WriteLine($" FieldId1 = {val?.Value}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Act
|
||||
// var result = await _service.GetRelatedUnitNamesAsync(jobId, unitId);
|
||||
|
||||
// Console.WriteLine($"Result: [{string.Join(", ", result)}]");
|
||||
|
||||
// // Assert
|
||||
// result.Should().Contain(new[] { "СХД-КМТ-CISCO-MDS9148-1-ДВС", "СХД-КМТ-CISCO-MDS9148-3-ДВС" });
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.20" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
<PackageReference Include="xunit" Version="2.4.2" />
|
||||
|
||||
255
PARR.DAL.Tests/TestDataGenerator.cs
Normal file
255
PARR.DAL.Tests/TestDataGenerator.cs
Normal file
@@ -0,0 +1,255 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
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.Linq;
|
||||
|
||||
namespace PARR.DAL.Tests
|
||||
{
|
||||
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