def test_dependency_self_referential(self): with self.assertRaisesMessage( RuntimeError, "Can't resolve dependencies for fixtures_regress.Circle3 in " "serialized app list."): serializers.sort_dependencies([('fixtures_regress', [Book, Circle3])])
def test_dependency_sorting_tight_circular_2(self): with self.assertRaisesMessage( RuntimeError, "Can't resolve dependencies for fixtures_regress.Circle1, " "fixtures_regress.Circle2 in serialized app list."): serializers.sort_dependencies([('fixtures_regress', [Circle1, Book, Circle2])])
def test_dependency_sorting_long(self): with self.assertRaisesMessage( RuntimeError, "Can't resolve dependencies for fixtures_regress.Circle1, " "fixtures_regress.Circle2, fixtures_regress.Circle3 in serialized " "app list."): serializers.sort_dependencies([ ('fixtures_regress', [Person, Circle2, Circle1, Circle3, Store, Book]) ])
def test_dependency_sorting_m2m_simple_circular(self): """ Resolving circular M2M relations without explicit through models should fail loudly """ with self.assertRaisesMessage( RuntimeError, "Can't resolve dependencies for fixtures_regress.M2MSimpleCircularA, " "fixtures_regress.M2MSimpleCircularB in serialized app list."): serializers.sort_dependencies([ ('fixtures_regress', [M2MSimpleCircularA, M2MSimpleCircularB]) ])
def get_objects(count_only=False): """ Collate the objects to be serialized. If count_only is True, just count the number of objects to be serialized. """ models = serializers.sort_dependencies(app_list.items()) for model in models: if model in excluded_models: continue if model._meta.proxy and model._meta.proxy_for_model not in models: warnings.warn( "%s is a proxy model and won't be serialized." % model._meta.label, category=ProxyModelWarning, ) if not model._meta.proxy and router.allow_migrate_model( using, model): if use_base_manager: objects = model._base_manager else: objects = model._default_manager queryset = objects.using(using).order_by( model._meta.pk.name) if primary_keys: queryset = queryset.filter(pk__in=primary_keys) if count_only: yield queryset.order_by().count() else: yield from queryset.iterator()
def test_dependency_sorting(self): """ It doesn't matter what order you mention the models, Store *must* be serialized before then Person, and both must be serialized before Book. """ sorted_deps = serializers.sort_dependencies([('fixtures_regress', [Book, Person, Store])]) self.assertEqual(sorted_deps, [Store, Person, Book])
def test_dependency_sorting_m2m_complex_circular_2(self): """ Circular M2M relations with explicit through models should be serializable This test tests the circularity with explicit natural_key.dependencies """ sorted_deps = serializers.sort_dependencies([('fixtures_regress', [ M2MComplexCircular2A, M2MComplexCircular2B, M2MCircular2ThroughAB ])]) self.assertEqual(sorted_deps[:2], [M2MComplexCircular2A, M2MComplexCircular2B]) self.assertEqual(sorted_deps[2:], [M2MCircular2ThroughAB])
def test_dependency_sorting_m2m_complex(self): """ M2M relations with explicit through models should NOT count as dependencies. The through model itself will have dependencies, though. """ sorted_deps = serializers.sort_dependencies([ ('fixtures_regress', [M2MComplexA, M2MComplexB, M2MThroughAB]) ]) # Order between M2MComplexA and M2MComplexB doesn't matter. The through # model has dependencies to them though, so it should come last. self.assertEqual(sorted_deps[-1], M2MThroughAB)
def test_dependency_sorting_m2m_simple(self): """ M2M relations without explicit through models SHOULD count as dependencies Regression test for bugs that could be caused by flawed fixes to #14226, namely if M2M checks are removed from sort_dependencies altogether. """ sorted_deps = serializers.sort_dependencies([ ('fixtures_regress', [M2MSimpleA, M2MSimpleB]) ]) self.assertEqual(sorted_deps, [M2MSimpleB, M2MSimpleA])
def test_dependency_sorting_m2m_complex_circular_1(self): """ Circular M2M relations with explicit through models should be serializable """ A, B, C, AtoB, BtoC, CtoA = (M2MComplexCircular1A, M2MComplexCircular1B, M2MComplexCircular1C, M2MCircular1ThroughAB, M2MCircular1ThroughBC, M2MCircular1ThroughCA) sorted_deps = serializers.sort_dependencies([ ('fixtures_regress', [A, B, C, AtoB, BtoC, CtoA]) ]) # The dependency sorting should not result in an error, and the # through model should have dependencies to the other models and as # such come last in the list. self.assertEqual(sorted_deps[:3], [A, B, C]) self.assertEqual(sorted_deps[3:], [AtoB, BtoC, CtoA])
def get_objects(): for model in serializers.sort_dependencies(app_list): if (model._meta.can_migrate(self.connection) and router.allow_migrate_model(self.connection.alias, model)): queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name) yield from queryset.iterator()
def test_dependency_sorting_normal(self): sorted_deps = serializers.sort_dependencies([ ('fixtures_regress', [Person, ExternalDependency, Book]) ]) self.assertEqual(sorted_deps, [Person, Book, ExternalDependency])
def test_dependency_sorting_dangling(self): sorted_deps = serializers.sort_dependencies([ ('fixtures_regress', [Person, Circle1, Store, Book]) ]) self.assertEqual(sorted_deps, [Circle1, Store, Person, Book])
def test_dependency_sorting_5(self): sorted_deps = serializers.sort_dependencies([('fixtures_regress', [Person, Book, Store])]) self.assertEqual(sorted_deps, [Store, Person, Book])