# 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/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: hakoblog/action/user.py
# class UserAction:
#     @classmethod
#     def create(self, db, name):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO user (
#                         id, name, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, name, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_user_created(cls, db):
#         global_user_name = CONFIG.GLOBAL_USER_NAME
#         user = UserLoader.find_by_name(db, global_user_name)
#         if user is None:
#             cls.create(db, global_user_name)
#             user = UserLoader.find_by_name(db, global_user_name)
#         return user
# 
# 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)])
# 
# @contextmanager
# def global_user(name):
#     prev_name = CONFIG.GLOBAL_USER_NAME
#     CONFIG.GLOBAL_USER_NAME = name
#     yield name
#     CONFIG.GLOBAL_USER_NAME = prev_name


import tests.hakoblog  # noqa: F401
from hakoblog.db import DB
from hakoblog.loader.user import UserLoader
from hakoblog.action.user import UserAction
from tests.util import random_string, global_user




def test_create():
    db = DB()

    name = random_string(10)

# 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/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: hakoblog/action/user.py
# class UserAction:
#     @classmethod
#     def create(self, db, name):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO user (
#                         id, name, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, name, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_user_created(cls, db):
#         global_user_name = CONFIG.GLOBAL_USER_NAME
#         user = UserLoader.find_by_name(db, global_user_name)
#         if user is None:
#             cls.create(db, global_user_name)
#             user = UserLoader.find_by_name(db, global_user_name)
#         return user
# 
# 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)])
# 
# @contextmanager
# def global_user(name):
#     prev_name = CONFIG.GLOBAL_USER_NAME
#     CONFIG.GLOBAL_USER_NAME = name
#     yield name
#     CONFIG.GLOBAL_USER_NAME = prev_name


# Path: tests/action/test_user.py
import tests.hakoblog  # noqa: F401
from hakoblog.db import DB
from hakoblog.loader.user import UserLoader
from hakoblog.action.user import UserAction
from tests.util import random_string, global_user





def test_create():
    db = DB()

    name = random_string(10)

    UserAction.create(db, name)