# Path: subrosa/models/UsersModel.py
# class Users(BaseModel):
# 
#     """
#     Models and methods related to user database interaction
#     """
# 
#     username = CharField(max_length=40, unique=True, index=True)
#     hash = CharField()
#     real_name = CharField(max_length=40, null=True)
#     description = TextField(null=True)
#     about = TextField(null=True)
# 
#     @staticmethod
#     def check_any_exist():
#         """ Check if any users exist """
#         return Users.get_count() > 0
# 
#     @staticmethod
#     def check_exists(username):
# 
#         """ Check if user with given username already exists """
# 
#         return Users.select()\
#                .where(Users.username == username)\
#                .exists()
# 
#     @staticmethod
#     def get_user(id):
#         """ Get user by id """
#         return Users.get_single("id", id)
# 
#     @staticmethod
#     def get_user_by_username(username):
# 
#         """ Get user by his username , returns 0 if not exists """
# 
#         return Users.get_single("username", username)
# 
#     @staticmethod
#     @db.commit_on_success
#     def create_user(username, password, description=None, real_name=None):
# 
#         """ Create new user """
# 
#         try:
#             return Users.create(username=username,
#                                 hash=generate_password_hash(password),
#                                 description=description,
#                                 real_name=real_name).get_id()
# 
#         except:
#             handle_errors("Error creating user")
#             raise
# 
#     def check_password(self, password):
# 
#         """ Compare password against the one in db """
# 
#         return check_password_hash(self.hash, password)
# 
#     def __repr__(self):
#         return "<User: %s>" % self.username
# 
#     class Meta:
#         order_by = ("username",)


import unittest
from subrosa.models.UsersModel import Users
from playhouse.test_utils import test_database
from peewee import *
db = SqliteDatabase(":memory:")

db._flag = "db1"


class TestUsersMethods(unittest.TestCase):

    
    def test_sanity(self):

        with test_database(db, (Users,)):
            self.assertTrue(Users.table_exists())
            self.assertFalse(Users.check_any_exist())


    def test_creating_user(self):

        with test_database(db, (Users,)):
            Users.create_user(username = "konrad", password = "secret")

            self.assertTrue(Users.get_user(1))
            self.assertTrue(Users.check_exists("konrad"))
            self.assertEquals(1, Users.select().count())


            Users.create_user(username = "malgosia",\
                              password = "secret",\
                              real_name = "Malgosia Samosia", \
                              description = "test")


# Path: subrosa/models/UsersModel.py
# class Users(BaseModel):
# 
#     """
#     Models and methods related to user database interaction
#     """
# 
#     username = CharField(max_length=40, unique=True, index=True)
#     hash = CharField()
#     real_name = CharField(max_length=40, null=True)
#     description = TextField(null=True)
#     about = TextField(null=True)
# 
#     @staticmethod
#     def check_any_exist():
#         """ Check if any users exist """
#         return Users.get_count() > 0
# 
#     @staticmethod
#     def check_exists(username):
# 
#         """ Check if user with given username already exists """
# 
#         return Users.select()\
#                .where(Users.username == username)\
#                .exists()
# 
#     @staticmethod
#     def get_user(id):
#         """ Get user by id """
#         return Users.get_single("id", id)
# 
#     @staticmethod
#     def get_user_by_username(username):
# 
#         """ Get user by his username , returns 0 if not exists """
# 
#         return Users.get_single("username", username)
# 
#     @staticmethod
#     @db.commit_on_success
#     def create_user(username, password, description=None, real_name=None):
# 
#         """ Create new user """
# 
#         try:
#             return Users.create(username=username,
#                                 hash=generate_password_hash(password),
#                                 description=description,
#                                 real_name=real_name).get_id()
# 
#         except:
#             handle_errors("Error creating user")
#             raise
# 
#     def check_password(self, password):
# 
#         """ Compare password against the one in db """
# 
#         return check_password_hash(self.hash, password)
# 
#     def __repr__(self):
#         return "<User: %s>" % self.username
# 
#     class Meta:
#         order_by = ("username",)


# Path: subrosa/tests/test_users_db.py
import unittest
from subrosa.models.UsersModel import Users
from playhouse.test_utils import test_database
from peewee import *

db = SqliteDatabase(":memory:")

db._flag = "db1"


class TestUsersMethods(unittest.TestCase):

    
    def test_sanity(self):

        with test_database(db, (Users,)):
            self.assertTrue(Users.table_exists())
            self.assertFalse(Users.check_any_exist())


    def test_creating_user(self):

        with test_database(db, (Users,)):
            Users.create_user(username = "konrad", password = "secret")

            self.assertTrue(Users.get_user(1))
            self.assertTrue(Users.check_exists("konrad"))
            self.assertEquals(1, Users.select().count())


            Users.create_user(username = "malgosia",\
                              password = "secret",\
                              real_name = "Malgosia Samosia", \
                              description = "test")


            self.assertEquals(2, Users.select().count())