askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/tests/unit/test_presentation_layer.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.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 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
test_presentation_layer.py · 9.6 KB · 308 lines
1#!/usr/bin/env python3
2"""
3Tests for the Presentation Layer
4"""
5
6import pytest
7import sys
8import os
9
10# Add parent directory to path
11sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
12
13from presentation_layer import PresentationLayer, UIRenderer
14from bootstrap_components import ExtendedBootstrapRenderer
15from tailwind_components import TailwindRenderer
16
17
18class TestPresentationLayer:
19 """Test the presentation layer core functionality"""
20
21 def setup_method(self):
22 """Setup test fixtures"""
23 # Register renderers
24 PresentationLayer.add_renderer('bootstrap', ExtendedBootstrapRenderer())
25 PresentationLayer.add_renderer('tailwind', TailwindRenderer())
26
27 # Sample data structure
28 self.test_data = {
29 'type': 'card',
30 'title': 'Test Card',
31 'description': 'Test Description',
32 'body': 'Test Body Content'
33 }
34
35 def test_bootstrap_renderer_registered(self):
36 """Test Bootstrap renderer is registered"""
37 assert 'bootstrap' in PresentationLayer.RENDERERS
38 assert isinstance(PresentationLayer.RENDERERS['bootstrap'], ExtendedBootstrapRenderer)
39
40 def test_tailwind_renderer_registered(self):
41 """Test Tailwind renderer is registered"""
42 assert 'tailwind' in PresentationLayer.RENDERERS
43 assert isinstance(PresentationLayer.RENDERERS['tailwind'], TailwindRenderer)
44
45 def test_render_with_bootstrap(self):
46 """Test rendering with Bootstrap"""
47 html = PresentationLayer.render(self.test_data, 'bootstrap')
48 assert html is not None
49 assert 'card' in html
50 assert 'Test Card' in html
51 # When both body and description are provided, body takes precedence
52 assert 'Test Body Content' in html
53
54 def test_render_with_tailwind(self):
55 """Test rendering with Tailwind"""
56 html = PresentationLayer.render(self.test_data, 'tailwind')
57 assert html is not None
58 assert 'Test Card' in html
59 assert 'Test Description' in html
60
61 def test_render_with_invalid_framework(self):
62 """Test rendering with invalid framework raises error"""
63 with pytest.raises(ValueError, match="Unknown framework"):
64 PresentationLayer.render(self.test_data, 'invalid')
65
66 def test_render_string_passthrough(self):
67 """Test that strings are passed through unchanged"""
68 html = PresentationLayer.render("Hello World", 'bootstrap')
69 assert html == "Hello World"
70
71 def test_render_list(self):
72 """Test rendering a list of components"""
73 data = [
74 {'type': 'card', 'title': 'Card 1'},
75 {'type': 'card', 'title': 'Card 2'}
76 ]
77 html = PresentationLayer.render(data, 'bootstrap')
78 assert 'Card 1' in html
79 assert 'Card 2' in html
80
81
82class TestBootstrapComponents:
83 """Test Bootstrap component rendering"""
84
85 def setup_method(self):
86 """Setup test fixtures"""
87 self.renderer = ExtendedBootstrapRenderer()
88
89 def test_render_navbar(self):
90 """Test navbar rendering"""
91 data = {
92 'type': 'navbar',
93 'brand': 'TestApp',
94 'links': [
95 {'text': 'Home', 'url': '/'},
96 {'text': 'About', 'url': '/about'}
97 ]
98 }
99 html = self.renderer.render(data)
100 assert 'navbar' in html
101 assert 'TestApp' in html
102 assert 'Home' in html
103 assert 'About' in html
104
105 def test_render_card(self):
106 """Test card rendering"""
107 data = {
108 'type': 'card',
109 'title': 'Test Card',
110 'body': 'Card body content'
111 }
112 html = self.renderer.render(data)
113 assert 'card' in html
114 assert 'Test Card' in html
115 assert 'Card body content' in html
116
117 def test_render_button(self):
118 """Test button rendering"""
119 data = {
120 'type': 'button',
121 'text': 'Click Me',
122 'variant': 'primary'
123 }
124 html = self.renderer.render(data)
125 assert 'btn' in html
126 assert 'btn-primary' in html
127 assert 'Click Me' in html
128
129 def test_render_form(self):
130 """Test form rendering"""
131 data = {
132 'type': 'form',
133 'fields': [
134 {
135 'type': 'input',
136 'name': 'username',
137 'label': 'Username',
138 'placeholder': 'Enter username'
139 }
140 ]
141 }
142 html = self.renderer.render(data)
143 assert '<form' in html
144 assert 'Username' in html
145 assert 'Enter username' in html
146
147 def test_render_table(self):
148 """Test table rendering"""
149 data = {
150 'type': 'table',
151 'headers': ['Name', 'Age'],
152 'rows': [
153 ['John', '30'],
154 ['Jane', '25']
155 ]
156 }
157 html = self.renderer.render(data)
158 assert '<table' in html
159 assert 'Name' in html
160 assert 'Age' in html
161 assert 'John' in html
162 assert '30' in html
163
164 def test_render_with_id(self):
165 """Test component rendering with ID"""
166 data = {
167 'type': 'div',
168 'id': 'test-div',
169 'content': 'Test content'
170 }
171 html = self.renderer.render(data)
172 assert 'id="test-div"' in html
173 assert 'Test content' in html
174
175
176class TestTailwindComponents:
177 """Test Tailwind component rendering"""
178
179 def setup_method(self):
180 """Setup test fixtures"""
181 self.renderer = TailwindRenderer()
182
183 def test_render_card(self):
184 """Test Tailwind card rendering"""
185 data = {
186 'type': 'card',
187 'title': 'Tailwind Card',
188 'body': 'Card content'
189 }
190 html = self.renderer.render(data)
191 assert 'Tailwind Card' in html
192 assert 'Card content' in html
193 assert 'shadow' in html # Tailwind shadow class
194
195 def test_render_button(self):
196 """Test Tailwind button rendering"""
197 data = {
198 'type': 'button',
199 'text': 'Tailwind Button',
200 'variant': 'primary'
201 }
202 html = self.renderer.render(data)
203 assert 'Tailwind Button' in html
204 assert 'bg-indigo' in html # Tailwind color class
205
206 def test_render_grid(self):
207 """Test Tailwind grid rendering"""
208 data = {
209 'type': 'grid',
210 'columns': 3,
211 'items': [
212 {'type': 'div', 'content': 'Item 1'},
213 {'type': 'div', 'content': 'Item 2'},
214 {'type': 'div', 'content': 'Item 3'}
215 ]
216 }
217 html = self.renderer.render(data)
218 assert 'grid' in html
219 assert 'Item 1' in html
220 assert 'Item 2' in html
221 assert 'Item 3' in html
222
223
224class TestDataStructureConversion:
225 """Test conversion from HTML to data structures"""
226
227 def test_simple_conversion(self):
228 """Test that data structures are more efficient than HTML"""
229 # HTML approach (old)
230 html_string = """
231 <div class="card">
232 <div class="card-header">
233 <h5 class="card-title">User Profile</h5>
234 </div>
235 <div class="card-body">
236 <p class="card-text">User information goes here</p>
237 </div>
238 </div>
239 """
240
241 # Data structure approach (new)
242 data_structure = {
243 'type': 'card',
244 'title': 'User Profile',
245 'body': 'User information goes here'
246 }
247
248 # Compare sizes (tokens)
249 html_tokens = len(html_string.split())
250 data_tokens = len(str(data_structure).split())
251
252 # Data structure should be more compact
253 assert data_tokens < html_tokens
254
255 def test_framework_agnostic(self):
256 """Test same data structure works with multiple frameworks"""
257 data = {
258 'type': 'alert',
259 'message': 'This is a test alert',
260 'variant': 'info'
261 }
262
263 # Register renderers
264 PresentationLayer.add_renderer('bootstrap', ExtendedBootstrapRenderer())
265 PresentationLayer.add_renderer('tailwind', TailwindRenderer())
266
267 # Render with both frameworks
268 bootstrap_html = PresentationLayer.render(data, 'bootstrap')
269 tailwind_html = PresentationLayer.render(data, 'tailwind')
270
271 # Both should contain the message
272 assert 'This is a test alert' in bootstrap_html
273 assert 'This is a test alert' in tailwind_html
274
275 # But use different classes
276 assert 'alert' in bootstrap_html # Bootstrap class
277 assert 'border-l-4' in tailwind_html # Tailwind class
278
279
280class TestComponentMarketplace:
281 """Test component marketplace functionality"""
282
283 def test_marketplace_import(self):
284 """Test that marketplace can be imported"""
285 from component_marketplace import marketplace
286 assert marketplace is not None
287
288 def test_get_component(self):
289 """Test getting a component from marketplace"""
290 from component_marketplace import marketplace
291 nav_component = marketplace.get_component('unified-nav')
292 assert nav_component is not None
293 assert nav_component['name'] == 'Unified Navigation'
294
295 def test_use_component_with_params(self):
296 """Test using a component with parameters"""
297 from component_marketplace import marketplace
298 hero = marketplace.use_component(
299 'gradient-hero',
300 title='Test Title',
301 subtitle='Test Subtitle'
302 )
303 assert hero['title'] == 'Test Title'
304 assert hero['subtitle'] == 'Test Subtitle'
305
306
307if __name__ == "__main__":
308 pytest.main([__file__, '-v'])