-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor refactoring + unit tests added
- Loading branch information
1 parent
42e8ecb
commit d4ae2a1
Showing
32 changed files
with
966 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Reactive; | ||
using System.Reactive.Linq; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using System.Windows.Data; | ||
|
||
namespace Genius.PriceChecker.UI.Forms | ||
{ | ||
internal class Helpers | ||
{ | ||
public static Type GetListItemType(object value) | ||
{ | ||
if (value is ListCollectionView listCollectionView) | ||
value = listCollectionView.SourceCollection; | ||
|
||
if (value is ITypedObservableList typedObservableList) | ||
return typedObservableList.ItemType; | ||
|
||
return value.GetType().GetGenericArguments().Single(); | ||
} | ||
|
||
public static string MakeCaptionFromPropertyName(string propertyName) | ||
{ | ||
return Regex.Replace(propertyName, @"(?<=[^$])([A-Z])", " $1"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel; | ||
|
||
namespace Genius.PriceChecker.UI.Forms | ||
{ | ||
public interface ITypedObservableList | ||
{ | ||
Type ItemType { get; } | ||
} | ||
|
||
public class TypedObservableList<TContract, TType> : ObservableCollection<TContract>, ITypedObservableList, ITypedList | ||
{ | ||
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) | ||
{ | ||
return TypeDescriptor.GetProperties(typeof(TType)); | ||
} | ||
|
||
public string GetListName(PropertyDescriptor[] listAccessors) | ||
{ | ||
return null; | ||
} | ||
|
||
public Type ItemType => typeof(TType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
PriceChecker.UI.Tests/Helpers/TrackerScanContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
using System; | ||
using System.Reactive.Subjects; | ||
using AutoFixture; | ||
using Genius.PriceChecker.Core.Messages; | ||
using Genius.PriceChecker.Core.Models; | ||
using Genius.PriceChecker.UI.Helpers; | ||
using Xunit; | ||
|
||
namespace Genius.PriceChecker.UI.Tests.Helpers | ||
{ | ||
public class TrackerScanContextTests : TestBase | ||
{ | ||
private readonly Fixture _fixture = new(); | ||
private readonly TrackerScanContext _sut; | ||
|
||
// Session values: | ||
private readonly Subject<ProductAutoScanStartedEvent> _productAutoScanStartedEventSubject; | ||
private TrackerScanStatus? _lastStatus = null; | ||
private double? _lastProgress = null; | ||
|
||
public TrackerScanContextTests() | ||
{ | ||
_productAutoScanStartedEventSubject = CreateEventSubject<ProductAutoScanStartedEvent>(); | ||
|
||
_sut = new TrackerScanContext(EventBusMock.Object); | ||
|
||
_sut.ScanProgress.Subscribe(x => { | ||
_lastStatus = x.Status; | ||
_lastProgress = x.Progress; | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void NotifyStarted__Resets_state_and_calculates_initial_progress() | ||
{ | ||
// Arrange | ||
var count = 10; | ||
|
||
// Act | ||
_sut.NotifyStarted(count); | ||
|
||
// Verify | ||
var expectedProgress = 1d / (count * 2); | ||
Assert.True(_sut.IsStarted); | ||
Assert.False(_sut.HasErrors); | ||
Assert.False(_sut.HasNewLowestPrice); | ||
Assert.Equal(0, _sut.FinishedJobs); | ||
Assert.Equal(TrackerScanStatus.InProgress, _lastStatus); | ||
Assert.Equal(expectedProgress, _lastProgress); | ||
} | ||
|
||
[Fact] | ||
public void NotifyProgressChange__When_ScannedOk__Increases_progress() | ||
{ | ||
// Arrange | ||
var count = 2; | ||
_sut.NotifyStarted(count); | ||
|
||
// Act | ||
_sut.NotifyProgressChange(ProductScanStatus.ScannedOk); | ||
|
||
// Verify | ||
var expectedProgress = 0.5d; // 50% of 2 jobs | ||
Assert.True(_sut.IsStarted); | ||
Assert.False(_sut.HasErrors); | ||
Assert.False(_sut.HasNewLowestPrice); | ||
Assert.Equal(1, _sut.FinishedJobs); | ||
Assert.Equal(TrackerScanStatus.InProgress, _lastStatus); | ||
Assert.Equal(expectedProgress, _lastProgress); | ||
} | ||
|
||
[Fact] | ||
public void NotifyProgressChange__When_scanned_last_job__Finishes_progress() | ||
{ | ||
// Arrange | ||
_sut.NotifyStarted(2); | ||
|
||
// Act | ||
_sut.NotifyProgressChange(ProductScanStatus.ScannedOk); | ||
_sut.NotifyProgressChange(ProductScanStatus.ScannedOk); | ||
|
||
// Verify | ||
Assert.False(_sut.IsStarted); | ||
Assert.False(_sut.HasErrors); | ||
Assert.False(_sut.HasNewLowestPrice); | ||
Assert.Equal(2, _sut.FinishedJobs); | ||
Assert.Equal(TrackerScanStatus.Finished, _lastStatus); | ||
Assert.Equal(1, _lastProgress); | ||
} | ||
|
||
[Fact] | ||
public void NotifyProgressChange__When_ScannedWithErrors__Reports_about_errors() | ||
{ | ||
// Arrange | ||
_sut.NotifyStarted(_fixture.Create<int>()); | ||
|
||
// Act | ||
_sut.NotifyProgressChange(ProductScanStatus.ScannedWithErrors); | ||
|
||
// Verify | ||
Assert.True(_sut.HasErrors); | ||
Assert.Equal(TrackerScanStatus.InProgressWithErrors, _lastStatus); | ||
} | ||
|
||
[Fact] | ||
public void NotifyProgressChange__When_ScannedNewLowest__Reports_about_new_lowest() | ||
{ | ||
// Arrange | ||
_sut.NotifyStarted(_fixture.Create<int>()); | ||
|
||
// Act | ||
_sut.NotifyProgressChange(ProductScanStatus.ScannedNewLowest); | ||
|
||
// Verify | ||
Assert.True(_sut.HasNewLowestPrice); | ||
} | ||
|
||
[Fact] | ||
public void ProductAutoScanStartedEvent_fired__Calls_NotifyStarted() | ||
{ | ||
// Arrange | ||
var count = 10; | ||
|
||
// Act | ||
_productAutoScanStartedEventSubject.OnNext(new ProductAutoScanStartedEvent(count)); | ||
|
||
// Verify | ||
var expectedProgress = 1d / (count * 2); | ||
Assert.True(_sut.IsStarted); | ||
Assert.False(_sut.HasErrors); | ||
Assert.False(_sut.HasNewLowestPrice); | ||
Assert.Equal(0, _sut.FinishedJobs); | ||
Assert.Equal(TrackerScanStatus.InProgress, _lastStatus); | ||
Assert.Equal(expectedProgress, _lastProgress); | ||
} | ||
} | ||
} |
Oops, something went wrong.