Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion GeoHealthCheck/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def flush_runs():
all_runs = Run.query.all()
run_count = 0
for run in all_runs:
days_old = (datetime.now(timezone.utc) - run.checked_datetime).days
days_old = (datetime.now(timezone.utc) -
run.checked_datetime.replace(tzinfo=timezone.utc)).days
if days_old > retention_days:
run_count += 1
DB.session.delete(run)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ load-data = "invoke load-data"
drop-data = "invoke drop-data"
create-secret-key = "invoke create-secret-key"
run = "python GeoHealthCheck/app.py"
flush-runs = "python GeoHealthCheck/models.py flush"
docs = "invoke refresh-docs"
clean = "invoke clean"
runner-daemon = "invoke runner-daemon"
Expand Down
9 changes: 9 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ def drop_data(c):
c.run(f'python {models_py} drop')


@task
def flush_runs(c):
"""delete Runs older than retention time from database"""

models_py = Path('GeoHealthCheck/models.py')

c.run(f'python {models_py} flush')


@task
def create_hash(c, password):
"""Create hash, mainly for passwords"""
Expand Down
52 changes: 45 additions & 7 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@

import unittest
import os
from datetime import timedelta

from init import App
from models import (DB, Resource, Run, load_data, Recipient)
from models import (DB, Resource, Run, load_data, flush_runs, Recipient)
from healthcheck import run_test_resource
from notifications import _parse_webhook_location
from resourceauth import ResourceAuth
Expand Down Expand Up @@ -89,6 +90,43 @@ def testRunResoures(self):
'Run should be success for %s report=%s' %
(resource.url, str(resource.runs[0])))

def testFlushRuns(self):
# Do the one healthcheck for one Resource.
resource = Resource.query.first()
result = run_test_resource(resource)
print('resource: %s result=%s' % (resource.url, result.success))
run = Run(resource, result)

print('Adding Run: success=%s, response_time=%ss\n'
% (str(run.success), run.response_time))
self.db.session.add(run)
self.db.session.commit()
self.db.session.close()

flush_runs()

# Verify
resource = Resource.query.first()
# Resource should have one recent Run
self.assertEqual(
resource.runs.count(), 1,
'RunCount should be 1 for %s' % resource.url)

run = resource.runs.first()

# Outdate the Run
run.checked_datetime = run.checked_datetime - timedelta(days=365)
self.db.session.add(run)
self.db.session.commit()
self.db.session.close()
flush_runs()
# Verify
resource = Resource.query.first()
# Resource should have one recent Run
self.assertEqual(
resource.runs.count(), 0,
'RunCount should be 0 for %s' % resource.url)

def testNotificationsApi(self):
Rcp = Recipient
test_emails = ['test@test.com', 'other@test.com', 'unused@test.com']
Expand Down Expand Up @@ -157,7 +195,7 @@ def testSetGetResoureAuth(self):
'data': {
'username': 'the_user',
'password': 'the_password'
}
}
}

resource.auth = auth_dict
Expand All @@ -172,7 +210,7 @@ def testSetGetResoureAuth(self):
'type': 'Bearer Token',
'data': {
'token': 'a8KeTFOceitnRWT3M2rt'
}
}
}

resource.auth = auth_dict
Expand Down Expand Up @@ -204,7 +242,7 @@ def testResoureAuthPlugins(self):
'data': {
'username': 'the_user',
'password': 'the_password'
}
}
}

auth_obj = ResourceAuth.create(auth_dict)
Expand All @@ -223,7 +261,7 @@ def testResoureAuthPlugins(self):
'data': {
'username': '',
'password': ''
}
}
}
auth_obj = ResourceAuth.create(auth_dict)
self.assertEqual(auth_obj.verify(), False)
Expand All @@ -233,7 +271,7 @@ def testResoureAuthPlugins(self):
'data': {
'username': None,
'password': None
}
}
}
auth_obj = ResourceAuth.create(auth_dict)
self.assertEqual(auth_obj.verify(), False)
Expand All @@ -243,7 +281,7 @@ def testResoureAuthPlugins(self):
'type': 'Bearer Token',
'data': {
'token': 'a8KeTFOceitnRWT3M2rt'
}
}
}

auth_obj = ResourceAuth.create(auth_dict)
Expand Down
Loading