diff --git a/app/models.py b/app/models.py index 5655d5f..07b1fa3 100644 --- a/app/models.py +++ b/app/models.py @@ -4,18 +4,44 @@ from sqlalchemy.orm import relationship db = SQLAlchemy() +# +# Categorized projects +# + +class Project(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String) + labels = relationship("ProjectLabel", back_populates="project") + + class Category(db.Model): id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(32)) - labels = relationship("CategoryLabel", back_populates='category') + name = db.Column(db.String) + labels = relationship("Label", back_populates="category") + projects = relationship("ProjectLabel", back_populates="category") -class CategoryLabel(db.Model): +class Label(db.Model): id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(32)) + name = db.Column(db.String, unique=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) - category = relationship("Category", back_populates='labels') + category = relationship("Category", back_populates="labels") + projects = relationship("ProjectLabel", back_populates="label") + + +class ProjectLabel(db.Model): + project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True) + category_id = db.Column(db.Integer, db.ForeignKey('category.id'), primary_key=True) + label_id = db.Column(db.Integer, db.ForeignKey('label.id')) + + project = relationship("Project", back_populates="labels") + category = relationship("Category", back_populates="projects") + label = relationship("Label", back_populates="projects") + +# +# Agents +# class AgentBap(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -49,22 +75,12 @@ class Agent(db.Model): permanent = db.Column(db.Integer) # integer boolean -class Project(db.Model): - id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(100), unique=True) - - class Service(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) abbr = db.Column(db.String(50), unique=True) -class Function(db.Model): - id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(100), unique=True) - - class Capacity(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) diff --git a/tests/common_db_feed.py b/tests/common_db_feed.py new file mode 100644 index 0000000..dbe76db --- /dev/null +++ b/tests/common_db_feed.py @@ -0,0 +1,39 @@ +from app.models import Category, db, Label, Project, ProjectLabel + +categorized_labels = {'pole': ['Spatial', 'Sol'], 'domaine': ['soleil-terre', 'atmosphere', 'r&t', 'géologie']} +projects_categories = {'ChemCam': {'pole': 'Spatial', 'domaine': 'géologie'}, + 'Pilot': {'pole': 'Spatial', 'domaine': 'atmosphere'}, + 'Ambre': {'pole': 'Spatial', 'domaine': 'soleil-terre'}} + + +def feed_projects(): + for c_name, labels in categorized_labels.items(): + n_c = Category(name=c_name) + db.session.add(n_c) + for l_name in labels: + n_l = Label(name=l_name, category=n_c) + db.session.add(n_l) + + db.session.commit() + + project_names = projects_categories.keys() + + for p_name in set(project_names): + n_p = Project(name=p_name) + db.session.add(n_p) + db.session.commit() + + for p, categories in projects_categories.items(): + n_p = db.session.query(Project).filter(Project.name == p).one() + for c_name, l_name in categories.items(): + n_c = db.session.query(Category).filter(Category.name == c_name).one() + n_l = db.session.query(Label).filter(Label.name == l_name).one() + n_pc = ProjectLabel(project=n_p, category=n_c, label=n_l) + db.session.add(n_pc) + + db.session.commit() + + +def show_categories(): + for c in db.session.query(Category).all(): + print(c.name + ": ", ",".join([l.name for l in c.categorized_labels])) diff --git a/tests/db_tests.py b/tests/db_tests.py new file mode 100644 index 0000000..18772be --- /dev/null +++ b/tests/db_tests.py @@ -0,0 +1,28 @@ +import unittest + +from app import create_app, db +from app.models import Project +from pdc_config import TestConfig +from tests.common_db_feed import feed_projects + + +class DbBaseTestCase(unittest.TestCase): + def setUp(self): + self.app = create_app(TestConfig) + # force db uri to sqlite memory + self.app.config.update( + SQLALCHEMY_DATABASE_URI='sqlite:///:memory:' + ) + self.app_context = self.app.app_context() + self.app_context.push() + db.create_all() + feed_projects() + + def tearDown(self): + db.session.remove() + db.drop_all() + self.app_context.pop() + + def test_first(self): + projects = Project.query.all() + self.assertEqual(3, len(projects)) -- libgit2 0.21.2