def test_multiple_copy_channel_ids(self):
        """
        This test ensures that as we copy nodes across various channels, that their original_channel_id and
        source_channel_id values are properly updated.
        """
        title = "Dolly"
        num_nodes = 10
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        new_node = ContentNode.objects.create(title="Heyo!",
                                              parent=self.channel.main_tree, kind=topic)
        self.channel.save()
        _create_nodes(num_nodes, title, parent=new_node)

        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.get_channel() == self.channel

        assert self.channel.main_tree.parent is None
        _check_nodes(new_node, title, original_channel_id=self.channel.id,
                     source_channel_id=self.channel.id, channel=self.channel)

        channels = [
            self.channel,
            testdata.channel(),
            testdata.channel(),
            testdata.channel(),
            testdata.channel()
        ]

        copy_node_root = new_node
        for i in range(1, len(channels)):
            print("Copying channel {} nodes to channel {}".format(i - 1, i))
            channel = channels[i]
            prev_channel = channels[i - 1]

            prev_channel.main_tree._mark_unchanged()
            prev_channel.main_tree.changed = False
            assert prev_channel.main_tree.get_changed_fields() == {}
            prev_channel.main_tree.save()
            prev_channel.main_tree.refresh_from_db()
            assert prev_channel.main_tree.changed is False

            # simulate a clean, right-after-publish state to ensure only new channel
            # is marked as change
            channel.main_tree.changed = False
            channel.main_tree.save()
            channel.main_tree.refresh_from_db()
            assert channel.main_tree.changed is False

            # make sure we always copy the copy we made in the previous go around :)
            copy_node_root = duplicate_node_bulk(copy_node_root, parent=channel.main_tree)

            _check_nodes(copy_node_root, original_channel_id=self.channel.id,
                         source_channel_id=prev_channel.id, channel=channel)
            channel.main_tree.refresh_from_db()
            assert channel.main_tree.changed is True
            assert channel.main_tree.get_descendants().filter(changed=True).exists()

            prev_channel.main_tree.refresh_from_db()
            assert prev_channel.main_tree.changed is False
    def test_multiple_copy_channel_ids(self):
        """
        This test ensures that as we copy nodes across various channels, that their original_channel_id and
        source_channel_id values are properly updated.
        """
        title = "Dolly"
        num_nodes = 10
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        new_node = ContentNode.objects.create(title="Heyo!", parent=self.channel.main_tree, kind=topic)
        self.channel.save()
        # assert self.channel.main_tree.get_root() == self.channel.main_tree
        # assert self.channel.main_tree.get_channel() == self.channel
        _create_nodes(num_nodes, title, parent=new_node)

        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.get_channel() == self.channel

        assert self.channel.main_tree.parent is None
        _check_nodes(new_node, title, original_channel_id=self.channel.id, source_channel_id=self.channel.id, channel=self.channel)

        channels = [
            self.channel,
            testdata.channel(),
            testdata.channel(),
            testdata.channel(),
            testdata.channel()
        ]

        copy_node_root = new_node
        for i in range(1, len(channels)):
            print("Copying channel {} nodes to channel {}".format(i - 1, i))
            channel = channels[i]
            prev_channel = channels[i - 1]

            prev_channel.main_tree._mark_unchanged()
            prev_channel.main_tree.changed = False
            assert prev_channel.main_tree.get_changed_fields() == {}
            prev_channel.main_tree.save()
            prev_channel.main_tree.refresh_from_db()
            assert prev_channel.main_tree.changed is False

            # simulate a clean, right-after-publish state to ensure only new channel is marked as change
            channel.main_tree.changed = False
            channel.main_tree.save()
            channel.main_tree.refresh_from_db()
            assert channel.main_tree.changed is False

            # make sure we always copy the copy we made in the previous go around :)
            copy_node_root = nodes.duplicate_node_bulk(copy_node_root, parent=channel.main_tree)

            _check_nodes(copy_node_root, original_channel_id=self.channel.id, source_channel_id=prev_channel.id, channel=channel)
            channel.main_tree.refresh_from_db()
            assert channel.main_tree.changed is True
            assert channel.main_tree.get_descendants().filter(changed=True).exists()

            prev_channel.main_tree.refresh_from_db()
            assert prev_channel.main_tree.changed is False
