# Path: tests/base_testcase.py
# class BoilerTestCase(FlaskTestCase):
#     """
#     Boiler test case
#     Every boiler test should extend from this base class as it sets up
#     boiler-specific test app
#     """
#     def setUp(self):
#         super().setUp(app)
# 
# Path: boiler/collections/api_collection.py
# class ApiCollection(PaginatedCollection):
#     """
#     API Collection
#     Works the same way as a paginated collection, but also applies
#     serializer to each item. Useful in API responses.
#     """
#     def __init__(self, query, *_, serialize_function, **kwargs):
#         self.serializer = serialize_function
#         super().__init__(query, **kwargs)
# 
#     def __iter__(self):
#         """ Performs generator-based iteration through page items """
#         offset = 0
#         while offset < len(self.items):
#             item = self.items[offset]
#             offset += 1
#             yield self.serializer(item)
# 
#     def dict(self):
#         """ Returns current collection as a dictionary """
#         collection = super().dict()
#         serialized_items = []
#         for item in collection['items']:
#             serialized_items.append(self.serializer(item))
# 
#         collection['items'] = serialized_items
#         return collection
# 
#     def json(self):
#         """ Returns a json representation of collection """
#         return dumps(self.dict())
# 
# Path: boiler/feature/orm.py
# def orm_feature(app):
# 
# Path: tests/boiler_test_app/models.py
# class User(db.Model):
#     """
#     User model
#     Represents a very basic user entity with the functionality to register,
#     via email and password or an OAuth provider, login, recover password and
#     be authorised and authenticated.
# 
#     Please not this object must only be instantiated from flask app context
#     as it will try to pull config settings from current_app.config.
#     """
# 
#     id = db.Column(db.Integer, primary_key=True, nullable=False)
#     _email = db.Column('email', db.String(128), nullable=False, unique=True)
#     _password = db.Column('password', db.String(256))
# 
#     # -------------------------------------------------------------------------
#     # Public API
#     # -------------------------------------------------------------------------
# 
#     def __init__(self, *args, **kwargs):
#         """ Instantiate with optional keyword data to set """
#         if 'id' in kwargs:
#             del kwargs['id']
#         super().__init__(*args, **kwargs)
# 
#     def __repr__(self):
#         """ Printable representation of user """
#         u = '<User id="{}" email="{}">'
#         return u.format(self.id, self.email_secure)
# 
#     def generate_hash(self, length=30):
#         """ Generate random string of given length """
#         import random, string
#         chars = string.ascii_letters + string.digits
#         ran = random.SystemRandom().choice
#         hash = ''.join(ran(chars) for i in range(length))
#         return hash
# 
#     # -------------------------------------------------------------------------
#     # Email
#     # -------------------------------------------------------------------------
# 
#     @hybrid_property
#     def email(self):
#         """ Hybrid getter """
#         return self._email
# 
#     @property
#     def email_secure(self):
#         """ Obfuscated email used for display """
#         email = self._email
#         if not email: return ''
#         address, host = email.split('@')
#         if len(address) <= 2: return ('*' * len(address)) + '@' + host
# 
#         import re
#         host = '@' + host
#         obfuscated = re.sub(r'[a-zA-z0-9]', '*', address[1:-1])
#         return address[:1] + obfuscated + address[-1:] + host
# 
#     @email.setter
#     def email(self, email):
#         """ Set email and generate confirmation """
#         if email == self.email:
#             return
# 
#         email = email.lower()
#         if self._email is None:
#             self._email = email
#         else:
#             self.email_new = email
#             self.require_email_confirmation()
# 
#     # -------------------------------------------------------------------------
#     # Password
#     # -------------------------------------------------------------------------
# 
#     @hybrid_property
#     def password(self):
#         """ Hybrid password getter """
#         return self._password
# 
#     @password.setter
#     def password(self, password):
#         """ Encode a string and set as password """
#         self._password = password


