Skip to content

Commit

Permalink
Add test for relationship array post
Browse files Browse the repository at this point in the history
  • Loading branch information
duk3luk3 committed Jan 8, 2018
1 parent ff0f02d commit fe9d6f2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
22 changes: 21 additions & 1 deletion sqlalchemy_jsonapi/tests/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Boolean, Column, ForeignKey, Unicode, UnicodeText
from sqlalchemy import Boolean, Column, ForeignKey, Unicode, UnicodeText, Table
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import backref, relationship, validates
from sqlalchemy_jsonapi import (
Expand Down Expand Up @@ -92,6 +92,10 @@ def allow_delete(self):
""" Just like a popular social media site, we won't delete users. """
return False

PostTags = Table('post_tag', db.Model.metadata,
Column('post_id', UUIDType, ForeignKey('posts.id')),
Column('tag_id', UUIDType, ForeignKey('tags.id'))
)

class BlogPost(Timestamp, db.Model):
"""Post model, as if this is a blog."""
Expand All @@ -110,6 +114,10 @@ class BlogPost(Timestamp, db.Model):
backref=backref('posts',
lazy='dynamic'))

tags = relationship("BlogTag",
secondary=PostTags,
back_populates="posts")

@validates('title')
def validate_title(self, key, title):
"""Keep titles from getting too long."""
Expand All @@ -126,6 +134,18 @@ def allow_view(self):
def prevent_altering_of_logs(self):
return False

class BlogTag(Timestamp, db.Model):
"""Blogs can have tags now"""

__tablename__ = 'tags'

id = Column(UUIDType, default=uuid4, primary_key=True)
slug = Column(Unicode(100), unique=True)
description = Column(UnicodeText)

posts = relationship("BlogPost",
secondary=PostTags,
back_populates="tags")

class BlogComment(Timestamp, db.Model):
"""Comment for each Post."""
Expand Down
9 changes: 8 additions & 1 deletion sqlalchemy_jsonapi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from flask.testing import FlaskClient
from sqlalchemy.orm import sessionmaker
from app import db as db_
from app import app, User, BlogPost, BlogComment, Log
from app import app, User, BlogPost, BlogComment, BlogTag, Log
from faker import Faker

Session = sessionmaker()
Expand Down Expand Up @@ -116,6 +116,13 @@ def bunch_of_posts(user, session):
BlogComment(author=user, content=fake.paragraph()))
session.commit()

@pytest.fixture
def bunch_of_tags(session):
tags = [BlogTag(slug=fake.word(), description=fake.text()) for x in range(3)]
for tag in tags:
session.add(tag)
session.commit()
return tags

@pytest.fixture
def comment(user, post, session):
Expand Down
37 changes: 37 additions & 0 deletions sqlalchemy_jsonapi/tests/test_collection_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ def test_200_resource_creation_with_relationships(user, client):
'id'
] == str(user.id)

def test_200_resource_creation_with_relationship_array(user, bunch_of_tags, client):
payload = {
'data': {
'type': 'blog-posts',
'attributes': {
'title': 'Some title',
'content': 'Hello, World!',
'is-published': True
},
'relationships': {
'author': {
'data': {
'type': 'users',
'id': str(user.id)
}
},
'tags': {
'data': [{
'type': 'blog-tags',
'id': str(tag.id)
} for tag in bunch_of_tags
]
}
}
}
}
response = client.post(
'/api/blog-posts/', data=json.dumps(payload),
content_type='application/vnd.api+json').validate(201)
assert response.json_data['data']['type'] == 'blog-posts'
post_id = response.json_data['data']['id']
response = client.get(
'/api/blog-posts/{}/?include=author'.format(post_id)).validate(200)
assert response.json_data['data']['relationships']['author']['data'][
'id'
] == str(user.id)


def test_403_when_access_is_denied(client):
payload = {'data': {'type': 'logs'}}
Expand Down

0 comments on commit fe9d6f2

Please sign in to comment.