# -*- coding: utf-8 -*-

import os
import unittest
import datetime
import webtest
from tornado.wsgi import WSGIAdapter
from playhouse.sqlite_ext import SqliteExtDatabase
from playhouse.test_utils import test_database

from catstalk.cat import Cat
from catstalk.static import POST_TEMPLATE
from catstalk.server import get_app
from catstalk.models import Tag, Post

test_db = SqliteExtDatabase(":memory:")


class ServerTestCase(unittest.TestCase):
    def tearDown(self):
        try:
            os.remove("blog_data.sqlite3")
        except OSError:
            pass

    def test_tornado_server(self):
        with test_database(test_db, (Tag, Post)):
            # Add some post
            date = datetime.datetime.now()
            str_date = date.strftime("%Y-%m-%d %H:%M:%S")
            content = u"%s%s" % (
                POST_TEMPLATE.replace("{{date}}", str_date).replace("{{title}}", "Test"), "Hello World!"
            )
            temp = content.split("---")
            content = "---\n%s\ntag: test\n---%s" % (temp[1], temp[2])
            Cat.compile_post(content)

            app = webtest.TestApp(WSGIAdapter(get_app()))
            # All Tag
            response = app.get("/api/tags")
            self.assertEqual(response.json_body[0]["name"], "test")
            # Tag Detail
            response = app.get("/api/tags/none", expect_errors=True)
            self.assertEqual(response.status_code, 404)
            response = app.get("/api/tags/test")
            self.assertEqual(response.status_code, 200)
            response = response.json_body
            self.assertEqual(response["max_page"], 1)
            self.assertEqual(response["posts"][0]["tag"], "test")
            self.assertEqual(response["posts"][0]["date"], str_date)
            self.assertEqual(response["posts"][0]["title"], "Test")
            self.assertEqual(response["posts"][0]["content"], "<p>Hello World!</p>\n")

@@ -574,117 +574,25 @@
 def 
-test_tornado_server(self):%0A        with test_database(test_db, (Tag, Post)):%0A            # Add some post%0A    
+add_posts(self):%0A
     
@@ -626,20 +626,16 @@
 e.now()%0A
-    
         
@@ -680,36 +680,32 @@
 %25M:%25S%22)%0A        
-  
-  
 content = u%22%25s%25s
@@ -710,20 +710,16 @@
 %25s%22 %25 (%0A
-    
         
@@ -823,18 +823,10 @@
     
- 
-   )%0A    
+)%0A
     
@@ -857,20 +857,16 @@
 (%22---%22)%0A
-    
         
@@ -924,20 +924,16 @@
 emp%5B2%5D)%0A
-    
         
@@ -962,66 +962,210 @@
 nt)%0A
-%0A            app = webtest.TestApp(WSGIAdapter(get_app()
+        return str_date%0A%0A    def add_tags(self):%0A        for i in range(0, 10):%0A            Tag.create(name=%22tag%25d%22 %25 i)%0A%0A    def test_tag_detail(self):%0A        with test_database(test_db, (Tag, Post
 ))
+:
 %0A   
@@ -1180,14 +1180,20 @@
  # A
-ll Tag
+dd some post
 %0A   
@@ -1205,106 +1205,92 @@
     
-response = app.get(%22/api/tags%22)%0A            self.assertEqual(response.json_body%5B0%5D%5B%22name%22%5D, %22test%22
+str_date = self.add_posts()%0A            app = webtest.TestApp(WSGIAdapter(get_app())
 )%0A  
@@ -1928,8 +1928,478 @@
 /p%3E%5Cn%22)%0A
+%0A    def test_all_tag(self):%0A        with test_database(test_db, (Tag, Post)):%0A            # Add some tags%0A            self.add_tags()%0A            app = webtest.TestApp(WSGIAdapter(get_app()))%0A            # All Tag%0A            response = app.get(%22/api/tags%22)%0A            self.assertEqual(response.status_code, 200)%0A            response = response.json_body%0A            for i in range(0, len(response)):%0A                self.assertEqual(response%5Bi%5D%5B%22name%22%5D, %22tag%25d%22 %25 i)%0A

change test for server