Exemplo n.º 1
0
        public async Task GetAlertDefinitionAsync_InvalidAlertDefinitionId_NotFoundException()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context, CreateMapper());

            // Act & Assert
            await Assert.ThrowsAsync <NotFoundException>(async() => await repository.GetAlertDefinitionAsync(Guid.NewGuid()));
        }
Exemplo n.º 2
0
        public async Task GetStockAsync_StockIdDoesNotExist_NotFoundException()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context);

            // Act & Assert
            await Assert.ThrowsAsync <NotFoundException>(async() => await repository.GetStockAsync(Guid.NewGuid()));
        }
Exemplo n.º 3
0
        public async Task FindStocksAsync_NoMatch_EmptyListReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context);

            // Act
            var stocks = await repository.FindStocksAsync("XYZ", CancellationToken.None);

            // Assert
            stocks.Should().BeEmpty();
        }
Exemplo n.º 4
0
        public async Task GetAlertDefinitionsAsync_UnknownAppUserIdProvided_EmptyListReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context, CreateMapper());

            // Act
            var results = await repository.GetAlertDefinitionsAsync(Guid.NewGuid());

            // Assert
            results.Should().BeEmpty();
        }
Exemplo n.º 5
0
        public async Task GetAlertDefinitionByStockIdAsync_UnknownStockId_EmptyArray()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var mapper     = CreateMapper();
            var repository = CreateRepository(context, mapper);

            // Act
            var results = await repository.GetAlertDefinitionsByStockIdAsync(Guid.NewGuid());

            // Assert
            results.Should().BeEmpty();
        }
Exemplo n.º 6
0
        public async Task FindStocksAsync_Match_StocksReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context);

            // Act
            var stocks = await repository.FindStocksAsync("M", CancellationToken.None);

            // Assert
            stocks.Count().Should().Be(2);
            stocks.Any(s => s.Symbol == "MSFT").Should().BeTrue();
            stocks.Any(s => s.Symbol == "MRK").Should().BeTrue();
        }
Exemplo n.º 7
0
        public async Task GetStockAsync_StockIdExists_StockReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository    = CreateRepository(context);
            var existingStock = await context.Stocks.FirstAsync();

            // Act
            var stock = await repository.GetStockAsync(existingStock.StockId);

            // Assert
            stock.StockId.Should().Be(existingStock.StockId);
            stock.Symbol.Should().Be(existingStock.Symbol);
            stock.LastPrice.Should().Be(existingStock.LastPrice);
        }
Exemplo n.º 8
0
        public async Task SaveAsync_Update_AlertDefinitionSaved()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var mapper          = CreateMapper();
            var alertDefinition = await CreateAndSaveComplexAlertDefinitionAsync(context, mapper);

            alertDefinition.Name = "Updated Definition";
            alertDefinition.RootCriteria.ChildrenCriteria.Remove(alertDefinition.RootCriteria.ChildrenCriteria.Last());
            alertDefinition.RootCriteria.ChildrenCriteria.Add(
                new AlertCriteria
            {
                Type             = CriteriaType.Composite,
                Operator         = CriteriaOperator.And,
                ChildrenCriteria = new List <AlertCriteria>
                {
                    new AlertCriteria
                    {
                        Type     = CriteriaType.DailyPercentageGainLoss,
                        Operator = CriteriaOperator.LessThanOrEqualTo,
                        Level    = -0.02M
                    },
                    new AlertCriteria
                    {
                        Type     = CriteriaType.DailyPercentageGainLoss,
                        Operator = CriteriaOperator.GreaterThanOrEqualTo,
                        Level    = -0.06M
                    }
                }
            }
                );

            // Create new context (with same DB name) to match service lifetime of context when running as a service
            context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context, mapper);

            // Act
            await repository.SaveAsync(alertDefinition);

            // Assert
            var result = await repository.GetAlertDefinitionAsync(alertDefinition.AlertDefinitionId);

            AssertAlertDefinition(alertDefinition, result);
        }
