Skip to content

Commit

Permalink
Fix order of events
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 13, 2020
1 parent 014ddd8 commit 92d86b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
7 changes: 4 additions & 3 deletions tests/NetDaemon.Daemon.Tests/Daemon/NetDaemonHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public async Task SubscribeChangedStateForEntityWillMakeCorrectCallback()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", fromState: "off", toState: "on");

string? reportedState = "";

Expand All @@ -270,6 +269,7 @@ public async Task SubscribeChangedStateForEntityWillMakeCorrectCallback()
return Task.CompletedTask;
});

AddChangedEvent("binary_sensor.pir", fromState: "off", toState: "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

// ASSERT
Expand All @@ -281,8 +281,6 @@ public async Task SubscribeChangedStateForAllChangesWillMakeCorrectCallbacks()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", fromState: "off", toState: "on");
DefaultHassClientMock.AddChangedEvent("light.mylight", fromState: "on", toState: "off");

int nrOfTimesCalled = 0;

Expand All @@ -294,6 +292,9 @@ public async Task SubscribeChangedStateForAllChangesWillMakeCorrectCallbacks()
return Task.CompletedTask;
});

AddChangedEvent("binary_sensor.pir", fromState: "off", toState: "on");
AddChangedEvent("light.mylight", fromState: "on", toState: "off");

await RunFakeDaemonUntilTimeout();

// ASSERT
Expand Down
53 changes: 28 additions & 25 deletions tests/NetDaemon.Daemon.Tests/Fluent/FluentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public async Task EntityOnStateChangedLamdaTurnOnLightCallsCorrectServiceCall()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -67,6 +66,7 @@ public async Task EntityOnStateChangedLamdaTurnOnLightCallsCorrectServiceCall()
.TurnOn()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -78,7 +78,6 @@ public async Task EntityOnStateChangedLamdaWithMultipleEntitiesCallsCorrectServi
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

var MotionEnabled = true;

Expand All @@ -89,6 +88,7 @@ public async Task EntityOnStateChangedLamdaWithMultipleEntitiesCallsCorrectServi
.Toggle()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("toggle", Times.Once());
Expand All @@ -100,7 +100,6 @@ public async Task OneEntityWithSeveralShouldCallsCorrectServiceCall()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -109,6 +108,7 @@ public async Task OneEntityWithSeveralShouldCallsCorrectServiceCall()
.Toggle()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("toggle", Times.Once());
Expand All @@ -121,17 +121,18 @@ public async Task EntityOnStateChangedMultipleTimesCallsCorrectServiceCall()
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);

AddChangedEvent("binary_sensor.pir", "off", "on");
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.UseEntity("light.correct_entity")
.TurnOn()
.Execute();
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.UseEntity("light.correct_entity")
.TurnOn()
.Execute();


AddChangedEvent("binary_sensor.pir", "off", "on");
AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Exactly(2));
VerifyCallServiceTuple("light", "turn_on",
("entity_id", "light.correct_entity"));
Expand All @@ -142,7 +143,6 @@ public async Task EntityOnStateChangedTurnOnLightCallsCorrectServiceCall()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -151,6 +151,7 @@ public async Task EntityOnStateChangedTurnOnLightCallsCorrectServiceCall()
.TurnOn()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -162,7 +163,6 @@ public async Task EntityOnStateChangedEntitiesTurnOnLightCallsCorrectServiceCall
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -171,6 +171,7 @@ public async Task EntityOnStateChangedEntitiesTurnOnLightCallsCorrectServiceCall
.TurnOn()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -183,7 +184,6 @@ public async Task EntityOnStateChangedEntitiesLambdaTurnOnLightCallsCorrectServi
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);

AddChangedEvent("binary_sensor.pir", "off", "on");


// Fake the
Expand All @@ -199,6 +199,7 @@ public async Task EntityOnStateChangedEntitiesLambdaTurnOnLightCallsCorrectServi
.TurnOn()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -211,7 +212,6 @@ public async Task EntityOnStateChangedTurnOnLightCallsCorrectServiceCallButNoTur
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);

AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -227,6 +227,7 @@ public async Task EntityOnStateChangedTurnOnLightCallsCorrectServiceCallButNoTur
.TurnOff()
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -240,7 +241,6 @@ public async Task EntityOnStateChangedTurnOnLightWithAttributesCallsCorrectServi
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

DefaultDaemonApp
.Entity("binary_sensor.pir")
Expand All @@ -250,6 +250,8 @@ public async Task EntityOnStateChangedTurnOnLightWithAttributesCallsCorrectServi
.WithAttribute("transition", 0)
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");

await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallServiceTimes("turn_on", Times.Once());
Expand All @@ -263,7 +265,6 @@ public async Task EntityOnStateDefaultTriggerOnAnyStateChange()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

var triggered = false;

Expand All @@ -278,6 +279,8 @@ public async Task EntityOnStateDefaultTriggerOnAnyStateChange()
})
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");

await RunFakeDaemonUntilTimeout().ConfigureAwait(false);
// ASSERT
Assert.True(triggered);
Expand All @@ -288,7 +291,6 @@ public async Task EntityOnStateNotTriggerOnSameState()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "off");

var triggered = false;

Expand All @@ -303,6 +305,8 @@ public async Task EntityOnStateNotTriggerOnSameState()
})
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "off");

await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

// ASSERT
Expand All @@ -314,7 +318,6 @@ public async Task EntityOnStateIncludeAttributesTriggerOnSameState()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "off");

var triggered = false;

Expand All @@ -329,6 +332,7 @@ public async Task EntityOnStateIncludeAttributesTriggerOnSameState()
})
.Execute();

AddChangedEvent("binary_sensor.pir", "off", "off");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);
// ASSERT
Assert.True(triggered);
Expand Down Expand Up @@ -548,7 +552,6 @@ public async Task EntityOnStateCallFunction()
{
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

string? actualToState = "";
string? actualFromState = "";
Expand All @@ -563,6 +566,7 @@ public async Task EntityOnStateCallFunction()
return Task.CompletedTask;
}).Execute();

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

Assert.Equal("on", actualToState);
Expand All @@ -573,9 +577,8 @@ public async Task EntityOnStateCallFunction()
[Fact]
public async Task EntityOnStateTriggerScript()
{
await InitializeFakeDaemon().ConfigureAwait(false);
// ARRANGE
AddChangedEvent("binary_sensor.pir", "on", "off");
await InitializeFakeDaemon().ConfigureAwait(false);

// ACT
DefaultDaemonApp
Expand All @@ -584,6 +587,7 @@ public async Task EntityOnStateTriggerScript()
.RunScript("thescript")
.Execute();

AddChangedEvent("binary_sensor.pir", "on", "off");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

VerifyCallService("script", "thescript", false);
Expand Down Expand Up @@ -787,14 +791,13 @@ await DefaultDaemonApp
public async Task EntityDelayUntilStateChangeShouldReturnTrue()
{
// ARRANGE

await InitializeFakeDaemon().ConfigureAwait(false);
AddChangedEvent("binary_sensor.pir", "off", "on");

var delayResult = DefaultDaemonApp
.Entity("binary_sensor.pir")
.DelayUntilStateChange(to: "on");

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

Assert.True(delayResult.Task.IsCompletedSuccessfully);
Expand All @@ -807,12 +810,12 @@ public async Task EntityDelayUntilStateChangeLamdaShouldReturnTrue()
// ARRANGE
await InitializeFakeDaemon().ConfigureAwait(false);

AddChangedEvent("binary_sensor.pir", "off", "on");

var delayResult = DefaultDaemonApp
.Entity("binary_sensor.pir")
.DelayUntilStateChange((to, _) => to?.State == "on");

AddChangedEvent("binary_sensor.pir", "off", "on");
await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

Assert.True(delayResult.Task.IsCompletedSuccessfully);
Expand Down

0 comments on commit 92d86b5

Please sign in to comment.