# Path: farmsList/user/models.py
# class User(UserMixin, SurrogatePK, Model):
#     __tablename__ = 'users'
#     username = Column(db.String(80), unique=True, nullable=False)
#     email = Column(db.String(80), unique=True, nullable=False)
#     #: The hashed password
#     password = Column(db.String(128), nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, username, email, password=None, **kwargs):
#         db.Model.__init__(self, username=username, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({username!r})>'.format(username=self.username)
# 
# class Role(SurrogatePK, Model):
#     __tablename__ = 'roles'
#     name = Column(db.String(80), unique=True, nullable=False)
#     user_id = ReferenceCol('users', nullable=True)
#     user = relationship('User', backref='roles')
# 
#     def __init__(self, name, **kwargs):
#         db.Model.__init__(self, name=name, **kwargs)
# 
#     def __repr__(self):
#         return '<Role({name})>'.format(name=self.name)
# 
# Path: tests/factories.py
# class UserFactory(BaseFactory):
#     username = Sequence(lambda n: "user{0}".format(n))
#     email = Sequence(lambda n: "user{0}@example.com".format(n))
#     password = PostGenerationMethodCall('set_password', 'example')
#     active = True
# 
#     class Meta:
#         model = User


import datetime as dt
import pytest
from farmsList.user.models import User, Role
from .factories import UserFactory

    def test_get_by_id(self):
        user = User('foo', 'foo@bar.com')
        user.save()

        retrieved = User.get_by_id(user.id)
        assert retrieved == user

    def test_created_at_defaults_to_datetime(self):
        user = User(username='foo', email='foo@bar.com')
        user.save()
        assert bool(user.created_at)
        assert isinstance(user.created_at, dt.datetime)

    def test_password_is_nullable(self):
        user = User(username='foo', email='foo@bar.com')
        user.save()
        assert user.password is None

    def test_factory(self, db):
        user = UserFactory(password="myprecious")
        db.session.commit()
        assert bool(user.username)
        assert bool(user.email)
        assert bool(user.created_at)
        assert user.is_admin is False
        assert user.active is True
        assert user.check_password('myprecious')

    def test_check_password(self):

# Path: farmsList/user/models.py
# class User(UserMixin, SurrogatePK, Model):
#     __tablename__ = 'users'
#     username = Column(db.String(80), unique=True, nullable=False)
#     email = Column(db.String(80), unique=True, nullable=False)
#     #: The hashed password
#     password = Column(db.String(128), nullable=True)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, username, email, password=None, **kwargs):
#         db.Model.__init__(self, username=username, email=email, **kwargs)
#         if password:
#             self.set_password(password)
#         else:
#             self.password = None
# 
#     def set_password(self, password):
#         self.password = bcrypt.generate_password_hash(password)
# 
#     def check_password(self, value):
#         return bcrypt.check_password_hash(self.password, value)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({username!r})>'.format(username=self.username)
# 
# class Role(SurrogatePK, Model):
#     __tablename__ = 'roles'
#     name = Column(db.String(80), unique=True, nullable=False)
#     user_id = ReferenceCol('users', nullable=True)
#     user = relationship('User', backref='roles')
# 
#     def __init__(self, name, **kwargs):
#         db.Model.__init__(self, name=name, **kwargs)
# 
#     def __repr__(self):
#         return '<Role({name})>'.format(name=self.name)
# 
# Path: tests/factories.py
# class UserFactory(BaseFactory):
#     username = Sequence(lambda n: "user{0}".format(n))
#     email = Sequence(lambda n: "user{0}@example.com".format(n))
#     password = PostGenerationMethodCall('set_password', 'example')
#     active = True
# 
#     class Meta:
#         model = User


# Path: tests/test_models.py
import datetime as dt
import pytest
from farmsList.user.models import User, Role
from .factories import UserFactory


    def test_get_by_id(self):
        user = User('foo', 'foo@bar.com')
        user.save()

        retrieved = User.get_by_id(user.id)
        assert retrieved == user

    def test_created_at_defaults_to_datetime(self):
        user = User(username='foo', email='foo@bar.com')
        user.save()
        assert bool(user.created_at)
        assert isinstance(user.created_at, dt.datetime)

    def test_password_is_nullable(self):
        user = User(username='foo', email='foo@bar.com')
        user.save()
        assert user.password is None

    def test_factory(self, db):
        user = UserFactory(password="myprecious")
        db.session.commit()
        assert bool(user.username)
        assert bool(user.email)
        assert bool(user.created_at)
        assert user.is_admin is False
        assert user.active is True
        assert user.check_password('myprecious')

    def test_check_password(self):

        user = User.create(username="foo", email="foo@bar.com",