import json
from pathlib import Path

from gravity.defaults import (
    DEFAULT_GUNICORN_BIND,
    DEFAULT_GUNICORN_TIMEOUT,
    DEFAULT_GUNICORN_WORKERS,
    DEFAULT_INSTANCE_NAME,
    CELERY_DEFAULT_CONFIG

)


def test_register_defaults(galaxy_yml, galaxy_root_dir, state_dir, default_config_manager):
    default_config_manager.add([str(galaxy_yml)])
    assert str(galaxy_yml) in default_config_manager.state['config_files']
    state = default_config_manager.state['config_files'][str(galaxy_yml)]
    assert state['config_type'] == 'galaxy'
    assert state['instance_name'] == DEFAULT_INSTANCE_NAME
    assert state['services'] == []
    attributes = state['attribs']
    assert attributes['app_server'] == 'gunicorn'
    assert Path(attributes['log_dir']) == Path(state_dir) / 'log'
    assert Path(attributes['galaxy_root']) == galaxy_root_dir
    gunicorn_attributes = attributes['gunicorn']
    assert gunicorn_attributes['bind'] == DEFAULT_GUNICORN_BIND
    assert gunicorn_attributes['workers'] == DEFAULT_GUNICORN_WORKERS
    assert gunicorn_attributes['timeout'] == DEFAULT_GUNICORN_TIMEOUT
    assert gunicorn_attributes['extra_args'] == ""
    assert attributes['celery'] == CELERY_DEFAULT_CONFIG


def test_register_non_default(galaxy_yml, default_config_manager):
    new_bind = 'localhost:8081'
    concurrency = 4
    galaxy_yml.write(json.dumps({
        'galaxy': None,
        'gravity': {
            'gunicorn': {
                'bind': new_bind
            },
            'celery': {
                'concurrency': concurrency
            }
        }
    }))
    default_config_manager.add([str(galaxy_yml)])
    state = default_config_manager.state['config_files'][str(galaxy_yml)]
    gunicorn_attributes = state['attribs']['gunicorn']
    assert gunicorn_attributes['bind'] == new_bind
    assert gunicorn_attributes['workers'] == DEFAULT_GUNICORN_WORKERS
    celery_attributes = state['attribs']['celery']
    assert celery_attributes['concurrency'] == concurrency


def test_deregister(galaxy_yml, default_config_manager):
    default_config_manager.add([str(galaxy_yml)])
    assert str(galaxy_yml) in default_config_manager.state['config_files']
    default_config_manager.remove([str(galaxy_yml)])
    assert str(galaxy_yml) not in default_config_manager.state['config_files']

@@ -2172,24 +2172,89 @@
 fig_files'%5D%0A
+    assert default_config_manager.is_registered(str(galaxy_yml))%0A
     default_
@@ -2353,28 +2353,1100 @@
 nager.state%5B'config_files'%5D%0A
+    assert not default_config_manager.is_registered(str(galaxy_yml))%0A%0A%0Adef test_rename(galaxy_root_dir, state_dir, default_config_manager):%0A    galaxy_yml_sample = galaxy_root_dir / %22config%22 / %22galaxy.yml.sample%22%0A    default_config_manager.add(%5Bstr(galaxy_yml_sample)%5D)%0A    galaxy_yml = galaxy_root_dir / %22config%22 / %22galaxy.yml%22%0A    galaxy_yml_sample.copy(galaxy_yml)%0A    assert default_config_manager.is_registered(str(galaxy_yml_sample.realpath()))%0A    assert not default_config_manager.is_registered(str(galaxy_yml))%0A    default_config_manager.rename(str(galaxy_yml_sample.realpath()), str(galaxy_yml))%0A    assert not default_config_manager.is_registered(str(galaxy_yml_sample.realpath()))%0A    assert default_config_manager.is_registered(str(galaxy_yml))%0A%0A%0Adef test_auto_register(galaxy_yml, default_config_manager, monkeypatch):%0A    monkeypatch.setenv(%22GALAXY_CONFIG_FILE%22, str(galaxy_yml))%0A    assert not default_config_manager.is_registered(str(galaxy_yml))%0A    default_config_manager.auto_register()%0A    assert default_config_manager.is_registered(str(galaxy_yml))%0A

Test rename and auto-register