track_url with same parameters returns original id

This commit is contained in:
James Turk 2011-09-28 15:50:32 -04:00
parent da1a3385e1
commit 51480fafcc
2 changed files with 20 additions and 6 deletions

View File

@ -84,9 +84,19 @@ class Client(object):
url url
URL to start tracking URL to start tracking
""" """
if self.db.tracked.find_one({'url': url}): tracked = self.db.tracked.find_one({'url': url})
self.log('track', url=url, error='already tracked')
raise ValueError('%s is already tracked' % url) # if data is already tracked and this is just a duplicate call
# return the original object
if tracked:
if (tracked['metadata'] == kwargs and
tracked['versioning'] == versioning and
tracked['update_mins'] == update_mins):
return tracked['_id']
else:
self.log('track', url=url, error='tracking conflict')
raise ValueError('%s is already tracked with different '
'metadata' % url)
self.log('track', url=url) self.log('track', url=url)
return self.db.tracked.insert(dict(url=url, versioning=versioning, return self.db.tracked.insert(dict(url=url, versioning=versioning,

View File

@ -42,7 +42,7 @@ class ClientTests(TestCase):
def test_track_url(self): def test_track_url(self):
# basic insert # basic insert
self.client.track_url('http://example.com', update_mins=30, pi=3) id1 = self.client.track_url('http://example.com', update_mins=30, pi=3)
obj = self.client.db.tracked.find_one() obj = self.client.db.tracked.find_one()
assert '_random' in obj assert '_random' in obj
assert obj['update_mins'] == 30 assert obj['update_mins'] == 30
@ -53,11 +53,15 @@ class ClientTests(TestCase):
assert log['action'] == 'track' assert log['action'] == 'track'
assert log['url'] == 'http://example.com' assert log['url'] == 'http://example.com'
# can't track same URL twice # track same url again with same metadata returns id
id2 = self.client.track_url('http://example.com', update_mins=30, pi=3)
assert id1 == id2
# can't track same URL twice with different metadata
assert_raises(ValueError, self.client.track_url, 'http://example.com') assert_raises(ValueError, self.client.track_url, 'http://example.com')
# logged error # logged error
assert self.client.db.logs.find_one({'error': 'already tracked'}) assert self.client.db.logs.find_one({'error': 'tracking conflict'})
def test_md5_versioning(self): def test_md5_versioning(self):