package com.ruoyi.system.service.impl; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import com.ruoyi.system.domain.TTest; import com.ruoyi.system.domain.TTestDataOverride; import com.ruoyi.system.mapper.TTestMapper; import com.ruoyi.system.service.ITTestService; /** Ticket01:三类查询直接返回数据库计算的当前有效数据,删除不抹除原始事实。 */ @ExtendWith(MockitoExtension.class) class TestDataQueryServiceEffectiveDataTest { @Mock private TTestMapper mapper; @Mock private ITTestService tests; @Test void appliesRealtimeEditAndFiltersDeletedRow() { when(tests.selectTestById(7L)).thenReturn(new TTest()); Map edited = row("deviceId", 22L, "channelId", 8L, "sequenceId", 3L, "voltage", 3.7d); Map deleted = row("deviceId", 22L, "channelId", 8L, "sequenceId", 4L, "voltage", 3.8d); TTestDataOverride edit = override("22:8:3", "{\"voltage\":\"4.1000\"}", false); TTestDataOverride remove = override("22:8:4", null, true); when(mapper.selectPersistedRecords(7L)).thenReturn(List.of(edited, deleted)); when(mapper.selectDataOverrides(7L, "REALTIME")).thenReturn(List.of(edit, remove)); List> result = new TestDataQueryServiceImpl(mapper, tests).records(7L); assertEquals(1, result.size()); assertEquals("4.1000", result.get(0).get("voltage")); assertEquals(3L, result.get(0).get("sequenceId")); } @Test void usesStepAndCycleStableIdentities() { when(tests.selectTestById(8L)).thenReturn(new TTest()); Map step = row("deviceId", 22L, "channelId", 8L, "cycleNo", 2, "stepNo", 4, "capacity", 1d); Map cycle = row("deviceId", 22L, "channelId", 8L, "cycleNo", 2, "efficiency", 90d); when(mapper.selectPersistedSteps(8L)).thenReturn(List.of(step)); when(mapper.selectPersistedCycles(8L)).thenReturn(List.of(cycle)); when(mapper.selectDataOverrides(8L, "STEP")).thenReturn(List.of(override("step:22:8:2:4", "{\"capacity\":\"2.0000\"}", false))); when(mapper.selectDataOverrides(8L, "CYCLE")).thenReturn(List.of(override("cycle:22:8:2", "{\"efficiency\":\"95.00\"}", false))); TestDataQueryServiceImpl service = new TestDataQueryServiceImpl(mapper, tests); assertEquals("2.0000", service.steps(8L).get(0).get("capacity")); assertEquals("95.00", service.cycles(8L).get(0).get("efficiency")); assertTrue(service.cycles(8L).get(0).containsKey("cycleNo")); } private static TTestDataOverride override(String identity, String values, boolean deleted) { TTestDataOverride result = new TTestDataOverride(); result.setStableIdentity(identity); result.setFieldValues(values); result.setDeleted(deleted); return result; } private static Map row(Object... values) { Map result = new HashMap<>(); for (int index = 0; index < values.length; index += 2) result.put(String.valueOf(values[index]), values[index + 1]); return result; } }