Exemplo n.º 9
0
        public async Task SaveAsync_Insert_AlertDefinitionSaved()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var mapper          = CreateMapper();
            var repository      = CreateRepository(context, mapper);
            var alertDefinition = await CreateComplexAlertDefinitionAsync(context, mapper);

            // Act
            await repository.SaveAsync(alertDefinition);

            // Assert
            var result = await repository.GetAlertDefinitionAsync(alertDefinition.AlertDefinitionId);

            AssertAlertDefinition(alertDefinition, result);
        }
Exemplo n.º 10
0
        public async Task GetAlertDefinitionAsync_AlertDefinitionId_AlertDefinitionReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var mapper     = CreateMapper();
            var repository = CreateRepository(context, mapper);
            var expectedAlertDefinition = await CreateAndSaveComplexAlertDefinitionAsync(context, mapper);

            // Act
            var result = await repository.GetAlertDefinitionAsync(expectedAlertDefinition.AlertDefinitionId);

            // Assert
            result.AlertDefinitionId.Should().Be(expectedAlertDefinition.AlertDefinitionId);
            result.AppUser.AppUserId.Should().Be(expectedAlertDefinition.AppUser.AppUserId);
            result.Stock.StockId.Should().Be(expectedAlertDefinition.Stock.StockId);

            AssertCriteria(expectedAlertDefinition.RootCriteria, result.RootCriteria);
        }
Exemplo n.º 11
0
        public async Task GetSubscribedStocksAsync_StocksWithEnabledAlertDefinitionsReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository      = CreateRepository(context);
            var expectedSymbols = await(from a in context.AlertDefinitions
                                        where a.Status == AlertDefinitionStatuses.Enabled
                                        select a.Stock.Symbol).Distinct().ToListAsync();

            // Act
            var stocks = (await repository.GetSubscribedStocksAsync()).ToList();

            // Assert
            stocks.Count.Should().Be(expectedSymbols.Count);
            foreach (var symbol in expectedSymbols)
            {
                stocks.Count(s => s.Symbol == symbol).Should().Be(1);
            }
        }
Exemplo n.º 12
0
        public async Task GetAlertDefinitionsAsync_AppUserIdProvided_AlertDefinitionsReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var repository = CreateRepository(context, CreateMapper());
            var appUser    = await context.AppUsers.FirstAsync();

            var expectedAlertDefinitionIds = (from ad in context.AlertDefinitions
                                              where ad.AppUserId == appUser.AppUserId
                                              select ad.AlertDefinitionId).ToList();

            // Act
            var results = (await repository.GetAlertDefinitionsAsync(appUser.AppUserId)).ToList();

            // Assert
            results.Count.Should().Be(expectedAlertDefinitionIds.Count);
            foreach (var r in results)
            {
                expectedAlertDefinitionIds.Should().Contain(r.AlertDefinitionId);
            }
        }
Exemplo n.º 13
0
        public async Task GetAlertDefinitionByStockIdAsync_KnownStockId_AlertDefinitionsReturned()
        {
            // Arrange
            var context = await InMemoryDbContextFactory.CreateDatabaseContextAsync();

            var mapper     = CreateMapper();
            var repository = CreateRepository(context, mapper);
            var stock      = await context.Stocks.FirstAsync();

            var expectedAlertDefinitionIds = (from ad in context.AlertDefinitions
                                              where ad.StockId == stock.StockId
                                              select ad.AlertDefinitionId).ToList();

            // Act
            var results = (await repository.GetAlertDefinitionsByStockIdAsync(stock.StockId)).ToList();

            // Assert
            results.Count.Should().Be(expectedAlertDefinitionIds.Count);
            foreach (var r in results)
            {
                expectedAlertDefinitionIds.Should().Contain(r.AlertDefinitionId);
            }
        }