Added tests for user_items filter urls.
This commit is contained in:
parent
fb0f6d7ccd
commit
fbfd4932e6
106
tests.py
106
tests.py
@ -1,3 +1,5 @@
|
|||||||
|
import codecs
|
||||||
|
import ujson as json
|
||||||
import unittest
|
import unittest
|
||||||
from wanikaniburned import app, db, User, datetime_format, parse_range
|
from wanikaniburned import app, db, User, datetime_format, parse_range
|
||||||
from flask.ext.testing import TestCase
|
from flask.ext.testing import TestCase
|
||||||
@ -93,5 +95,109 @@ class TestFormats(unittest.TestCase):
|
|||||||
self.assertEqual(parse_range("1,4-6"), [1, 4, 5, 6])
|
self.assertEqual(parse_range("1,4-6"), [1, 4, 5, 6])
|
||||||
|
|
||||||
|
|
||||||
|
class TestUserItems(BaseTestCase):
|
||||||
|
|
||||||
|
with codecs.open('testuserdata.json', encoding='utf-8') as input_file:
|
||||||
|
user_data = json.load(input_file)
|
||||||
|
user_radicals = user_data['radicals']
|
||||||
|
user_kanji = user_data['kanji']
|
||||||
|
user_vocabulary = user_data['vocabulary']
|
||||||
|
|
||||||
|
def get_user(self):
|
||||||
|
user = User('test', False)
|
||||||
|
user.radicals = json.dumps(self.user_radicals, ensure_ascii=False)
|
||||||
|
user.kanji = json.dumps(self.user_kanji, ensure_ascii=False)
|
||||||
|
user.vocabulary = json.dumps(self.user_vocabulary, ensure_ascii=False)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def login(self, user):
|
||||||
|
return self.client.post('/', data={'api_key': user.api_key})
|
||||||
|
|
||||||
|
def count_filter(self, item_list, item_type=None, level_range=list(range(0, 61))):
|
||||||
|
if item_type is None:
|
||||||
|
item_type = ['burned']
|
||||||
|
return sum(1 for item in item_list if item['user_specific'] and item['user_specific']['srs'] in item_type
|
||||||
|
and item['level'] in level_range)
|
||||||
|
|
||||||
|
def flatten_list_to_string(self, input_list):
|
||||||
|
return ",".join(map(str, input_list))
|
||||||
|
|
||||||
|
def test_count_default(self):
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items')
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary))
|
||||||
|
|
||||||
|
def test_filter_single_type(self):
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?item_types=radical')
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals))
|
||||||
|
self.assertEqual(response['kanji_count'], 0)
|
||||||
|
self.assertEqual(response['vocabulary_count'], 0)
|
||||||
|
|
||||||
|
def test_filter_double_type(self):
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?item_types=radical,kanji')
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji))
|
||||||
|
self.assertEqual(response['vocabulary_count'], 0)
|
||||||
|
|
||||||
|
def test_filter_all_type(self):
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?item_types=radical,kanji,vocab')
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary))
|
||||||
|
|
||||||
|
def test_filter_single_level(self):
|
||||||
|
test_data = [1]
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?level_range=' + (self.flatten_list_to_string(test_data)))
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals, level_range=test_data))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji, level_range=test_data))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary, level_range=test_data))
|
||||||
|
|
||||||
|
def test_filter_multiple_levels(self):
|
||||||
|
test_data = [1, 2, 4]
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?level_range=' + (self.flatten_list_to_string(test_data)))
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals, level_range=test_data))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji, level_range=test_data))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary, level_range=test_data))
|
||||||
|
|
||||||
|
def test_filter_single_state(self):
|
||||||
|
test_data = ['enlighten']
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?item_state=' + (self.flatten_list_to_string(test_data)))
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals, test_data))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji, test_data))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary, test_data))
|
||||||
|
|
||||||
|
def test_filter_multiple_state(self):
|
||||||
|
test_data = ['guru', 'master', 'apprentice']
|
||||||
|
user = self.get_user()
|
||||||
|
self.login(user)
|
||||||
|
response = self.client.get('/user_items?item_state=' + (self.flatten_list_to_string(test_data)))
|
||||||
|
response = json.loads(response.data)
|
||||||
|
self.assertEqual(response['radical_count'], self.count_filter(self.user_radicals, test_data))
|
||||||
|
self.assertEqual(response['kanji_count'], self.count_filter(self.user_kanji, test_data))
|
||||||
|
self.assertEqual(response['vocabulary_count'], self.count_filter(self.user_vocabulary, test_data))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user