Skip to content

Commit

Permalink
Application mocking for default Rx API (#269)
Browse files Browse the repository at this point in the history
* Added RxAppMock for easy mocking

* Fix test

* Adding some convenient functions

* Better test coverage
  • Loading branch information
helto4real authored Dec 30, 2020
1 parent 4e5085e commit 050ff3d
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/App/NetDaemon.App/Common/INetDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void ListenEvent(Func<FluentEventProperty, bool> funcSelector,
/// </summary>
[SuppressMessage("", "CA1716")]
public interface INetDaemonAppBase :
INetDaemonInitialableApp, IDoLogging, IAsyncDisposable, IEquatable<INetDaemonAppBase>
INetDaemonInitialableApp, INetDaemonAppLogging, IAsyncDisposable, IEquatable<INetDaemonAppBase>
{
/// <summary>
/// The dependencies that needs to be initialized before this app
Expand Down Expand Up @@ -432,7 +432,7 @@ void ListenServiceCall(string domain, string service,
/// <summary>
/// Interface for logging capabilities in NetDaemon Apps
/// </summary>
public interface IDoLogging
public interface INetDaemonAppLogging
{
/// <summary>
/// Logs an informational message
Expand Down
10 changes: 5 additions & 5 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace NetDaemon.Common.Reactive
/// <summary>
/// Base class for using the Reactive paradigm for apps
/// </summary>
public abstract class NetDaemonRxApp : NetDaemonAppBase, INetDaemonReactive
public abstract class NetDaemonRxApp : NetDaemonAppBase, INetDaemonRxApp
{
private readonly CancellationTokenSource _cancelTimers;
private EventObservable? _eventObservables;
Expand Down Expand Up @@ -97,7 +97,7 @@ public async override ValueTask DisposeAsync()
}

/// <inheritdoc/>
public RxEntity Entities(Func<IEntityProperties, bool> func)
public IRxEntityBase Entities(Func<IEntityProperties, bool> func)
{
_ = Daemon ?? throw new NetDaemonNullReferenceException($"{nameof(Daemon)} cant be null!");

Expand All @@ -115,17 +115,17 @@ public RxEntity Entities(Func<IEntityProperties, bool> func)
}

/// <inheritdoc/>
public RxEntity Entities(params string[] entityIds) => Entities((IEnumerable<string>)entityIds);
public IRxEntityBase Entities(params string[] entityIds) => Entities((IEnumerable<string>)entityIds);

/// <inheritdoc/>
public RxEntity Entities(IEnumerable<string> entityIds)
public IRxEntityBase Entities(IEnumerable<string> entityIds)
{
_ = Daemon ?? throw new NetDaemonNullReferenceException($"{nameof(Daemon)} cant be null!");
return new RxEntity(this, entityIds);
}

/// <inheritdoc/>
public RxEntity Entity(string entityId)
public IRxEntityBase Entity(string entityId)
{
_ = Daemon ?? throw new NetDaemonNullReferenceException($"{nameof(Daemon)} cant be null!");
return new RxEntity(this, new string[] { entityId });
Expand Down
38 changes: 13 additions & 25 deletions src/App/NetDaemon.App/Common/Reactive/INetDaemonReactive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@

namespace NetDaemon.Common.Reactive
{
/// <summary>
/// Interface for objects that implements CallService
/// </summary>
public interface ICallService
{
/// <summary>
/// Calls service in Home Assistant
/// </summary>
/// <param name="domain">Domain of sevice</param>
/// <param name="service">Service name</param>
/// <param name="data">Data provided to service. Use anonomous type</param>
void CallService(string domain, string service, dynamic? data);
}

/// <summary>
/// Implements the System.Reactive pattern for NetDaemon Apps
/// </summary>
public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity, IRunScript
public interface INetDaemonRxApp : INetDaemonAppBase, IRxEntity
{
/// <summary>
/// The observable events
Expand Down Expand Up @@ -73,13 +59,15 @@ public interface INetDaemonReactive : INetDaemonAppBase, ICallService, IRxEntity
/// </summary>
/// <param name="entityId"></param>
EntityState? State(string entityId);
}

/// <summary>
/// Interface for objects that implements CallService
/// </summary>
public interface IRunScript
{
/// <summary>
/// Calls service in Home Assistant
/// </summary>
/// <param name="domain">Domain of sevice</param>
/// <param name="service">Service name</param>
/// <param name="data">Data provided to service. Use anonomous type</param>
void CallService(string domain, string service, dynamic? data);

/// <summary>
/// Calls service in Home Assistant
/// </summary>
Expand All @@ -96,25 +84,25 @@ public interface IRxEntity
/// Select entities to perform actions on
/// </summary>
/// <param name="func">Lambda expression</param>
RxEntity Entities(Func<IEntityProperties, bool> func);
IRxEntityBase Entities(Func<IEntityProperties, bool> func);

/// <summary>
/// Entities to perform actions on
/// </summary>
/// <param name="entityIds">List of entities</param>
RxEntity Entities(IEnumerable<string> entityIds);
IRxEntityBase Entities(IEnumerable<string> entityIds);

/// <summary>
/// Entities to perform actions on
/// </summary>
/// <param name="entityIds">List of entities</param>
RxEntity Entities(params string[] entityIds);
IRxEntityBase Entities(params string[] entityIds);

/// <summary>
/// Entity to perform actions on
/// </summary>
/// <param name="entityId">EntityId</param>
RxEntity Entity(string entityId);
IRxEntityBase Entity(string entityId);
}

/// <summary>
Expand Down
37 changes: 13 additions & 24 deletions src/App/NetDaemon.App/Common/Reactive/RxEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
namespace NetDaemon.Common.Reactive
{
/// <summary>
/// Interface for objects implements Toggle
/// IRxEntityBase interface represents what you can do on
/// Entity("").WhatYouCanDo(); and Entities(n=> n.Xyz).WhatYouCanDo();
/// </summary>
public interface ICanTurnOnAndOff
public interface IRxEntityBase
{
/// <summary>
/// Set entity state
/// </summary>
/// <param name="state">The state to set, primitives only</param>
/// <param name="attributes">The attributes to set. Use anonomous type</param>
void SetState(dynamic state, dynamic? attributes = null);

/// <summary>
/// Toggles state on/off on entity
/// </summary>
Expand All @@ -30,13 +38,7 @@ public interface ICanTurnOnAndOff
/// </summary>
/// <param name="attributes">The attributes to set. Use anonomous type.</param>
void TurnOn(dynamic? attributes = null);
}

/// <summary>
/// Interface for objects exposing observable state changes
/// </summary>
public interface IObserve
{
/// <summary>
/// Observable, All state changes inkluding attributes
/// </summary>
Expand All @@ -48,28 +50,15 @@ public interface IObserve
IObservable<(EntityState Old, EntityState New)> StateChanges { get; }
}

/// <summary>
/// Interface for objects implements SetState
/// </summary>
public interface ISetState
{
/// <summary>
/// Set entity state
/// </summary>
/// <param name="state">The state to set, primitives only</param>
/// <param name="attributes">The attributes to set. Use anonomous type</param>
void SetState(dynamic state, dynamic? attributes = null);
}

/// <summary>
/// Implements the entity of Rx API
/// </summary>
public class RxEntity : ICanTurnOnAndOff, ISetState, IObserve
public class RxEntity : IRxEntityBase
{
/// <summary>
/// The protected daemon app instance
/// </summary>
protected INetDaemonReactive DaemonRxApp { get; }
protected INetDaemonRxApp DaemonRxApp { get; }
/// <summary>
/// Entity ids being handled by the RxEntity
/// </summary>
Expand All @@ -80,7 +69,7 @@ public class RxEntity : ICanTurnOnAndOff, ISetState, IObserve
/// </summary>
/// <param name="daemon">The NetDaemon host object</param>
/// <param name="entityIds">Unique entity id:s</param>
public RxEntity(INetDaemonReactive daemon, IEnumerable<string> entityIds)
public RxEntity(INetDaemonRxApp daemon, IEnumerable<string> entityIds)
{
DaemonRxApp = daemon;
EntityIds = entityIds;
Expand Down
5 changes: 5 additions & 0 deletions src/Fakes/NetDaemon.Fakes/DaemonHostTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public DaemonHostTestBase()
};
}

/// <summary>
/// Default RxAppMock
/// </summary>
public RxAppMock DefaultRxAppMock { get; } = new();

/// <summary>
/// Returns default DaemonHost mock
/// </summary>
Expand Down
Loading

0 comments on commit 050ff3d

Please sign in to comment.