def snapshot_factory(): snapshot = model.Snapshot() snapshot.creation_time = datetime(1999, 1, 1) snapshot.resource_type = 'dummy' snapshot.resource_pkey = 1 snapshot.resource_name = 'dummy' snapshot.operation = 'added' snapshot.data = '{}' return snapshot
def snapshot_factory(): snapshot = model.Snapshot() snapshot.creation_time = datetime(1999, 1, 1) snapshot.resource_type = "dummy" snapshot.resource_pkey = 1 snapshot.resource_name = "dummy" snapshot.operation = "added" snapshot.data = "{}" return snapshot
def _create(operation: str, entity: model.Base, auth_user: Optional[model.User]) -> model.Snapshot: resource_type, resource_pkey, resource_name = ( model.util.get_resource_info(entity)) snapshot = model.Snapshot() snapshot.creation_time = datetime.utcnow() snapshot.operation = operation snapshot.resource_type = resource_type snapshot.resource_pkey = resource_pkey snapshot.resource_name = resource_name snapshot.user = auth_user return snapshot
def test_serialize_snapshot(user_factory): auth_user = user_factory() snapshot = model.Snapshot() snapshot.operation = snapshot.OPERATION_CREATED snapshot.resource_type = 'type' snapshot.resource_name = 'id' snapshot.user = user_factory(name='issuer') snapshot.data = {'complex': list('object')} snapshot.creation_time = datetime(1997, 1, 1) with patch('szurubooru.func.users.serialize_micro_user'): users.serialize_micro_user.return_value = 'mocked' assert snapshots.serialize_snapshot(snapshot, auth_user) == { 'operation': 'created', 'type': 'type', 'id': 'id', 'user': '******', 'data': {'complex': list('object')}, 'time': datetime(1997, 1, 1), }
def test_serialize_snapshot(user_factory): auth_user = user_factory() snapshot = model.Snapshot() snapshot.operation = snapshot.OPERATION_CREATED snapshot.resource_type = "type" snapshot.resource_name = "id" snapshot.user = user_factory(name="issuer") snapshot.data = {"complex": list("object")} snapshot.creation_time = datetime(1997, 1, 1) with patch("szurubooru.func.users.serialize_micro_user"): users.serialize_micro_user.return_value = "mocked" assert snapshots.serialize_snapshot(snapshot, auth_user) == { "operation": "created", "type": "type", "id": "id", "user": "******", "data": { "complex": list("object") }, "time": datetime(1997, 1, 1), }
def test_cascade_deletions(post_factory, user_factory, comment_factory): user = user_factory() post = post_factory() post.user = user post_score = model.PostScore() post_score.post = post post_score.user = user post_score.time = datetime(1997, 1, 1) post_score.score = 1 post.scores.append(post_score) post_favorite = model.PostFavorite() post_favorite.post = post post_favorite.user = user post_favorite.time = datetime(1997, 1, 1) post.favorited_by.append(post_favorite) post_feature = model.PostFeature() post_feature.post = post post_feature.user = user post_feature.time = datetime(1997, 1, 1) post.features.append(post_feature) comment = comment_factory(post=post, user=user) comment_score = model.CommentScore() comment_score.comment = comment comment_score.user = user comment_score.time = datetime(1997, 1, 1) comment_score.score = 1 comment.scores.append(comment_score) snapshot = model.Snapshot() snapshot.user = user snapshot.creation_time = datetime(1997, 1, 1) snapshot.resource_type = "-" snapshot.resource_pkey = 1 snapshot.resource_name = "-" snapshot.operation = "-" db.session.add_all([user, post, comment, snapshot]) db.session.commit() assert not db.session.dirty assert post.user is not None and post.user.user_id is not None assert db.session.query(model.User).count() == 1 assert db.session.query(model.Post).count() == 1 assert db.session.query(model.PostScore).count() == 1 assert db.session.query(model.PostFeature).count() == 1 assert db.session.query(model.PostFavorite).count() == 1 assert db.session.query(model.Comment).count() == 1 assert db.session.query(model.CommentScore).count() == 1 assert db.session.query(model.Snapshot).count() == 1 db.session.delete(user) db.session.commit() assert not db.session.dirty assert db.session.query(model.User).count() == 0 assert db.session.query(model.Post).count() == 1 assert db.session.query(model.Post)[0].user is None assert db.session.query(model.PostScore).count() == 0 assert db.session.query(model.PostFeature).count() == 0 assert db.session.query(model.PostFavorite).count() == 0 assert db.session.query(model.Comment).count() == 1 assert db.session.query(model.Comment)[0].user is None assert db.session.query(model.CommentScore).count() == 0 assert db.session.query(model.Snapshot).count() == 1 assert db.session.query(model.Snapshot)[0].user is None