-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Justyns purge old data #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Justyns purge old data #2059
Changes from all commits
c89cd6a
fd48fc5
d5ca97b
bf3b77e
67b0365
93fd6fa
cba85ca
53d7e07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| """The tests for the Recorder component.""" | ||
| # pylint: disable=too-many-public-methods,protected-access | ||
| import unittest | ||
| import time | ||
| import json | ||
| from unittest.mock import patch | ||
|
|
||
| from homeassistant.const import MATCH_ALL | ||
|
|
@@ -10,7 +12,7 @@ | |
|
|
||
|
|
||
| class TestRecorder(unittest.TestCase): | ||
| """Test the chromecast module.""" | ||
| """Test the recorder module.""" | ||
|
|
||
| def setUp(self): # pylint: disable=invalid-name | ||
| """Setup things to be run when tests are started.""" | ||
|
|
@@ -25,6 +27,52 @@ def tearDown(self): # pylint: disable=invalid-name | |
| self.hass.stop() | ||
| recorder._INSTANCE.block_till_done() | ||
|
|
||
| def _add_test_states(self): | ||
| """Add multiple states to the db for testing.""" | ||
| now = int(time.time()) | ||
| five_days_ago = now - (60*60*24*5) | ||
| attributes = {'test_attr': 5, 'test_attr_10': 'nice'} | ||
|
|
||
| self.hass.pool.block_till_done() | ||
| recorder._INSTANCE.block_till_done() | ||
| for event_id in range(5): | ||
| if event_id < 3: | ||
| timestamp = five_days_ago | ||
| state = 'purgeme' | ||
| else: | ||
| timestamp = now | ||
| state = 'dontpurgeme' | ||
| recorder.query("INSERT INTO states (" | ||
| "entity_id, domain, state, attributes," | ||
| "last_changed, last_updated, created," | ||
| "utc_offset, event_id)" | ||
| "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", | ||
| ('test.recorder2', 'sensor', state, | ||
| json.dumps(attributes), timestamp, timestamp, | ||
| timestamp, -18000, event_id + 1000)) | ||
|
|
||
| def _add_test_events(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """Add a few events for testing.""" | ||
| now = int(time.time()) | ||
| five_days_ago = now - (60*60*24*5) | ||
| event_data = {'test_attr': 5, 'test_attr_10': 'nice'} | ||
|
|
||
| self.hass.pool.block_till_done() | ||
| recorder._INSTANCE.block_till_done() | ||
| for event_id in range(5): | ||
| if event_id < 2: | ||
| timestamp = five_days_ago | ||
| event_type = 'EVENT_TEST_PURGE' | ||
| else: | ||
| timestamp = now | ||
| event_type = 'EVENT_TEST' | ||
| recorder.query("INSERT INTO events" | ||
| "(event_type, event_data, origin, created," | ||
| "time_fired, utc_offset)" | ||
| "VALUES (?, ?, ?, ?, ?, ?)", | ||
| (event_type, json.dumps(event_data), 'LOCAL', | ||
| timestamp, timestamp, -18000)) | ||
|
|
||
| def test_saving_state(self): | ||
| """Test saving and restoring a state.""" | ||
| entity_id = 'test.recorder' | ||
|
|
@@ -76,3 +124,56 @@ def event_listener(event): | |
| # Recorder uses SQLite and stores datetimes as integer unix timestamps | ||
| assert event.time_fired.replace(microsecond=0) == \ | ||
| db_event.time_fired.replace(microsecond=0) | ||
|
|
||
| def test_purge_old_states(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """Test deleting old states.""" | ||
| self._add_test_states() | ||
| # make sure we start with 5 states | ||
| states = recorder.query_states('SELECT * FROM states') | ||
| self.assertEqual(len(states), 5) | ||
|
|
||
| # run purge_old_data() | ||
| recorder._INSTANCE.purge_days = 4 | ||
| recorder._INSTANCE._purge_old_data() | ||
|
|
||
| # we should only have 2 states left after purging | ||
| states = recorder.query_states('SELECT * FROM states') | ||
| self.assertEqual(len(states), 2) | ||
|
|
||
| def test_purge_old_events(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have Zero occurrences of tests_ in the repo. leading me to believe that farcy is being a bit overzealous here...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So its in the comment not the code.. fixed |
||
| """Test deleting old events.""" | ||
| self._add_test_events() | ||
| events = recorder.query_events('SELECT * FROM events WHERE ' | ||
| 'event_type LIKE "EVENT_TEST%"') | ||
| self.assertEqual(len(events), 5) | ||
|
|
||
| # run purge_old_data() | ||
| recorder._INSTANCE.purge_days = 4 | ||
| recorder._INSTANCE._purge_old_data() | ||
|
|
||
| # now we should only have 3 events left | ||
| events = recorder.query_events('SELECT * FROM events WHERE ' | ||
| 'event_type LIKE "EVENT_TEST%"') | ||
| self.assertEqual(len(events), 3) | ||
|
|
||
| def test_purge_disabled(self): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| """Test leaving purge_days disabled.""" | ||
| self._add_test_states() | ||
| self._add_test_events() | ||
| # make sure we start with 5 states and events | ||
| states = recorder.query_states('SELECT * FROM states') | ||
| events = recorder.query_events('SELECT * FROM events WHERE ' | ||
| 'event_type LIKE "EVENT_TEST%"') | ||
| self.assertEqual(len(states), 5) | ||
| self.assertEqual(len(events), 5) | ||
|
|
||
| # run purge_old_data() | ||
| recorder._INSTANCE.purge_days = None | ||
| recorder._INSTANCE._purge_old_data() | ||
|
|
||
| # we should have all of our states still | ||
| states = recorder.query_states('SELECT * FROM states') | ||
| events = recorder.query_events('SELECT * FROM events WHERE ' | ||
| 'event_type LIKE "EVENT_TEST%"') | ||
| self.assertEqual(len(states), 5) | ||
| self.assertEqual(len(events), 5) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
farcy v1.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
358 occurrences of add, 9 of adds. Our style seems to be more add then adds...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So its in the comment not the code.. fixed