Merge pull request #151 from EverythingMe/feature_data_source

Fix issue with serializing unicode queries.
This commit is contained in:
Arik Fraimovich 2014-03-24 14:31:13 +02:00
commit 2ba4bcd98e
3 changed files with 24 additions and 4 deletions

View File

@ -78,6 +78,8 @@ class Manager(object):
def refresh_queries(self): def refresh_queries(self):
# TODO: this will only execute scheduled queries that were executed before. I think this is # TODO: this will only execute scheduled queries that were executed before. I think this is
# a reasonable assumption, but worth revisiting. # a reasonable assumption, but worth revisiting.
# TODO: move this logic to the model.
outdated_queries = models.Query.select(peewee.Func('first_value', models.Query.id)\ outdated_queries = models.Query.select(peewee.Func('first_value', models.Query.id)\
.over(partition_by=[models.Query.query_hash, models.Query.data_source]))\ .over(partition_by=[models.Query.query_hash, models.Query.data_source]))\
.join(models.QueryResult)\ .join(models.QueryResult)\
@ -116,6 +118,7 @@ class Manager(object):
logging.info("[Manager][%s] Inserted query data; id=%s", query_hash, query_result.id) logging.info("[Manager][%s] Inserted query data; id=%s", query_hash, query_result.id)
# TODO: move this logic to the model?
updated_count = models.Query.update(latest_query_data=query_result).\ updated_count = models.Query.update(latest_query_data=query_result).\
where(models.Query.query_hash==query_hash, models.Query.data_source==data_source_id).\ where(models.Query.query_hash==query_hash, models.Query.data_source==data_source_id).\
execute() execute()

View File

@ -80,6 +80,13 @@ class RedisObject(object):
return obj return obj
def fix_unicode(string):
if isinstance(string, unicode):
return string
return string.decode('utf-8')
class Job(RedisObject): class Job(RedisObject):
HIGH_PRIORITY = 1 HIGH_PRIORITY = 1
LOW_PRIORITY = 2 LOW_PRIORITY = 2
@ -108,7 +115,7 @@ class Job(RedisObject):
} }
conversions = { conversions = {
'query': lambda query: query.decode('utf-8'), 'query': fix_unicode,
'priority': int, 'priority': int,
'updated_at': float, 'updated_at': float,
'status': int, 'status': int,
@ -121,7 +128,7 @@ class Job(RedisObject):
name = 'job' name = 'job'
def __init__(self, redis_connection, query, priority, **kwargs): def __init__(self, redis_connection, query, priority, **kwargs):
kwargs['query'] = query kwargs['query'] = fix_unicode(query)
kwargs['priority'] = priority kwargs['priority'] = priority
kwargs['query_hash'] = gen_query_hash(kwargs['query']) kwargs['query_hash'] = gen_query_hash(kwargs['query'])
self.new_job = 'id' not in kwargs self.new_job = 'id' not in kwargs

View File

@ -1,3 +1,5 @@
# coding=utf-8
import time import time
from unittest import TestCase from unittest import TestCase
from mock import patch from mock import patch
@ -86,5 +88,13 @@ class TestJob(TestCase):
self.assertEquals(now - updated_at, job.query_time) self.assertEquals(now - updated_at, job.query_time)
self.assertIsNone(job.error) self.assertIsNone(job.error)
def test_done_failed(self): def test_unicode_serialization(self):
pass unicode_query = u"יוניקוד"
job = Job(redis_connection, query=unicode_query, priority=self.priority)
self.assertEquals(job.query, unicode_query)
job.save()
loaded_job = Job.load(redis_connection, job.id)
self.assertEquals(loaded_job.query, unicode_query)