diff options
author | Priit Laes <plaes@plaes.org> | 2010-08-01 17:20:01 +0300 |
---|---|---|
committer | Priit Laes <plaes@plaes.org> | 2010-08-01 17:20:01 +0300 |
commit | 73ae40c9a407e4743cc6086a277abcaf6c9ab146 (patch) | |
tree | 6c13f7138ac2a903a42cad13ed1fe49f3b665a4e | |
parent | Use transactions in herd check plugin (diff) | |
download | gsoc2010-grumpy-73ae40c9a407e4743cc6086a277abcaf6c9ab146.tar.gz gsoc2010-grumpy-73ae40c9a407e4743cc6086a277abcaf6c9ab146.tar.bz2 gsoc2010-grumpy-73ae40c9a407e4743cc6086a277abcaf6c9ab146.zip |
Update Category and Package model, rework unittests
-rw-r--r-- | grumpy/models.py | 56 | ||||
-rw-r--r-- | grumpy/testsuite/__init__.py | 20 | ||||
-rw-r--r-- | grumpy/testsuite/pkgmodel.py | 101 |
3 files changed, 96 insertions, 81 deletions
diff --git a/grumpy/models.py b/grumpy/models.py index e26c24a..c3f0b71 100644 --- a/grumpy/models.py +++ b/grumpy/models.py @@ -14,7 +14,7 @@ from datetime import datetime from flaskext.sqlalchemy import SQLAlchemy from sqlalchemy.sql import func -import json, random, string +import json, random, string, time db = SQLAlchemy(app) @@ -33,13 +33,16 @@ class Category(db.Model): __tablename__ = 'categories' id = db.Column('id', db.Integer, primary_key=True) - cat = db.Column('category', db.String, nullable=False, unique=True) + category = db.Column('category', db.String, nullable=False, unique=True) - def __init__(self, cat): - self.cat = cat + packages = db.relationship("Package", backref='category', \ + cascade='all, delete, delete-orphan') + + def __init__(self, category): + self.category = category def __repr__(self): - return '<%s> "%s"' % (self.__class__.__name__, self.cat) + return '<%s> "%s"' % (self.__class__.__name__, self.category) class Developer(db.Model): """Represents developers in the system""" @@ -157,13 +160,13 @@ class Package(db.Model): __tablename__ = 'packages' id = db.Column('id', db.Integer, primary_key=True) - cat = db.Column('cat', db.String, nullable=False) pkg = db.Column('pkg', db.String, nullable=False) - cp = db.Column('cp', db.String, nullable=False, unique=True) + pkg_cat = db.Column('pkg_cat', db.String, nullable=False, unique=True) desc = db.Column('desc', db.String) ldesc = db.Column('ldesc', db.String) homepage = db.Column('homepage', db.String) mtime = db.Column('mtime', db.DateTime) + cat_id = db.Column('cat_id', db.Integer, db.ForeignKey('categories.id')) ebuilds = db.relationship(Ebuild, backref='package', \ cascade='all, delete, delete-orphan') @@ -173,29 +176,28 @@ class Package(db.Model): backref='packages') herds = db.relationship(Herd, secondary=package_herds, backref='packages') - def __init__(self, cat, pkg, desc, ldesc, homepage, mtime): - self.cat = cat - self.pkg = pkg - self.cp = "%s/%s" % (pkg, cat) - self.desc = desc - self.ldesc = ldesc - self.homepage = homepage + def __init__(self, ebuild_src, mtime=time.time()): + self.pkg = ebuild_src.package + self.pkg_cat = "%s/%s" % (self.pkg, ebuild_src.category) + self.desc = ebuild_src.description + self.homepage = ebuild_src.homepage self.mtime = datetime.fromtimestamp(mtime) - self.devs = [] - self.herds = [] - self.ebuilds = [] + # shared pkg data (from metadata.xml) + self.ldesc = ebuild_src.longdescription + self.devs = [] # TODO + self.herds = [] # TODO def __repr__(self): - return '<%s> - %s' % (self.__class__.__name__, self.cp) - - def rename(self, newcat, newpkg): - """Rename package and its ebuilds""" - cp = "%s/%s" % (newpkg, newcat) - self.cat = newcat - self.pkg = newpkg - self.cp = cp - for ebuild in self.ebuilds: - ebuild.cpv = "%s-%s" % (cp, ebuild.version) + return '<%s> - %s' % (self.__class__.__name__, self.pkg_cat) + +# def rename(self, newcat, newpkg): +# """Rename package and its ebuilds""" +# cp = "%s/%s" % (newpkg, newcat) +# self.cat = newcat +# self.pkg = newpkg +# self.cp = cp +# for ebuild in self.ebuilds: +# ebuild.cpv = "%s-%s" % (cp, ebuild.version) class PkgIssue(db.Model): """Package-related issues""" diff --git a/grumpy/testsuite/__init__.py b/grumpy/testsuite/__init__.py index 4884a04..9b17c8a 100644 --- a/grumpy/testsuite/__init__.py +++ b/grumpy/testsuite/__init__.py @@ -12,12 +12,23 @@ import unittest from grumpy import app from grumpy.models import db -from pkgcore.ebuild import ebuild_src +from pkgcore.ebuild import ebuild_src, repo_objs class GrumpyTestCase(unittest.TestCase): - def get_pkg(self, cpv, data={}): - o = ebuild_src.base(*([] + [None, cpv])) + def get_pkg(self, cpv, pkg_data={}, data={}): + # We need to set up info from metadata.xml separately + valid_keys = ("longdescription", "maintainers", "herds") + for x in valid_keys: + pkg_data.setdefault(x, "") + metadata = repo_objs.MetadataXml(None) + for key, value in pkg_data.iteritems(): + if key not in valid_keys: + continue + object.__setattr__(metadata, "_" + key, value) + shared_pkg_data = repo_objs.SharedPkgData(metadata, None) + + o = ebuild_src.package(shared_pkg_data, None, cpv) if data is not None: object.__setattr__(o, 'data', data) return o @@ -31,12 +42,13 @@ class GrumpyTestCase(unittest.TestCase): self.db = db def tearDown(self): + self.db.session.rollback() self.db.drop_all() def suite(): from . import favorites, pkgmodel, usermodel suite = unittest.TestSuite() - suite.addTest(favorites.suite()) + #suite.addTest(favorites.suite()) suite.addTest(pkgmodel.suite()) suite.addTest(usermodel.suite()) return suite diff --git a/grumpy/testsuite/pkgmodel.py b/grumpy/testsuite/pkgmodel.py index 87d12e9..a6f8b3b 100644 --- a/grumpy/testsuite/pkgmodel.py +++ b/grumpy/testsuite/pkgmodel.py @@ -14,16 +14,48 @@ from grumpy.models import Category, Ebuild, Package import time, unittest -categories = ['app-misc', 'app-test'] - class PkgModelTestCase(GrumpyTestCase): - def make_categories(self): - for cat in categories: - self.db.session.add(Category(cat)) - self.db.session.commit() + def test_portage_data_model(self): + ## Create portage data + # TODO: metadata.xml contents + # app-misc/testpkg + C1 = 'app-misc' + data = {'EAPI': '0', 'SLOT': '1.1', \ + 'DESCRIPTION': 'This is a simple description', \ + 'LONGDESCRIPTION': 'This is long description from metadata.xml', \ + 'HOMEPAGE': 'http://example.org/testpkg-app-misc', \ + 'KEYWORDS':'x86 amd64', \ + 'IUSE':'test +positive flags -negative split-flag'} + sdata = {'longdescription': 'foo!'} + p1 = self.get_pkg('%s/testpkg-1.0-r3' % C1, sdata, data) + data['KEYWORDS'] = 'x86 ~amd64' + p2 = self.get_pkg('%s/testpkg-1.0-r4' % C1, sdata, data) + # app-test/testpkg + C2 = 'app-test' + data = {'EAPI': '0', 'SLOT': '1.0', \ + 'DESCRIPTION': 'This is a also a simple description', \ + 'HOMEPAGE': 'http://example.org/testpkg-app-test', \ + 'KEYWORDS':'x86 ~amd64', \ + 'IUSE':'test +positive flags -negative split-flag'} + p3 = self.get_pkg('%s/testpkg-1.0-r3' % C2, sdata, data) + + with self.app.test_request_context(): + ## Create and test categories + c = [Category(C1), Category(C2)] + self.db.session.add_all(c) + self.db.session.commit() + assert Category.query.count() == 2 + + ## Create and test packages + c[0].packages.append(Package(p1)) + # TODO: Need to figure out a convenience method... + #c[0].packages.append(Package(p2)) + c[1].packages.append(Package(p3)) + self.db.session.commit() + assert Package.query.count() == 2 - def make_package_and_ebuilds(self): + def make_packages_and_ebuild(self, categories): # Create package... p = Package(categories[0], 'testpkg', 'Test Package', \ 'Long Description', 'http://example.com/test', \ @@ -38,49 +70,18 @@ class PkgModelTestCase(GrumpyTestCase): self.db.session.commit() return p - def test_ebuild_parsing(self): - with self.app.test_request_context(): - p = self.make_package_and_ebuilds() - e = Ebuild.query.first() - assert e.iuse == 'test,flags,split-flag' - assert e.iuse_neg == 'negative' - assert e.iuse_pos == 'positive' - assert e.keywords == 'x86,amd64' - assert e.eapi == 0 - assert e.slot == '1.1' - assert e.pcv == 'testpkg/app-misc-1.0-r3' - assert e.package == p - - def test_ebuild_syncing(self): - with self.app.test_request_context(): - p = self.make_package_and_ebuilds() - e = Ebuild.query.first() - data = {'EAPI': '1', 'SLOT': '1.2'} - e.sync(self.get_pkg('app-misc/testpkg-1.0-r3', data)) - self.db.session.commit() - assert e.eapi == 1 - assert e.slot == '1.2' - assert e.keywords == '' - assert e.pcv == 'testpkg/app-misc-1.0-r3' - assert e.package == p - - def test_category_and_package_model(self): - with self.app.test_request_context(): - self.make_categories() - self.make_package_and_ebuilds() - assert Category.query.count() == 2 - assert Ebuild.query.count() == 2 - assert Package.query.count() == 1 - p = Package.query.first() - assert len(p.ebuilds) == 2 - - # Test garbage collection - self.db.session.delete(p) - self.db.session.commit() - assert Category.query.count() == 2 - # Cascade delete should also get rid of ebuilds - assert Ebuild.query.count() == 0 - assert Package.query.count() == 0 +# def test_ebuild_parsing(self): +# with self.app.test_request_context(): +# p = self.make_package_and_ebuilds() +# e = Ebuild.query.first() +# assert e.iuse == 'test,flags,split-flag' +# assert e.iuse_neg == 'negative' +# assert e.iuse_pos == 'positive' +# assert e.keywords == 'x86,amd64' +# assert e.eapi == 0 +# assert e.slot == '1.1' +# assert e.pcv == 'testpkg/app-misc-1.0-r3' +# assert e.package == p def suite(): suite = unittest.TestSuite() |