from unittest import mock
from nose.plugins.attrib import attr
from tests.base_testcase import BoilerTestCase
from faker import Factory
from boiler.collections import ApiCollection
from boiler.feature.orm import db
from tests.boiler_test_app.models import User
        """ Getting collection as dictionary """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        collection = collection.dict()
        self.assertIsInstance(collection, dict)
        self.assertIsInstance(collection['items'], list)

        # assert each item is serialized
        for item in collection['items']:
            self.assertIsInstance(item, str)
            self.assertTrue(item.startswith('<User id='))

    def test_can_get_collection_as_json(self):
        """ Getting API collection as json """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        json = collection.json()
        self.assertIsInstance(json, str)

    def test_can_get_printable_representation(self):
        """ Getting printable representation of a collection """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        printable = collection.__repr__()
        self.assertTrue(printable.startswith('<ApiCollection'))

    def test_can_iterate_through_page_items(self):
        """ Iterating through collection items """
        self.create_fake_data(2)
        collection = ApiCollection(

# Path: tests/base_testcase.py
# class BoilerTestCase(FlaskTestCase):
#     """
#     Boiler test case
#     Every boiler test should extend from this base class as it sets up
#     boiler-specific test app
#     """
#     def setUp(self):
#         super().setUp(app)
# 
# Path: boiler/collections/api_collection.py
# class ApiCollection(PaginatedCollection):
#     """
#     API Collection
#     Works the same way as a paginated collection, but also applies
#     serializer to each item. Useful in API responses.
#     """
#     def __init__(self, query, *_, serialize_function, **kwargs):
#         self.serializer = serialize_function
#         super().__init__(query, **kwargs)
# 
#     def __iter__(self):
#         """ Performs generator-based iteration through page items """
#         offset = 0
#         while offset < len(self.items):
#             item = self.items[offset]
#             offset += 1
#             yield self.serializer(item)
# 
#     def dict(self):
#         """ Returns current collection as a dictionary """
#         collection = super().dict()
#         serialized_items = []
#         for item in collection['items']:
#             serialized_items.append(self.serializer(item))
# 
#         collection['items'] = serialized_items
#         return collection
# 
#     def json(self):
#         """ Returns a json representation of collection """
#         return dumps(self.dict())
# 
# Path: boiler/feature/orm.py
# def orm_feature(app):
# 
# Path: tests/boiler_test_app/models.py
# class User(db.Model):
#     """
#     User model
#     Represents a very basic user entity with the functionality to register,
#     via email and password or an OAuth provider, login, recover password and
#     be authorised and authenticated.
# 
#     Please not this object must only be instantiated from flask app context
#     as it will try to pull config settings from current_app.config.
#     """
# 
#     id = db.Column(db.Integer, primary_key=True, nullable=False)
#     _email = db.Column('email', db.String(128), nullable=False, unique=True)
#     _password = db.Column('password', db.String(256))
# 
#     # -------------------------------------------------------------------------
#     # Public API
#     # -------------------------------------------------------------------------
# 
#     def __init__(self, *args, **kwargs):
#         """ Instantiate with optional keyword data to set """
#         if 'id' in kwargs:
#             del kwargs['id']
#         super().__init__(*args, **kwargs)
# 
#     def __repr__(self):
#         """ Printable representation of user """
#         u = '<User id="{}" email="{}">'
#         return u.format(self.id, self.email_secure)
# 
#     def generate_hash(self, length=30):
#         """ Generate random string of given length """
#         import random, string
#         chars = string.ascii_letters + string.digits
#         ran = random.SystemRandom().choice
#         hash = ''.join(ran(chars) for i in range(length))
#         return hash
# 
#     # -------------------------------------------------------------------------
#     # Email
#     # -------------------------------------------------------------------------
# 
#     @hybrid_property
#     def email(self):
#         """ Hybrid getter """
#         return self._email
# 
#     @property
#     def email_secure(self):
#         """ Obfuscated email used for display """
#         email = self._email
#         if not email: return ''
#         address, host = email.split('@')
#         if len(address) <= 2: return ('*' * len(address)) + '@' + host
# 
#         import re
#         host = '@' + host
#         obfuscated = re.sub(r'[a-zA-z0-9]', '*', address[1:-1])
#         return address[:1] + obfuscated + address[-1:] + host
# 
#     @email.setter
#     def email(self, email):
#         """ Set email and generate confirmation """
#         if email == self.email:
#             return
# 
#         email = email.lower()
#         if self._email is None:
#             self._email = email
#         else:
#             self.email_new = email
#             self.require_email_confirmation()
# 
#     # -------------------------------------------------------------------------
#     # Password
#     # -------------------------------------------------------------------------
# 
#     @hybrid_property
#     def password(self):
#         """ Hybrid password getter """
#         return self._password
# 
#     @password.setter
#     def password(self, password):
#         """ Encode a string and set as password """
#         self._password = password


# Path: tests/collections_tests/api_collection_test.py
from unittest import mock
from nose.plugins.attrib import attr
from tests.base_testcase import BoilerTestCase
from faker import Factory
from boiler.collections import ApiCollection
from boiler.feature.orm import db
from tests.boiler_test_app.models import User

        """ Getting collection as dictionary """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        collection = collection.dict()
        self.assertIsInstance(collection, dict)
        self.assertIsInstance(collection['items'], list)

        # assert each item is serialized
        for item in collection['items']:
            self.assertIsInstance(item, str)
            self.assertTrue(item.startswith('<User id='))

    def test_can_get_collection_as_json(self):
        """ Getting API collection as json """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        json = collection.json()
        self.assertIsInstance(json, str)

    def test_can_get_printable_representation(self):
        """ Getting printable representation of a collection """
        serializer = self.serializer
        collection = ApiCollection(User.query, serialize_function=serializer)
        printable = collection.__repr__()
        self.assertTrue(printable.startswith('<ApiCollection'))

    def test_can_iterate_through_page_items(self):
        """ Iterating through collection items """
        self.create_fake_data(2)
        collection = ApiCollection(

            User.query,