Esempio n. 1
0
    def _fixture_teardown(self):
        from dmqs.repository import Repository
        repository = Repository()
        repository.clean()

        for restore_config in self.apps_config:
            unpatch_models(restore_config[0], restore_config[1])
def test_repository_save():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update = repository.save(model_name, instance)

    assert update == False
    assert repository.__dict__['data'][model_name] == [instance]
    assert type(repository.__dict__['data'][model_name]) == list
    assert repository.__dict__['data'][model_name][0].id == 1
Esempio n. 3
0
def test_repository_save():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update = repository.save(model_name, instance)

    assert update == False
    assert repository.__dict__['data'][model_name] == [instance]
    assert type(repository.__dict__['data'][model_name]) == list
    assert repository.__dict__['data'][model_name][0].id == 1
Esempio n. 4
0
def test_memorify_one_to_one_field():
    """
        The idea here is to create objects on the database, memorify
        single relations (ForeignKey and OneToOne) and then check if
        and check if the relation returns expected data from memory
    """
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection

    old_name = connection.creation.create_test_db(verbosity=1, autoclobber=True)

    from dmqs.repository import Repository

    repository = Repository()

    other_dog = Dog(name="Dogao 2")
    other_dog.save()

    repository.save(Dog.__name__, other_dog)

    friend = Friend(name="Name")
    friend.other_dog = other_dog
    friend.save()

    repository.save(Friend.__name__, friend)

    assert friend.other_dog == other_dog

    memorify_single_relations(friend)

    unpatch_info = patch_models("django_app")
    # the model is patched to make sure that Friend.objects.* comes from
    # memory and not from the database

    memory_person = Friend.objects.get(id=1)

    assert memory_person.other_dog == other_dog
    assert memory_person.other_dog.name == other_dog.name
    assert isinstance(Friend.objects, MemoryManager)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()

    # we must do that to not break other tests
    unpatch_models("django_app", unpatch_info)
Esempio n. 5
0
def test_m2m():
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection

    connection.creation.create_test_db(verbosity=1, autoclobber=True)

    from dmqs.repository import Repository

    repository = Repository()

    friend = Friend(name="Friend")
    friend.save()

    other_friend = Friend(name="Name")
    other_friend.save()

    other_friend.friends.add(friend)
    other_friend.save()

    repository.save(Friend.__name__, other_friend)
    repository.save(Friend.__name__, friend)

    other_friend.m2m_data = {"friends": [1]}

    Friend.objects.all().delete()
    BestFriend.objects.all().delete()
    Friendship.objects.all().delete()

    unpatch_info = patch_models("django_app")

    memorify_m2m(other_friend, other_friend.m2m_data)

    assert isinstance(other_friend.__dict__["friends"], MemoryManager)
    assert list(other_friend.__dict__["friends"].all()) == [friend]

    assert isinstance(other_friend.friends, MemoryManager)
    assert list(other_friend.friends.all()) == [friend]

    unpatch_models("django_app", unpatch_info)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()
Esempio n. 6
0
def test_query_delete():
    from datetime import date

    person1 = type_and_instance_attr_eq('Person',
                                        name="Name 1",
                                        nickname="Nickname 1",
                                        birthday=date(2011, 6, 20),
                                        age=30,
                                        memory=True)

    person2 = type_and_instance_attr_eq('Person',
                                        name="Name 2",
                                        nickname="Nickname 2",
                                        birthday=date(2011, 6, 22),
                                        age=57,
                                        memory=True)

    person3 = type_and_instance_attr_eq('Person',
                                        name="Name 3",
                                        nickname="Nickname 3",
                                        birthday=date(2011, 4, 20),
                                        age=30,
                                        memory=True)

    person4 = type_and_instance_attr_eq('Person',
                                        name="Name 4",
                                        nickname="Nickname 4",
                                        birthday=date(2010, 6, 20),
                                        age=30,
                                        memory=True)

    data = [person1, person2, person3, person4]

    queryset = MemoryQuerySet(person1.__class__, data=data)
    repository = Repository()
    repository.__dict__['data']['Person'] = data

    queryset.filter(age__gte=10, birthday__lte=date(2011, 05, 05)).delete()
    assert len(queryset.all()) == 2
    assert queryset.all().count() == 2

    queryset = MemoryQuerySet(person1.__class__, data=data)
    assert len(queryset.all()) == 2
    assert queryset.all().count() == 2
