def test_get(self): context = TestContext() record_a = context.new_record(work_title='a') post_a = context.new_post(record_a['id'], comment='!') record_b = context.new_record(work_title='b') post_b = context.new_post(record_b['id'], comment='!') context2 = TestContext() record_a2 = context2.new_record(work_title='a') post_a2 = context2.new_post(record_a2['id'], comment='!') path = '/api/v2/posts' response = self.client.get(path, {'count': 2}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 2) self.assertEqual(response.json()[0]['id'], post_a2['id']) self.assertEqual(response.json()[1]['id'], post_b['id']) # Test before_id response = self.client.get(path, {'before_id': post_b['id']}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 1) self.assertEqual(response.json()[0]['id'], post_a['id']) # Test min_record_count indexer.run() # We need index to use this feature response = self.client.get(path, {'min_record_count': 2}) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 2) self.assertEqual(response.json()[0]['id'], post_a2['id']) self.assertEqual(response.json()[1]['id'], post_a['id'])
def test_get(self): context = TestContext() title = 'Fate/stay night' alt_title = 'Fate stay night' record = context.new_record(work_title=title) path = '/api/v2/works/%s' % record['work_id'] post = context.new_post(record['id'], status=u'1', comment=u'comment') for x in range(3): context2 = TestContext() record2 = context2.new_record(work_title=alt_title) post2 = context.new_post(record['id'], status=u'2', comment='') # Get by ID response = self.client.get(path) self.assertEqual(response.status_code, 200) work = response.json() expected = { 'id': record['work_id'], 'title': u'Fate/stay night', 'episodes': [ {'number': 1, 'post_count': 1}, {'number': 2} ], 'alt_titles': [u'Fate stay night'], 'record_count': 4, } self.assertEqual(work, expected) # Should have rank after indexing indexer.run() response = self.client.get(path) self.assertEqual(response.status_code, 200) work = response.json() expected['rank'] = 1 self.assertEqual(work, expected) # Should have record if logged in response = context.get(path) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['record']['id'], record['id']) # If logged in and no record should not have record field context3 = TestContext() response = context3.get(path) self.assertEqual(response.status_code, 200) self.assertTrue('record' not in response.json()) # Lookup by title response = self.client.get('/api/v2/works/_/%s' % title) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), work) response = self.client.get('/api/v2/works/_/%s' % alt_title) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), work)
def test_get(self): context = TestContext() work1_id = context.new_record(work_title='A')['work_id'] work1 = Work.objects.get(id=work1_id) work1.raw_metadata = u'''periods: ["2015Q2"] id: oregairu2 title: 역시 내 청춘 러브코메디는 잘못됐다 속 namu_ref: 역시 내 청춘 러브코메디는 잘못됐다 schedule: - 04-03 01:46 #금 - TBS schedule_kr: - 04-08 23:00 #수 - ANIMAX source: lightnovel studio: Feel ann_id: 16329 image: ann16329.jpg website: http://www.tbs.co.jp/anime/oregairu/''' work1.metadata = yaml.load(work1.raw_metadata) work1.save() work2_id = context.new_record(work_title='B')['work_id'] work2 = Work.objects.get(id=work2_id) work2.raw_metadata = u'''periods: ["2015Q3"] id: gatchaman-crowds-insight title: 갓챠맨 크로우즈 인사이트 namu_ref: GATCHAMAN CROWDS schedule: - 07-05 01:55 #일 - NTV schedule_kr: - 07-08 23:00 #수 - ANIPLUS source: original studio: 타츠노코 website: http://www.ntv.co.jp/GC_insight/ ann_id: 16697 image: ann16697.jpg''' work2.metadata = yaml.load(work2.raw_metadata) work2.save() work3_id = context.new_record(work_title='C')['work_id'] work3 = Work.objects.get(id=work3_id) work3.raw_metadata = u'''periods: ["2015Q1", "2015Q3"] id: 059239ce ann_id: 16098 title: THE IDOLM@STER 신데렐라 걸즈 namu_ref: 아이돌 마스터 신데렐라 걸즈/애니메이션 schedule: #- 01-10 00:00 #Sat - 07-18 00:00 #토 - MX schedule_kr: #- 01-20 22:30 #Tue - 07-28 22:30 #화 - ANIPLUS image: ann16098.jpg source: game studio: A-1 픽쳐스 website: http://imas-cinderella.com/''' work3.metadata = yaml.load(work3.raw_metadata) work3.save() indexer.run() path = '/api/v2/table/periods/2015Q3' response = self.client.get(path) self.assertEqual(response.status_code, 200) self.assertEqual([work['id'] for work in response.json()], [work2_id, work3_id]) response = self.client.get(path, {'only_first_period': 'true'}) self.assertEqual(response.status_code, 200) self.assertEqual([work['id'] for work in response.json()], [work2_id]) context2 = TestContext() record = context2.new_record(work_title=work2.title) response = context2.get(path) self.assertEqual(response.status_code, 200) self.assertEqual([work.get('record') for work in response.json()], [record, None])
def handle(self, *args, **options): indexer.run()
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'animeta.settings' import sys sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../..')) sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/..')) import django; django.setup() from django.conf import settings from search import indexer from search.models import WorkPeriodIndex from table.models import item_json, Period period = '2015Q2' indexer.run() indexes = WorkPeriodIndex.objects.filter(period=period).all() for index in indexes: print '---' print index.work.raw_metadata