Skip to content

Commit 023514d

Browse files
committed
Add doctests for flask-sqlalchemy docs
1 parent 456e30a commit 023514d

1 file changed

Lines changed: 128 additions & 55 deletions

File tree

docs/tut_flask.rst

Lines changed: 128 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,72 @@ Setup & Usage with Flask-SQLAlchemy
33

44
Initialize Flask app and sqlalchemy
55

6-
.. code-block:: python
6+
.. testsetup::
77

8-
from pprint import pprint
9-
from flask import Flask
10-
from flask_sqlalchemy import SQLAlchemy
8+
__name__ = "__main__"
119

12-
from sqlalchemy_mptt.mixins import BaseNestedSets
10+
.. testcode::
1311

14-
app = Flask(__name__)
15-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
16-
db = SQLAlchemy(app)
12+
from pprint import pprint
13+
from flask import Flask
14+
from flask_sqlalchemy import SQLAlchemy
15+
16+
from sqlalchemy_mptt.mixins import BaseNestedSets
17+
18+
app = Flask(__name__)
19+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
20+
db = SQLAlchemy(app)
1721

1822
Make models.
1923

20-
.. code-block:: python
21-
:emphasize-lines: 1
24+
.. testcode::
2225

23-
class Category(db.Model, BaseNestedSets):
24-
__tablename__ = 'categories'
25-
id = db.Column(db.Integer, primary_key=True)
26-
name = db.Column(db.String(400), index=True, unique=True)
27-
items = db.relationship("Product", backref='item', lazy='dynamic')
26+
class Category(db.Model, BaseNestedSets):
27+
__tablename__ = 'categories'
28+
id = db.Column(db.Integer, primary_key=True)
29+
name = db.Column(db.String(400), index=True, unique=True)
30+
items = db.relationship("Product", backref='item', lazy='dynamic')
2831

29-
def __repr__(self):
30-
return '<Category {}>'.format(self.name)
32+
def __repr__(self):
33+
return '<Category {}>'.format(self.name)
3134

3235

33-
class Product(db.Model):
34-
__tablename__ = 'products'
35-
id = db.Column(db.Integer, primary_key=True)
36-
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
37-
name = db.Column(db.String(475), index=True)
36+
class Product(db.Model):
37+
__tablename__ = 'products'
38+
id = db.Column(db.Integer, primary_key=True)
39+
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
40+
name = db.Column(db.String(475), index=True)
3841

3942
Represent data of tree in table
4043
-------------------------------
4144

4245
Add data to table with tree.
4346

44-
.. code-block:: python
45-
46-
db.session.add(Category(name="root")) # root node
47-
db.session.add_all( # first branch of tree
48-
[
49-
Category(name="foo", parent_id=1),
50-
Category(name="bar", parent_id=2),
51-
Category(name="baz", parent_id=3),
52-
]
53-
)
54-
db.session.add_all( # second branch of tree
55-
[
56-
Category(name="foo1", parent_id=1),
57-
Category(name="bar1", parent_id=5),
58-
Category(name="baz1", parent_id=5),
59-
]
60-
)
61-
62-
db.drop_all()
63-
db.create_all()
64-
db.session.commit()
47+
.. testcode::
48+
49+
app.app_context().push()
50+
51+
.. testcode::
52+
53+
db.session.add(Category(name="root")) # root node
54+
db.session.add_all( # first branch of tree
55+
[
56+
Category(name="foo", parent_id=1),
57+
Category(name="bar", parent_id=2),
58+
Category(name="baz", parent_id=3),
59+
]
60+
)
61+
db.session.add_all( # second branch of tree
62+
[
63+
Category(name="foo1", parent_id=1),
64+
Category(name="bar1", parent_id=5),
65+
Category(name="baz1", parent_id=5),
66+
]
67+
)
68+
69+
db.drop_all()
70+
db.create_all()
71+
db.session.commit()
6572

6673
The database entries are added:
6774

@@ -127,16 +134,17 @@ something like:
127134
| --------------------------
128135
4 4(baz)5
129136
130-
.. code-block:: python
137+
.. testcode::
131138

132-
categories = Category.query.all()
139+
categories = Category.query.all()
133140

