aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorminimumserious <commande.romain@gmail.com>2013-06-13 00:16:26 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-06-20 01:40:14 +0200
commite3e654a2686d195bcad4ca2723bf31936af2d270 (patch)
tree359f4d50ef9b53e5956b2ab753f2fac1558802e6
parentf21612942888b3a2290ee6b4468b6934090fe7af (diff)
downloadfactory-boy-e3e654a2686d195bcad4ca2723bf31936af2d270.tar
factory-boy-e3e654a2686d195bcad4ca2723bf31936af2d270.tar.gz
Added some tests for SQLAlchemyModelFactory
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/alchemyapp/__init__.py0
-rw-r--r--tests/alchemyapp/models.py47
-rw-r--r--tests/test_alchemy.py134
4 files changed, 182 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 1529e21..9960382 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -8,3 +8,4 @@ from .test_django import *
from .test_fuzzy import *
from .test_using import *
from .test_utils import *
+from .test_alchemy import *
diff --git a/tests/alchemyapp/__init__.py b/tests/alchemyapp/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/alchemyapp/__init__.py
diff --git a/tests/alchemyapp/models.py b/tests/alchemyapp/models.py
new file mode 100644
index 0000000..e0193d4
--- /dev/null
+++ b/tests/alchemyapp/models.py
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2013 Romain Commandé
+#
+# 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.
+
+
+"""Helpers for testing SQLAlchemy apps."""
+
+from sqlalchemy import Column, Integer, Unicode, create_engine
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import scoped_session, sessionmaker
+
+session = scoped_session(sessionmaker())
+engine = create_engine('sqlite://')
+session.configure(bind=engine)
+Base = declarative_base()
+
+
+class StandardModel(Base):
+ __tablename__ = 'StandardModelTable'
+
+ id = Column(Integer(), primary_key=True)
+ foo = Column(Unicode(20))
+
+
+class NonIntegerPk(Base):
+ __tablename__ = 'NonIntegerPk'
+
+ id = Column(Unicode(20), primary_key=True)
+
+Base.metadata.create_all(engine)
diff --git a/tests/test_alchemy.py b/tests/test_alchemy.py
new file mode 100644
index 0000000..a1d8576
--- /dev/null
+++ b/tests/test_alchemy.py
@@ -0,0 +1,134 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2013 Romain Command&
+#
+# 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.
+
+"""Tests for factory_boy/SQLAlchemy interactions."""
+
+import factory
+from .compat import unittest
+
+
+try:
+ import sqlalchemy
+except ImportError:
+ sqlalchemy = None
+
+if sqlalchemy:
+ from factory import SQLAlchemyModelFactory
+ from .alchemyapp import models
+else:
+
+ class Fake(object):
+ FACTORY_SESSION = None
+
+ models = Fake()
+ models.StandardModel = Fake()
+ models.NonIntegerPk = Fake()
+ models.session = Fake()
+ SQLAlchemyModelFactory = Fake
+
+
+class StandardFactory(SQLAlchemyModelFactory):
+ FACTORY_FOR = models.StandardModel
+ FACTORY_SESSION = models.session
+
+ id = factory.Sequence(lambda n: n)
+ foo = factory.Sequence(lambda n: u'foo%d' % n)
+
+
+class NonIntegerPkFactory(SQLAlchemyModelFactory):
+ FACTORY_FOR = models.NonIntegerPk
+ FACTORY_SESSION = models.session
+
+ id = factory.Sequence(lambda n: u'foo%d' % n)
+
+
+@unittest.skipIf(sqlalchemy is None, "SQLalchemy not installed.")
+class SQLAlchemyPkSequenceTestCase(unittest.TestCase):
+
+ def setUp(self):
+ super(SQLAlchemyPkSequenceTestCase, self).setUp()
+ StandardFactory.reset_sequence()
+ NonIntegerPkFactory.FACTORY_SESSION.rollback()
+
+ def test_pk_first(self):
+ std = StandardFactory.build()
+ self.assertEqual(u'foo1', std.foo)
+
+ def test_pk_many(self):
+ std1 = StandardFactory.build()
+ std2 = StandardFactory.build()
+ self.assertEqual(u'foo1', std1.foo)
+ self.assertEqual(u'foo2', std2.foo)
+
+ def test_pk_creation(self):
+ std1 = StandardFactory.create()
+ self.assertEqual(u'foo1', std1.foo)
+ self.assertEqual(1, std1.id)
+
+ StandardFactory.reset_sequence()
+ std2 = StandardFactory.create()
+ self.assertEqual(u'foo2', std2.foo)
+ self.assertEqual(2, std2.id)
+
+ def test_pk_force_value(self):
+ std1 = StandardFactory.create(id=10)
+ self.assertEqual(u'foo1', std1.foo) # sequence was set before pk
+ self.assertEqual(10, std1.id)
+
+ StandardFactory.reset_sequence()
+ std2 = StandardFactory.create()
+ self.assertEqual(u'foo11', std2.foo)
+ self.assertEqual(11, std2.id)
+
+
+@unittest.skipIf(sqlalchemy is None, "SQLalchemy not installed.")
+class SQLAlchemyNonIntegerPkTestCase(unittest.TestCase):
+ def setUp(self):
+ super(SQLAlchemyNonIntegerPkTestCase, self).setUp()
+ NonIntegerPkFactory.reset_sequence()
+ NonIntegerPkFactory.FACTORY_SESSION.rollback()
+
+ def test_first(self):
+ nonint = NonIntegerPkFactory.build()
+ self.assertEqual(u'foo1', nonint.id)
+
+ def test_many(self):
+ nonint1 = NonIntegerPkFactory.build()
+ nonint2 = NonIntegerPkFactory.build()
+
+ self.assertEqual(u'foo1', nonint1.id)
+ self.assertEqual(u'foo2', nonint2.id)
+
+ def test_creation(self):
+ nonint1 = NonIntegerPkFactory.create()
+ self.assertEqual(u'foo1', nonint1.id)
+
+ NonIntegerPkFactory.reset_sequence()
+ nonint2 = NonIntegerPkFactory.build()
+ self.assertEqual(u'foo1', nonint2.id)
+
+ def test_force_pk(self):
+ nonint1 = NonIntegerPkFactory.create(id='foo10')
+ self.assertEqual(u'foo10', nonint1.id)
+
+ NonIntegerPkFactory.reset_sequence()
+ nonint2 = NonIntegerPkFactory.create()
+ self.assertEqual(u'foo1', nonint2.id)