diff --git a/sqlalchemy_jsonapi/unittests/__init__.py b/sqlalchemy_jsonapi/unittests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sqlalchemy_jsonapi/unittests/models.py b/sqlalchemy_jsonapi/unittests/models.py new file mode 100644 index 0000000..81fa49c --- /dev/null +++ b/sqlalchemy_jsonapi/unittests/models.py @@ -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) diff --git a/sqlalchemy_jsonapi/unittests/test_serializer_post_collection.py b/sqlalchemy_jsonapi/unittests/test_serializer_post_collection.py new file mode 100644 index 0000000..234a128 --- /dev/null +++ b/sqlalchemy_jsonapi/unittests/test_serializer_post_collection.py @@ -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) diff --git a/sqlalchemy_jsonapi/unittests/utils/__init__.py b/sqlalchemy_jsonapi/unittests/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sqlalchemy_jsonapi/unittests/utils/testcases.py b/sqlalchemy_jsonapi/unittests/utils/testcases.py new file mode 100644 index 0000000..5864052 --- /dev/null +++ b/sqlalchemy_jsonapi/unittests/utils/testcases.py @@ -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)