def test_set_on_individual_object(self):
        car = Car()
        car2 = Car()
        car.sedan = Sedan()

        with self.assertRaises(Car.sedan.RelatedObjectDoesNotExist):
            car2.sedan
Beispiel #2
0
    def test_empty_queryset_filter(self):
        car1 = Car(id=101)
        car2 = Car(id=102)

        mockset = MockSet(car1, car2)
        self.assertEqual(mockset.count(), 2)
        self.assertEqual(mockset.filter(Q()).count(), 2)
    def test_query_get_raises_specific_exception(self):
        item_1 = Car(model='battle')
        item_2 = Car(model='pious')
        item_3 = Car(model='hummus')

        self.mock_set = MockSet(item_1, item_2, item_3, model=Car)
        self.assertRaises(Car.DoesNotExist, self.mock_set.get, model='clowncar')
    def test_set_on_individual_object(self):
        car = Car()
        car2 = Car()
        car.sedan = Sedan()

        with self.assertRaises(Car.sedan.RelatedObjectDoesNotExist):
            car2.sedan
    def test_set_explicit_collection(self):
        m = Manufacturer()
        m.car_set.add(Car(speed=95))

        car = Car(speed=100)
        m.car_set = MockSet(car)

        self.assertIs(m.car_set.first(), car)
Beispiel #6
0
    def test_query_filters_model_objects_by_pk(self):
        item_1 = Car(speed=1, id=101)
        item_2 = Car(speed=2, id=102)

        self.mock_set.add(item_1, item_2)
        results = list(self.mock_set.filter(pk=102))

        assert results == [item_2]
Beispiel #7
0
    def test_filter_keeps_class(self):
        item_1 = Car(model='battle')
        item_2 = Car(model='pious')
        item_3 = Car(model='hummus')

        self.mock_set = MockSet(item_1, item_2, item_3, model=Car)
        filtered = self.mock_set.filter(model__endswith='s')
        self.assertRaises(Car.DoesNotExist, filtered.get, model='clowncar')
Beispiel #8
0
    def test_query_filters_related_model_objects(self):
        item_1 = Car(make=Manufacturer(name='apple'))
        item_2 = Car(make=Manufacturer(name='banana'))
        item_3 = Car(make=Manufacturer(name='cherry'))

        self.mock_set.add(item_1, item_2, item_3)
        results = list(self.mock_set.filter(make__name='cherry'))

        assert results == [item_3]
    def test_query_distinct_django_model_with_fields(self):
        item_1 = Car(speed=1, model='a')
        item_2 = Car(speed=2, model='b')
        item_3 = Car(speed=1, model='c')

        self.mock_set.add(item_2, item_3, item_1, item_3)
        results = list(self.mock_set.order_by('speed', 'model').distinct('speed'))

        assert results == [item_1, item_2], results
Beispiel #10
0
    def test_query_distinct_django_model(self):
        item_1 = Car(speed=1)
        item_2 = Car(speed=2)
        item_3 = Car(speed=3)

        self.mock_set.add(item_2, item_3, item_1, item_3)
        results = list(self.mock_set.distinct().order_by('speed'))

        assert results == [item_1, item_2, item_3], results
Beispiel #11
0
    def test_convert_to_pks(self):
        car1 = Car(id=101)
        car2 = Car(id=102)
        car3 = Car(id=103)

        old_cars = MockSet(car1, car2)
        all_cars = MockSet(car1, car2, car3)

        matches = all_cars.filter(pk__in=old_cars)

        self.assertEqual(list(old_cars), list(matches))
    def test_query_aggregate_on_related_field(self):
        with mocked_relations(Manufacturer):
            cars = [Car(speed=1), Car(speed=2), Car(speed=3)]

            make = Manufacturer()
            make.car_set = MockSet(*cars)

            self.mock_set.add(make)

            result = self.mock_set.aggregate(Avg('car__speed'))
            assert result['car__speed__avg'] == sum([c.speed for c in cars]) / len(cars)
Beispiel #13
0
    def test_query_filters_model_objects(self):
        item_1 = Car(speed=1)
        item_2 = Sedan(speed=2)
        item_3 = Car(speed=3)

        item_2.sedan = item_2

        self.mock_set.add(item_1, item_2, item_3)
        results = list(self.mock_set.filter(speed=3))

        assert results == [item_3]
