Esempio n. 1
0
 def test_query_create_raises_value_error_when_kwarg_key_is_not_in_concrete_fields(self):
     qs = MockSet(
         model=create_model('first', 'second', 'third')
     )
     attrs = dict(first=1, second=2, third=3, fourth=4)
     with self.assertRaises(FieldError):
         qs.create(**attrs)
 def test_query_create_raises_value_error_when_kwarg_key_is_not_in_concrete_fields(self):
     qs = MockSet(
         model=create_model('first', 'second', 'third')
     )
     attrs = dict(first=1, second=2, third=3, fourth=4)
     with self.assertRaises(FieldError):
         qs.create(**attrs)
    def test_query_update_does_not_allow_related_model_fields(self):
        objects = [MockModel(foo=MockModel(bar=1)), MockModel(foo=MockModel(bar=2))]
        qs = MockSet(*objects, model=create_model('foo'))

        target = dict(foo__bar=2)
        with self.assertRaises(FieldError) as cm:
            qs.update(**target)

        assert 'Cannot update model field \'{}\''.format(next(iter(target))) in str(cm.exception)
Esempio n. 4
0
    def test_query_update_does_not_allow_related_model_fields(self):
        objects = [MockModel(foo=MockModel(bar=1)), MockModel(foo=MockModel(bar=2))]
        qs = MockSet(*objects, model=create_model('foo'))

        target = dict(foo__bar=2)
        with self.assertRaises(FieldError) as cm:
            qs.update(**target)

        assert 'Cannot update model field \'{}\''.format(next(iter(target))) in str(cm.exception)
Esempio n. 5
0
 def test_query_creates_new_model_and_adds_to_set(self):
     qs = MockSet(model=create_model('foo', 'bar', 'none'))
     attrs = dict(foo=1, bar='a')
     obj = qs.create(**attrs)
     obj.save.assert_called_once_with(force_insert=True, using=ANY)
     assert obj in [x for x in qs]
     assert hasattr(obj, 'foo') and obj.foo == 1
     assert hasattr(obj, 'bar') and obj.bar == 'a'
     assert hasattr(obj, 'none') and obj.none is None
Esempio n. 6
0
    def test_query_creates_new_model_and_adds_to_set(self):
        qs = MockSet(model=create_model('foo', 'bar', 'none'))
        attrs = dict(foo=1, bar='a')
        obj = qs.create(**attrs)

        assert obj in [x for x in qs]
        assert hasattr(obj, 'foo') and obj.foo == 1
        assert hasattr(obj, 'bar') and obj.bar == 'a'
        assert hasattr(obj, 'none') and obj.none is None
Esempio n. 7
0
    def test_query_creates_new_model_and_adds_to_set(self):
        qs = MockSet(model=create_model('foo', 'bar', 'none'))
        attrs = dict(foo=1, bar='a')
        obj = qs.create(**attrs)

        assert obj in [x for x in qs]
        assert hasattr(obj, 'foo') and obj.foo == 1
        assert hasattr(obj, 'bar') and obj.bar == 'a'
        assert hasattr(obj, 'none') and obj.none is None
Esempio n. 8
0
    def test_query_update_with_multiple_values(self):
        objects = [MockModel(foo=1), MockModel(foo=2), MockModel(foo=3)]
        qs = MockSet(*objects, model=create_model('foo', 'bar'))

        set_foo, set_bar = 4, 5
        qs.update(foo=set_foo, bar=set_bar)

        for x in qs:
            assert x.foo == set_foo, x.foo
            assert x.bar == set_bar, x.bar
Esempio n. 9
0
    def test_query_update_with_multiple_values(self):
        objects = [MockModel(foo=1), MockModel(foo=2), MockModel(foo=3)]
        qs = MockSet(*objects, model=create_model('foo', 'bar'))

        set_foo, set_bar = 4, 5
        qs.update(foo=set_foo, bar=set_bar)

        for x in qs:
            assert x.foo == set_foo, x.foo
            assert x.bar == set_bar, x.bar
Esempio n. 10
0
    def test_query_update_or_create_creates_new_model_when_no_match(self):
        item_1 = MockModel(foo=1)
        item_2 = MockModel(foo=2)
        item_3 = MockModel(foo=3)

        qs = MockSet(model=create_model('foo'))
        qs.add(item_1, item_2, item_3)
        obj, created = qs.update_or_create(foo=4)

        assert hasattr(obj, 'foo') and obj.foo == 4
        assert created is True
