<commit_before>from rest_framework import status
from django.test import TestCase
from kpi.models.collection import Collection
from kpi.models.survey_asset import SurveyAsset
from django.contrib.auth.models import User

class CreateCollectionTests(TestCase):
    fixtures = ['test_data']

    def setUp(self):
        self.user = User.objects.all()[0]
        self.coll = Collection.objects.create(owner=self.user)

    def test_import_survey_assets_to_collection(self):
        self.assertEqual(self.coll.survey_assets.count(), 0)
        self.coll.survey_assets.create(name='test', content=[
                {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                {'type': 'text', 'label': 'Q2', 'name': 'q2'},
            ])
        self.assertEqual(self.coll.survey_assets.count(), 1)

    def test_assets_are_deleted_with_collection(self):
        '''
        right now, this does make it easy to delete survey_assets within a
        collection.
        '''
        asset = self.coll.survey_assets.create(name='test', content=[
                {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                {'type': 'text', 'label': 'Q2', 'name': 'q2'},
            ])
        self.assertEqual(SurveyAsset.objects.filter(id=asset.id).count(), 1)
        self.assertEqual(SurveyAsset.objects.count(), 1)
        self.coll.delete()
        self.assertEqual(SurveyAsset.objects.filter(id=asset.id).count(), 0)
        self.assertEqual(SurveyAsset.objects.count(), 0)

    def test_descendants_are_deleted_with_collection(self):
        # TODO
        # also assets of descendants
        pass

    def test_create_collection_with_survey_assets(self):
        self.assertEqual(Collection.objects.count(), 1)
        self.assertEqual(SurveyAsset.objects.count(), 0)
        Collection.objects.create(name='test_collection', owner=self.user, survey_assets=[
                {
                    'name': 'test_survey_asset',
                    'content': [
                        {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                    ]
                },
                {
                    'name': 'test_survey_asset',
                    'content': [
                        {'type': 'text', 'label': 'Q2', 'name': 'q2'},
                    ]
                },
            ])
        self.assertEqual(SurveyAsset.objects.count(), 2)
        self.assertEqual(Collection.objects.count(), 2)

    def test_create_child_collection(self):
        self.assertEqual(Collection.objects.count(), 1)
        child = Collection.objects.create(name='test_child_collection',
            owner=User.objects.first(), parent=self.coll)
        self.assertEqual(Collection.objects.count(), 2)
        self.assertEqual(self.coll.get_children()[0], child)
        self.assertEqual(self.coll.get_children().count(), 1)
        self.assertEqual(child.get_ancestors()[0], self.coll)
        self.assertEqual(child.get_ancestors().count(), 1)

    # Leave in this class or create a new one?
    def test_move_standalone_collection_into_collection(self):
        # TODO
        pass

    def test_move_collection_from_collection_to_standalone(self):
        # TODO
        pass

    def test_move_collection_between_collections(self):
        # TODO
        pass

class ShareCollectionTests(TestCase):
    fixtures = ['test_data']

    def setUp(self):
        # TODO
        pass

    def test_user_view_permission_on_standalone_collection(self):
        # TODO
        # Grant and revoke for all of these
        pass

    def test_user_edit_permission_on_standalone_collection(self):
        # TODO
        pass

    def test_user_view_permission_on_parent_collection(self):
        # TODO
        # Child with no direct permissions must be affected accordingly
        pass

    def test_user_edit_permission_on_parent_collection(self):
        # TODO
        pass

    def test_user_view_permission_on_child_collection(self):
        # TODO<commit_msg>Collection tree tests<commit_after>from rest_framework import status
from django.test import TestCase
from kpi.models.collection import Collection
from kpi.models.survey_asset import SurveyAsset
from django.contrib.auth.models import User

class CreateCollectionTests(TestCase):
    fixtures = ['test_data']

    def setUp(self):
        self.user = User.objects.all()[0]
        self.coll = Collection.objects.create(owner=self.user)

    def test_import_survey_assets_to_collection(self):
        self.assertEqual(self.coll.survey_assets.count(), 0)
        self.coll.survey_assets.create(name='test', content=[
                {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                {'type': 'text', 'label': 'Q2', 'name': 'q2'},
            ])
        self.assertEqual(self.coll.survey_assets.count(), 1)

    def test_assets_are_deleted_with_collection(self):
        '''
        right now, this does make it easy to delete survey_assets within a
        collection.
        '''
        asset = self.coll.survey_assets.create(name='test', content=[
                {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                {'type': 'text', 'label': 'Q2', 'name': 'q2'},
            ])
        self.assertEqual(SurveyAsset.objects.filter(id=asset.id).count(), 1)
        self.assertEqual(SurveyAsset.objects.count(), 1)
        self.coll.delete()
        self.assertEqual(SurveyAsset.objects.filter(id=asset.id).count(), 0)
        self.assertEqual(SurveyAsset.objects.count(), 0)

    def test_descendants_are_deleted_with_collection(self):
        self.assertEqual(Collection.objects.count(), 1)
        child = Collection.objects.create(name='test_child_collection',
            owner=User.objects.first(), parent=self.coll)
        grandchild = Collection.objects.create(name='test_child_collection',
            owner=User.objects.first(), parent=child)
        self.assertEqual(Collection.objects.count(), 3)
        child_sa = child.survey_assets.create()
        grandchild_sa = grandchild.survey_assets.create()
        self.assertEqual(SurveyAsset.objects.count(), 2)
        self.coll.delete()
        self.assertEqual(Collection.objects.count(), 0)
        self.assertEqual(SurveyAsset.objects.count(), 0)

    def test_create_collection_with_survey_assets(self):
        self.assertEqual(Collection.objects.count(), 1)
        self.assertEqual(SurveyAsset.objects.count(), 0)
        Collection.objects.create(name='test_collection', owner=self.user, survey_assets=[
                {
                    'name': 'test_survey_asset',
                    'content': [
                        {'type': 'text', 'label': 'Q1', 'name': 'q1'},
                    ]
                },
                {
                    'name': 'test_survey_asset',
                    'content': [
                        {'type': 'text', 'label': 'Q2', 'name': 'q2'},
                    ]
                },
            ])
        self.assertEqual(SurveyAsset.objects.count(), 2)
        self.assertEqual(Collection.objects.count(), 2)

    def test_create_child_collection(self):
        self.assertEqual(Collection.objects.count(), 1)
        child = Collection.objects.create(name='test_child_collection',
            owner=User.objects.first(), parent=self.coll)
        self.assertEqual(Collection.objects.count(), 2)
        self.assertEqual(self.coll.get_children()[0], child)
        self.assertEqual(self.coll.get_children().count(), 1)
        self.assertEqual(child.get_ancestors()[0], self.coll)
        self.assertEqual(child.get_ancestors().count(), 1)

    # Leave in this class or create a new one?
    def test_move_standalone_collection_into_collection(self):
        self.assertEqual(Collection.objects.count(), 1)
        standalone = Collection.objects.create(name='move_me',
            owner=User.objects.first())
        self.assertEqual(Collection.objects.count(), 2)
        self.assertEqual(standalone.parent, None)
        standalone.parent = self.coll
        standalone.save()
        self.assertEqual(self.coll.get_children()[0], standalone)
        self.assertEqual(self.coll.get_children().count(), 1)
        self.assertEqual(standalone.get_ancestors()[0], self.coll)
        self.assertEqual(standalone.get_ancestors().count(), 1)

    def test_move_collection_from_collection_to_standalone(self):
        self.assertEqual(Collection.objects.count(), 1)
        child = Collection.objects.create(name='move_me_too',
            owner=User.objects.first(), parent=self.coll)
        self.assertEqual(Collection.objects.count(), 2)
        self.assertEqual(child.parent, self.coll)
        child.parent = None
        child.save()
        self.assertEqual(self.coll.get_children().count(), 0)
        self.assertEqual(child.get_ancestors().count(), 0)

    def test_move_collection_between_collections(self):
        self.assertEqual(Collection.objects.count(), 1)
        adoptive_parent = Collection.objects.create(name='adoptive_parent',
            owner=User.objects.first())
        child = Collection.objects.create(name='on_the_move',
            owner=User.objects.first(), parent=self.coll)
        self.assertEqual(Collection.objects.count(), 3)
        self.assertEqual(self.coll.get_children()[0], child)
        self.assertEqual(self.coll.get_children().count(), 1)
        self.assertEqual(child.get_ancestors()[0], self.coll)
        self.assertEqual(child.get_ancestors().count(), 1)
        child.parent = adoptive_parent
        child.save()
        self.assertEqual(self.coll.get_children().count(), 0)
        self.assertEqual(adoptive_parent.get_children()[0], child)
        self.assertEqual(adoptive_parent.get_children().count(), 1)
        self.assertEqual(child.get_ancestors()[0], adoptive_parent)
        self.assertEqual(child.get_ancestors().count(), 1)

class ShareCollectionTests(TestCase):
    fixtures = ['test_data']

    def setUp(self):
        # TODO
        pass

    def test_user_view_permission_on_standalone_collection(self):
        # TODO
        # Grant and revoke for all of these
        pass

    def test_user_edit_permission_on_standalone_collection(self):
        # TODO
        pass

    def test_user_view_permission_on_parent_collection(self):
        # TODO
        # Child with no direct permissions must be affected accordingly
        pass

    def test_user_edit_permission_on_parent_collection(self):
        # TODO
        pass

    def test_user_view_permission_on_child_collection(self):
        # TODO
{'fraction_non_alphanumeric': 0.12479709729781342, 'fraction_numerical': 0.008020624462904613, 'mean_word_length': 2.6545708304256803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2735736', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2915, 'n_words': 501}