import datetime
import fetch_tumblr

from noveltorpedo import models
from django.test import TestCase

class MockPost(object):
    def __init__(self, title=""):
        super(MockPost, self).__init__()
        self.title = title
        self.body = ""
        self.timestamp = (datetime.datetime.now() -
                          datetime.datetime.fromtimestamp(0)).total_seconds()

    def set_body(self, body=""):
        self.body = body
        return self

class MockBlog(object):
    def __init__(self, blog_name, blog_title, is_nsfw):
        super(MockBlog, self).__init__()
        self.name = blog_name
        self.title = blog_title
        self.is_nsfw = is_nsfw
        self.posts = []

    def add_post(self, post):
        self.posts.append(post)

class MockTumblrClient(object):
    def __init__(self):
        super(MockTumblrClient, self).__init__()
        self.blogs = {}

    def add_blog(self, blog):
        self.blogs[blog.name] = blog

    def blog_info(self, blog_name):
        """
        Needs to return a dictionary with the key "blog"
        which itself is a dictionary with keys "is_nsfw"
        (bool) and "title" (string).
        """
        # If this fails it will raise a KeyError, as the real pytumblr does.
        blog = self.blogs[blog_name]
        return {"blog":blog.__dict__}

    def posts(self, blog_name, **kwargs):
        """
        Expects type=text, filter=text, offset integer,
        and limit an integer up to 20. Returns a dictionary
        with the key "posts" containing a list of
        dictionaries with keys "timestamp" "title" "body"
        """
        # If this fails it will raise a KeyError, as the real pytumblr does.
        blog = self.blogs[blog_name]
        return {"posts":map(dict, blog.posts)}

class TumblrTests(TestCase):
    def setUp(self):
        fetch_tumblr.host_attrs["wait"] = 0
        fetch_tumblr.tumblr = models.Host.objects.get_or_create(**fetch_tumblr.host_attrs)[0]
        fetch_tumblr.client = MockTumblrClient()
        mock_blog = MockBlog("mock_blog", "My Mockup Blog", False)
        post = MockPost("Title of the Post").set_body("Contents of the post.")
        mock_blog.add_post(post)
        fetch_tumblr.client.add_blog(mock_blog)

    def test_create_storyhost(self):
        created_sh = fetch_tumblr.get_or_create_storyhost("mock_blog")
        retrieved_sh = fetch_tumblr.get_or_create_storyhost("mock_blog")
        self.assertEqual(created_sh, retrieved_sh)

@@ -1,12 +1,121 @@
+%22%22%22%0ATests for the Tumblr fetcher. Run with noveltorpedo/website/manage.py test%0Awhile in this directory.%0A%22%22%22%0A%0A
 import datet
@@ -215,20 +215,20 @@
 ass Mock
-Post
+Blog
 (object)
@@ -256,16 +256,38 @@
 lf, 
-title=%22%22
+blog_name, blog_title, is_nsfw
 ):%0A 
@@ -303,20 +303,20 @@
 per(Mock
-Post
+Blog
 , self).
@@ -343,523 +343,364 @@
 elf.
-title = title%0A        self.body = %22%22%0A        self.timestamp = (datetime.datetime.now() -%0A                          datetime.datetime.fromtimestamp(0)).total_seconds()%0A%0A    def set_body(self, body=%22%22):%0A        self.body = body%0A        return self%0A%0Aclass MockBlog(object):%0A    def __init__(self, blog_name, blog_title, is_nsfw):%0A        super(MockBlog, self).__init__()%0A        self.name = blog_name%0A        self.title = blog_title%0A        self.is_nsfw = is_nsfw%0A        self.posts = %5B%5D%0A%0A    def add_post(self, post):
+name = blog_name%0A        self.title = blog_title%0A        self.is_nsfw = is_nsfw%0A        self.posts = %5B%5D%0A%0A    def add_post(self, title=%22%22, body=%22%22):%0A        post = %7B%7D%0A        post%5B%22title%22%5D = title%0A        post%5B%22body%22%5D = body%0A        post%5B%22timestamp%22%5D = (datetime.datetime.now() -%0A                          datetime.datetime.fromtimestamp(0)).total_seconds()
 %0A   
@@ -2059,20 +2059,23 @@
     
-post = MockP
+mock_blog.add_p
 ost(
@@ -2097,19 +2097,10 @@
 ost%22
-).set_body(
+, 
 %22Con
@@ -2124,41 +2124,8 @@
 .%22)%0A
-        mock_blog.add_post(post)%0A
     

Change mock post to a plain dict.