Skip to content

Commit

Permalink
Implement DB retrieval in db_store and add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeetShetty committed Dec 9, 2016
1 parent fbc1a41 commit 6009168
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 41 deletions.
40 changes: 0 additions & 40 deletions greenpithumb/db_retriever.py

This file was deleted.

81 changes: 80 additions & 1 deletion greenpithumb/db_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import collections


class DbStoreBase(object):
"""Base class for storing information in a database."""

Expand All @@ -11,7 +14,7 @@ def __init__(self, cursor):


class SoilMoistureStore(DbStoreBase):
"""Stores timestamp and soil moisture readings."""
"""Stores and retrieves timestamp and soil moisture readings."""

def store_soil_moisture(self, timestamp, soil_moisture):
"""Inserts moisture and timestamp info into an SQLite database.
Expand All @@ -37,6 +40,19 @@ def latest_soil_moisture(self):
else:
return results[0][0]

def retrieve_soil_moisture(self):
"""Returns timestamp and soil moisture readings.
Return values are a list of namedtuples with 'timestamp' and
'soil_moisture' fields.
"""
self._cursor.execute("SELECT * FROM soil_moisture")
SoilMoistureRecord = collections.namedtuple(
'SoilMoistureRecord', ['timestamp', 'soil_moisture'])
soil_moisture_data = map(SoilMoistureRecord._make,
self._cursor.fetchall())
return soil_moisture_data


class AmbientLightStore(DbStoreBase):
"""Stores timestamp and ambient light readings."""
Expand All @@ -52,6 +68,19 @@ def store_ambient_light(self, timestamp, ambient_light):
self._cursor.execute('INSERT INTO ambient_light VALUES (?, ?)',
(timestamp, ambient_light))

def retrieve_ambient_light(self):
"""Returns timestamp and ambient light readings.
Return values are a list of namedtuples with 'timestamp' and
'ambient_light' fields.
"""
self._cursor.execute("SELECT * FROM ambient_light")
AmbientLightRecord = collections.namedtuple(
'AmbientLightRecord', ['timestamp', 'ambient_light'])
ambient_light_data = map(AmbientLightRecord._make,
self._cursor.fetchall())
return ambient_light_data


class HumidityStore(DbStoreBase):
"""Stores timestamp and ambient humidity readings."""
Expand All @@ -67,6 +96,18 @@ def store_humidity(self, timestamp, humidity):
self._cursor.execute('INSERT INTO ambient_humidity VALUES (?, ?)',
(timestamp, humidity))

def retrieve_humidity(self):
"""Returns timestamp and relative humidity readings.
Return values are a list of namedtuples with 'timestamp' and
'humidity' fields.
"""
self._cursor.execute("SELECT * FROM ambient_humidity")
HumidityRecord = collections.namedtuple('HumidityRecord',
['timestamp', 'humidity'])
humidity_data = map(HumidityRecord._make, self._cursor.fetchall())
return humidity_data


class ReservoirLevelStore(DbStoreBase):
"""Stores timestamp and reservoir level readings."""
Expand All @@ -82,6 +123,19 @@ def store_reservoir_level(self, timestamp, reservoir_level):
self._cursor.execute('INSERT INTO reservoir_level VALUES (?, ?)',
(timestamp, reservoir_level))

def retrieve_reservoir_level(self):
"""Returns timestamp and reservoir level readings.
Return values are a list of namedtuples with 'timestamp' and
'reservoir_level' fields.
"""
self._cursor.execute("SELECT * FROM reservoir_level")
ReservoirLevelRecord = collections.namedtuple(
'ReservoirLevelRecord', ['timestamp', 'reservoir_level'])
reservoir_level_data = map(ReservoirLevelRecord._make,
self._cursor.fetchall())
return reservoir_level_data


class TemperatureStore(DbStoreBase):
"""Stores timestamp and ambient temperature readings."""
Expand All @@ -97,6 +151,18 @@ def store_temperature(self, timestamp, temperature):
self._cursor.execute('INSERT INTO temperature VALUES (?, ?)',
(timestamp, temperature))

def retrieve_temperature(self):
"""Returns timestamp and temperature(C) readings.
Return values are a list of namedtuples with 'timestamp' and
'temperature' fields.
"""
self._cursor.execute("SELECT * FROM temperature")
TemperatureRecord = collections.namedtuple('TemperatureRecord',
['timestamp', 'temperature'])
temperature_data = map(TemperatureRecord._make, self._cursor.fetchall())
return temperature_data


class WateringEventStore(DbStoreBase):
"""Stores timestamp and volume of water pumped to plant."""
Expand All @@ -110,3 +176,16 @@ def store_water_pumped(self, timestamp, water_pumped):
"""
self._cursor.execute('INSERT INTO watering_events VALUES (?, ?)',
(timestamp, water_pumped))

def retrieve_water_pumped(self):
"""Returns timestamp and volume of water pumped.
Return values are a list of namedtuples with 'timestamp' and
'water_pumped' fields.
"""
self._cursor.execute("SELECT * FROM watering_events")
WateringEventRecord = collections.namedtuple(
'WateringEventRecord', ['timestamp', 'water_pumped'])
watering_event_data = map(WateringEventRecord._make,
self._cursor.fetchall())
return watering_event_data
83 changes: 83 additions & 0 deletions tests/test_db_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def test_latest_soil_moisture_empty_database(self):
moisture = store.latest_soil_moisture()
self.assertIsNone(moisture)

