public void WriteAsyncWhenResultIsValidShouldWriteToDatabase()
        {
            var factory = DatabaseHelper.GetMockConnectionFactory();

            using (var connection = factory.CreateConnectionAsync().Result)
            {
                DatabaseHelper.CreateTablesIfNotExist();
                var deviceId = AddDummyDevviceToInMemoryDatabase();

                var result = new ConveyorSimulator.SimulationResult
                {
                    Id       = deviceId,
                    Speed    = 213,
                    DateTime = DateTime.UtcNow,
                    PackageTrackingAlarmState = PackageTrackingAlarmState.Okay,
                    CurrentRecipeCount        = 2,
                    CurrentBoards             = 3
                };

                var writer = new DatabaseWriter(factory, Mock.Of <ILogger>());

                writer.WriteAsync(result).Wait();

                var results = connection.Query("SELECT * FROM DeviceReadings");

                Assert.AreEqual(1, results.Count());
            }
        }
Пример #2
0
        public async Task WriteAsync(ConveyorSimulator.SimulationResult result)
        {
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            using (var connection = await _connectionFactory.CreateConnectionAsync())
            {
                var sql = "INSERT INTO DeviceReadings (DeviceId, DateTime, Speed," +
                          " PackageTrackingAlarmState, CurrentBoards, CurrentRecipeCount)" +
                          $" VALUES(@{nameof(ConveyorSimulator.SimulationResult.Id)}, @{nameof(ConveyorSimulator.SimulationResult.DateTime)}," +
                          $" @{nameof(ConveyorSimulator.SimulationResult.Speed)}, @{nameof(ConveyorSimulator.SimulationResult.PackageTrackingAlarmState)}," +
                          $" @{nameof(ConveyorSimulator.SimulationResult.CurrentBoards)}, @{nameof(ConveyorSimulator.SimulationResult.CurrentRecipeCount)})";

                await connection.ExecuteAsync(sql, result);

                _logger.Information($"A deviceReading with ID '{result.Id}' added.");
            }
        }