diff options
| author | Hunternif | 2019-04-16 18:53:30 +0700 |
|---|---|---|
| committer | Hunternif | 2019-04-16 19:08:31 +0700 |
| commit | 91a31eba4372a59592bec892fe82ec7a3fc8dc7e (patch) | |
| tree | 8990c05191dd1d3d076dfcf5367dd263fd6a46c4 | |
| parent | af059d648ad6a4ffd19321c7c1197cf4a56cc07f (diff) | |
Update metric models and create first unit test
| -rw-r--r-- | server/szurubooru/model/__init__.py | 1 | ||||
| -rw-r--r-- | server/szurubooru/model/metric.py | 26 | ||||
| -rw-r--r-- | server/szurubooru/model/tag.py | 9 | ||||
| -rw-r--r-- | server/szurubooru/tests/model/test_metric.py | 66 |
4 files changed, 75 insertions, 27 deletions
diff --git a/server/szurubooru/model/__init__.py b/server/szurubooru/model/__init__.py index 4892b97..202e434 100644 --- a/server/szurubooru/model/__init__.py +++ b/server/szurubooru/model/__init__.py @@ -2,6 +2,7 @@ from szurubooru.model.base import Base from szurubooru.model.user import User, UserToken from szurubooru.model.tag_category import TagCategory from szurubooru.model.tag import Tag, TagName, TagSuggestion, TagImplication +from szurubooru.model.metric import Metric, PostMetric, PostMetricRange from szurubooru.model.post import ( Post, PostTag, diff --git a/server/szurubooru/model/metric.py b/server/szurubooru/model/metric.py index d879b8d..b4003e9 100644 --- a/server/szurubooru/model/metric.py +++ b/server/szurubooru/model/metric.py @@ -22,12 +22,7 @@ class PostMetric(Base): version = sa.Column('version', sa.Integer, default=1, nullable=False) value = sa.Column('value', sa.Float, nullable=False, index=True) - metric = sa.orm.relationship( - 'Metric', - backref=sa.orm.backref('post_metrics', cascade='all, delete-orphan')) - - def __init__(self, value) -> None: - self.value = value + post = sa.orm.relationship('Post') __mapper_args__ = { 'version_id_col': version, @@ -60,14 +55,7 @@ class PostMetricRange(Base): low = sa.Column('low', sa.Float, nullable=False) high = sa.Column('low', sa.Float, nullable=False) - metric = sa.orm.relationship( - 'Metric', - backref=sa.orm.backref('post_metric_ranges', cascade='all, delete-orphan')) - - def __init__(self, tag_id: int, low: float, high: float) -> None: - self.tag_id = tag_id - self.low = low - self.high = high + post = sa.orm.relationship('Post') __mapper_args__ = { 'version_id_col': version, @@ -92,6 +80,11 @@ class Metric(Base): min = sa.Column('min', sa.Float, nullable=False) max = sa.Column('max', sa.Float, nullable=False) + post_metrics = sa.orm.relationship( + 'PostMetric', backref='metric', cascade='all, delete-orphan') + post_metric_ranges = sa.orm.relationship( + 'PostMetricRange', backref='metric', cascade='all, delete-orphan') + post_metric_count = sa.orm.column_property( sa.sql.expression.select( [sa.sql.expression.func.count(PostMetric.post_id)]) @@ -104,11 +97,6 @@ class Metric(Base): .where(PostMetricRange.tag_id == tag_id) .correlate_except(PostMetricRange)) - def __init__(self, tag_id: int, min: float, max: float) -> None: - self.tag_id = tag_id - self.min = min - self.max = max - __mapper_args__ = { 'version_id_col': version, 'version_id_generator': False, diff --git a/server/szurubooru/model/tag.py b/server/szurubooru/model/tag.py index 2d6d015..61d1625 100644 --- a/server/szurubooru/model/tag.py +++ b/server/szurubooru/model/tag.py @@ -1,7 +1,6 @@ import sqlalchemy as sa from szurubooru.model.base import Base from szurubooru.model.post import PostTag -from szurubooru.model.metric import Metric class TagSuggestion(Base): @@ -101,6 +100,7 @@ class Tag(Base): primaryjoin=tag_id == TagImplication.parent_id, secondaryjoin=tag_id == TagImplication.child_id, lazy='joined') + metric = sa.orm.relationship('Metric', backref=sa.orm.backref('tag', uselist=False)) post_count = sa.orm.column_property( sa.sql.expression.select( @@ -136,13 +136,6 @@ class Tag(Base): ), deferred=True) - has_metric = sa.orm.column_property( - sa.sql.expression.exists( - sa.sql.expression.select([Metric.tag_id]) - .where(Metric.tag_id == tag_id) - ), - deferred=True) - __mapper_args__ = { 'version_id_col': version, 'version_id_generator': False, diff --git a/server/szurubooru/tests/model/test_metric.py b/server/szurubooru/tests/model/test_metric.py new file mode 100644 index 0000000..28a66f0 --- /dev/null +++ b/server/szurubooru/tests/model/test_metric.py @@ -0,0 +1,66 @@ +from szurubooru import db, model + + +def test_saving_metric(post_factory, tag_factory): + post = post_factory() + tag = tag_factory() + metric = model.Metric(tag=tag, min=1., max=10.) + post_metric = model.PostMetric(metric=metric, post=post, value=5.5) + post_metric_range = model.PostMetricRange(metric=metric, post=post, low=2., high=8.) + db.session.add_all([post, tag, metric, post_metric, post_metric_range]) + db.session.commit() + + assert metric.tag_id is not None + assert post_metric.tag_id is not None + assert post_metric.post_id is not None + assert post_metric_range.tag_id is not None + assert post_metric_range.post_id is not None + + metric = ( + db.session + .query(model.Metric) + .filter(model.Metric.tag_id == tag.tag_id) + .one()) + assert metric.min == 1. + assert metric.max == 10. + + post_metric = ( + db.session + .query(model.PostMetric) + .filter(model.PostMetric.tag_id == tag.tag_id and + model.PostMetric.post_id == post.post_id) + .one()) + assert post_metric.value == 5.5 + + post_metric_range = ( + db.session + .query(model.PostMetricRange) + .filter(model.PostMetricRange.tag_id == tag.tag_id and + model.PostMetricRange.post_id == post.post_id) + .one()) + assert post_metric_range.low == 2. + assert post_metric_range.high == 8. + + +def test_cascade_delete_metric(): + pass + + +def test_cascade_delete_tag(): + pass + + +def test_cascade_delete_post(): + pass + + +def test_tag_without_metric(): + pass + + +def test_post_tag_without_metric(): + pass + + +def test_metric_counts(): + pass |