Skip to content

Commit

Permalink
Added some fault tests (#244)
Browse files Browse the repository at this point in the history
* Added some fault tests

* Remove warnings

* Fix debug experience
  • Loading branch information
helto4real committed Dec 11, 2020
1 parent cfb2f80 commit 8a5469c
Show file tree
Hide file tree
Showing 3 changed files with 563 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ ENV TZ=Europe/Stockholm

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=noninteractive
ENV TEST=AK
ENV TEST=AK

# We do not load local assemblies
ENV HASS_DISABLE_LOCAL_ASM=true
136 changes: 135 additions & 1 deletion tests/NetDaemon.Daemon.Tests/NetDaemonApp/FaultyAppsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using JoySoftware.HomeAssistant.Client;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -16,7 +19,7 @@ public FaultyAppTests() : base()
_app = new DaemonAppTestApp();
_app.Id = "id";
DefaultDaemonHost.InternalRunningAppInstances[_app.Id] = App;
_app.StartUpAsync(DefaultDaemonHost);
_app.StartUpAsync(DefaultDaemonHost).Wait();
}

public NetDaemon.Common.NetDaemonApp App => _app;
Expand All @@ -42,5 +45,136 @@ public async Task ARunTimeErrorShouldLogError()

LoggerMock.AssertLogged(LogLevel.Error, Times.Once());
}

[Fact]
public async Task ARunTimeErrorShouldNotBreakOtherApps()
{
// ARRANGE
bool eventRun = false;
App
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.Call((entity, from, to) =>
{
// Do conversion error
int x = int.Parse("ss");
return Task.CompletedTask;
}).Execute();

App
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.Call((entity, from, to) =>
{
// Do conversion error
eventRun = true;
return Task.CompletedTask;
}).Execute();

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

await RunDefauldDaemonUntilCanceled();

LoggerMock.AssertLogged(LogLevel.Error, Times.Once());
Assert.True(eventRun);

}

[Fact]
public async Task MissingAttributeShouldNotBreakOtherApps()
{
// ARRANGE
bool eventRun = false;
App
.Entities(e => e.Attribute!.does_not_exist == "yay")
.WhenStateChange()
.Call((entity, from, to) =>
{
// Do conversion error
int x = int.Parse("ss");
return Task.CompletedTask;
}).Execute();

App
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.Call((entity, from, to) =>
{
// Do conversion error
eventRun = true;
return Task.CompletedTask;
}).Execute();

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

await RunDefauldDaemonUntilCanceled();

// LoggerMock.AssertLogged(LogLevel.Error, Times.Once());
Assert.True(eventRun);

}

[Fact]
public async Task MissingEntityShouldNotBreakOtherApps()
{
// ARRANGE
bool eventRun = false;
App
.Entity("binary_sensor.pir")
.WhenStateChange()
.Call((entity, from, to) =>
{
// Do conversion error
App.Entity("does_not_exist").TurnOn();
return Task.CompletedTask;
}).Execute();

App
.Entity("binary_sensor.pir")
.WhenStateChange("on")
.Call((entity, from, to) =>
{
// Do conversion error
eventRun = true;
return Task.CompletedTask;
}).Execute();

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

await RunDefauldDaemonUntilCanceled();

Assert.True(eventRun);

}

private void AddDefaultEvent()
{
DefaultHassClientMock.FakeEvents.Enqueue(new HassEvent
{
EventType = "state_changed",
Data = new HassStateChangedEventData
{
EntityId = "binary_sensor.pir",
NewState = new HassState
{
State = "on",
Attributes = new Dictionary<string, object>
{
["device_class"] = "motion"
},
LastChanged = DateTime.Now,
LastUpdated = DateTime.Now
},
OldState = new HassState
{
State = "off",
Attributes = new Dictionary<string, object>
{
["device_class"] = "motion"
}
}
}
});
}
}
}
Loading

0 comments on commit 8a5469c

Please sign in to comment.