Exemple #1
0
def transactional_db(django_db_blocker, request):
    # Django's/Pytest Django's handling of this is garbage
    # The database is wipe of initial data and never repopulated so we have to do
    # all of it ourselves
    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    from django.test import TransactionTestCase
    test_case = TransactionTestCase(methodName='__init__')
    test_case._pre_setup()

    # Dump all initial data into a string :+1:
    for connection in connections.all():
        if connection.settings_dict['TEST']['MIRROR']:
            continue
        connection._test_serialized_contents = connection.creation.serialize_db_to_string(
        )

    yield None

    test_case.serialized_rollback = True
    test_case._post_teardown()

    # Disconnect post save listeners because they screw up deserialization
    receivers, post_save.receivers = post_save.receivers, []

    if test_case.available_apps is not None:
        apps.unset_available_apps()

    for connection in connections.all():
        if connection.settings_dict['TEST']['MIRROR']:
            connection.close()
            continue
        # Everything has to be in a single transaction to avoid violating key constraints
        # It also makes it run significantly faster
        with transaction.atomic():
            connection.creation.deserialize_db_from_string(
                connection._test_serialized_contents)

    if test_case.available_apps is not None:
        apps.set_available_apps(test_case.available_apps)

    post_save.receivers = receivers
Exemple #2
0
def transactional_db(django_db_blocker, request):
    # Django's/Pytest Django's handling of this is garbage
    # The database is wipe of initial data and never repopulated so we have to do
    # all of it ourselves
    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    from django.test import TransactionTestCase
    test_case = TransactionTestCase(methodName='__init__')
    test_case._pre_setup()

    # Dump all initial data into a string :+1:
    for connection in connections.all():
        if connection.settings_dict['TEST']['MIRROR']:
            continue
        connection._test_serialized_contents = connection.creation.serialize_db_to_string()

    yield None

    test_case.serialized_rollback = True
    test_case._post_teardown()

    # Disconnect post save listeners because they screw up deserialization
    receivers, post_save.receivers = post_save.receivers, []

    if test_case.available_apps is not None:
        apps.unset_available_apps()

    for connection in connections.all():
        if connection.settings_dict['TEST']['MIRROR']:
            connection.close()
            continue
        # Everything has to be in a single transaction to avoid violating key constraints
        # It also makes it run significantly faster
        with transaction.atomic():
            connection.creation.deserialize_db_from_string(connection._test_serialized_contents)

    if test_case.available_apps is not None:
        apps.set_available_apps(test_case.available_apps)

    post_save.receivers = receivers
Exemple #3
0
 def setUp(self):
     TransactionTestCase.setUp(self)
     self.factory = ModelFactory()
Exemple #4
0
 def tearDown(self):
     ac = transaction.get_autocommit(using=None)
     transaction.rollback()
     transaction.set_autocommit(True)
     ac = transaction.get_autocommit(using=None)
     TransactionTestCase.tearDown(self)
