Exemplo n.º 1
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
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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()
Exemplo n.º 7
0
    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)
Exemplo n.º 8
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