Exemple #3
0
    def test_channel_stats(self):
        """
        Test that correct channel data is included in the get_channel_stats call.
        """
        num_channels = 10
        num_public = 0
        lang = Language.objects.all().first()
        for _ in range(num_channels):
            c = channel(name=random_string(20))
            c.language = lang
            public = bool(random.randint(0, 1))
            if public:
                num_public += 1
                c.public = public
            c.save()
        assert num_public > 0

        num_private = num_channels - num_public
        public_ids = Channel.objects.filter(public=True).values_list(
            'main_tree__tree_id', flat=True)
        public_node_count = ContentNode.objects.filter(
            tree_id__in=public_ids).count()

        stats = get_channel_stats()
        assert stats['deleted']['count'] == 0
        assert stats['public']['count'] == num_public
        assert stats['public']['node_count'] == public_node_count
        assert stats['public']['languages'] == [(lang.lang_code, num_public)]
        assert stats['private']['count'] == num_private
        assert stats['private']['languages'] == [(lang.lang_code, num_private)]
Exemple #4
0
 def setUp(self):
     super(BaseAPITestCase, self).setUp()
     self.channel = testdata.channel()
     self.user = testdata.user()
     token, _new = Token.objects.get_or_create(user=self.user)
     self.header = {"Authorization": "Token {0}".format(token)}
     self.client = APIClient()
     self.client.force_authenticate(self.user)
     self.channel.main_tree.refresh_from_db()
Exemple #5
0
 def setUp(self):
     super(BaseAPITestCase, self).setUp()
     self.channel = testdata.channel()
     self.user = testdata.user()
     self.channel.editors.add(self.user)
     self.token, _new = Token.objects.get_or_create(user=self.user)
     self.client = APIClient()
     self.client.force_authenticate(
         self.user)  # This will skip all authentication checks
     self.channel.main_tree.refresh_from_db()
