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