Esempio n. 7
0
def test_query_delete():
    from datetime import date

    person1 = type_and_instance_attr_eq('Person',
                                name="Name 1",
                                nickname="Nickname 1",
                                birthday=date(2011, 6, 20),
                                age=30,
                                memory=True)

    person2 = type_and_instance_attr_eq('Person',
                                name="Name 2",
                                nickname="Nickname 2",
                                birthday=date(2011, 6, 22),
                                age=57,
                                memory=True)

    person3 = type_and_instance_attr_eq('Person',
                                name="Name 3",
                                nickname="Nickname 3",
                                birthday=date(2011, 4, 20),
                                age=30,
                                memory=True)

    person4 = type_and_instance_attr_eq('Person',
                                name="Name 4",
                                nickname="Nickname 4",
                                birthday=date(2010, 6, 20),
                                age=30,
                                memory=True)

    data = [person1, person2, person3, person4]

    queryset = MemoryQuerySet(person1.__class__, data=data)
    repository = Repository()
    repository.__dict__['data']['Person'] = data

    queryset.filter(age__gte=10,birthday__lte=date(2011, 05, 05)).delete()
    assert len(queryset.all()) == 2
    assert queryset.all().count() == 2

    queryset = MemoryQuerySet(person1.__class__, data=data)
    assert len(queryset.all()) == 2
    assert queryset.all().count() == 2
def test_repository_delete():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update1 = repository.save(model_name, instance)

    instance2 = model_mock(model_name)
    update2 = repository.save(model_name, instance2)

    assert update1 == False
    assert update2 == False

    delete1 = repository.delete(model_name, [instance])
    delete2 = repository.delete(model_name, [instance2])

    assert len(repository.get_models(model_name)) == 0
def test_repository_get_models():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update1 = repository.save(model_name, instance)

    instance2 = model_mock(model_name)
    update2 = repository.save(model_name, instance2)

    assert update1 == False
    assert update2 == False
    assert repository.get_models(model_name) == [instance, instance2]
Esempio n. 10
0
def test_repository_delete():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update1 = repository.save(model_name, instance)

    instance2 = model_mock(model_name)
    update2 = repository.save(model_name, instance2)

    assert update1 == False
    assert update2 == False

    delete1 = repository.delete(model_name, [instance])
    delete2 = repository.delete(model_name, [instance2])

    assert len(repository.get_models(model_name)) == 0
Esempio n. 11
0
def test_repository_get_models():
    repository = Repository()
    repository.clean()

    model_name = 'ModelName'
    instance = model_mock(model_name)
    update1 = repository.save(model_name, instance)

    instance2 = model_mock(model_name)
    update2 = repository.save(model_name, instance2)

    assert update1 == False
    assert update2 == False
    assert repository.get_models(model_name) == [instance, instance2]
Esempio n. 12
0
def test_memorify_one_to_one_field():
    '''
        The idea here is to create objects on the database, memorify
        single relations (ForeignKey and OneToOne) and then check if
        and check if the relation returns expected data from memory
    '''
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection
    old_name = connection.creation.create_test_db(verbosity=1,
                                                  autoclobber=True)

    from dmqs.repository import Repository
    repository = Repository()

    other_dog = Dog(name="Dogao 2")
    other_dog.save()

    repository.save(Dog.__name__, other_dog)

    friend = Friend(name="Name")
    friend.other_dog = other_dog
    friend.save()

    repository.save(Friend.__name__, friend)

    assert friend.other_dog == other_dog

    memorify_single_relations(friend)

    unpatch_info = patch_models("django_app")
    # the model is patched to make sure that Friend.objects.* comes from
    # memory and not from the database

    memory_person = Friend.objects.get(id=1)

    assert memory_person.other_dog == other_dog
    assert memory_person.other_dog.name == other_dog.name
    assert isinstance(Friend.objects, MemoryManager)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()

    # we must do that to not break other tests
    unpatch_models("django_app", unpatch_info)
