From b0fbd24c69a155c4f9d58f5e4dab8209afeb3660 Mon Sep 17 00:00:00 2001 From: Raphaël Barrois Date: Wed, 15 Jul 2015 23:15:13 +0200 Subject: Add examples folder. This should contain examples of "using factory_boy with third-party frameworks". --- examples/Makefile | 9 +++++ examples/flask_alchemy/demoapp.py | 55 +++++++++++++++++++++++++++++ examples/flask_alchemy/demoapp_factories.py | 27 ++++++++++++++ examples/flask_alchemy/requirements.txt | 3 ++ examples/flask_alchemy/test_demoapp.py | 35 ++++++++++++++++++ examples/requirements.txt | 1 + 6 files changed, 130 insertions(+) create mode 100644 examples/Makefile create mode 100644 examples/flask_alchemy/demoapp.py create mode 100644 examples/flask_alchemy/demoapp_factories.py create mode 100644 examples/flask_alchemy/requirements.txt create mode 100644 examples/flask_alchemy/test_demoapp.py create mode 100644 examples/requirements.txt (limited to 'examples') 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 '' % 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 '' % (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 -- cgit v1.2.3