Beispiel #14
0
    def test_query_convert_values_list_to_pks(self):
        car1 = Car(id=101)
        car2 = Car(id=102)
        car3 = Car(id=103)

        old_cars = MockSet(car1, car2)
        old_car_pks = old_cars.values_list("pk", flat=True)
        all_cars = MockSet(car1, car2, car3)

        matches = all_cars.filter(pk__in=old_car_pks)

        self.assertEqual(list(old_cars), list(matches))
Beispiel #15
0
    def test_query_filters_model_objects_by_bad_field(self):
        item_1 = Car(speed=1)
        item_2 = Sedan(speed=2)
        item_3 = Car(speed=3)

        item_2.sedan = item_2

        self.mock_set.add(item_1, item_2, item_3)
        with self.assertRaisesRegexp(
                FieldError, r"Cannot resolve keyword 'bad_field' into field\. "
                r"Choices are 'id', 'make', 'make_id', 'model', 'passengers', 'sedan', 'speed', 'variations'\."
        ):
            self.mock_set.filter(bad_field='bogus')
Beispiel #16
0
    def test_mocked_relations_context_manager(self):
        m = Manufacturer()

        with mocked_relations(Manufacturer):
            self.assertEqual(0, m.car_set.count())
            m.car_set.add(Car())
            self.assertEqual(1, m.car_set.count())
Beispiel #17
0
 def test_mocked_relations_with_garbage_collection(self):
     self.longMessage = True
     for group_index in range(10):
         m = Manufacturer()
         self.assertEqual(0, m.car_set.count(),
                          'group_index: {}'.format(group_index))
         m.car_set.add(Car())
         self.assertEqual(1, m.car_set.count())
         del m
Beispiel #18
0
    def test_mocked_relations_is_match_in_children(self):
        car = Car()
        manufacturer = Manufacturer()
        manufacturer.car_set.add(car)
        Manufacturer.objects.add(manufacturer)

        car_manufacturers = Manufacturer.objects.filter(car=car)

        self.assertEqual([manufacturer], list(car_manufacturers))
    def test_model_mocker_generic(self):
        with ModelMocker(Car):
            # New instance gets inserted
            obj = Car(speed=4)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id), obj)

            # Existing instance gets updated
            obj = Car(id=obj.id, speed=5)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id).speed, obj.speed)

            # Trying to update an instance that doesn't exists creates it
            obj = Car(id=123, speed=5)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id), obj)
    def test_query_values_of_nested_field(self):
        with mocked_relations(Manufacturer, Car):
            make = Manufacturer(name='vw')
            self.mock_set.add(make)

            polo = Car(make=make, model='polo', speed=240)
            golf = Car(make=make, model='golf', speed=260)

            polo_white = CarVariation(car=polo, color='white')
            golf_white = CarVariation(car=golf, color='white')
            golf_black = CarVariation(car=golf, color='black')

            make.car_set = MockSet(polo, golf)
            polo.variations = MockSet(polo_white)
            golf.variations = MockSet(golf_white, golf_black)

            data = list(self.mock_set.values('car__model', 'car__variations__color', 'name'))
            assert {'name': make.name, 'car__model': polo.model, 'car__variations__color': polo_white.color} in data
            assert {'name': make.name, 'car__model': golf.model, 'car__variations__color': golf_white.color} in data
            assert {'name': make.name, 'car__model': golf.model, 'car__variations__color': golf_black.color} in data
Beispiel #21
0
    def test_mocked_relations_mock_twice(self):
        """ Don't reset the mocking if a class is mocked twice.

        Could happen where Sedan is mocked on the class, and Car (the base
        class) is mocked on a method.
        """
        Car.objects.add(Car(speed=95))
        self.assertEqual(1, Car.objects.count())

        with mocked_relations(Car):
            self.assertEqual(1, Car.objects.count())
    def test_mock_is_removed(self):
        m = Manufacturer()

        with patch.object(Manufacturer, 'car_set', MockOneToManyMap(Manufacturer.car_set)):
            m.car_set = MockSet(Car(speed=95))
            self.assertEqual(1, m.car_set.count())

        with self.assertRaisesRegexp(
                NotSupportedError,
                'Mock database tried to execute SQL for Car model'):
            m.car_set.count()
