# 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/action/blog.py
# class BlogAction:
#     @classmethod
#     def create(self, db, owner_id, title):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO blog (
#                         id, owner_id, title, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, owner_id, title, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_blog_created(cls, db):
#         global_user = UserAction.ensure_global_user_created(db)
#         blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         if blog is None:
#             cls.create(db, global_user.id, "%s's blog" % (global_user.name,))
#             blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         return blog
# 
# Path: tests/util.py
# def web_client():
#     return web.test_client()
# 
# @contextmanager
# def global_user(name):
#     prev_name = CONFIG.GLOBAL_USER_NAME
#     CONFIG.GLOBAL_USER_NAME = name
#     yield name
#     CONFIG.GLOBAL_USER_NAME = prev_name
# 
# 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_entry(blog=None):
#     db = DB()
# 
#     if blog is None:
#         blog = create_blog()
# 
#     new_entry_id = EntryAction.post(
#         db,
#         blog_id=blog.id,
#         title=random_string(30),
#         body=random_string(100),
#     )
#     return EntryLoader.find_by_id(db, new_entry_id)


import tests.hakoblog  # noqa: F401
from pyquery import PyQuery as pq
from hakoblog.db import DB
from hakoblog.action.blog import BlogAction
from tests.util import web_client, global_user, random_string, create_entry





def test_entry():
    db = DB()
    with global_user(random_string(5)):
        blog = BlogAction.ensure_global_blog_created(db)
        entry = create_entry(blog=blog)


# 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/action/blog.py
# class BlogAction:
#     @classmethod
#     def create(self, db, owner_id, title):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO blog (
#                         id, owner_id, title, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, owner_id, title, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_blog_created(cls, db):
#         global_user = UserAction.ensure_global_user_created(db)
#         blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         if blog is None:
#             cls.create(db, global_user.id, "%s's blog" % (global_user.name,))
#             blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         return blog
# 
# Path: tests/util.py
# def web_client():
#     return web.test_client()
# 
# @contextmanager
# def global_user(name):
#     prev_name = CONFIG.GLOBAL_USER_NAME
#     CONFIG.GLOBAL_USER_NAME = name
#     yield name
#     CONFIG.GLOBAL_USER_NAME = prev_name
# 
# 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_entry(blog=None):
#     db = DB()
# 
#     if blog is None:
#         blog = create_blog()
# 
#     new_entry_id = EntryAction.post(
#         db,
#         blog_id=blog.id,
#         title=random_string(30),
#         body=random_string(100),
#     )
#     return EntryLoader.find_by_id(db, new_entry_id)


# Path: tests/web/test_entry.py
import tests.hakoblog  # noqa: F401
from pyquery import PyQuery as pq
from hakoblog.db import DB
from hakoblog.action.blog import BlogAction
from tests.util import web_client, global_user, random_string, create_entry






def test_entry():
    db = DB()
    with global_user(random_string(5)):
        blog = BlogAction.ensure_global_blog_created(db)
        entry = create_entry(blog=blog)


        res1 = web_client().get('/entry/' + str(entry.id))