Repository: wagtail/wagtail
File: wagtail/tests/test_streamfield.py (Python)

Code before:

        # the body should now be a stream consisting of a single rich_text block
        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 1)
        self.assertIsInstance(fetched_body[0].value, RichText)
        self.assertEqual(fetched_body[0].value.source, "<h2>hello world</h2>")

    def test_can_append(self):
        self.json_body.body.append(("text", "bar"))
        self.json_body.save()

        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 2)
        self.assertEqual(fetched_body[0].block_type, "text")
        self.assertEqual(fetched_body[0].value, "foo")
        self.assertEqual(fetched_body[1].block_type, "text")
        self.assertEqual(fetched_body[1].value, "bar")

    def test_can_append_on_queried_instance(self):
        # The test is analog to test_can_append(), but instead of working with the
        # in-memory version from JSONStreamModel.objects.create(), we query a fresh
        # instance from the db.
        # The difference is that the append below adds its data to child blocks that
        # have not yet been lazy loaded, which caused a crash in previous versions.
        self.json_body = JSONStreamModel.objects.get(pk=self.json_body.pk)
        self.json_body.body.append(("text", "bar"))
        self.json_body.save()

        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 2)
        self.assertEqual(fetched_body[0].block_type, "text")
        self.assertEqual(fetched_body[0].value, "foo")
        self.assertEqual(fetched_body[1].block_type, "text")
        self.assertEqual(fetched_body[1].value, "bar")

    def test_complex_assignment(self):
        page = StreamPage(title="Test page", body=[])
        page.body = [
            ("rich_text", "<h2>hello world</h2>"),
            (
                "books",
                [
                    ("title", "Great Expectations"),
                    ("author", "Charles Dickens"),
                ],
            ),
        ]
        self.assertEqual(page.body[0].block_type, "rich_text")

Review comment (bug):
How about

```suggestion
        # It tests adding data to child blocks that
        # have not yet been lazy loaded. This would previously crash.
```
?

Code after:

        # the body should now be a stream consisting of a single rich_text block
        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 1)
        self.assertIsInstance(fetched_body[0].value, RichText)
        self.assertEqual(fetched_body[0].value.source, "<h2>hello world</h2>")

    def test_can_append(self):
        self.json_body.body.append(("text", "bar"))
        self.json_body.save()

        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 2)
        self.assertEqual(fetched_body[0].block_type, "text")
        self.assertEqual(fetched_body[0].value, "foo")
        self.assertEqual(fetched_body[1].block_type, "text")
        self.assertEqual(fetched_body[1].value, "bar")

    def test_can_append_on_queried_instance(self):
        # The test is analog to test_can_append(), but instead of working with the
        # in-memory version from JSONStreamModel.objects.create(), we query a fresh
        # instance from the db.
        # It tests adding data to child blocks that
        # have not yet been lazy loaded. This would previously crash.
        self.json_body = JSONStreamModel.objects.get(pk=self.json_body.pk)
        self.json_body.body.append(("text", "bar"))
        self.json_body.save()

        fetched_body = JSONStreamModel.objects.get(id=self.json_body.id).body
        self.assertIsInstance(fetched_body, StreamValue)
        self.assertEqual(len(fetched_body), 2)
        self.assertEqual(fetched_body[0].block_type, "text")
        self.assertEqual(fetched_body[0].value, "foo")
        self.assertEqual(fetched_body[1].block_type, "text")
        self.assertEqual(fetched_body[1].value, "bar")

    def test_complex_assignment(self):
        page = StreamPage(title="Test page", body=[])
        page.body = [
            ("rich_text", "<h2>hello world</h2>"),
            (
                "books",
                [
                    ("title", "Great Expectations"),
                    ("author", "Charles Dickens"),
                ],
            ),
        ]
        self.assertEqual(page.body[0].block_type, "rich_text")