def test_get_form():
    test_value = {'testing': 1, 'count': 2}
    with form('TEST') as form_class:
        value = RuntimeSetting(key="TEST", raw_value=json.dumps(test_value))
        form_instance = value.get_form()
        assert isinstance(form_instance, form_class)
        assert form_instance.is_bound is True
        assert form_instance.data == test_value
def test_delete_restores_default():
    test_value = {'testing': 1, 'count': 2}
    value = RuntimeSetting(key="TEST", raw_value=json.dumps(test_value))
    value.save()
    db_obj = value.__class__.objects.get(key='TEST')
    with form('TEST'):
        assert db_obj.value == {'count': 2}
        value.delete()
        db_obj2 = value.__class__.objects.get(key='TEST')
        assert db_obj2.raw_value == '{"amdefault": null}'
        assert db_obj2.value == {}
def test_can_serialize_modelchoices():
    user1 = get_user_model().objects.create(username='******')
    user2 = get_user_model().objects.create(username='******')
    with userform('USERFORM') as form_class:
        data = {'single_user': user1.pk,
                'many_users': [user1.pk, user2.pk],
                'another': 3}
        value = RuntimeSetting(key="USERFORM")
        value.value = data
        assert '"single_user": "******"' in value.raw_value
        assert '"many_users": ["1", "2"]' in value.raw_value
        assert value.value['single_user'] == user1
        assert set(value.value['many_users']) == set([user1, user2])
def test_api_get_single(api_client):
    test_value = {'testing': 1, 'count': 4}
    setting = RuntimeSetting(key="TEST2")
    with form('TEST2'):
        setting.value = test_value
    setting.save()
    url = reverse('runtimesetting-detail', args=(setting.pk,))
    with form('TEST2'):
        response = api_client.get(url)
    assert response.data == {
        'value': {'count': 4},
        'key': 'TEST2'
    }
def test_api_getlist(api_client):
    test_value = {'testing': 1, 'count': 2}
    setting = RuntimeSetting(key="TEST")
    with form('TEST'):
        setting.value = test_value
    setting.save()
    url = reverse('runtimesetting-list')
    with form('TEST'):
        response = api_client.get(url)
    assert response.data == [
        OrderedDict(
            [('key', 'TEST'), 
             ('value', {'count': 2})]
        )
    ]
def test_api_put_does_update(api_client):
    initial_value = {
        'key': 'TEST4',
        'value': {'testing': 1, 'count': 1},
    }
    setting = RuntimeSetting(key="TEST4")
    with form('TEST4'):
        setting.value = initial_value
    setting.save()
    test_value = {
        'key': 'TEST4',
        'value': {'testing': 1, 'count': 25},
    }
    url = reverse('runtimesetting-detail', args=(setting.pk,))
    with form('TEST4'):
        response = api_client.put(url, data=test_value, format='json')
    assert response.data == {
        'value': {'count': 25},
        'key': 'TEST4'
    }
    obj = RuntimeSetting.objects.get(key='TEST4')
    with form('TEST4'):
        assert obj.value == {'count': 25}
def test_has_changed():
    test_value = {'testing': 1, 'count': 2}
    with form('TEST'):
        value = RuntimeSetting(key="TEST", raw_value=json.dumps(test_value))
        assert value.has_changed() is True
def test_get_form_class():
    with form('TEST') as form_class:
        value = RuntimeSetting(key="TEST")
        assert value.get_form_class() == form_class
    with pytest.raises(KeyError):
        value.get_form_class()