summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2015-07-15 23:15:13 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2015-07-15 23:15:13 +0200
commitb0fbd24c69a155c4f9d58f5e4dab8209afeb3660 (patch)
tree7135e74933e7166563296023527aebf1122d4ea8
parent63edb526bc4efd8cf7abe260f2787f55d2953e39 (diff)
downloadfactory-boy-b0fbd24c69a155c4f9d58f5e4dab8209afeb3660.tar
factory-boy-b0fbd24c69a155c4f9d58f5e4dab8209afeb3660.tar.gz
Add examples folder.
This should contain examples of "using factory_boy with third-party frameworks".
-rw-r--r--Makefile6
-rw-r--r--dev_requirements.txt1
-rw-r--r--examples/Makefile9
-rw-r--r--examples/flask_alchemy/demoapp.py55
-rw-r--r--examples/flask_alchemy/demoapp_factories.py27
-rw-r--r--examples/flask_alchemy/requirements.txt3
-rw-r--r--examples/flask_alchemy/test_demoapp.py35
-rw-r--r--examples/requirements.txt1
8 files changed, 136 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 8883015..79b5e82 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
PACKAGE=factory
TESTS_DIR=tests
DOC_DIR=docs
+EXAMPLES_DIR=examples
# Use current python binary instead of system default.
COVERAGE = python $(shell which coverage)
@@ -43,9 +44,12 @@ clean:
@rm -rf tmp_test/
-test: install-deps
+test: install-deps example-test
python -W default setup.py test
+example-test:
+ $(MAKE) -C $(EXAMPLES_DIR) test
+
pylint:
pylint --rcfile=.pylintrc --report=no $(PACKAGE)/
diff --git a/dev_requirements.txt b/dev_requirements.txt
index d55129a..22261a1 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -1,4 +1,5 @@
-r requirements.txt
+-r examples/requirements.txt
coverage
Django
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..6064a9b
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,9 @@
+EXAMPLES = flask_alchemy
+
+TEST_TARGETS = $(addprefix runtest-,$(EXAMPLES))
+
+test: $(TEST_TARGETS)
+
+
+$(TEST_TARGETS): runtest-%:
+ cd $* && PYTHONPATH=../.. python -m unittest
diff --git a/examples/flask_alchemy/demoapp.py b/examples/flask_alchemy/demoapp.py
new file mode 100644
index 0000000..4ab42b0
--- /dev/null
+++ b/examples/flask_alchemy/demoapp.py
@@ -0,0 +1,55 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2015 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+from flask import Flask
+from flask.ext.sqlalchemy import SQLAlchemy
+
+app = Flask(__name__)
+app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
+db = SQLAlchemy(app)
+
+
+class User(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ username = db.Column(db.String(80), unique=True)
+ email = db.Column(db.String(120), unique=True)
+
+ def __init__(self, username, email):
+ self.username = username
+ self.email = email
+
+ def __repr__(self):
+ return '<User %r>' % self.username
+
+
+class UserLog(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ message = db.Column(db.String(1000))
+
+ user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
+ user = db.relationship('User', backref=db.backref('logs', lazy='dynamic'))
+
+ def __init__(self, message, user):
+ self.message = message
+ self.user = user
+
+ def __repr__(self):
+ return '<Log for %r: %s>' % (self.user, self.message)
diff --git a/examples/flask_alchemy/demoapp_factories.py b/examples/flask_alchemy/demoapp_factories.py
new file mode 100644
index 0000000..6b71d04
--- /dev/null
+++ b/examples/flask_alchemy/demoapp_factories.py
@@ -0,0 +1,27 @@
+import factory
+import factory.alchemy
+import factory.fuzzy
+
+import demoapp
+
+
+class BaseFactory(factory.alchemy.SQLAlchemyModelFactory):
+ class Meta:
+ abstract = True
+ sqlalchemy_session = demoapp.db.session
+
+
+class UserFactory(BaseFactory):
+ class Meta:
+ model = demoapp.User
+
+ username = factory.fuzzy.FuzzyText()
+ email = factory.fuzzy.FuzzyText()
+
+
+class UserLogFactory(BaseFactory):
+ class Meta:
+ model = demoapp.UserLog
+
+ message = factory.fuzzy.FuzzyText()
+ user = factory.SubFactory(UserFactory)
diff --git a/examples/flask_alchemy/requirements.txt b/examples/flask_alchemy/requirements.txt
new file mode 100644
index 0000000..3ee3e5e
--- /dev/null
+++ b/examples/flask_alchemy/requirements.txt
@@ -0,0 +1,3 @@
+-r ../../requirements.txt
+Flask
+Flask-SQLAlchemy
diff --git a/examples/flask_alchemy/test_demoapp.py b/examples/flask_alchemy/test_demoapp.py
new file mode 100644
index 0000000..b485a92
--- /dev/null
+++ b/examples/flask_alchemy/test_demoapp.py
@@ -0,0 +1,35 @@
+import os
+import unittest
+import tempfile
+
+import demoapp
+import demoapp_factories
+
+class DemoAppTestCase(unittest.TestCase):
+
+ def setUp(self):
+ demoapp.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
+ demoapp.app.config['TESTING'] = True
+ self.app = demoapp.app.test_client()
+ self.db = demoapp.db
+ self.db.create_all()
+
+ def tearDown(self):
+ self.db.drop_all()
+
+ def test_user_factory(self):
+ user = demoapp_factories.UserFactory()
+ self.db.session.commit()
+ self.assertIsNotNone(user.id)
+ self.assertEqual(1, len(demoapp.User.query.all()))
+
+ def test_userlog_factory(self):
+ userlog = demoapp_factories.UserLogFactory()
+ self.db.session.commit()
+ self.assertIsNotNone(userlog.id)
+ self.assertIsNotNone(userlog.user.id)
+ self.assertEqual(1, len(demoapp.User.query.all()))
+ self.assertEqual(1, len(demoapp.UserLog.query.all()))
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/examples/requirements.txt b/examples/requirements.txt
new file mode 100644
index 0000000..5e11ca5
--- /dev/null
+++ b/examples/requirements.txt
@@ -0,0 +1 @@
+-r flask_alchemy/requirements.txt