Exemple #6
0
    def test_duplicate_nodes(self):
        """
        Ensures that when we copy nodes, the new channel gets marked as changed but the old channel doesn't,
        and that the nodes point to the new channel.
        """
        num_nodes = 10
        title = "Dolly"
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.channel.main_tree = ContentNode.objects.create(title="Heyo!",
                                                            kind=topic)
        self.channel.save()
        # assert self.channel.main_tree.get_root() == self.channel.main_tree
        # assert self.channel.main_tree.get_channel() == self.channel
        _create_nodes(num_nodes, title, parent=self.channel.main_tree)

        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.get_channel() == self.channel

        assert self.channel.main_tree.parent is None
        _check_nodes(self.channel.main_tree,
                     title,
                     original_channel_id=self.channel.id,
                     source_channel_id=self.channel.id,
                     channel=self.channel)

        new_channel = testdata.channel()

        # simulate a clean, right-after-publish state to ensure only new channel is marked as change
        self.channel.main_tree.changed = False
        self.channel.main_tree.save()
        self.channel.main_tree.refresh_from_db()
        assert self.channel.main_tree.changed is False

        new_channel.main_tree.changed = False
        new_channel.main_tree.save()
        new_channel.main_tree.refresh_from_db()
        assert new_channel.main_tree.changed is False

        new_tree = nodes.duplicate_node_bulk(self.channel.main_tree,
                                             parent=new_channel.main_tree)

        _check_nodes(new_tree,
                     title,
                     original_channel_id=self.channel.id,
                     source_channel_id=self.channel.id,
                     channel=new_channel)
        new_channel.main_tree.refresh_from_db()
        assert new_channel.main_tree.changed is True

        self.channel.main_tree.refresh_from_db()
        assert self.channel.main_tree.changed is False
    def _setup_original_and_deriative_nodes(self):
        # Setup original channel
        self.channel = testdata.channel()  # done in base class but doesn't hurt to do again...
        self.channel.main_tree = self._create_minimal_tree(withsubs=False)
        self.channel.save()

        # Setup derivative channel
        self.new_channel = Channel.objects.create(name='derivative of teschannel', source_id='lkajs')
        self.new_channel.save()
        self.new_channel.main_tree = self._create_empty_tree()
        self.new_channel.main_tree.save()
        new_tree = nodes.duplicate_node_bulk(self.channel.main_tree, parent=self.new_channel.main_tree)
        self.new_channel.main_tree = new_tree
        # self.new_channel.main_tree.save()   #  InvalidMove: A node may not be made a child of any of its descendants.
        self.new_channel.main_tree.refresh_from_db()

        # Return video nodes we need for this test
        orig_video = self.channel.main_tree.children.all()[0]
        cloned_video = self.new_channel.main_tree.children.all()[0]
        return orig_video, cloned_video
    def _setup_original_and_deriative_nodes(self):
        # Setup original channel
        self.channel = testdata.channel()  # done in base class but doesn't hurt to do again...
        self.channel.main_tree = self._create_minimal_tree(withsubs=False)
        self.channel.save()

        # Setup derivative channel
        self.new_channel = Channel.objects.create(name='derivative of teschannel', source_id='lkajs')
        self.new_channel.save()
        self.new_channel.main_tree = self._create_empty_tree()
        self.new_channel.main_tree.save()
        new_tree = duplicate_node_bulk(self.channel.main_tree,
                                       parent=self.new_channel.main_tree)
        self.new_channel.main_tree = new_tree
        self.new_channel.main_tree.refresh_from_db()

        # Return video nodes we need for this test
        orig_video = self.channel.main_tree.children.all()[0]
        cloned_video = self.new_channel.main_tree.children.all()[0]
        return orig_video, cloned_video
    def test_duplicate_nodes(self):
        """
        Ensures that when we copy nodes, the new channel gets marked as changed but the old channel doesn't,
        and that the nodes point to the new channel.
        """
        num_nodes = 10
        title = "Dolly"
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.channel.main_tree = ContentNode.objects.create(title="Heyo!", kind=topic)
        self.channel.save()
        # assert self.channel.main_tree.get_root() == self.channel.main_tree
        # assert self.channel.main_tree.get_channel() == self.channel
        _create_nodes(num_nodes, title, parent=self.channel.main_tree)

        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.get_channel() == self.channel

        assert self.channel.main_tree.parent is None
        _check_nodes(self.channel.main_tree, title, original_channel_id=self.channel.id, source_channel_id=self.channel.id, channel=self.channel)

        new_channel = testdata.channel()

        # simulate a clean, right-after-publish state to ensure only new channel is marked as change
        self.channel.main_tree.changed = False
        self.channel.main_tree.save()
        self.channel.main_tree.refresh_from_db()
        assert self.channel.main_tree.changed is False

        new_channel.main_tree.changed = False
        new_channel.main_tree.save()
        new_channel.main_tree.refresh_from_db()
        assert new_channel.main_tree.changed is False

        new_tree = nodes.duplicate_node_bulk(self.channel.main_tree, parent=new_channel.main_tree)

        _check_nodes(new_tree, title, original_channel_id=self.channel.id, source_channel_id=self.channel.id, channel=new_channel)
        new_channel.main_tree.refresh_from_db()
        assert new_channel.main_tree.changed is True

        self.channel.main_tree.refresh_from_db()
        assert self.channel.main_tree.changed is False
    def setUp(self):
        super(NodeGettersTestCase, self).setUp()

        self.channel = testdata.channel()
        self.topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.thumbnail_data = "allyourbase64arebelongtous"
    def test_move_nodes(self):
        """
        Ensures that moving nodes properly removes them from the original parent
        and adds them to the new one, and marks the new and old parents as changed,
        and that the node channel info gets updated as well.
        """
        title = "A Node on the Move"
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.channel.main_tree = ContentNode.objects.create(title="Heyo!", kind=topic)
        self.channel.save()
        _create_nodes(10, title, parent=self.channel.main_tree)

        assert self.channel.main_tree.get_descendant_count() == 10
        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.parent is None

        _check_nodes(self.channel.main_tree, title, original_channel_id=self.channel.id,
                     source_channel_id=self.channel.id, channel=self.channel)

        new_channel = testdata.channel()
        new_channel.editors.add(self.user)
        new_channel.main_tree.get_children().delete()
        new_channel_node_count = new_channel.main_tree.get_descendants().count()

        nodes = []

        for node in self.channel.main_tree.get_children():
            nodes.append({'id': node.pk})

        assert self.channel.main_tree.pk not in [node['id'] for node in nodes]

        # simulate a clean, right-after-publish state for both trees to ensure they are marked
        # changed after this
        self.channel.main_tree.changed = False
        self.channel.main_tree.save()
        new_channel.main_tree.changed = False
        new_channel.main_tree.save()

        move_nodes(new_channel.id, new_channel.main_tree.id, nodes,
                   min_order=0, max_order=len(nodes))

        ContentNode.objects.partial_rebuild(self.channel.main_tree.tree_id)
        self.channel.main_tree.refresh_from_db()
        new_channel.main_tree.refresh_from_db()

        # these can get out of sync if we don't do a rebuild
        assert self.channel.main_tree.get_descendants().count() ==\
            self.channel.main_tree.get_descendant_count()

        assert self.channel.main_tree != new_channel.main_tree
        assert self.channel.main_tree.changed is True
        assert new_channel.main_tree.changed is True

        assert self.channel.main_tree.get_descendant_count() == 0
        if new_channel.main_tree.get_descendants().count() > 10:
            def recursive_print(node, indent=0):
                for child in node.get_children():
                    print("{}Node: {}".format(" " * indent, child.title))
                    recursive_print(child, indent + 4)
            recursive_print(new_channel.main_tree)

        assert new_channel.main_tree.get_descendants().count() == new_channel_node_count + 10

        assert not self.channel.main_tree.get_descendants().filter(changed=True).exists()
        assert new_channel.main_tree.get_descendants().filter(changed=True).exists()

        # TODO: Should a newly created node that was moved still have the channel it was moved
        # from as its origin/source
        _check_nodes(new_channel.main_tree, title=title, original_channel_id=self.channel.id,
                     source_channel_id=self.channel.id, channel=new_channel)