Esempio n. 11
0
    def test_query_get_or_create_raises_does_multiple_objects_returned_when_more_than_one_match_with_defaults(
            self):
        qs = MockSet(model=create_model('first', 'second', 'third'))
        item_1 = MockModel(first=1)
        item_2 = MockModel(first=1)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        qs.add(item_1, item_2, item_3)
        with self.assertRaises(MultipleObjectsReturned):
            qs.get_or_create(first=1, defaults={'second': 2})
Esempio n. 12
0
    def test_query_get_or_create_creates_new_model_when_no_match(self):
        item_1 = MockModel(foo=1)
        item_2 = MockModel(foo=2)
        item_3 = MockModel(foo=3)

        qs = MockSet(model=create_model('foo'))
        qs.add(item_1, item_2, item_3)
        obj, created = qs.get_or_create(foo=4)

        assert hasattr(obj, 'foo') and obj.foo == 4
        assert created is True
Esempio n. 13
0
    def test_query_update_or_create_raises_does_multiple_objects_returned_when_more_than_one_match_with_defaults(self):
        qs = MockSet(
            model=create_model('first', 'second', 'third')
        )
        item_1 = MockModel(first=1)
        item_2 = MockModel(first=1)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        qs.add(item_1, item_2, item_3)
        with self.assertRaises(MultipleObjectsReturned):
            qs.update_or_create(first=1, defaults={'second': 2})
Esempio n. 14
0
    def test_query_update_or_create_gets_existing_unique_match_with_defaults(self):
        qs = MockSet(
            model=create_model('first', 'second', 'third')
        )
        item_1 = MockModel(first=1)
        item_2 = MockModel(second=2)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        obj, created = qs.update_or_create(defaults={'first': 3, 'third': 1}, second=2)

        assert hasattr(obj, 'second') and obj.second == 2
        assert created is False
Esempio n. 15
0
    def test_query_update_or_create_gets_existing_unique_match_with_defaults(self):
        qs = MockSet(
            model=create_model('first', 'second', 'third')
        )
        item_1 = MockModel(first=1)
        item_2 = MockModel(second=2)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        obj, created = qs.update_or_create(defaults={'first': 3, 'third': 1}, second=2)

        assert hasattr(obj, 'second') and obj.second == 2
        assert created is False
Esempio n. 16
0
    def test_query_get_or_create_creates_new_model_when_no_match_with_defaults(
            self):
        qs = MockSet(model=create_model('first', 'second', 'third'))
        item_1 = MockModel(first=1)
        item_2 = MockModel(second=2)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        obj, created = qs.get_or_create(defaults={'first': 3}, second=1)

        assert hasattr(obj, 'first') and obj.first == 3
        assert hasattr(obj, 'second') and obj.second == 1
        assert hasattr(obj, 'third') and obj.third is None
        assert created is True
Esempio n. 17
0
    def test_query_update_or_create_creates_new_model_when_no_match_with_defaults(self):
        qs = MockSet(
            model=create_model('first', 'second', 'third')
        )
        item_1 = MockModel(first=1)
        item_2 = MockModel(second=2)
        item_3 = MockModel(third=3)
        qs.add(item_1, item_2, item_3)

        obj, created = qs.update_or_create(defaults={'first': 3}, second=1)

        assert hasattr(obj, 'first') and obj.first == 3
        assert hasattr(obj, 'second') and obj.second == 1
        assert hasattr(obj, 'third') and obj.third is None
        assert created is True
Esempio n. 18
0
    def test_query_datetimes_month(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.datetime(2017, 1, 2, 1, 2, 3))
        item2 = MockModel(date_begin=datetime.datetime(2017, 1, 19, 4, 5, 6))
        item3 = MockModel(date_begin=datetime.datetime(2017, 2, 4, 7, 8, 9))
        qs.add(item1, item2, item3)

        result = qs.datetimes('date_begin', 'month', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 1, 0, 0, 0)
        assert result[1] == datetime.datetime(2017, 2, 1, 0, 0, 0)

        result = qs.datetimes('date_begin', 'month', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 2, 1, 0, 0, 0)
        assert result[1] == datetime.datetime(2017, 1, 1, 0, 0, 0)
