Esempio n. 1
0
def test_aws_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = AWS()
    record.name = '인천'
    record.height = 1234
    record.is_raining = True
    record.rain15 = 111.111
    record.rain60 = 222.222
    record.rain6h = 333.333
    record.rain12h = 444.444
    record.rainday = 555.555
    record.temperature = 12.34
    record.wind_direction1 = 'SSW'
    record.wind_speed1 = 11.11
    record.wind_direction10 = 'NNE'
    record.wind_speed10 = 22.22
    record.humidity = 55
    record.pressure = 1234.56
    record.location = '인천광역시 중구 전동'
    record.observed_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.observed_at == now_dt
    assert record.observed_datetime == now_dt.replace(tzinfo=None)
    assert record.observed_timezone is pytz.timezone('Asia/Seoul')
Esempio n. 2
0
def test_json_cache_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = JSONCache()
    record.name = 'test'
    record.body = 'Hello!'
    record.created_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.created_at == now_dt
    assert record.created_datetime == now_dt.astimezone(UTC)
    assert record.created_timezone == gettz('Asia/Seoul')
Esempio n. 3
0
def test_rss_feed_sub_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = RSSFeedURL()
    record.url = 'http://example.com'
    record.channel = 'C1'
    record.updated_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.updated_at == now_dt
    assert record.updated_datetime == now_dt.replace(tzinfo=None)
    assert record.updated_timezone == gettz('Asia/Seoul')
Esempio n. 4
0
def test_json_cache_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = JSONCache()
    record.name = 'test'
    record.body = 'Hello!'
    record.created_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.created_at == now_dt
    assert record.created_datetime == now_dt.replace(tzinfo=None)
    assert record.created_timezone is pytz.timezone('Asia/Seoul')
Esempio n. 5
0
def test_memo_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = Memo()
    record.keyword = 'test'
    record.text = 'test test'
    record.author = 'U1'
    record.created_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.created_at == now_dt
    assert record.created_datetime == now_dt.replace(tzinfo=None)
    assert record.created_timezone is pytz.timezone('Asia/Seoul')
Esempio n. 6
0
def test_memo_model_with_aware_dt(fx_sess):
    now_dt = now()

    record = Memo()
    record.keyword = 'test'
    record.text = 'test test'
    record.author = 'U1'
    record.created_at = now_dt

    with fx_sess.begin():
        fx_sess.add(record)

    assert record.created_at == now_dt
    assert record.created_datetime == now_dt.astimezone(UTC)
    assert record.created_timezone == gettz('Asia/Seoul')
