SQL查询操作顺序

时间:2014-08-01 21:15:39

标签: c# sql unit-testing

星期五。我的大脑是油炸的。这很简单,我很惭愧地问这个:

我只是想查询我的事件表(它具有非空的开始时间和可以为空的结束时间)。但是,在我的单元测试中,我不断获得2条记录(2:00和4:00)记录,而不仅仅是我期望的2点)

SELECT 
    EventId, 
    TaskId, 
    MachineId, 
    LoginId, 
    EventStartTimeUtc, 
    EventEndTimeUtc, 
    OpCode, 
    UnitId, 
    PositionId, 
    WebId, 
    Comment, 
    MakereadyCount, 
    GrossCount, 
    NetCount, 
    PerpetualGross, 
    PerpetualNet, 
    PerpetualMakeready, 
    TaskState, 
    EventTypeId, 
    IsAutoEvent, 
    IsTransferred, 
    LastUpdatedTimeUtc
FROM Event
WHERE MachineId = #MachineId# 
    AND EventStartTimeUtc >= #StartTimeUtc# 
    AND (EventEndTimeUtc IS NULL 
        OR ((EventEndTimeUtc IS NOT NULL) AND EventEndTimeUtc <![CDATA[<=]]> #EndTimeUtc#))
[Test]
public void ShouldSelectEventsInRange()
{
    //Arrange
    TaskDto testTask = _testRepository.CreateTask(new TaskDto { TaskId = 1234567 }, true);

    var machineId = ((ArtemisRepository)_testRepository).CreateMachine(123, "MR40SIM", "0V7", 200, 100, 555555); //Requires a 555555 down-task to exist in database

    EventRecordDto result = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 1, 50, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});
    EventRecordDto result2 = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 2, 0, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});
    EventRecordDto result3 = _testRepository.CreateEvent(new EventRecordDto {TaskId = testTask.TaskId, MachineId = machineId, EventStartTimeUtc = new DateTime(2014, 4, 15, 4, 0, 0), OpCode = "100", MakereadyCount = 1752, GrossCount = 5660, NetCount = 2512, Comment = "Test Event", IsAutoEvent = false, IsTransferred = false});

    //Act
    var results = _testRepository.SelectEventsInRange(machineId, new DateTime(2014, 4, 15, 2, 0, 0), new DateTime(2014, 4, 15, 3, 59, 59));

    //Assert
    Assert.IsTrue(results.Count == 1, "{0} records came, instead of the 1 record expeted!", new object[] { results.Count });
    Assert.IsTrue(results.Any(r => r.EventId == result2.EventId), "Expected Event (Id: {0}), Actual Event (ID: {1})", new object[] { result2.EventId, results[0].EventId});
}

1 个答案:

答案 0 :(得分:1)

查询要求EventStartTimeUtc大于或等于2:00的记录,这些记录都是,以及EventEndTimeUtc为空的记录,它们都是。

你的意思是EventStartTimeUtc你在WHERE子句中有EventEndTimeUtc吗?