Esempio n. 13
0
def test_m2m():
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection
    connection.creation.create_test_db(verbosity=1, autoclobber=True)

    from dmqs.repository import Repository
    repository = Repository()

    friend = Friend(name="Friend")
    friend.save()

    other_friend = Friend(name="Name")
    other_friend.save()

    other_friend.friends.add(friend)
    other_friend.save()

    repository.save(Friend.__name__, other_friend)
    repository.save(Friend.__name__, friend)

    other_friend.m2m_data = {'friends': [1]}

    Friend.objects.all().delete()
    BestFriend.objects.all().delete()
    Friendship.objects.all().delete()

    unpatch_info = patch_models("django_app")

    memorify_m2m(other_friend, other_friend.m2m_data)

    assert isinstance(other_friend.__dict__['friends'], MemoryManager)
    assert list(other_friend.__dict__['friends'].all()) == [friend]

    assert isinstance(other_friend.friends, MemoryManager)
    assert list(other_friend.friends.all()) == [friend]

    unpatch_models("django_app", unpatch_info)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()
Esempio n. 14
0
def pytest_runtest_setup():
    repository = Repository()
    repository.clean()
    MemoryQuerySet.fetch_from_repo = False
Esempio n. 15
0
def test_m2m_with_through():
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection
    from datetime import date
    connection.creation.create_test_db(verbosity=1, autoclobber=True)

    from dmqs.repository import Repository
    repository = Repository()

    friend = Friend(name="Friend")
    friend.save()

    other_friend = Friend(name="Name")
    other_friend.save()

    other_friend2 = Friend(name="Name2")
    other_friend2.save()

    other_friend3 = Friend(name="Name3")
    other_friend3.save()

    best_friend = BestFriend()
    best_friend.person = other_friend
    best_friend.nickname = "nickname"
    best_friend.save()

    best_friend2 = BestFriend()
    best_friend2.person = other_friend2
    best_friend2.nickname = "nickname2"
    best_friend2.save()

    friendship = Friendship()
    friendship.since = date.today()
    friendship.best_friend1 = best_friend
    friendship.best_friend2 = friend
    friendship.save()

    friendship2 = Friendship()
    friendship2.since = date.today()
    friendship2.best_friend1 = best_friend2
    friendship2.best_friend2 = friend
    friendship2.save()

    friendship3 = Friendship()
    friendship3.since = date.today()
    friendship3.best_friend1 = best_friend2
    friendship3.best_friend2 = other_friend3
    friendship3.save()

    repository.save(Friend.__name__, other_friend)
    repository.save(Friend.__name__, other_friend2)
    repository.save(Friend.__name__, other_friend3)
    repository.save(Friend.__name__, friend)
    repository.save(BestFriend.__name__, best_friend)
    repository.save(BestFriend.__name__, best_friend2)
    repository.save(Friendship.__name__, friendship)
    repository.save(Friendship.__name__, friendship2)
    repository.save(Friendship.__name__, friendship3)

    Friend.objects.all().delete()
    BestFriend.objects.all().delete()
    Friendship.objects.all().delete()

    unpatch_info = patch_models("django_app")

    memorify_m2m(friend, {})
    memorify_m2m(other_friend3, {})

    assert isinstance(friend.__dict__['best_friends'], MemoryManager)
    assert list(
        friend.__dict__['best_friends'].all()) == [best_friend, best_friend2]
    assert list(friend.__dict__['best_friends'].filter(
        nickname__endswith="2")) == [best_friend2]

    assert isinstance(other_friend3.__dict__['best_friends'], MemoryManager)
    assert list(other_friend3.__dict__['best_friends'].all()) == [best_friend2]
    assert list(other_friend3.__dict__['best_friends'].filter(
        nickname__endswith="2")) == [best_friend2]

    assert isinstance(friend.best_friends, MemoryManager)
    assert list(friend.best_friends.all()) == [best_friend, best_friend2]
    assert list(
        friend.best_friends.filter(nickname__endswith="2")) == [best_friend2]

    assert isinstance(other_friend3.best_friends, MemoryManager)
    assert list(other_friend3.best_friends.all()) == [best_friend2]
    assert list(other_friend3.best_friends.filter(nickname__endswith="2")) == [
        best_friend2
    ]

    unpatch_models("django_app", unpatch_info)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()