Esempio n. 7
0
async def test_aws(fx_sess):
    bot = FakeBot()
    bot.add_channel('C1', 'general')
    bot.add_user('U1', 'item4')

    event = bot.create_message('C1', 'U1', ts='1234.56')

    await aws(bot, event, fx_sess, False, False, '서울')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == '날씨 정보 검색 준비가 진행중이에요. 잠시만 기다려주세요!'
    assert 'thread_ts' not in said.data

    cache = JSONCache()
    cache.name = 'aws'
    cache.body = {
        'observed_at':
        '2018-12-30T13:16:00+0900',
        'records': [
            {
                "id": 1,
                "name": "서울",
                "height": 1234,
                "rain": {
                    "is_raining": "Rain",
                    "rain15": 15.1,
                    "rain60": 60.2,
                    "rain3h": 180.3,
                    "rain6h": 360.4,
                    "rain12h": 720.5,
                    "rainday": 1440.6,
                },
                "temperature": 12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "서울특별시 가구",
            },
            {
                "id": 2,
                "name": "강남",
                "height": 1234,
                "rain": {
                    "is_raining": "Rain",
                    "rain15": 15.1,
                    "rain60": 60.2,
                    "rain3h": 180.3,
                    "rain6h": 360.4,
                    "rain12h": 720.5,
                    "rainday": 1440.6,
                },
                "temperature": -12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "서울특별시 강남구",
            },
            {
                "id": 2,
                "name": "가나",
                "height": 1234,
                "rain": {
                    "is_raining": "Clear",
                    "rain15": 0,
                    "rain60": 0,
                    "rain3h": 0,
                    "rain6h": 0,
                    "rain12h": 0,
                    "rainday": 0,
                },
                "temperature": 12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "지구 반대편 가나",
            },
        ]
    }
    cache.created_at = now()

    with fx_sess.begin():
        fx_sess.add(cache)

    # not found
    await aws(bot, event, fx_sess, False, False, '부천')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == '검색결과가 없어요!'
    assert 'thread_ts' not in said.data

    # rain
    await aws(bot, event, fx_sess, False, False, '서울')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@서울/서울특별시 가구]'
        ' 강수: 예(15min: 15.1/일일: 1440.6)'
        ' / 12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa')
    assert said.data['username'] == '서울 날씨'
    assert said.data['icon_emoji'] == ':umbrella_with_rain_drops:'
    assert 'thread_ts' not in said.data

    # snow
    await aws(bot, event, fx_sess, False, False, '강남')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@강남/서울특별시 강남구]'
        ' 강수: 예(15min: 15.1/일일: 1440.6)'
        ' / -12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa')
    assert said.data['username'] == '강남 날씨'
    assert said.data['icon_emoji'] == ':snowflake:'
    assert 'thread_ts' not in said.data

    # sun
    await aws(bot, event, fx_sess, False, False, '가나')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@가나/지구 반대편 가나]'
        ' 강수: 아니오'
        ' / 12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa')
    assert said.data['username'] == '가나 날씨'
    assert said.data['icon_emoji'] == ':sunny:'
    assert 'thread_ts' not in said.data

    # moon
    new_body = copy.deepcopy(cache.body)
    new_body['observed_at'] = '2018-12-30T22:16:00+0900'
    cache.body = new_body
    with fx_sess.begin():
        fx_sess.add(cache)

    await aws(bot, event, fx_sess, False, False, '가나')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 22시 16분@가나/지구 반대편 가나]'
        ' 강수: 아니오'
        ' / 12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa')
    assert said.data['username'] == '가나 날씨'
    assert said.data['icon_emoji'] == ':crescent_moon:'
    assert 'thread_ts' not in said.data

    # include address
    await aws(bot, event, fx_sess, False, True, '서울')

    assert len(bot.call_queue) == 2

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '서울 날씨'
    assert said.data['thread_ts'] == event.ts

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '강남 날씨'
    assert said.data['thread_ts'] == event.ts

    await aws(bot, event, fx_sess, False, True, '가나')

    assert len(bot.call_queue) == 1

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '가나 날씨'
    assert 'thread_ts' not in said.data

    # fuzzy
    await aws(bot, event, fx_sess, True, False, '강남')

    assert len(bot.call_queue) == 2

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '강남 날씨'
    assert said.data['thread_ts'] == event.ts

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '가나 날씨'
    assert said.data['thread_ts'] == event.ts

    await aws(bot, event, fx_sess, True, False, '서울')

    assert len(bot.call_queue) == 1

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '서울 날씨'
    assert 'thread_ts' not in said.data

    # fuzzy + include address
    await aws(bot, event, fx_sess, True, True, '서울')

    assert len(bot.call_queue) == 2

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '서울 날씨'
    assert said.data['thread_ts'] == event.ts

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '강남 날씨'
    assert said.data['thread_ts'] == event.ts

    await aws(bot, event, fx_sess, True, True, '가나')

    assert len(bot.call_queue) == 2

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '가나 날씨'
    assert said.data['thread_ts'] == event.ts

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['username'] == '강남 날씨'
    assert said.data['thread_ts'] == event.ts
