public SensorCost GetLastOrCreateSensorCost(string sensorId) { var lastSensorCost = this.SensorCosts .OrderByDescending(sensorCost => sensorCost.Id) .FirstOrDefault(sensorCost => sensorCost.SensorId == sensorId); if (lastSensorCost == null) { lastSensorCost = new SensorCost() { SensorId = sensorId, Title = "$", Value = 0.0F }; this.SensorCosts.Add(lastSensorCost); this.SaveChanges(); } return(lastSensorCost); }
public async Task CreateSensorCostAsync(string title, float value) { //Given var sensor = await CreateSensorAsync("*****@*****.**", "My Sensor", SensorTypes.EnergyLog); var cost = new SensorCost() { Title = title, SensorId = sensor.Id, Value = value }; //When this.DbContext.Add(cost); this.DbContext.SaveChanges(); var savedCost = this.DbContext.GetSensorCost(sensor.Id, cost.Id); //Then Assert.Equal(savedCost.Id, cost.Id); }