Esempio n. 16
0
from dmqs.manager import MemoryManager
from dmqs.repository import Repository

repository = Repository()

def type_and_instance(type_name, **kwargs):
    dummy_class = type(type_name, (object,), {})
    new_class = type(type_name,
                     (object,),
                     dict(id=lambda s: s.__dict__['id'],
                          objects=MemoryManager(dummy_class)))
    instance = new_class()
    instance.__dict__ = kwargs
    return instance

def test_memory_manager_all():
    person1 = type_and_instance('Person',
                                name="Name 1",
                                nickname="Nickname 1",
                                age=20,
                                memory=True)

    person2 = type_and_instance('Person',
                                name="Name 2",
                                nickname="Nickname 2",
                                age=30,
                                memory=True)

    person3 = type_and_instance('Person',
                                name="Name 2",
                                nickname="Nickname 3",
Esempio n. 17
0
def test_m2m_with_through():
    setup_test_environment()
    old_name = "django_app"

    from django.db import connection
    from datetime import date

    connection.creation.create_test_db(verbosity=1, autoclobber=True)

    from dmqs.repository import Repository

    repository = Repository()

    friend = Friend(name="Friend")
    friend.save()

    other_friend = Friend(name="Name")
    other_friend.save()

    other_friend2 = Friend(name="Name2")
    other_friend2.save()

    other_friend3 = Friend(name="Name3")
    other_friend3.save()

    best_friend = BestFriend()
    best_friend.person = other_friend
    best_friend.nickname = "nickname"
    best_friend.save()

    best_friend2 = BestFriend()
    best_friend2.person = other_friend2
    best_friend2.nickname = "nickname2"
    best_friend2.save()

    friendship = Friendship()
    friendship.since = date.today()
    friendship.best_friend1 = best_friend
    friendship.best_friend2 = friend
    friendship.save()

    friendship2 = Friendship()
    friendship2.since = date.today()
    friendship2.best_friend1 = best_friend2
    friendship2.best_friend2 = friend
    friendship2.save()

    friendship3 = Friendship()
    friendship3.since = date.today()
    friendship3.best_friend1 = best_friend2
    friendship3.best_friend2 = other_friend3
    friendship3.save()

    repository.save(Friend.__name__, other_friend)
    repository.save(Friend.__name__, other_friend2)
    repository.save(Friend.__name__, other_friend3)
    repository.save(Friend.__name__, friend)
    repository.save(BestFriend.__name__, best_friend)
    repository.save(BestFriend.__name__, best_friend2)
    repository.save(Friendship.__name__, friendship)
    repository.save(Friendship.__name__, friendship2)
    repository.save(Friendship.__name__, friendship3)

    Friend.objects.all().delete()
    BestFriend.objects.all().delete()
    Friendship.objects.all().delete()

    unpatch_info = patch_models("django_app")

    memorify_m2m(friend, {})
    memorify_m2m(other_friend3, {})

    assert isinstance(friend.__dict__["best_friends"], MemoryManager)
    assert list(friend.__dict__["best_friends"].all()) == [best_friend, best_friend2]
    assert list(friend.__dict__["best_friends"].filter(nickname__endswith="2")) == [best_friend2]

    assert isinstance(other_friend3.__dict__["best_friends"], MemoryManager)
    assert list(other_friend3.__dict__["best_friends"].all()) == [best_friend2]
    assert list(other_friend3.__dict__["best_friends"].filter(nickname__endswith="2")) == [best_friend2]

    assert isinstance(friend.best_friends, MemoryManager)
    assert list(friend.best_friends.all()) == [best_friend, best_friend2]
    assert list(friend.best_friends.filter(nickname__endswith="2")) == [best_friend2]

    assert isinstance(other_friend3.best_friends, MemoryManager)
    assert list(other_friend3.best_friends.all()) == [best_friend2]
    assert list(other_friend3.best_friends.filter(nickname__endswith="2")) == [best_friend2]

    unpatch_models("django_app", unpatch_info)

    connection.creation.destroy_test_db(old_name, 1)
    teardown_test_environment()
def test_repository_shared_state():
    repository1 = Repository()
    repository2 = Repository()

    assert id(repository1.__dict__) == id(repository2.__dict__)