2010-10-09 01:47:11 +02:00
|
|
|
from operator import attrgetter
|
|
|
|
|
2014-05-29 01:38:59 +02:00
|
|
|
from django.db import models
|
2018-11-26 20:05:02 +01:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2015-11-17 06:39:28 +01:00
|
|
|
from django.test.utils import isolate_apps
|
2014-05-29 01:38:59 +02:00
|
|
|
|
2016-01-02 12:00:25 +01:00
|
|
|
from .base_tests import BaseOrderWithRespectToTests
|
2015-11-20 17:31:39 +01:00
|
|
|
from .models import Answer, Dimension, Entity, Post, Question
|
2010-10-09 01:47:11 +02:00
|
|
|
|
|
|
|
|
2016-01-02 12:00:25 +01:00
|
|
|
class OrderWithRespectToBaseTests(BaseOrderWithRespectToTests, TestCase):
|
2015-03-26 17:52:11 +01:00
|
|
|
Answer = Answer
|
2016-01-02 12:00:25 +01:00
|
|
|
Post = Post
|
2015-03-26 17:52:11 +01:00
|
|
|
Question = Question
|
|
|
|
|
2014-11-27 23:00:28 +01:00
|
|
|
|
2018-11-26 20:05:02 +01:00
|
|
|
class OrderWithRespectToTests(SimpleTestCase):
|
2015-11-17 06:39:28 +01:00
|
|
|
@isolate_apps("order_with_respect_to")
|
2014-05-29 01:38:59 +02:00
|
|
|
def test_duplicate_order_field(self):
|
|
|
|
class Bar(models.Model):
|
2015-03-26 17:52:11 +01:00
|
|
|
class Meta:
|
|
|
|
app_label = "order_with_respect_to"
|
2014-05-29 01:38:59 +02:00
|
|
|
|
|
|
|
class Foo(models.Model):
|
2015-07-22 16:43:21 +02:00
|
|
|
bar = models.ForeignKey(Bar, models.CASCADE)
|
2014-05-29 01:38:59 +02:00
|
|
|
order = models.OrderWrt()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
order_with_respect_to = "bar"
|
2015-03-26 17:52:11 +01:00
|
|
|
app_label = "order_with_respect_to"
|
2014-05-29 01:38:59 +02:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
for field in Foo._meta.local_fields:
|
|
|
|
if isinstance(field, models.OrderWrt):
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
2015-11-20 17:31:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestOrderWithRespectToOneToOnePK(TestCase):
|
|
|
|
def test_set_order(self):
|
|
|
|
e = Entity.objects.create()
|
|
|
|
d = Dimension.objects.create(entity=e)
|
|
|
|
c1 = d.component_set.create()
|
|
|
|
c2 = d.component_set.create()
|
|
|
|
d.set_component_order([c1.id, c2.id])
|
2022-09-24 12:29:58 +02:00
|
|
|
self.assertQuerySetEqual(
|
2015-11-20 17:31:39 +01:00
|
|
|
d.component_set.all(), [c1.id, c2.id], attrgetter("pk")
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|