

import pytest
from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config
from whylogs.app.config import SessionConfig
from whylogs.app.session import Session
from pandas import util


def test_get_global_session():
    _session = None
    session = get_or_create_session()

    global_session = get_session()

    assert session == global_session


def test_reset():

    session = get_or_create_session()
    reset_default_session()
    global_session = get_session()
    assert global_session.project is not None


def test_session_log_dataframe():
    _session = None

    session = session_from_config(SessionConfig(
        "default-project", "default-pipeline", [], False
    ))
    df = util.testing.makeDataFrame()
    profile = session.log_dataframe(df)

    assert session.logger() is not None

    assert session.logger("default-project").dataset_name == "default-project"


def test_session_profile():

    session = session_from_config(SessionConfig(
        "default-project", "default-pipeline", [], False
    ))
    df = util.testing.makeDataFrame()
    profile = session.log_dataframe(df)
    assert profile is not None

    summary = profile.flat_summary()

    flat_summary = summary['summary']
    assert len(flat_summary) == 4


def test_profile_df():
    session = get_or_create_session()
    df = util.testing.makeDataFrame()
    log_profile = session.log_dataframe(df)
    profile = session.profile_dataframe(df)

    assert log_profile.name == profile.name
    assert log_profile.dataset_timestamp == profile.dataset_timestamp
    assert log_profile.session_timestamp == profile.session_timestamp

    assert len(profile.columns) == 4
    assert len(log_profile.tags) == 1
    assert len(profile.tags) == 2


def test_close_session():
    session = get_or_create_session()
    session.close()
    assert session.is_active() == False
    df = util.testing.makeDataFrame()
    log_profile = session.log_dataframe(df)
    assert log_profile == None
    profile = session.profile_dataframe(df)
    assert profile == None
    profile = session.new_profile(df)
    assert profile == None

    with pytest.raises(RuntimeError):
        session.logger()


def test_logger_cache():
    _session = None
    session = get_or_create_session()
    with session.logger("cache-test", with_rotation_time="s") as logger:
        logger.log({"name": 1})
    session.close()


def test_remove_logger():
    session = get_or_create_session()
    session.logger("default-project")
    with pytest.raises(KeyError):
        session.remove_logger("test")

[((12, 14, 12, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((14, 21, 14, 34), 'whylogs.app.session.get_session', 'get_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((21, 14, 21, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((22, 4, 22, 27), 'whylogs.app.session.reset_default_session', 'reset_default_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((23, 21, 23, 34), 'whylogs.app.session.get_session', 'get_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((33, 9, 33, 37), 'pandas.util.testing.makeDataFrame', 'util.testing.makeDataFrame', ({}, {}), '()', False, 'from pandas import util\n'), ((46, 9, 46, 37), 'pandas.util.testing.makeDataFrame', 'util.testing.makeDataFrame', ({}, {}), '()', False, 'from pandas import util\n'), ((57, 14, 57, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((58, 9, 58, 37), 'pandas.util.testing.makeDataFrame', 'util.testing.makeDataFrame', ({}, {}), '()', False, 'from pandas import util\n'), ((72, 14, 72, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((75, 9, 75, 37), 'pandas.util.testing.makeDataFrame', 'util.testing.makeDataFrame', ({}, {}), '()', False, 'from pandas import util\n'), ((89, 14, 89, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((96, 14, 96, 37), 'whylogs.app.session.get_or_create_session', 'get_or_create_session', ({}, {}), '()', False, 'from whylogs.app.session import get_or_create_session, get_session, get_logger, reset_default_session, session_from_config\n'), ((30, 34, 32, 5), 'whylogs.app.config.SessionConfig', 'SessionConfig', ({(31, 8, 31, 25): '"""default-project"""', (31, 27, 31, 45): '"""default-pipeline"""', (31, 47, 31, 49): '[]', (31, 51, 31, 56): 'False'}, {}), "('default-project', 'default-pipeline', [], False)", False, 'from whylogs.app.config import SessionConfig\n'), ((43, 34, 45, 5), 'whylogs.app.config.SessionConfig', 'SessionConfig', ({(44, 8, 44, 25): '"""default-project"""', (44, 27, 44, 45): '"""default-pipeline"""', (44, 47, 44, 49): '[]', (44, 51, 44, 56): 'False'}, {}), "('default-project', 'default-pipeline', [], False)", False, 'from whylogs.app.config import SessionConfig\n'), ((83, 9, 83, 36), 'pytest.raises', 'pytest.raises', ({(83, 23, 83, 35): 'RuntimeError'}, {}), '(RuntimeError)', False, 'import pytest\n'), ((98, 9, 98, 32), 'pytest.raises', 'pytest.raises', ({(98, 23, 98, 31): 'KeyError'}, {}), '(KeyError)', False, 'import pytest\n')]