def test_retrieve_soil_moisture(self):
mock_cursor = mock.Mock()
store = db_store.SoilMoistureStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 300),
(timestamp2, 400)]
soil_moisture_data = store.retrieve_soil_moisture()

self.assertEqual(soil_moisture_data[0].timestamp, timestamp1)
self.assertEqual(soil_moisture_data[0].soil_moisture, 300)
self.assertEqual(soil_moisture_data[1].timestamp, timestamp2)
self.assertEqual(soil_moisture_data[1].soil_moisture, 400)

def test_store_ambient_light(self):
"""Should insert timestamp and ambient light into database."""
timestamp = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000)
Expand All @@ -45,6 +59,20 @@ def test_store_ambient_light(self):
"INSERT INTO ambient_light VALUES (?, ?)", (timestamp,
ambient_light))

def test_retrieve_ambient_light(self):
mock_cursor = mock.Mock()
store = db_store.AmbientLightStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 300),
(timestamp2, 400)]
ambient_light_data = store.retrieve_ambient_light()

self.assertEqual(ambient_light_data[0].timestamp, timestamp1)
self.assertEqual(ambient_light_data[0].ambient_light, 300)
self.assertEqual(ambient_light_data[1].timestamp, timestamp2)
self.assertEqual(ambient_light_data[1].ambient_light, 400)

def test_store_humidity(self):
"""Should insert timestamp and humidity level into database."""
timestamp = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000)
Expand All @@ -55,6 +83,19 @@ def test_store_humidity(self):
mock_cursor.execute.assert_called_once_with(
"INSERT INTO ambient_humidity VALUES (?, ?)", (timestamp, humidity))

def test_retrieve_humidity(self):
mock_cursor = mock.Mock()
store = db_store.HumidityStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 50), (timestamp2, 51)]
humidity_data = store.retrieve_humidity()

self.assertEqual(humidity_data[0].timestamp, timestamp1)
self.assertEqual(humidity_data[0].humidity, 50)
self.assertEqual(humidity_data[1].timestamp, timestamp2)
self.assertEqual(humidity_data[1].humidity, 51)

def test_store_reservoir_level(self):
"""Should insert timestamp and reservoir level into detabase."""
timestamp = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000)
Expand All @@ -66,6 +107,20 @@ def test_store_reservoir_level(self):
"INSERT INTO reservoir_level VALUES (?, ?)", (timestamp,
reservoir_level))

def test_retrieve_reservoir_level(self):
mock_cursor = mock.Mock()
store = db_store.ReservoirLevelStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 1000),
(timestamp2, 1200)]
reservoir_level_data = store.retrieve_reservoir_level()

self.assertEqual(reservoir_level_data[0].timestamp, timestamp1)
self.assertEqual(reservoir_level_data[0].reservoir_level, 1000)
self.assertEqual(reservoir_level_data[1].timestamp, timestamp2)
self.assertEqual(reservoir_level_data[1].reservoir_level, 1200)

def test_store_temperature(self):
"""Should insert timestamp and temperature into database."""
timestamp = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000)
Expand All @@ -76,6 +131,20 @@ def test_store_temperature(self):
mock_cursor.execute.assert_called_once_with(
"INSERT INTO temperature VALUES (?, ?)", (timestamp, temperature))

def test_retrieve_temperature(self):
mock_cursor = mock.Mock()
store = db_store.TemperatureStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 21.0),
(timestamp2, 21.5)]
temperature_data = store.retrieve_temperature()

self.assertEqual(temperature_data[0].timestamp, timestamp1)
self.assertEqual(temperature_data[0].temperature, 21.0)
self.assertEqual(temperature_data[1].timestamp, timestamp2)
self.assertEqual(temperature_data[1].temperature, 21.5)

def test_store_water_pumped(self):
"""Should insert timestamp and volume of water pumped into database."""
timestamp = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000)
Expand All @@ -86,3 +155,17 @@ def test_store_water_pumped(self):
mock_cursor.execute.assert_called_once_with(
"INSERT INTO watering_events VALUES (?, ?)", (timestamp,
water_pumped))

def test_retrieve_water_pumped(self):
mock_cursor = mock.Mock()
store = db_store.WateringEventStore(mock_cursor)
timestamp1 = str(datetime.datetime(2016, 7, 23, 10, 51, 9, 928000))
timestamp2 = str(datetime.datetime(2016, 7, 23, 10, 52, 9, 928000))
mock_cursor.fetchall.return_value = [(timestamp1, 300),
(timestamp2, 301)]
watering_event_data = store.retrieve_water_pumped()

self.assertEqual(watering_event_data[0].timestamp, timestamp1)
self.assertEqual(watering_event_data[0].water_pumped, 300)
self.assertEqual(watering_event_data[1].timestamp, timestamp2)
self.assertEqual(watering_event_data[1].water_pumped, 301)

0 comments on commit 6009168

Please sign in to comment.