Esempio n. 8
0
async def test_aws(fx_sess):
    bot = FakeBot()
    bot.add_channel('C1', 'general')
    bot.add_user('U1', 'item4')

    event = bot.create_message('C1', 'U1', ts='1234.56')

    await aws(bot, event, fx_sess, '서울')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == '날씨 정보 검색 준비가 진행중이에요. 잠시만 기다려주세요!'
    assert 'thread_ts' not in said.data

    cache = JSONCache()
    cache.name = 'aws'
    cache.body = {
        'observed_at': '2018-12-30T13:16:00+0900',
        'records': [
            {
                "id": 1,
                "name": "서울",
                "height": 1234,
                "rain": {
                    "is_raining": "Rain",
                    "rain15": 15.1,
                    "rain60": 60.2,
                    "rain3h": 180.3,
                    "rain6h": 360.4,
                    "rain12h": 720.5,
                    "rainday": 1440.6,
                },
                "temperature": 12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "서울특별시 가구",
            },
            {
                "id": 2,
                "name": "강남",
                "height": 1234,
                "rain": {
                    "is_raining": "Rain",
                    "rain15": 15.1,
                    "rain60": 60.2,
                    "rain3h": 180.3,
                    "rain6h": 360.4,
                    "rain12h": 720.5,
                    "rainday": 1440.6,
                },
                "temperature": -12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "서울특별시 강남구",
            },
            {
                "id": 2,
                "name": "강남2",
                "height": 1234,
                "rain": {
                    "is_raining": "Clear",
                    "rain15": 0,
                    "rain60": 0,
                    "rain3h": 0,
                    "rain6h": 0,
                    "rain12h": 0,
                    "rainday": 0,
                },
                "temperature": 12.34,
                "wind1": {
                    "direction_code": 999.9,
                    "direction_text": "SSW",
                    "velocity": 11.11,
                },
                "wind10": {
                    "direction_code": 888.8,
                    "direction_text": "NNE",
                    "velocity": 22.22,
                },
                "humidity": 55,
                "atmospheric": 2345.67,
                "address": "강남2",
            },
        ]
    }
    cache.created_at = now()

    with fx_sess.begin():
        fx_sess.add(cache)

    # short keyword
    await aws(bot, event, fx_sess, '1')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '검색어가 너무 짧아요! 2글자 이상의 검색어를 사용해주세요!'
    )
    assert 'thread_ts' not in said.data

    # not found
    await aws(bot, event, fx_sess, '부천')

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '검색결과가 없어요! 한국 기상청 AWS가 설치되지 않은 장소 같아요!'
    )
    assert 'thread_ts' not in said.data

    # found 1
    await aws(bot, event, fx_sess, '서울')

    assert len(bot.call_queue) == 1

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@서울/서울특별시 가구]'
        ' 강수: 예(15min: 15.1/일일: 1440.6)'
        ' / 12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa'
    )
    assert said.data['username'] == '서울 날씨'
    assert said.data['icon_emoji'] == ':umbrella_with_rain_drops:'
    assert 'thread_ts' not in said.data

    # found 2+
    await aws(bot, event, fx_sess, '강남')

    assert len(bot.call_queue) == 2

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@강남/서울특별시 강남구]'
        ' 강수: 예(15min: 15.1/일일: 1440.6)'
        ' / -12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa'
    )
    assert said.data['username'] == '강남 날씨'
    assert said.data['icon_emoji'] == ':snowflake:'
    assert said.data['thread_ts'] == '1234.56'

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert said.data['text'] == (
        '[2018년 12월 30일 13시 16분@강남2/강남2]'
        ' 강수: 아니오'
        ' / 12.34℃ / 바람: 남남서 11.11㎧ / 습도: 55% / 해면기압: 2345.67hPa'
    )
    assert said.data['username'] == '강남2 날씨'
    assert said.data['icon_emoji'] == ':sunny:'
    assert said.data['thread_ts'] == '1234.56'

    # hit cooltime
    new_body = copy.deepcopy(cache.body)
    for _ in range(5):
        new_body['records'] += new_body['records']
    cache.body = new_body
    with fx_sess.begin():
        fx_sess.add(cache)

    await aws(bot, event, fx_sess, '강남')

    assert len(bot.call_queue) > 5
    bot.call_queue.clear()
    assert aws.last_call['C1']

    # block by cooltime
    await aws(bot, event, fx_sess, '강남')

    assert len(bot.call_queue) == 1

    said = bot.call_queue.pop(0)
    assert said.method == 'chat.postMessage'
    assert said.data['channel'] == 'C1'
    assert COOLTIME.match(said.data['text'])

    assert 'thread_ts' not in said.data