Skip to content

Commit

Permalink
add new unit test and setup testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson3 committed Feb 28, 2017
1 parent 94f1804 commit f9c223a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Empty file.
21 changes: 21 additions & 0 deletions sqlalchemy_jsonapi/unittests/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Model file for unit testing."""

from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy_jsonapi import JSONAPI


Base = declarative_base()


class User(Base):
"""Simple user model."""

__tablename__ = 'users'
id = Column(Integer, primary_key=True)
first = Column(String(50), nullable=False)
last = Column(String(50), nullable=False)


serializer = JSONAPI(Base)
44 changes: 44 additions & 0 deletions sqlalchemy_jsonapi/unittests/test_serializer_post_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Test for serializer's post_collection."""

from sqlalchemy_jsonapi.unittests.utils import testcases
from sqlalchemy_jsonapi.unittests.models import serializer


class PostCollection(testcases.SqlalchemyJsonapiTestCase):
"""Tests for serializer.post_collection."""

def test_201_resource_creation(self):
"""Create resource object succesfully."""
payload = {
'data': {
'type': 'users',
'attributes': {
'first': 'Sally',
'last': 'Smith'
}
}
}
response = serializer.post_collection(self.session, payload, 'users')

expected_response = {
'data': {
'attributes': {
'first': 'Sally',
'last': 'Smith'
},
'id': 1,
'relationships': {},
'type': 'users'
},
'included': [],
'jsonapi': {
'version': '1.0'
},
'meta': {
'sqlalchemy_jsonapi_version': '4.0.9'
}
}

actual = response.data
self.assertEqual(expected_response, actual)
self.assertEqual(201, response.status_code)
Empty file.
24 changes: 24 additions & 0 deletions sqlalchemy_jsonapi/unittests/utils/testcases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Testcases for sqlalchemy_jsonapi unittests."""

import unittest

from sqlalchemy_jsonapi.unittests.models import Base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine


class SqlalchemyJsonapiTestCase(unittest.TestCase):
"""Base test case for all tests."""

def setUp(self, *args, **kwargs):
"""Configure sqlalchemy and session."""
super(SqlalchemyJsonapiTestCase, self).setUp(*args, **kwargs)
self.engine = create_engine('sqlite://')
Session = sessionmaker(bind=self.engine)
self.session = Session()
Base.metadata.create_all(self.engine)

def tearDown(self, *args, **kwargs):
"""Reset the sqlalchemy engine."""
super(SqlalchemyJsonapiTestCase, self).tearDown(*args, **kwargs)
Base.metadata.drop_all(self.engine)

0 comments on commit f9c223a

Please sign in to comment.