def test_cluster_division_separates_cluster_with_unrelated_items(scotland, alba, england, goat): cluster_scotland = Cluster() cluster_scotland.save() cluster_england_and_goat = Cluster() cluster_england_and_goat.save() scotland.cluster = cluster_scotland scotland.save() alba.cluster = cluster_scotland alba.save() england.cluster = cluster_england_and_goat england.save() goat.cluster = cluster_england_and_goat goat.save() Cluster.clear_empty() assert Cluster.objects.count() == 2 Cluster.divide() # Django won't notice the clusters changing unless we re-read fields scotland.refresh_from_db() england.refresh_from_db() goat.refresh_from_db() # We expect one cluster to divide, so our original 2 are now 3 assert Cluster.objects.count() == 3 # Scotland cluster untouched as it has two identical items assert len(cluster_scotland) == 2 # England and Goat should be in their own clusters as they are unrelated assert england.cluster != goat.cluster