Skip to content

Commit

Permalink
Fix off by 1 error in last sent dates
Browse files Browse the repository at this point in the history
  • Loading branch information
kelockhart committed Sep 11, 2020
1 parent 9e5b8dc commit e03612f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
5 changes: 4 additions & 1 deletion myadsp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from kombu import Queue
import os
import json
import datetime
from sqlalchemy.orm import exc as ormexc

# ============================= INITIALIZATION ==================================== #
Expand Down Expand Up @@ -71,7 +72,9 @@ def task_process_myads(message):

# first fetch the myADS setup from /vault/get-myads
if last_sent:
r = app.client.get(app.conf.get('API_VAULT_MYADS_SETUP_DATE') % (userid, last_sent),
# the start date should be one day after the last sent date, so the results don't overlap
start_date = last_sent + datetime.timedelta(days=1)
r = app.client.get(app.conf.get('API_VAULT_MYADS_SETUP_DATE') % (userid, start_date),
headers={'Accept': 'application/json',
'Authorization': 'Bearer {0}'.format(app.conf.get('API_TOKEN'))})
else:
Expand Down
66 changes: 66 additions & 0 deletions myadsp/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import httpretty
from mock import patch
import datetime
try:
from urllib.parse import quote_plus
except ImportError:
Expand Down Expand Up @@ -185,6 +186,7 @@ def test_task_process_myads(self):
payload_to_html.return_value = '<em>html payload</em>'
send_email.return_value = 'this should be a MIMEMultipart object'

# non-200 from solr, so task should get tried again
tasks.task_process_myads(msg)
self.assertTrue(rerun_task.called)

Expand Down Expand Up @@ -398,3 +400,67 @@ def test_task_process_myads(self):
user = session.query(AuthorInfo).filter_by(id=123).first()
self.assertEqual(adsputils.get_date().date(), user.last_sent_weekly.date())
self.assertIsNone(user.last_sent_daily)

# postdate the weekly last_sent date
with self.app.session_scope() as session:
user = session.query(AuthorInfo).filter_by(id=123).first()
user.last_sent_weekly = user.last_sent_weekly - datetime.timedelta(days=30)
last_sent_weekly = user.last_sent_weekly
session.add(user)
session.commit()

start_date = last_sent_weekly + datetime.timedelta(days=1)
uri = self.app.conf['API_VAULT_MYADS_SETUP_DATE'] % (msg['userid'], start_date)
httpretty.register_uri(
httpretty.GET, uri,
content_type='application/json',
status=200,
body=json.dumps([{'id': 1,
'name': 'Query 1',
'qid': '1234567890abcdefghijklmnopqrstu1',
'active': True,
'stateful': True,
'frequency': 'daily',
'type': 'query',
'template': None,
'query': [{'q': 'title:"gravity waves" ' +
'entdate:[2019-08-03 TO 2019-08-04] bibstem:"arxiv"',
'sort': 'score desc, bibcode desc'}]},
{'id': 2,
'name': 'Query 2',
'qid': None,
'active': True,
'stateful': False,
'frequency': 'weekly',
'type': 'template',
'template': 'authors',
'data': {'data': 'author:Kurtz'},
'query': [{
'q': 'author:Kurtz entdate:["2020-01-01Z00:00" TO "2020-01-01Z23:59"] pubdate:[2019-00 TO *]',
'sort': 'score desc, bibcode desc'}]},
{'id': 3,
'name': 'Query 3',
'qid': None,
'active': True,
'stateful': True,
'frequency': 'daily',
'type': 'template',
'template': 'arxiv',
'data': 'star',
'classes': ['astro-ph'],
'query': [{'q': 'bibstem:arxiv (arxiv_class:(astro-ph.*) (star)) '
'entdate:["2020-01-01Z00:00" TO "2020-01-01Z23:59"] pubdate:[2019-00 TO *]',
'sort': 'score desc, bibcode desc'},
{'q': 'bibstem:arxiv (arxiv_class:(astro-ph.*) NOT (star)) '
'entdate:["2020-01-01Z00:00" TO "2020-01-01Z23:59"] pubdate:[2019-00 TO *]',
'sort': 'bibcode desc'}]
}
])
)

tasks.task_process_myads(msg)

with self.app.session_scope() as session:
user = session.query(AuthorInfo).filter_by(id=123).first()
self.assertEqual(adsputils.get_date().date(), user.last_sent_weekly.date())
self.assertIsNone(user.last_sent_daily)

0 comments on commit e03612f

Please sign in to comment.