Esempio n. 19
0
    def test_query_datetimes_month(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.datetime(2017, 1, 2, 1, 2, 3))
        item2 = MockModel(date_begin=datetime.datetime(2017, 1, 19, 4, 5, 6))
        item3 = MockModel(date_begin=datetime.datetime(2017, 2, 4, 7, 8, 9))
        qs.add(item1, item2, item3)

        result = qs.datetimes('date_begin', 'month', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 1, 0, 0, 0)
        assert result[1] == datetime.datetime(2017, 2, 1, 0, 0, 0)

        result = qs.datetimes('date_begin', 'month', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 2, 1, 0, 0, 0)
        assert result[1] == datetime.datetime(2017, 1, 1, 0, 0, 0)
Esempio n. 20
0
    def test_query_datetimes_second(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 3))
        item2 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 3))
        item3 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 9))

        qs.add(item1, item2, item3)

        result = qs.datetimes('date_begin', 'second', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 10, 1, 2, 3)
        assert result[1] == datetime.datetime(2017, 1, 10, 1, 2, 9)

        result = qs.datetimes('date_begin', 'second', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 10, 1, 2, 9)
        assert result[1] == datetime.datetime(2017, 1, 10, 1, 2, 3)
Esempio n. 21
0
    def test_query_dates_day(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.date(2017, 1, 2))
        item2 = MockModel(date_begin=datetime.date(2017, 2, 14))
        item3 = MockModel(date_begin=datetime.date(2017, 2, 14))

        qs.add(item1, item2, item3)

        result = qs.dates('date_begin', 'day', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.date(2017, 1, 2)
        assert result[1] == datetime.date(2017, 2, 14)

        result = qs.dates('date_begin', 'day', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.date(2017, 2, 14)
        assert result[1] == datetime.date(2017, 1, 2)
Esempio n. 22
0
    def test_query_dates_day(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.date(2017, 1, 2))
        item2 = MockModel(date_begin=datetime.date(2017, 2, 14))
        item3 = MockModel(date_begin=datetime.date(2017, 2, 14))

        qs.add(item1, item2, item3)

        result = qs.dates('date_begin', 'day', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.date(2017, 1, 2)
        assert result[1] == datetime.date(2017, 2, 14)

        result = qs.dates('date_begin', 'day', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.date(2017, 2, 14)
        assert result[1] == datetime.date(2017, 1, 2)
Esempio n. 23
0
    def test_query_datetimes_second(self):
        qs = MockSet(model=create_model('date_begin'))

        item1 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 3))
        item2 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 3))
        item3 = MockModel(date_begin=datetime.datetime(2017, 1, 10, 1, 2, 9))

        qs.add(item1, item2, item3)

        result = qs.datetimes('date_begin', 'second', 'ASC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 10, 1, 2, 3)
        assert result[1] == datetime.datetime(2017, 1, 10, 1, 2, 9)

        result = qs.datetimes('date_begin', 'second', 'DESC')

        assert len(result) == 2
        assert result[0] == datetime.datetime(2017, 1, 10, 1, 2, 9)
        assert result[1] == datetime.datetime(2017, 1, 10, 1, 2, 3)
Esempio n. 24
0
    def test_query_update_returns_number_of_affected_rows(self):
        objects = [MockModel(foo=1), MockModel(foo=1), MockModel(foo=2)]
        qs = MockSet(*objects, model=create_model('foo', 'bar'))
        count = qs.filter(foo=1).update(bar=2)

        assert count == len(objects) - 1, count
Esempio n. 25
0
 def test_create_model_raises_value_error_with_zero_arguments(self):
     with self.assertRaises(ValueError):
         create_model()
Esempio n. 26
0
 def test_query_create_model_raises_value_error_with_zero_arguments(self):
     with self.assertRaises(ValueError):
         create_model()
Esempio n. 27
0
    def test_query_update_returns_number_of_affected_rows(self):
        objects = [MockModel(foo=1), MockModel(foo=1), MockModel(foo=2)]
        qs = MockSet(*objects, model=create_model('foo', 'bar'))
        count = qs.filter(foo=1).update(bar=2)

        assert count == len(objects) - 1, count