def test_initialLimitsOverridesHistoryExtremesIfHistoryBoundedByInitialLimits(self): settings = {'initialpressurelow': 950, 'initialpressurehigh':1020} pressureHistory = PressureHistory(**settings) pressureHistory.add(951) pressureHistory.add(1019) self.assertEqual(pressureHistory.min, settings['initialpressurelow']) self.assertEqual(pressureHistory.max, settings['initialpressurehigh'])
def test_addCallsLogger(self): mock_logger = mock.create_autospec(CSVLogger) ph = PressureHistory(logger=mock_logger) somePressure = 1000 ph.add(somePressure) mock_logger.log.assert_called_once_with(somePressure)
def test_addFromIterCallsLogForEachValue(self): mock_logger = mock.create_autospec(CSVLogger) ph = PressureHistory(logger=mock_logger) pressureList = [1, 2] ph.addFromIterator(pressureList) for pressure in pressureList: mock_logger.log.assert_any_call(pressure)
def test_addFromIterCallsLogForEachValue(self): mock_logger = mock.create_autospec(CSVLogger) ph = PressureHistory(logger=mock_logger) pressureList = [1,2] ph.addFromIterator(pressureList) for pressure in pressureList: mock_logger.log.assert_any_call(pressure)
def test_initialLimitsOverridesHistoryExtremesIfHistoryBoundedByInitialLimits( self): settings = {'initialpressurelow': 950, 'initialpressurehigh': 1020} pressureHistory = PressureHistory(**settings) pressureHistory.add(951) pressureHistory.add(1019) self.assertEqual(pressureHistory.min, settings['initialpressurelow']) self.assertEqual(pressureHistory.max, settings['initialpressurehigh'])
def main(): args = parseCmdline() pressureHistorySettings = {} if args.initiallow: pressureHistorySettings['initialpressurelow'] = args.initiallow if args.initialhigh: pressureHistorySettings['initialpressurehigh'] = args.initialhigh if args.logFilename: csvfile = open(args.logFilename, 'w') pressureHistorySettings['logger'] = CSVLogger(csvfile=csvfile) pressureHistory = PressureHistory(**pressureHistorySettings) barometer = Barometer(sense_hat.SenseHat(), pressureHistory, updaterate=args.updaterate) barometer.run()
def test_initReadsFromKwargs(self): settings = {'initialpressurelow': 950, 'initialpressurehigh': 1020} pressureHistory = PressureHistory(**settings) self.assertEqual(pressureHistory.min, settings['initialpressurelow']) self.assertEqual(pressureHistory.max, settings['initialpressurehigh'])
def setUp(self): self.pressureHistory = PressureHistory()
class TestPressureHistory(unittest.TestCase): def setUp(self): self.pressureHistory = PressureHistory() def test_averageOfSingleValueIsSingleValue(self): somePressure = 42 self.pressureHistory.includeInAverage(somePressure) avgPressure = self.pressureHistory.averageOfAccumulated() self.assertEqual(avgPressure, somePressure) def test_averageOfNoValuesIsNone(self): avgPressure = self.pressureHistory.averageOfAccumulated() self.assertEqual(avgPressure, None) def test_addingAccumulatedAverageResetsAccumulator(self): somePressures = (42, 41, 40) for p in somePressures: self.pressureHistory.includeInAverage(p) self.pressureHistory.addFromAccumulator() self.assertEqual(len(self.pressureHistory.pressureAccumulator), 0) def test_addingAccumulatedAverageIsInPressureHistory(self): somePressure = 1020 otherPressure = 1001 self.pressureHistory.add(somePressure) self.pressureHistory.includeInAverage(otherPressure) self.pressureHistory.addFromAccumulator() self.assertEqual(self.pressureHistory.history[0], otherPressure) self.assertEqual(self.pressureHistory.history[1], somePressure) def test_latestValueAddedCanBeQueriedFor(self): somePressure = 1021 self.pressureHistory.add(somePressure) self.assertEqual(self.pressureHistory.latestAdded(), somePressure) def test_addDoesnModifyLength(self): self.pressureHistory.add(1) self.assertEqual(self.pressureHistory.history.maxlen, PressureHistory.MAXLEN) def test_addValueIsTheLatestValue(self): newPressure = 7 self.pressureHistory.add(newPressure) self.assertEqual(self.pressureHistory.history[0], newPressure) def test_fillingHistoryHasFirstValueNextToBeForgotten(self): for n in range(1, PressureHistory.MAXLEN + 1): self.pressureHistory.add(n) self.assertEqual( self.pressureHistory.history[PressureHistory.MAXLEN - 1], 1) def test_pressureMax(self): maxPressure = 100 for n in range(maxPressure + 1): self.pressureHistory.add(n) self.assertEqual(self.pressureHistory.max, maxPressure) def test_pressureMin(self): maxPressure = 100 for n in range(maxPressure + 1): self.pressureHistory.add(n) self.assertEqual(self.pressureHistory.min, 0) def test_pressureIsNormalizedToExtremes(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.pressureHistory.add((minPressure + maxPressure) / 2) self.assertEqual(self.pressureHistory.normalized()[0:3], [0.5, 1, 0]) def test_normalizedPressureIsFullLengthWithNoneIfNoValueAdded(self): self.assertEqual(self.pressureHistory.normalized(), [None] * self.pressureHistory.MAXLEN) def test_normalizedPressureHistoryHasExtremesAndNoneIfFewAdded(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.assertEqual(self.pressureHistory.normalized(), [1, 0] + [None] * (self.pressureHistory.MAXLEN - 2)) def test_normalizeOneValueClamps(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.assertEqual( self.pressureHistory.normalizeOneValue(minPressure - 1), 0) self.assertEqual( self.pressureHistory.normalizeOneValue(maxPressure + 1), 1) def test_normalizeOneValueWhenNothingAdded(self): somePressure = 1020 someOtherPressure = 1031 self.pressureHistory.add(somePressure) self.assertEqual( self.pressureHistory.normalizeOneValue(someOtherPressure), 0.5) def test_normalizeOneValueWhenOneAddedIsHalf(self): self.assertEqual(self.pressureHistory.normalizeOneValue(1020), 0.5) def test_normalizeOneValueNoneIsNone(self): self.assertEqual(self.pressureHistory.normalizeOneValue(None), None) def test_addFromIterator(self): self.pressureHistory.addFromIterator(iter((1, 2, 3, 4))) self.assertEqual(self.pressureHistory.max, 4) self.assertEqual(self.pressureHistory.min, 1)
def test_InitializingWithoutALoggerSetsLoggerToNoLogger(self): pressureHistory = PressureHistory() self.assertTrue(type(pressureHistory.logger) is NoLogger)
class TestPressureHistory(unittest.TestCase): def setUp(self): self.pressureHistory = PressureHistory() def test_averageOfSingleValueIsSingleValue(self): somePressure = 42 self.pressureHistory.includeInAverage(somePressure) avgPressure=self.pressureHistory.averageOfAccumulated() self.assertEqual(avgPressure, somePressure) def test_averageOfNoValuesIsNone(self): avgPressure=self.pressureHistory.averageOfAccumulated() self.assertEqual(avgPressure, None) def test_addingAccumulatedAverageResetsAccumulator(self): somePressures = (42,41,40) for p in somePressures: self.pressureHistory.includeInAverage(p) self.pressureHistory.addFromAccumulator() self.assertEqual(len(self.pressureHistory.pressureAccumulator), 0) def test_addingAccumulatedAverageIsInPressureHistory(self): somePressure=1020 otherPressure=1001 self.pressureHistory.add(somePressure) self.pressureHistory.includeInAverage(otherPressure) self.pressureHistory.addFromAccumulator() self.assertEqual(self.pressureHistory.history[0],otherPressure) self.assertEqual(self.pressureHistory.history[1],somePressure) def test_latestValueAddedCanBeQueriedFor(self): somePressure=1021 self.pressureHistory.add(somePressure) self.assertEqual(self.pressureHistory.latestAdded(), somePressure) def test_addDoesnModifyLength(self): self.pressureHistory.add(1) self.assertEqual(self.pressureHistory.history.maxlen, PressureHistory.MAXLEN) def test_addValueIsTheLatestValue(self): newPressure = 7 self.pressureHistory.add(newPressure) self.assertEqual(self.pressureHistory.history[0], newPressure) def test_fillingHistoryHasFirstValueNextToBeForgotten(self): for n in range(1,PressureHistory.MAXLEN+1): self.pressureHistory.add(n) self.assertEqual(self.pressureHistory.history[PressureHistory.MAXLEN-1],1) def test_pressureMax(self): maxPressure = 100 for n in range(maxPressure+1): self.pressureHistory.add(n) self.assertEqual(self.pressureHistory.max, maxPressure) def test_pressureMin(self): maxPressure = 100 for n in range(maxPressure+1): self.pressureHistory.add(n) self.assertEqual(self.pressureHistory.min, 0) def test_pressureIsNormalizedToExtremes(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.pressureHistory.add((minPressure+maxPressure)/2) self.assertEqual(self.pressureHistory.normalized()[0:3],[0.5,1,0]) def test_normalizedPressureIsFullLengthWithNoneIfNoValueAdded(self): self.assertEqual(self.pressureHistory.normalized(), [None]*self.pressureHistory.MAXLEN) def test_normalizedPressureHistoryHasExtremesAndNoneIfFewAdded(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.assertEqual(self.pressureHistory.normalized(),[1,0]+[None]*(self.pressureHistory.MAXLEN-2)) def test_normalizeOneValueClamps(self): minPressure = 1025 maxPressure = 1030 self.pressureHistory.add(minPressure) self.pressureHistory.add(maxPressure) self.assertEqual(self.pressureHistory.normalizeOneValue(minPressure-1), 0) self.assertEqual(self.pressureHistory.normalizeOneValue(maxPressure+1), 1) def test_normalizeOneValueWhenNothingAdded(self): somePressure = 1020 someOtherPressure = 1031 self.pressureHistory.add(somePressure) self.assertEqual(self.pressureHistory.normalizeOneValue(someOtherPressure), 0.5) def test_normalizeOneValueWhenOneAddedIsHalf(self): self.assertEqual(self.pressureHistory.normalizeOneValue(1020), 0.5) def test_normalizeOneValueNoneIsNone(self): self.assertEqual(self.pressureHistory.normalizeOneValue(None), None) def test_addFromIterator(self): self.pressureHistory.addFromIterator(iter((1,2,3,4))) self.assertEqual(self.pressureHistory.max, 4) self.assertEqual(self.pressureHistory.min, 1)