Skip to content

Commit

Permalink
Refactoring method names in DbStore classes (JeetShetty#90)
Browse files Browse the repository at this point in the history
The calling semantics of the DbStore classes are a bit redundant because we
end up with calls like 'temperature_store.store_temperature'. The class name
gives the type, so we don't have to repeat it in the method. I think if we do
this, it opens up the door to some refactoring around managing multiple
db store instances of different types.
  • Loading branch information
mtlynch committed Mar 25, 2017
1 parent 9eb9756 commit a562f1f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
22 changes: 11 additions & 11 deletions greenpithumb/db_store.py
Expand Up @@ -126,7 +126,7 @@ def __init__(self, connection):
class SoilMoistureStore(DbStoreBase):
"""Stores and retrieves timestamp and soil moisture readings."""

def store_soil_moisture(self, soil_moisture_record):
def insert(self, soil_moisture_record):
"""Inserts moisture and timestamp info into an SQLite database.
Args:
Expand All @@ -137,7 +137,7 @@ def store_soil_moisture(self, soil_moisture_record):
soil_moisture_record.soil_moisture))
self._connection.commit()

def latest_soil_moisture(self):
def get_latest(self):
"""Returns the most recent soil moisture reading."""
query = '''SELECT soil_moisture FROM soil_moisture
ORDER BY timestamp DESC
Expand All @@ -150,7 +150,7 @@ def latest_soil_moisture(self):
else:
return results[0][0]

def retrieve_soil_moisture(self):
def get(self):
"""Retrieves timestamp and soil moisture readings.
Returns:
Expand All @@ -167,7 +167,7 @@ def retrieve_soil_moisture(self):
class AmbientLightStore(DbStoreBase):
"""Stores timestamp and ambient light readings."""

def store_ambient_light(self, ambient_light_record):
def insert(self, ambient_light_record):
"""Inserts ambient light and timestamp info into an SQLite database.
Args:
Expand All @@ -178,7 +178,7 @@ def store_ambient_light(self, ambient_light_record):
ambient_light_record.ambient_light))
self._connection.commit()

def retrieve_ambient_light(self):
def get(self):
"""Retrieves timestamp and ambient light readings.
Returns:
Expand All @@ -195,7 +195,7 @@ def retrieve_ambient_light(self):
class HumidityStore(DbStoreBase):
"""Stores timestamp and ambient humidity readings."""

def store_humidity(self, humidity_record):
def insert(self, humidity_record):
"""Inserts humidity and timestamp info into an SQLite database.
Args:
Expand All @@ -206,7 +206,7 @@ def store_humidity(self, humidity_record):
humidity_record.humidity))
self._connection.commit()

def retrieve_humidity(self):
def get(self):
"""Retrieves timestamp and relative humidity readings.
Returns:
Expand All @@ -223,7 +223,7 @@ def retrieve_humidity(self):
class TemperatureStore(DbStoreBase):
"""Stores timestamp and ambient temperature readings."""

def store_temperature(self, temperature_record):
def insert(self, temperature_record):
"""Inserts temperature and timestamp info into an SQLite database.
Args:
Expand All @@ -234,7 +234,7 @@ def store_temperature(self, temperature_record):
temperature_record.temperature))
self._connection.commit()

def retrieve_temperature(self):
def get(self):
"""Retrieves timestamp and temperature(C) readings.
Returns:
Expand All @@ -251,7 +251,7 @@ def retrieve_temperature(self):
class WateringEventStore(DbStoreBase):
"""Stores timestamp and volume of water pumped to plant."""

def store_water_pumped(self, watering_event_record):
def insert(self, watering_event_record):
"""Inserts water volume and timestamp info into an SQLite database.
Args:
Expand All @@ -262,7 +262,7 @@ def store_water_pumped(self, watering_event_record):
watering_event_record.water_pumped))
self._connection.commit()

def retrieve_water_pumped(self):
def get(self):
"""Retrieves timestamp and volume of water pumped(in mL).
Returns:
Expand Down
10 changes: 5 additions & 5 deletions greenpithumb/record_processor.py
Expand Up @@ -33,15 +33,15 @@ def process_next_record(self):
"""
record = self._record_queue.get()
if isinstance(record, db_store.SoilMoistureRecord):
self._soil_moisture_store.store_soil_moisture(record)
self._soil_moisture_store.insert(record)
elif isinstance(record, db_store.AmbientLightRecord):
self._ambient_light_store.store_ambient_light(record)
self._ambient_light_store.insert(record)
elif isinstance(record, db_store.HumidityRecord):
self._humidity_store.store_humidity(record)
self._humidity_store.insert(record)
elif isinstance(record, db_store.TemperatureRecord):
self._temperature_store.store_temperature(record)
self._temperature_store.insert(record)
elif isinstance(record, db_store.WateringEventRecord):
self._watering_event_store.store_water_pumped(record)
self._watering_event_store.insert(record)
else:
raise UnsupportedRecordError('Unrecognized record type: %s' %
str(record))
68 changes: 34 additions & 34 deletions tests/test_db_store.py
Expand Up @@ -64,38 +64,38 @@ def setUp(self):
self.mock_connection = mock.Mock()
self.mock_connection.cursor.return_value = self.mock_cursor

def test_store_soil_moisture(self):
def test_insert_soil_moisture(self):
"""Should insert timestamp and moisture level into database."""
record = db_store.SoilMoistureRecord(
timestamp=datetime.datetime(
2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc),
soil_moisture=300)
store = db_store.SoilMoistureStore(self.mock_connection)
store.store_soil_moisture(record)
store.insert(record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO soil_moisture VALUES (?, ?)', (
'2016-07-23T10:51:09.928000+00:00', 300))
self.mock_connection.commit.assert_called_once()

def test_latest_soil_moisture(self):
def test_get_latest_soil_moisture(self):
store = db_store.SoilMoistureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [(300,)]
moisture = store.latest_soil_moisture()
moisture = store.get_latest()
self.assertEqual(300, moisture)

def test_latest_soil_moisture_empty_database(self):
def test_get_latest_soil_moisture_empty_database(self):
store = db_store.SoilMoistureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
moisture = store.latest_soil_moisture()
moisture = store.get_latest()
self.assertIsNone(moisture)

def test_retrieve_soil_moisture(self):
def test_get_soil_moisture(self):
store = db_store.SoilMoistureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [
('2016-07-23 10:51:09.928000-05:00', 300),
('2016-07-23 10:52:09.928000-05:00', 400)
]
soil_moisture_data = store.retrieve_soil_moisture()
soil_moisture_data = store.get()
soil_moisture_data.sort(
key=lambda SoilMoistureRecord: SoilMoistureRecord.timestamp)

Expand All @@ -110,32 +110,32 @@ def test_retrieve_soil_moisture(self):
2016, 7, 23, 10, 52, 9, 928000, tzinfo=UTC_MINUS_5))
self.assertEqual(soil_moisture_data[1].soil_moisture, 400)

def test_retrieve_soil_moisture_empty_database(self):
def test_get_soil_moisture_empty_database(self):
store = db_store.SoilMoistureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
soil_moisture_data = store.retrieve_soil_moisture()
soil_moisture_data = store.get()
self.assertEqual(soil_moisture_data, [])

def test_store_ambient_light(self):
def test_insert_ambient_light(self):
"""Should insert timestamp and ambient light into database."""
ambient_light_record = db_store.AmbientLightRecord(
timestamp=datetime.datetime(
2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc),
ambient_light=50.0)
store = db_store.AmbientLightStore(self.mock_connection)
store.store_ambient_light(ambient_light_record)
store.insert(ambient_light_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO ambient_light VALUES (?, ?)', (
'2016-07-23T10:51:09.928000+00:00', 50.0))
self.mock_connection.commit.assert_called_once()

def test_retrieve_ambient_light(self):
def test_get_ambient_light(self):
store = db_store.AmbientLightStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [
('2016-07-23 10:51:09.928000-05:00', 300),
('2016-07-23 10:52:09.928000-05:00', 400)
]
ambient_light_data = store.retrieve_ambient_light()
ambient_light_data = store.get()
ambient_light_data.sort(
key=lambda AmbientLightRecord: AmbientLightRecord.timestamp)

Expand All @@ -150,32 +150,32 @@ def test_retrieve_ambient_light(self):
2016, 7, 23, 10, 52, 9, 928000, tzinfo=UTC_MINUS_5))
self.assertEqual(ambient_light_data[1].ambient_light, 400)

def test_retrieve_ambient_light_empty_database(self):
def test_get_ambient_light_empty_database(self):
store = db_store.AmbientLightStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
ambient_light_data = store.retrieve_ambient_light()
ambient_light_data = store.get()
self.assertEqual(ambient_light_data, [])

def test_store_humidity(self):
def test_insert_humidity(self):
"""Should insert timestamp and humidity level into database."""
humidity_record = db_store.HumidityRecord(
timestamp=datetime.datetime(
2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc),
humidity=50.0)
store = db_store.HumidityStore(self.mock_connection)
store.store_humidity(humidity_record)
store.insert(humidity_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO ambient_humidity VALUES (?, ?)',
('2016-07-23T10:51:09.928000+00:00', 50.0))
self.mock_connection.commit.assert_called_once()

def test_retrieve_humidity(self):
def test_get_humidity(self):
store = db_store.HumidityStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [
('2016-07-23 10:51:09.928000-05:00', 50),
('2016-07-23 10:52:09.928000-05:00', 51)
]
humidity_data = store.retrieve_humidity()
humidity_data = store.get()
humidity_data.sort(key=lambda HumidityRecord: HumidityRecord.timestamp)

self.assertEqual(
Expand All @@ -189,32 +189,32 @@ def test_retrieve_humidity(self):
2016, 7, 23, 10, 52, 9, 928000, tzinfo=UTC_MINUS_5))
self.assertEqual(humidity_data[1].humidity, 51)

def test_retrieve_humidity_empty_database(self):
def test_get_humidity_empty_database(self):
store = db_store.HumidityStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
humidity_data = store.retrieve_humidity()
humidity_data = store.get()
self.assertEqual(humidity_data, [])

def test_store_temperature(self):
def test_insert_temperature(self):
"""Should insert timestamp and temperature into database."""
temperature_record = db_store.TemperatureRecord(
timestamp=datetime.datetime(
2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc),
temperature=21.1)
store = db_store.TemperatureStore(self.mock_connection)
store.store_temperature(temperature_record)
store.insert(temperature_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO temperature VALUES (?, ?)',
('2016-07-23T10:51:09.928000+00:00', 21.1))
self.mock_connection.commit.assert_called_once()

def test_retrieve_temperature(self):
def test_get_temperature(self):
store = db_store.TemperatureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [
('2016-07-23 10:51:09.928000-05:00', 21.0),
('2016-07-23 10:52:09.928000-05:00', 21.5)
]
temperature_data = store.retrieve_temperature()
temperature_data = store.get()
temperature_data.sort(
key=lambda TemperatureRecord: TemperatureRecord.timestamp)

Expand All @@ -229,32 +229,32 @@ def test_retrieve_temperature(self):
2016, 7, 23, 10, 52, 9, 928000, tzinfo=UTC_MINUS_5))
self.assertEqual(temperature_data[1].temperature, 21.5)

def test_retrieve_temperature_empty_database(self):
def test_get_temperature_empty_database(self):
store = db_store.TemperatureStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
temperature_data = store.retrieve_temperature()
temperature_data = store.get()
self.assertEqual(temperature_data, [])

def test_store_water_pumped(self):
def test_insert_water_pumped(self):
"""Should insert timestamp and volume of water pumped into database."""
watering_event_record = db_store.WateringEventRecord(
timestamp=datetime.datetime(
2016, 7, 23, 10, 51, 9, 928000, tzinfo=pytz.utc),
water_pumped=200.0)
store = db_store.WateringEventStore(self.mock_connection)
store.store_water_pumped(watering_event_record)
store.insert(watering_event_record)
self.mock_cursor.execute.assert_called_once_with(
'INSERT INTO watering_events VALUES (?, ?)', (
'2016-07-23T10:51:09.928000+00:00', 200.0))
self.mock_connection.commit.assert_called_once()

def test_retrieve_water_pumped(self):
def test_get_water_pumped(self):
store = db_store.WateringEventStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = [
('2016-07-23 10:51:09.928000-05:00', 300),
('2016-07-23 10:52:09.928000-05:00', 301)
]
watering_event_data = store.retrieve_water_pumped()
watering_event_data = store.get()
watering_event_data.sort(
key=lambda WaterintEventRecord: WaterintEventRecord.timestamp)

Expand All @@ -269,8 +269,8 @@ def test_retrieve_water_pumped(self):
2016, 7, 23, 10, 52, 9, 928000, tzinfo=UTC_MINUS_5))
self.assertEqual(watering_event_data[1].water_pumped, 301)

def test_retrieve_water_pumped_empty_database(self):
def test_get_water_pumped_empty_database(self):
store = db_store.WateringEventStore(self.mock_connection)
self.mock_cursor.fetchall.return_value = []
watering_event_data = store.retrieve_water_pumped()
watering_event_data = store.get()
self.assertEqual(watering_event_data, [])
13 changes: 5 additions & 8 deletions tests/test_record_processor.py
Expand Up @@ -33,8 +33,7 @@ def test_process_soil_moisture_record(self):
soil_moisture=300)
self.record_queue.put(record)
self.processor.process_next_record()
self.mock_soil_moisture_store.store_soil_moisture.assert_called_with(
record)
self.mock_soil_moisture_store.insert.assert_called_with(record)

def test_process_ambient_light_record(self):
record = db_store.AmbientLightRecord(
Expand All @@ -43,8 +42,7 @@ def test_process_ambient_light_record(self):
ambient_light=29.2)
self.record_queue.put(record)
self.processor.process_next_record()
self.mock_ambient_light_store.store_ambient_light.assert_called_with(
record)
self.mock_ambient_light_store.insert.assert_called_with(record)

def test_process_humidity_record(self):
record = db_store.HumidityRecord(
Expand All @@ -53,7 +51,7 @@ def test_process_humidity_record(self):
humidity=184.5)
self.record_queue.put(record)
self.processor.process_next_record()
self.mock_humidity_store.store_humidity.assert_called_with(record)
self.mock_humidity_store.insert.assert_called_with(record)

def test_process_temperature_record(self):
record = db_store.TemperatureRecord(
Expand All @@ -62,7 +60,7 @@ def test_process_temperature_record(self):
temperature=32.9)
self.record_queue.put(record)
self.processor.process_next_record()
self.mock_temperature_store.store_temperature.assert_called_with(record)
self.mock_temperature_store.insert.assert_called_with(record)

def test_process_watering_event_record(self):
record = db_store.WateringEventRecord(
Expand All @@ -71,8 +69,7 @@ def test_process_watering_event_record(self):
water_pumped=15.6)
self.record_queue.put(record)
self.processor.process_next_record()
self.mock_watering_event_store.store_water_pumped.assert_called_with(
record)
self.mock_watering_event_store.insert.assert_called_with(record)

def test_rejects_unsupported_record(self):
record = 'dummy invalid record'
Expand Down

0 comments on commit a562f1f

Please sign in to comment.