# Path: hakoblog/db.py
# class DB:
#     def __init__(self):
#         self.conn = MySQLdb.connect(
#             db=CONFIG.DATABASE,
#             host=CONFIG.DATABASE_HOST,
#             user=CONFIG.DATABASE_USER,
#             password=CONFIG.DATABASE_PASS,
#             cursorclass=MySQLdb.cursors.DictCursor,
#             charset="utf8",
#         )
#         self.conn.autocommit(True)
# 
#     def cursor(self):
#         return self.conn.cursor()
# 
#     def close(self):
#         self.conn.close()
# 
#     def uuid_short(self):
#         with self.conn.cursor() as cursor:
#             cursor.execute("SELECT UUID_SHORT() as uuid")
#             return cursor.fetchone().get("uuid")
# 
# Path: hakoblog/model/user.py
# class User:
#     def __init__(self, id, name):
#         self.id = id
#         self.name = name
# 
# Path: hakoblog/loader/user.py
# class UserLoader:
#     @classmethod
#     def find_by_name(cls, db, name):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, name
#                 FROM user
#                 WHERE name = %s
#                 LIMIT 1
#                 """,
#                 (name,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return User(**row)
# 
# Path: tests/util.py
# def random_string(length, seq=string.digits + string.ascii_letters):
#     sr = random.SystemRandom()
#     return ''.join([sr.choice(seq) for i in range(length)])
# 
# def create_user():
#     db = DB()
#     name = random_string(10)
#     UserAction.create(db, name)
#     return UserLoader.find_by_name(db, name)


import tests.hakoblog  # noqa: F401
from hakoblog.db import DB
from hakoblog.model.user import User
from hakoblog.loader.user import UserLoader
from tests.util import random_string, create_user




def test_find_by_name():
    db = DB()

    user = create_user()

    found_user = UserLoader.find_by_name(db, user.name)
    assert isinstance(found_user, User)
    assert found_user.name == user.name


# Path: hakoblog/db.py
# class DB:
#     def __init__(self):
#         self.conn = MySQLdb.connect(
#             db=CONFIG.DATABASE,
#             host=CONFIG.DATABASE_HOST,
#             user=CONFIG.DATABASE_USER,
#             password=CONFIG.DATABASE_PASS,
#             cursorclass=MySQLdb.cursors.DictCursor,
#             charset="utf8",
#         )
#         self.conn.autocommit(True)
# 
#     def cursor(self):
#         return self.conn.cursor()
# 
#     def close(self):
#         self.conn.close()
# 
#     def uuid_short(self):
#         with self.conn.cursor() as cursor:
#             cursor.execute("SELECT UUID_SHORT() as uuid")
#             return cursor.fetchone().get("uuid")
# 
# Path: hakoblog/model/user.py
# class User:
#     def __init__(self, id, name):
#         self.id = id
#         self.name = name
# 
# Path: hakoblog/loader/user.py
# class UserLoader:
#     @classmethod
#     def find_by_name(cls, db, name):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, name
#                 FROM user
#                 WHERE name = %s
#                 LIMIT 1
#                 """,
#                 (name,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return User(**row)
# 
# Path: tests/util.py
# def random_string(length, seq=string.digits + string.ascii_letters):
#     sr = random.SystemRandom()
#     return ''.join([sr.choice(seq) for i in range(length)])
# 
# def create_user():
#     db = DB()
#     name = random_string(10)
#     UserAction.create(db, name)
#     return UserLoader.find_by_name(db, name)


# Path: tests/loader/test_user.py
import tests.hakoblog  # noqa: F401
from hakoblog.db import DB
from hakoblog.model.user import User
from hakoblog.loader.user import UserLoader
from tests.util import random_string, create_user





def test_find_by_name():
    db = DB()

    user = create_user()

    found_user = UserLoader.find_by_name(db, user.name)
    assert isinstance(found_user, User)
    assert found_user.name == user.name


    not_found_user = UserLoader.find_by_name(db, random_string(10))