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