From 18666199b780aedaed75d21ffeb1b081316537bb Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Sat, 21 Dec 2019 02:13:15 +0900 Subject: [PATCH] Add test SpeedCalculator --- tests/test_speed_calculator.py | 54 +++++++ tests/testdata/speed/speedtest1.json | 188 +++++++++++++++++++++++ tests/testdata/speed/speedtest_none.json | 42 +++++ 3 files changed, 284 insertions(+) create mode 100644 tests/test_speed_calculator.py create mode 100644 tests/testdata/speed/speedtest1.json create mode 100644 tests/testdata/speed/speedtest_none.json diff --git a/tests/test_speed_calculator.py b/tests/test_speed_calculator.py new file mode 100644 index 0000000..69a902c --- /dev/null +++ b/tests/test_speed_calculator.py @@ -0,0 +1,54 @@ +import json +import pytest +import asyncio,aiohttp +from pytchat.parser.live import Parser +from pytchat.processors.compatible.processor import CompatibleProcessor +from pytchat.exceptions import ( + NoLivechatRendererException,NoYtinitialdataException, + ResponseContextError, NoContentsException) + +from pytchat.processors.speed_calculator import SpeedCalculator + +parser = Parser() + +def test_speed_1(mocker): + '''test speed normal + test json has 15 chatdata, duration is 30 seconds, + so the speed of chatdata is 30 chats/minute. + ''' + + processor = SpeedCalculator(capacity=30,video_id="") + + _json = _open_file("tests/testdata/speed/speedtest1.json") + + _, chatdata = parser.parse(json.loads(_json)) + data = { + "video_id" : "", + "timeout" : 10, + "chatdata" : chatdata + } + ret = processor.process([data]) + assert 30 == ret + +def test_speed_2(mocker): + '''test speed with no valid chat data + + ''' + + processor = SpeedCalculator(capacity=30,video_id="") + + _json = _open_file("tests/testdata/speed/speedtest_none.json") + + _, chatdata = parser.parse(json.loads(_json)) + data = { + "video_id" : "", + "timeout" : 10, + "chatdata" : chatdata + } + ret = processor.process([data]) + assert 0 == ret + + +def _open_file(path): + with open(path,mode ='r',encoding = 'utf-8') as f: + return f.read() diff --git a/tests/testdata/speed/speedtest1.json b/tests/testdata/speed/speedtest1.json new file mode 100644 index 0000000..f2e25a5 --- /dev/null +++ b/tests/testdata/speed/speedtest1.json @@ -0,0 +1,188 @@ +{ + "timing": { + "info": { + "st": 164 + } + }, + "csn": "", + "response": { + "responseContext": { + }, + "continuationContents": { + "liveChatContinuation": { + "continuations": [{ + "timedContinuationData": { + "timeoutMs": 10000, + "continuation": "continuation" + } + }], + "actions": [{ + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000000000000" + + } + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatTextMessageRenderer": { + + "timestampUsec": "1500000030000000" + + } + } + } + } + ]} + } + } +} + \ No newline at end of file diff --git a/tests/testdata/speed/speedtest_none.json b/tests/testdata/speed/speedtest_none.json new file mode 100644 index 0000000..de6528d --- /dev/null +++ b/tests/testdata/speed/speedtest_none.json @@ -0,0 +1,42 @@ +{ + "timing": { + "info": { + "st": 164 + } + }, + "csn": "", + "response": { + "responseContext": { + }, + "continuationContents": { + "liveChatContinuation": { + "continuations": [{ + "timedContinuationData": { + "timeoutMs": 10000, + "continuation": "continuation" + } + }], + "actions": [{ + "addChatItemAction": { + "liveChatPlaceholderItemRenderer": { + "id": "", + "timestampUsec": "1500000000000000" + } + } + }, + { + "addChatItemAction": { + "item": { + "liveChatPlaceholderItemRenderer": { + "id": "", + "timestampUsec": "1500000030000000" + + } + } + } + } + ]} + } + } +} + \ No newline at end of file