import pytest, os
from click.testing import CliRunner

import wandb, yaml, sys, os, argparse

@pytest.fixture
def config():
    return wandb.Config()

def init():
    os.mkdir(os.getcwd()+"/.wandb")
    return wandb.Config()

def test_empty_config(config):
    config.foo = 'bar'
    assert dict(config)['foo'] == 'bar'

def test_init_with_argparse():
    #TODO: this makes other tests dirty.
    p = argparse.ArgumentParser()
    p.add_argument("--foo")
    with CliRunner().isolated_filesystem():
        init()
        sys.argv = ["foo", "--foo=bar"]
        config = wandb.Config(p.parse_args())
        sys.argv = []
        assert config.foo == "bar"
        assert open(".wandb/latest.yaml").read() == """wandb_version: 1

foo:
  desc: null
  value: bar
"""

def test_persist_initial():
    with CliRunner().isolated_filesystem():
        config = init()
        config.foo = "bar"
        config.persist()
        assert dict(wandb.Config())['foo'] == 'bar'

def test_invalid_yaml():
    with CliRunner().isolated_filesystem():
        with open("config.yaml", "w") as f:
            f.write("{{a1932 }")
        with pytest.raises(wandb.Error):
            wandb.Config()

def test_persist_existing():
    with CliRunner().isolated_filesystem():
        config = init()
        config.foo = "bar"
        config.persist()
        config.foo = "baz"
        config.persist()
        assert dict(wandb.Config())['foo'] == 'baz'

def test_persist_overrides(config):
    with CliRunner().isolated_filesystem():
        config = init()
        config.foo = "bar"
        assert(yaml.load(open(".wandb/latest.yaml"))) == {'wandb_version': 1, 'foo': {'desc': None, 'value': 'bar'}}
    
def test_env_override():
    with CliRunner().isolated_filesystem():
        config = init()
        config.foo = "bar"
        os.environ['WANDB_SPECIAL'] = "baz"
        assert dict(wandb.Config())['special'] == 'baz'
        del os.environ['WANDB_SPECIAL']

def test_converts_env():
    with CliRunner().isolated_filesystem():
        os.environ['WANDB_INT'] = "1"
        conf = wandb.Config()
        print(dict(conf))
        assert conf.int == 1
        del os.environ['WANDB_INT']

def test_allows_getiter(config):
    config.foo = "bar"
    assert config['foo'] == "bar"

def test_repr_with_desc(config):
    config.foo = "bar"
    config.foo_desc = "Fantastic"
    assert 'Fantastic' in "%r" % config

def test_arg_overrides(config):
    sys.argv = ["cool.py", "--foo=1.0"]
    config.foo = "overriden"
    config.load_overrides()
    assert config.foo == 1.0

def test_str_yaml(config):
    config.foo = "bar"
    config.foo_desc = "Fantastic"
    assert str(config) == """wandb_version: 1

foo:
  desc: Fantastic
  value: bar
"""