def sub_repo():
    """
    Repository for class Sub. Clears register of all created
    InMemoryRepository instances
    """
    yield InMemoryRepository(Sub)
    InMemoryRepository._clear_all_repos()
def test_finding_sub_repos():
    """InMemoryRepository finds repos of subclasses correctly
    NB: notice opposite order of repo creation.
    """
    sub_repo = InMemoryRepository(Sub)
    InMemoryRepository(Super)
    sub_repo_super_klasses = sub_repo._SUPER_KLASSES_WITH_REPO[sub_repo._klass_qualname]
    assert sub_repo_super_klasses == ['test_in_memory.Super']
def test_load_from_filepath():
    contents = (
        "---\n"
        "foo: !<foo> {}\n"
    )
    with mock.patch('pca.utils.serialization.read_from_file') as mocked_read_from_file:
        mocked_read_from_file.return_value = contents
        InMemoryRepository.load_from_filepath('foo.yaml')

    foo_object = serialization.load(contents)['foo']
    assert isinstance(foo_object, FooClass)
def test_create_factory(mocker):
    """Repo `create` uses `factory` as a factory"""
    class A(object):
        pass

    a = A()
    m = mocker.Mock(return_value=a)
    repo = InMemoryRepository(A, factory=m)
    obj = repo.create('arg_value', kwarg_name='kwarg_value')
    assert obj == a
    m.assert_called_once_with('arg_value', kwarg_name='kwarg_value')
def test_create_and_insert():
    """Repo `create_and_insert`"""
    some_id = 12

    class A(object):
        def __init__(self, id_=some_id):
            self.id = id_

    repo = InMemoryRepository(A)
    obj = repo.create_and_insert(id_=some_id)
    assert obj.id == some_id
    assert repo.get(some_id) is obj
def test_create_klass(mocker):
    """Repo `create` uses `klass` as a factory"""
    m = mocker.Mock()

    class A(object):
        def __init__(self, *args, **kwargs):
            m(*args, **kwargs)

    repo = InMemoryRepository(A)
    obj = repo.create('arg_value', kwarg_name='kwarg_value')
    assert obj
    m.assert_called_once_with('arg_value', kwarg_name='kwarg_value')
def test_update():
    """Repo `update`"""
    some_id = 12

    class A(object):
        def __init__(self, id_=some_id):
            self.id = id_

    repo = InMemoryRepository(A)
    obj = repo.create_and_insert(id_=some_id)
    new_obj = A(some_id)
    success = repo.update(new_obj)
    assert success
    assert new_obj is not obj
    assert repo.get(some_id) is new_obj
def test_batch_update():
    """Repo `update`"""
    some_id = 12

    class A(object):
        def __init__(self, id_=some_id):
            self.id = id_

    repo = InMemoryRepository(A)
    objs = {id_: repo.create_and_insert(id_=id_) for id_ in range(6)}
    new_objs = [A(id_) for id_ in range(7)]
    success = repo.batch_update(new_objs)
    assert all(success[id_] for id_ in range(6))  # old instances has been updated
    assert not success[6]  # the new instance #6 hasn't been already in repo so it raised an error
    assert all(repo.get(id_) is new_objs[id_] for id_ in range(6))
    with pytest.raises(InMemoryRepository.NotFound):
        assert repo.get(6)  # and still the #6 is not there
def clear_all_repos():
    """Clears register of all created ConfigRepository instances"""
    yield
    InMemoryRepository._clear_all_repos()
def test_finding_super_repos():
    """InMemoryRepository finds repos of superclasses correctly"""
    InMemoryRepository(Super)
    sub_repo = InMemoryRepository(Sub)
    sub_repo_super_klasses = sub_repo._SUPER_KLASSES_WITH_REPO[sub_repo._klass_qualname]
    assert sub_repo_super_klasses == ['test_in_memory.Super']
def test_class_register(super_repo):
    other_super_repo = InMemoryRepository(Super)
    assert super_repo.all() == other_super_repo.all()