Beispiel #23
0
    def test_query_values_of_nested_field(self):
        with mocked_relations(Manufacturer, Car):
            make = Manufacturer(name='vw')
            self.mock_set.add(make)

            polo = Car(make=make, model='polo', speed=240)
            golf = Car(make=make, model='golf', speed=260)

            polo_white = CarVariation(car=polo, color='white')
            golf_white = CarVariation(car=golf, color='white')
            golf_black = CarVariation(car=golf, color='black')

            make.car_set = MockSet(polo, golf)
            polo.variations = MockSet(polo_white)
            golf.variations = MockSet(golf_white, golf_black)

            data = list(
                self.mock_set.values('car__model', 'car__variations__color',
                                     'name'))
            assert {
                'name': make.name,
                'car__model': polo.model,
                'car__variations__color': polo_white.color
            } in data
            assert {
                'name': make.name,
                'car__model': golf.model,
                'car__variations__color': golf_white.color
            } in data
            assert {
                'name': make.name,
                'car__model': golf.model,
                'car__variations__color': golf_black.color
            } in data
Beispiel #24
0
    def test_model_mocker_event_added_from_instance(self):
        objects = {}

        def car_added(obj):
            objects['added'] = obj

        with ModelMocker(Car) as mocker:
            mocker.objects.on('added', car_added)
            objects['car'] = Car(speed=300)
            objects['car'].save()

        self.assertIsInstance(objects['added'], Car)
        self.assertEqual(objects['added'], objects['car'])
Beispiel #25
0
    def test_set(self):
        car = Car()
        sedan = Sedan()
        car.sedan = sedan

        self.assertIs(car.sedan, sedan)
Beispiel #26
0
    def test_not_set(self):
        car = Car(id=99)

        with self.assertRaises(Car.sedan.RelatedObjectDoesNotExist):
            car.sedan
Beispiel #27
0
    def test_not_mocked(self):
        car = Car(id=99)

        with self.assertRaises(NotSupportedError):
            car.sedan
Beispiel #28
0
    def test_model_mocker_callable_with_custom_method(self, mocker):
        obj = Car()
        obj.save()

        mocker.method('validate_price').assert_called_with()
Beispiel #29
0
    def test_set(self):
        m = Manufacturer()
        car = Car(speed=95)
        m.car_set.add(car)

        self.assertIs(m.car_set.first(), car)
    def test_set(self):
        car = Car()
        sedan = Sedan()
        car.sedan = sedan

        self.assertIs(car.sedan, sedan)
    def test_model_mocker_with_custom_method(self):
        with self.CarModelMocker(Car, 'validate_price') as mocker:
            obj = Car()
            obj.save()

            mocker.method('validate_price').assert_called_with()
Beispiel #32
0
    def test_set_on_individual_object(self):
        m = Manufacturer()
        m.car_set.add(Car(speed=95))
        m2 = Manufacturer()

        self.assertEqual(0, m2.car_set.count())
    def test_model_mocker_callable_with_custom_method(self, mocker):
        obj = Car()
        obj.save()

        mocker.method('validate_price').assert_called_with()
Beispiel #34
0
    def test_model_mocker_instance_save(self):
        with ModelMocker(Car):
            # New instance gets inserted
            obj = Car(speed=4)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id), obj)

            # Existing instance gets updated
            obj = Car(id=obj.id, speed=5)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id).speed, obj.speed)

            # Trying to update an instance that doesn't exists creates it
            obj = Car(id=123, speed=5)
            obj.save()
            self.assertEqual(Car.objects.get(pk=obj.id), obj)
Beispiel #35
0
    def test_mocked_relations_decorator(self):
        m = Manufacturer()

        self.assertEqual(0, m.car_set.count())
        m.car_set.add(Car())
        self.assertEqual(1, m.car_set.count())
Beispiel #36
0
    def test_model_mocker_with_custom_method(self):
        with self.CarModelMocker(Car, 'validate_price') as mocker:
            obj = Car()
            obj.save()

            mocker.method('validate_price').assert_called_with()