1. from operator import attrgetter
    
  2. 
    
  3. from django.db import models
    
  4. from django.test import SimpleTestCase, TestCase
    
  5. from django.test.utils import isolate_apps
    
  6. 
    
  7. from .base_tests import BaseOrderWithRespectToTests
    
  8. from .models import Answer, Dimension, Entity, Post, Question
    
  9. 
    
  10. 
    
  11. class OrderWithRespectToBaseTests(BaseOrderWithRespectToTests, TestCase):
    
  12.     Answer = Answer
    
  13.     Post = Post
    
  14.     Question = Question
    
  15. 
    
  16. 
    
  17. class OrderWithRespectToTests(SimpleTestCase):
    
  18.     @isolate_apps("order_with_respect_to")
    
  19.     def test_duplicate_order_field(self):
    
  20.         class Bar(models.Model):
    
  21.             class Meta:
    
  22.                 app_label = "order_with_respect_to"
    
  23. 
    
  24.         class Foo(models.Model):
    
  25.             bar = models.ForeignKey(Bar, models.CASCADE)
    
  26.             order = models.OrderWrt()
    
  27. 
    
  28.             class Meta:
    
  29.                 order_with_respect_to = "bar"
    
  30.                 app_label = "order_with_respect_to"
    
  31. 
    
  32.         count = 0
    
  33.         for field in Foo._meta.local_fields:
    
  34.             if isinstance(field, models.OrderWrt):
    
  35.                 count += 1
    
  36. 
    
  37.         self.assertEqual(count, 1)
    
  38. 
    
  39. 
    
  40. class TestOrderWithRespectToOneToOnePK(TestCase):
    
  41.     def test_set_order(self):
    
  42.         e = Entity.objects.create()
    
  43.         d = Dimension.objects.create(entity=e)
    
  44.         c1 = d.component_set.create()
    
  45.         c2 = d.component_set.create()
    
  46.         d.set_component_order([c1.id, c2.id])
    
  47.         self.assertQuerysetEqual(
    
  48.             d.component_set.all(), [c1.id, c2.id], attrgetter("pk")
    
  49.         )