WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.

0 downloads

dbbasic-content/tests/test_blocks.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-content.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-content.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-content.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Release v0.2.0 - Pages support 25b7e5e 1 year, 1 month ago History
test_blocks.py · 7.3 KB · 246 lines
1"""
2Tests for block validation.
3"""
4
5import pytest
6from dbbasic_content.blocks import (
7 BlockType,
8 validate_block,
9 validate_blocks,
10 BLOCK_SCHEMAS
11)
12
13
14class TestBlockValidation:
15 """Test block validation functionality."""
16
17 def test_validate_paragraph_block(self):
18 """Test validating paragraph block."""
19 block = {
20 'type': 'paragraph',
21 'data': {'content': 'Hello world'}
22 }
23 is_valid, error = validate_block(block)
24 assert is_valid
25 assert error == ""
26
27 def test_validate_heading_block(self):
28 """Test validating heading block."""
29 block = {
30 'type': 'heading',
31 'data': {'level': 2, 'content': 'Heading'}
32 }
33 is_valid, error = validate_block(block)
34 assert is_valid
35
36 def test_validate_list_block(self):
37 """Test validating list block."""
38 block = {
39 'type': 'list',
40 'data': {
41 'listType': 'unordered',
42 'items': ['Item 1', 'Item 2']
43 }
44 }
45 is_valid, error = validate_block(block)
46 assert is_valid
47
48 def test_validate_code_block(self):
49 """Test validating code block."""
50 block = {
51 'type': 'code',
52 'data': {
53 'language': 'python',
54 'code': 'print("hello")',
55 'title': 'Example'
56 }
57 }
58 is_valid, error = validate_block(block)
59 assert is_valid
60
61 def test_validate_image_block(self):
62 """Test validating image block."""
63 block = {
64 'type': 'image',
65 'data': {
66 'src': '/images/photo.jpg',
67 'alt': 'Photo',
68 'caption': 'A nice photo'
69 }
70 }
71 is_valid, error = validate_block(block)
72 assert is_valid
73
74 def test_validate_quote_block(self):
75 """Test validating quote block."""
76 block = {
77 'type': 'quote',
78 'data': {
79 'content': 'To be or not to be',
80 'author': 'Shakespeare'
81 }
82 }
83 is_valid, error = validate_block(block)
84 assert is_valid
85
86 def test_validate_card_block(self):
87 """Test validating card block."""
88 block = {
89 'type': 'card',
90 'data': {
91 'header': 'Card Header',
92 'content': 'Card content'
93 }
94 }
95 is_valid, error = validate_block(block)
96 assert is_valid
97
98 def test_validate_card_list_block(self):
99 """Test validating card_list block."""
100 block = {
101 'type': 'card_list',
102 'data': {
103 'cards': [
104 {'header': 'Card 1', 'content': 'Content 1'},
105 {'header': 'Card 2', 'content': 'Content 2'}
106 ]
107 }
108 }
109 is_valid, error = validate_block(block)
110 assert is_valid
111
112 def test_invalid_block_missing_type(self):
113 """Test block without type field."""
114 block = {
115 'data': {'content': 'Hello'}
116 }
117 is_valid, error = validate_block(block)
118 assert not is_valid
119 assert "missing 'type'" in error.lower()
120
121 def test_invalid_block_missing_data(self):
122 """Test block without data field."""
123 block = {
124 'type': 'paragraph'
125 }
126 is_valid, error = validate_block(block)
127 assert not is_valid
128 assert "missing 'data'" in error.lower()
129
130 def test_invalid_block_not_dict(self):
131 """Test block that's not a dictionary."""
132 block = "not a dict"
133 is_valid, error = validate_block(block)
134 assert not is_valid
135 assert "dictionary" in error.lower()
136
137 def test_invalid_paragraph_missing_content(self):
138 """Test paragraph block missing required content field."""
139 block = {
140 'type': 'paragraph',
141 'data': {}
142 }
143 is_valid, error = validate_block(block)
144 assert not is_valid
145 assert "requires field 'content'" in error
146
147 def test_invalid_heading_missing_content(self):
148 """Test heading block missing required content field."""
149 block = {
150 'type': 'heading',
151 'data': {'level': 2}
152 }
153 is_valid, error = validate_block(block)
154 assert not is_valid
155 assert "requires field 'content'" in error
156
157 def test_invalid_list_missing_items(self):
158 """Test list block missing required items field."""
159 block = {
160 'type': 'list',
161 'data': {'listType': 'ordered'}
162 }
163 is_valid, error = validate_block(block)
164 assert not is_valid
165 assert "requires field 'items'" in error
166
167 def test_invalid_code_missing_code(self):
168 """Test code block missing required code field."""
169 block = {
170 'type': 'code',
171 'data': {'language': 'python'}
172 }
173 is_valid, error = validate_block(block)
174 assert not is_valid
175 assert "requires field 'code'" in error
176
177 def test_unknown_block_type_allowed(self):
178 """Test that unknown block types are allowed (extensibility)."""
179 block = {
180 'type': 'custom_block_type',
181 'data': {'custom': 'data'}
182 }
183 is_valid, error = validate_block(block)
184 assert is_valid # Unknown types are allowed
185
186 def test_validate_blocks_list(self):
187 """Test validating a list of blocks."""
188 blocks = [
189 {
190 'type': 'paragraph',
191 'data': {'content': 'Para 1'}
192 },
193 {
194 'type': 'heading',
195 'data': {'level': 2, 'content': 'Heading'}
196 }
197 ]
198 is_valid, errors = validate_blocks(blocks)
199 assert is_valid
200 assert len(errors) == 0
201
202 def test_validate_blocks_with_errors(self):
203 """Test validating blocks with some invalid blocks."""
204 blocks = [
205 {
206 'type': 'paragraph',
207 'data': {'content': 'Valid'}
208 },
209 {
210 'type': 'paragraph',
211 'data': {} # Missing content
212 },
213 {
214 'data': {'content': 'No type'} # Missing type
215 }
216 ]
217 is_valid, errors = validate_blocks(blocks)
218 assert not is_valid
219 assert len(errors) == 2
220 assert "Block 1" in errors[0]
221 assert "Block 2" in errors[1]
222
223 def test_validate_blocks_not_list(self):
224 """Test validating non-list blocks."""
225 blocks = "not a list"
226 is_valid, errors = validate_blocks(blocks)
227 assert not is_valid
228 assert "must be a list" in errors[0].lower()
229
230 def test_block_types_enum(self):
231 """Test BlockType enum values."""
232 assert BlockType.PARAGRAPH == "paragraph"
233 assert BlockType.HEADING == "heading"
234 assert BlockType.LIST == "list"
235 assert BlockType.CARD == "card"
236 assert BlockType.CODE == "code"
237 assert BlockType.IMAGE == "image"
238 assert BlockType.QUOTE == "quote"
239
240 def test_all_block_types_have_schemas(self):
241 """Test that all block types have schemas defined."""
242 for block_type in BlockType:
243 assert block_type in BLOCK_SCHEMAS
244 schema = BLOCK_SCHEMAS[block_type]
245 assert 'required' in schema
246 assert 'properties' in schema