Skip to content

Commit

Permalink
added test to global dictionary, fixed small bugs.
Browse files Browse the repository at this point in the history
issue #6
  • Loading branch information
i-pavlov committed Apr 5, 2014
1 parent 01c3b6d commit db642f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
8 changes: 3 additions & 5 deletions handlers/global_dictionary_word_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
from handlers.base_handlers.service_request_handler import ServiceRequestHandler


def make_timestamp():
return int(1000 * time.time())

StrategyTypeChooseConstant = 200


Expand Down Expand Up @@ -72,7 +69,8 @@ def post(self):
max_timestamp = max(max_timestamp, word_time)
downloaded_word = ndb.gql(u"SELECT * from GlobalDictionaryWord WHERE word = '{0}'".format(word.word)).get()
word_list.append({"word": word.word, "E": downloaded_word.E, "D": downloaded_word.D, "U": downloaded_word.used_times, "tags": downloaded_word.tags})
GlobalDictionaryJson(json=json.dumps(word_list), timestamp=max_timestamp).put()
if word_list != []:
GlobalDictionaryJson(json=json.dumps(word_list), timestamp=max_timestamp).put()


class UpdateAllJsonsHandler(AdminRequestHandler):
Expand Down Expand Up @@ -128,8 +126,8 @@ def get(self, *args, **kwargs):
response_json = {"words":[]}
for diff_json in ndb.gql("SELECT timestamp FROM GlobalDictionaryJson "
"ORDER BY timestamp"):
max_timestamp = max(max_timestamp, diff_json.timestamp)
if diff_json.timestamp > device_timestamp:
max_timestamp = max(max_timestamp, diff_json.timestamp)
for res_json in ndb.gql("SELECT * FROM GlobalDictionaryJson "
"WHERE timestamp = {0} "
"ORDER BY timestamp".format(diff_json.timestamp)):
Expand Down
27 changes: 25 additions & 2 deletions tests/global_dictionary_word_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from objects.global_dictionary_word import GlobalDictionaryWord
from objects.GlobalDictionaryJSON import GlobalDictionaryJson
from google.appengine.ext import testbed
from google.appengine.ext import deferred
from google.appengine.ext import ndb
from tests.base_functions import *
import unittest2

import time
import main
import json
import base64
Expand Down Expand Up @@ -93,6 +93,29 @@ def test_add(self):
break
self.assertTrue(ok)

request = make_request("/api/global_dictionary/get_words/{0}".format(timestamp), "GET", True, '0')
response = request.get_response(main.app)
self.assertEqual(json.loads(response.body)["words"], [])
self.assertEqual(json.loads(response.body)["timestamp"], timestamp)
time.sleep(0.01)
request = make_request("/admin/global_dictionary/add_words", "POST", True, 'json=["f", "g", "h"]')
request.get_response(main.app)
task_response = self.run_tasks(1)
self.assertEqual(task_response[0].status_int, 200)

request = make_request("/admin/global_dictionary/update_json", "POST", True, '0')
request.get_response(main.app)
task_response = self.run_tasks(1)
self.assertEqual(task_response[0].status_int, 200)

self.assertEqual(GlobalDictionaryWord.query().count(), 7)
self.assertEqual(GlobalDictionaryJson.query().count(), 2)

request = make_request("/api/global_dictionary/get_words/0", "GET", True, '0')
response = request.get_response(main.app)

self.assertEqual(len(json.loads(response.body)["words"]), 7)

def test_more(self):
request = make_request("/admin/global_dictionary/add_words",
"POST", True,
Expand Down

0 comments on commit db642f3

Please sign in to comment.