From ed158c8f9f072555a62295ed25a589be0cc5e7b8 Mon Sep 17 00:00:00 2001 From: Konsta Vesterinen Date: Sun, 14 Dec 2014 12:44:50 +0200 Subject: [PATCH] Refactor tests --- tests/mixins.py | 171 ++++++++++++++++++++++ tests/relationships/test_chained_join.py | 178 ++--------------------- 2 files changed, 180 insertions(+), 169 deletions(-) create mode 100644 tests/mixins.py diff --git a/tests/mixins.py b/tests/mixins.py new file mode 100644 index 0000000..9bb52d4 --- /dev/null +++ b/tests/mixins.py @@ -0,0 +1,171 @@ +import sqlalchemy as sa + + +class ThreeLevelDeepOneToOne(object): + def create_models(self): + class Catalog(self.Base): + __tablename__ = 'catalog' + id = sa.Column(sa.Integer, primary_key=True) + category = sa.orm.relationship( + 'Category', + uselist=False, + backref='catalog' + ) + + class Category(self.Base): + __tablename__ = 'category' + id = sa.Column(sa.Integer, primary_key=True) + catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id')) + + sub_category = sa.orm.relationship( + 'SubCategory', + uselist=False, + backref='category' + ) + + class SubCategory(self.Base): + __tablename__ = 'sub_category' + id = sa.Column(sa.Integer, primary_key=True) + category_id = sa.Column(sa.Integer, sa.ForeignKey('category.id')) + product = sa.orm.relationship( + 'Product', + uselist=False, + backref='sub_category' + ) + + class Product(self.Base): + __tablename__ = 'product' + id = sa.Column(sa.Integer, primary_key=True) + price = sa.Column(sa.Integer) + + sub_category_id = sa.Column( + sa.Integer, sa.ForeignKey('sub_category.id') + ) + + self.Catalog = Catalog + self.Category = Category + self.SubCategory = SubCategory + self.Product = Product + + +class ThreeLevelDeepOneToMany(object): + def create_models(self): + class Catalog(self.Base): + __tablename__ = 'catalog' + id = sa.Column(sa.Integer, primary_key=True) + + categories = sa.orm.relationship('Category', backref='catalog') + + class Category(self.Base): + __tablename__ = 'category' + id = sa.Column(sa.Integer, primary_key=True) + catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id')) + + sub_categories = sa.orm.relationship( + 'SubCategory', backref='category' + ) + + class SubCategory(self.Base): + __tablename__ = 'sub_category' + id = sa.Column(sa.Integer, primary_key=True) + category_id = sa.Column(sa.Integer, sa.ForeignKey('category.id')) + products = sa.orm.relationship( + 'Product', + backref='sub_category' + ) + + class Product(self.Base): + __tablename__ = 'product' + id = sa.Column(sa.Integer, primary_key=True) + price = sa.Column(sa.Numeric) + + sub_category_id = sa.Column( + sa.Integer, sa.ForeignKey('sub_category.id') + ) + + def __repr__(self): + return '' % self.id + + self.Catalog = Catalog + self.Category = Category + self.SubCategory = SubCategory + self.Product = Product + + +class ThreeLevelDeepManyToMany(object): + def create_models(self): + catalog_category = sa.Table( + 'catalog_category', + self.Base.metadata, + sa.Column('catalog_id', sa.Integer, sa.ForeignKey('catalog.id')), + sa.Column('category_id', sa.Integer, sa.ForeignKey('category.id')) + ) + + category_subcategory = sa.Table( + 'category_subcategory', + self.Base.metadata, + sa.Column( + 'category_id', + sa.Integer, + sa.ForeignKey('category.id') + ), + sa.Column( + 'subcategory_id', + sa.Integer, + sa.ForeignKey('sub_category.id') + ) + ) + + subcategory_product = sa.Table( + 'subcategory_product', + self.Base.metadata, + sa.Column( + 'subcategory_id', + sa.Integer, + sa.ForeignKey('sub_category.id') + ), + sa.Column( + 'product_id', + sa.Integer, + sa.ForeignKey('product.id') + ) + ) + + class Catalog(self.Base): + __tablename__ = 'catalog' + id = sa.Column(sa.Integer, primary_key=True) + + categories = sa.orm.relationship( + 'Category', + backref='catalogs', + secondary=catalog_category + ) + + class Category(self.Base): + __tablename__ = 'category' + id = sa.Column(sa.Integer, primary_key=True) + + sub_categories = sa.orm.relationship( + 'SubCategory', + backref='categories', + secondary=category_subcategory + ) + + class SubCategory(self.Base): + __tablename__ = 'sub_category' + id = sa.Column(sa.Integer, primary_key=True) + products = sa.orm.relationship( + 'Product', + backref='sub_categories', + secondary=subcategory_product + ) + + class Product(self.Base): + __tablename__ = 'product' + id = sa.Column(sa.Integer, primary_key=True) + price = sa.Column(sa.Numeric) + + self.Catalog = Catalog + self.Category = Category + self.SubCategory = SubCategory + self.Product = Product diff --git a/tests/relationships/test_chained_join.py b/tests/relationships/test_chained_join.py index 550e73b..3e748ca 100644 --- a/tests/relationships/test_chained_join.py +++ b/tests/relationships/test_chained_join.py @@ -1,90 +1,16 @@ -import sqlalchemy as sa - from sqlalchemy_utils.relationships import chained_join from tests import TestCase +from tests.mixins import ( + ThreeLevelDeepManyToMany, + ThreeLevelDeepOneToMany, + ThreeLevelDeepOneToOne, +) -class TestChainedJoinForManyToManyToManyToMany(TestCase): +class TestChainedJoinFoDeepToManyToMany(ThreeLevelDeepManyToMany, TestCase): dns = 'postgres://postgres@localhost/sqlalchemy_utils_test' create_tables = False - def create_models(self): - catalog_category = sa.Table( - 'catalog_category', - self.Base.metadata, - sa.Column('catalog_id', sa.Integer, sa.ForeignKey('catalog.id')), - sa.Column('category_id', sa.Integer, sa.ForeignKey('category.id')) - ) - - category_subcategory = sa.Table( - 'category_subcategory', - self.Base.metadata, - sa.Column( - 'category_id', - sa.Integer, - sa.ForeignKey('category.id') - ), - sa.Column( - 'subcategory_id', - sa.Integer, - sa.ForeignKey('sub_category.id') - ) - ) - - subcategory_product = sa.Table( - 'subcategory_product', - self.Base.metadata, - sa.Column( - 'subcategory_id', - sa.Integer, - sa.ForeignKey('sub_category.id') - ), - sa.Column( - 'product_id', - sa.Integer, - sa.ForeignKey('product.id') - ) - ) - - class Catalog(self.Base): - __tablename__ = 'catalog' - id = sa.Column(sa.Integer, primary_key=True) - - categories = sa.orm.relationship( - 'Category', - backref='catalogs', - secondary=catalog_category - ) - - class Category(self.Base): - __tablename__ = 'category' - id = sa.Column(sa.Integer, primary_key=True) - - sub_categories = sa.orm.relationship( - 'SubCategory', - backref='categories', - secondary=category_subcategory - ) - - class SubCategory(self.Base): - __tablename__ = 'sub_category' - id = sa.Column(sa.Integer, primary_key=True) - products = sa.orm.relationship( - 'Product', - backref='sub_categories', - secondary=subcategory_product - ) - - class Product(self.Base): - __tablename__ = 'product' - id = sa.Column(sa.Integer, primary_key=True) - price = sa.Column(sa.Numeric) - - self.Catalog = Catalog - self.Category = Category - self.SubCategory = SubCategory - self.Product = Product - def test_simple_join(self): assert str(chained_join(self.Catalog.categories)) == ( 'catalog_category JOIN category ON ' @@ -120,52 +46,10 @@ class TestChainedJoinForManyToManyToManyToMany(TestCase): ) -class TestChainedJoinFor3LevelDeepOneToMany(TestCase): +class TestChainedJoinForDeepOneToMany(ThreeLevelDeepOneToMany, TestCase): dns = 'postgres://postgres@localhost/sqlalchemy_utils_test' create_tables = False - def create_models(self): - class Catalog(self.Base): - __tablename__ = 'catalog' - id = sa.Column(sa.Integer, primary_key=True) - - categories = sa.orm.relationship('Category', backref='catalog') - - class Category(self.Base): - __tablename__ = 'category' - id = sa.Column(sa.Integer, primary_key=True) - catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id')) - - sub_categories = sa.orm.relationship( - 'SubCategory', backref='category' - ) - - class SubCategory(self.Base): - __tablename__ = 'sub_category' - id = sa.Column(sa.Integer, primary_key=True) - category_id = sa.Column(sa.Integer, sa.ForeignKey('category.id')) - products = sa.orm.relationship( - 'Product', - backref='sub_category' - ) - - class Product(self.Base): - __tablename__ = 'product' - id = sa.Column(sa.Integer, primary_key=True) - price = sa.Column(sa.Numeric) - - sub_category_id = sa.Column( - sa.Integer, sa.ForeignKey('sub_category.id') - ) - - def __repr__(self): - return '' % self.id - - self.Catalog = Catalog - self.Category = Category - self.SubCategory = SubCategory - self.Product = Product - def test_simple_join(self): assert str(chained_join(self.Catalog.categories)) == 'category' @@ -192,53 +76,9 @@ class TestChainedJoinFor3LevelDeepOneToMany(TestCase): ) -class TestChainedJoinForOneToOneToOneToOne(TestCase): +class TestChainedJoinForDeepOneToOne(ThreeLevelDeepOneToOne, TestCase): dns = 'postgres://postgres@localhost/sqlalchemy_utils_test' - - def create_models(self): - class Catalog(self.Base): - __tablename__ = 'catalog' - id = sa.Column(sa.Integer, primary_key=True) - category = sa.orm.relationship( - 'Category', - uselist=False, - backref='catalog' - ) - - class Category(self.Base): - __tablename__ = 'category' - id = sa.Column(sa.Integer, primary_key=True) - catalog_id = sa.Column(sa.Integer, sa.ForeignKey('catalog.id')) - - sub_category = sa.orm.relationship( - 'SubCategory', - uselist=False, - backref='category' - ) - - class SubCategory(self.Base): - __tablename__ = 'sub_category' - id = sa.Column(sa.Integer, primary_key=True) - category_id = sa.Column(sa.Integer, sa.ForeignKey('category.id')) - product = sa.orm.relationship( - 'Product', - uselist=False, - backref='sub_category' - ) - - class Product(self.Base): - __tablename__ = 'product' - id = sa.Column(sa.Integer, primary_key=True) - price = sa.Column(sa.Integer) - - sub_category_id = sa.Column( - sa.Integer, sa.ForeignKey('sub_category.id') - ) - - self.Catalog = Catalog - self.Category = Category - self.SubCategory = SubCategory - self.Product = Product + create_tables = False def test_simple_join(self): assert str(chained_join(self.Catalog.category)) == 'category'