Exemple #12
0
 def setUp(self):
     super(BaseTestCase, self).setUp()
     self.channel = testdata.channel()
     self.user = testdata.user()
     self.channel.main_tree.refresh_from_db()
    def setUp(self):
        super(NodeOperationsTestCase, self).setUp()

        self.channel = testdata.channel()
    def setUp(self):
        super(NodeOperationsTestCase, self).setUp()

        self.channel = testdata.channel()
    def setUp(self):
        super(NodeGettersTestCase, self).setUp()

        self.channel = testdata.channel()
        self.topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.thumbnail_data = "allyourbase64arebelongtous"
Exemple #16
0
 def setUp(self):
     super(BaseTestCase, self).setUp()
     self.channel = testdata.channel()
     self.user = testdata.user()
     self.channel.editors.add(self.user)
     self.channel.main_tree.refresh_from_db()
    def test_move_nodes(self):
        """
        Ensures that moving nodes properly removes them from the original parent and adds them to the new one,
        and marks the new and old parents as changed, and that the node channel info gets updated as well.
        """
        title = "A Node on the Move"
        topic, _created = ContentKind.objects.get_or_create(kind="Topic")
        self.channel.main_tree = ContentNode.objects.create(title="Heyo!", kind=topic)
        self.channel.save()
        _create_nodes(10, title, parent=self.channel.main_tree)

        assert self.channel.main_tree.get_descendant_count() == 10
        assert self.channel.main_tree.changed is True
        assert self.channel.main_tree.parent is None

        _check_nodes(self.channel.main_tree, title, original_channel_id=self.channel.id, source_channel_id=self.channel.id, channel=self.channel)

        new_channel = testdata.channel()
        new_channel.editors.add(self.user)
        new_channel.main_tree.get_children().delete()
        new_channel_node_count = new_channel.main_tree.get_descendants().count()

        move_data = {
            'target_parent': new_channel.main_tree.id,
            'channel_id': new_channel.id,
            'nodes': []
        }

        for node in self.channel.main_tree.get_children():
            move_data['nodes'].append({'id': node.pk})

        assert self.channel.main_tree.pk not in [node['id'] for node in move_data['nodes']]

        # simulate a clean, right-after-publish state for both trees to ensure they are marked changed after this
        self.channel.main_tree.changed = False
        self.channel.main_tree.save()
        new_channel.main_tree.changed = False
        new_channel.main_tree.save()

        request = self.create_post_request(reverse_lazy('move_nodes'), data=json.dumps(move_data), content_type='application/json')
        nodes.move_nodes(request)

        ContentNode.objects.partial_rebuild(self.channel.main_tree.tree_id)
        self.channel.main_tree.refresh_from_db()
        new_channel.main_tree.refresh_from_db()

        # these can get out of sync if we don't do a rebuild
        assert self.channel.main_tree.get_descendants().count() == self.channel.main_tree.get_descendant_count()

        assert self.channel.main_tree != new_channel.main_tree
        assert self.channel.main_tree.changed is True
        assert new_channel.main_tree.changed is True

        assert self.channel.main_tree.get_descendant_count() == 0
        if new_channel.main_tree.get_descendants().count() > 10:
            def recursive_print(node, indent=0):
                for child in node.get_children():
                    print("{}Node: {}".format(" " * indent, child.title))
                    recursive_print(child, indent + 4)
            recursive_print(new_channel.main_tree)

        assert new_channel.main_tree.get_descendants().count() == new_channel_node_count + 10

        assert not self.channel.main_tree.get_descendants().filter(changed=True).exists()
        assert new_channel.main_tree.get_descendants().filter(changed=True).exists()

        # TODO: Should a newly created node that was moved still have the channel it was moved from as its origin/source
        _check_nodes(new_channel.main_tree, title=title, original_channel_id=self.channel.id, source_channel_id=self.channel.id, channel=new_channel)