yeg
2026-09-02 4220194f54535388daad46136f8da0452441b806
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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<String, Object> edited = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "sequenceId", 3L,
                "voltage", 3.7d);
        Map<String, Object> deleted = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "sequenceId", 4L,
                "voltage", 3.8d);
        TTestDataOverride edit = override("1:22:22:0:8:3", "{\"voltage\":\"4.1000\"}", false);
        TTestDataOverride remove = override("1:22:22:0:8:4", null, true);
        when(mapper.selectPersistedRecords(7L)).thenReturn(List.of(edited, deleted));
        when(mapper.selectDataOverrides(7L, "REALTIME")).thenReturn(List.of(edit, remove));
 
        List<Map<String, Object>> 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<String, Object> step = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "cycleNo", 2, "stepNo", 4,
                "capacity", 1d);
        Map<String, Object> cycle = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "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:1:22:22:0:8:2:4",
                "{\"capacity\":\"2.0000\"}", false)));
        when(mapper.selectDataOverrides(8L, "CYCLE")).thenReturn(List.of(override("cycle:1:22:22:0: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<String, Object> row(Object... values)
    {
        Map<String, Object> result = new HashMap<>();
        for (int index = 0; index < values.length; index += 2) result.put(String.valueOf(values[index]), values[index + 1]);
        return result;
    }
}