134-
for item in categories:
135-
print(item)
136-
pprint(item.drilldown_tree())
137-
print()
141+
for item in categories:
142+
print(item)
143+
pprint(item.drilldown_tree())
144+
print()
138145

139-
.. code-block:: text
146+
.. testoutput::
147+
:options: +NORMALIZE_WHITESPACE
140148

141149
<Category root>
142150
[{'children': [{'children': [{'children': [{'node': <Category baz>}],
@@ -170,7 +178,7 @@ something like:
170178

171179
Represent it to JSON format:
172180

173-
.. code-block:: python
181+
.. testcode::
174182

175183
def cat_to_json(item):
176184
return {
@@ -182,7 +190,8 @@ Represent it to JSON format:
182190
pprint(item.drilldown_tree(json=True, json_fields=cat_to_json))
183191
print()
184192

185-
.. code-block:: text
193+
.. testoutput::
194+
:options: +NORMALIZE_WHITESPACE
186195

187196
[{'children': [{'children': [{'children': [{'id': 4,
188197
'label': '<Category baz>',
@@ -253,7 +262,7 @@ Returns a list containing the ancestors and the node itself in tree order.
253262
-----|-----
254263
4 4(baz)5
255264
256-
.. code-block:: python
265+
.. testcode::
257266

258267
for item in categories:
259268
print(item)
@@ -262,7 +271,8 @@ Returns a list containing the ancestors and the node itself in tree order.
262271
pprint(item.path_to_root().all())
263272
print()
264273

265-
.. code-block:: text
274+
.. testoutput::
275+
:options: +NORMALIZE_WHITESPACE
266276

267277
<Category root>
268278
<Category root>
@@ -295,7 +305,7 @@ Returns a list containing the ancestors and the node itself in tree order.
295305
Full code
296306
---------
297307

298-
.. code-block:: python3
308+
.. testcode::
299309

300310
from pprint import pprint
301311
from flask import Flask
@@ -324,6 +334,7 @@ Full code
324334
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
325335
name = db.Column(db.String(475), index=True)
326336

337+
app.app_context().push()
327338
db.session.add(Category(name="root")) # root node
328339
db.session.add_all( # first branch of tree
329340
[
@@ -443,3 +454,65 @@ Full code
443454
<Category root>
444455
[<Category baz1>, <Category foo1>, <Category root>]
445456
'''
457+
458+
.. testoutput::
459+
:options: +NORMALIZE_WHITESPACE
460+
:hide:
461+
462+
<Category root>
463+
[{'children': [{'children': [{'children': [{'node': <Category baz>}],
464+
'node': <Category bar>}],
465+
'node': <Category foo>},
466+
{'children': [{'node': <Category bar1>},
467+
{'node': <Category baz1>}],
468+
'node': <Category foo1>}],
469+
'node': <Category root>}]
470+
471+
<Category foo>
472+
[{'children': [{'children': [{'node': <Category baz>}],
473+
'node': <Category bar>}],
474+
'node': <Category foo>}]
475+
476+
<Category bar>
477+
[{'children': [{'node': <Category baz>}], 'node': <Category bar>}]
478+
479+
<Category baz>
480+
[{'node': <Category baz>}]
481+
482+
<Category foo1>
483+
[{'children': [{'node': <Category bar1>}, {'node': <Category baz1>}],
484+
'node': <Category foo1>}]
485+
486+
<Category bar1>
487+
[{'node': <Category bar1>}]
488+
489+
<Category baz1>
490+
[{'node': <Category baz1>}]
491+
492+
<Category root>
493+
<Category root>
494+
[<Category root>]
495+
496+
<Category foo>
497+
<Category root>
498+
[<Category foo>, <Category root>]
499+
500+
<Category bar>
501+
<Category root>
502+
[<Category bar>, <Category foo>, <Category root>]
503+
504+
<Category baz>
505+
<Category root>
506+
[<Category baz>, <Category bar>, <Category foo>, <Category root>]
507+
508+
<Category foo1>
509+
<Category root>
510+
[<Category foo1>, <Category root>]
511+
512+
<Category bar1>
513+
<Category root>
514+
[<Category bar1>, <Category foo1>, <Category root>]
515+
516+
<Category baz1>
517+
<Category root>
518+
[<Category baz1>, <Category foo1>, <Category root>]

0 commit comments

Comments
 (0)