Exemple #5
0
    def setUp(self):
        TransactionTestCase.setUp(self)
        ac = transaction.get_autocommit(using=None)  # flailing, debugging
        transaction.set_autocommit(False)
        ac = transaction.get_autocommit(using=None)

        sptext = None
        with open("storedprocs.sql", "r") as spf:
            sptext = spf.read()
        self.assertTrue(sptext)
        self.assertTrue(len(sptext) > 100)
        cursor = connection.cursor()
        cursor.execute(sptext)
        cursor.close()
        djuser = User.objects.create_user(
            'testuser1', '*****@*****.**', 'twisted')
        self.userp = TCUserProfile.objects.get(djuser=djuser)
        self.userp.prefs.history = 4
        self.userp.prefs.nature = 1
        self.userp.prefs.social = 2
        self.userp.prefs.save()
        djrevuser1 = User.objects.create_user(
            'testrevuser1', '*****@*****.**', 'twisted')
        self.rev1p = TCUserProfile.objects.get(djuser=djrevuser1)
        self.rev1p.prefs.history = 2
        self.rev1p.prefs.nature = 2
        self.rev1p.prefs.social = 4
        self.rev1p.prefs.save()
        djrevuser2 = User.objects.create_user(
            'testrevuser2', '*****@*****.**', 'twisted')
        self.rev2p = TCUserProfile.objects.get(djuser=djrevuser2)
        self.rev2p.prefs.history = 4
        self.rev2p.prefs.nature = 2
        self.rev2p.prefs.save()
        # populate places
        p_earth = Place(name="Earth", latitude=0, longitude=0, radiuskm=21000)
        p_earth.parent = None
        p_earth.save()
        p_asia = Place(
            name="Asia", latitude=30, longitude=97, radiuskm=7000, parent=p_earth)
        p_asia.save()
        p_sea = Place(name="Southeast Asia", latitude=10.1,
                      longitude=106.75, radiuskm=1300, parent=p_asia)
        p_sea.save()
        p_thailand = Place(name="Thailand", latitude=13.75,
                           longitude=100.4833, radiuskm=500, parent=p_sea, iscountry=True)
        p_thailand.save()
        pa = PlaceAlias(place=p_thailand, name="thaithai")
        pa.save()
        p_norththai = Place(name="Northern Thailand", latitude=18.35,
                            longitude=99.5, radiuskm=250, parent=p_thailand)
        p_norththai.save()
        p_souththai = Place(name="Southern Thailand", latitude=9.08,
                            longitude=100.28, radiuskm=250, parent=p_thailand)
        p_souththai.save()

        p = Place(name="Krabi", latitude=9.08, longitude=100.28,
                  radiuskm=10, parent=p_souththai)
        p.save()
        rat = Ratings(history=1, budget=3, nature=4, social=1,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev1p, ratings=rat, comment="Kinda boring")
        rev.save()
        rat = Ratings(history=1, budget=1, nature=5, social=1,
                      relaxing=1, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(place=p, user=self.rev2p, ratings=rat,
                     comment="A little stressful, just hung out here a day or so en route to Andaman side.")
        rev.save()

        p = Place(name="Chiang Rai", latitude=19.50,
                  longitude=100, radiuskm=10, parent=p_norththai)
        p.save()
        # no ratings

        p = Place(name="Chiang Mai", latitude=18.50,
                  longitude=99, radiuskm=10, parent=p_norththai)
        p.save()
        rat = Ratings(history=4, budget=3, nature=4, social=4,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(place=p, user=self.rev1p, ratings=rat,
                     comment="Traveler hub of northern Thailand")
        rev.save()
        rat = Ratings(history=3, budget=3, nature=3, social=3,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev2p, ratings=rat, comment="Massages!")
        rev.save()

        p = Place(name="Ko Tao", latitude=10.08, longitude=99.83,
                  radiuskm=6, parent=p_souththai)
        p.save()
        rat = Ratings(history=1, budget=3, nature=4, social=5,
                      relaxing=4, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev1p, ratings=rat, comment="diving and debauchery")
        rev.save()
        rat = Ratings(history=0, budget=3, nature=5, social=4,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev2p, ratings=rat, comment="island wonder.")
        rev.save()

        p = Place(name="Ko Pha Ngan", latitude=9.75,
                  longitude=100.0, radiuskm=6, parent=p_souththai)
        p.save()
        rat = Ratings(history=1, budget=4, nature=5, social=5,
                      relaxing=4, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(place=p, user=self.rev1p, ratings=rat,
                     comment="partying on the beach and serene hiking")
        rev.save()
        rat = Ratings(history=0, budget=5, nature=5, social=6,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev2p, ratings=rat, comment="everything you need.")
        rev.save()

        p = Place(name="Ko Lanta", latitude=7.6, longitude=99.07,
                  radiuskm=15, parent=p_souththai)
        p.save()
        rat = Ratings(history=2, budget=3, nature=3, social=1,
                      relaxing=4, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev1p, ratings=rat, comment="beauty and beaches")
        rev.save()
        rat = Ratings(history=3, budget=4, nature=3, social=2,
                      relaxing=5, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev2p, ratings=rat, comment="get away from it all")
        rev.save()

        p = Place(name="Ko Phi Phi", latitude=7.74,
                  longitude=98.77, radiuskm=6, parent=p_souththai)
        p.save()
        rat = Ratings(history=2, budget=3, nature=5, social=5,
                      relaxing=3, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(
            place=p, user=self.rev1p, ratings=rat, comment="beauty and beaches")
        rev.save()
        rat = Ratings(history=1, budget=2, nature=5, social=5,
                      relaxing=4, touristy=2, active=3, modern=2)
        rat.save()
        rev = Review(place=p, user=self.rev2p, ratings=rat,
                     comment="tourist paradise, and the beach from the beach")
        rev.save()
        transaction.commit()
        connection.commit()
Exemple #6
0
 def test_filter_group(self):
     t = TransactionTestCase()
     qs = fixture.group_query_filter()
     result = fixture.filter_group_name().filter(qs, 'ex')
     qs2 = GroupFilter.custom_filter(self, qs, '', 'ex')
     t.assertQuerysetEqual(qs2, [repr(r) for r in result])
Exemple #7
0
 def test_filter_from_person(self):
     t = TransactionTestCase()
     qs = fixture.from_person_query_filter()
     result = fixture.filter_person_name().filter(qs, 'ex')
     qs2 = FromPersonFilter.custom_filter(self, qs, '', 'ex')
     t.assertQuerysetEqual(qs2, [repr(r) for r in result])