# Path: tests/test_models.py
# class MongoTestMixin(object):
# 
#     def setUp(self):
#         """
#         Create the mongomock connection
#         """
#         self.conn = connect(host='mongomock://localhost')
#         assert isinstance(self.conn, MockMongoClient)
# 
#         # Clear out the database
#         for feed in Feed.objects(): feed.delete()
#         for post in Post.objects(): post.delete()
# 
#     def tearDown(self):
#         """
#         Drop the mongomock connection
#         """
#         assert isinstance(self.conn, MockMongoClient)
#         self.conn = None
# 
#     def assertDateTimeEqual(self, dta, dtb):
#         """
#         Assert that two datetimes are within 1 second of each other
#         """
#         dta = dta.replace(microsecond=0)
#         dtb = dta.replace(microsecond=0)
# 
#         if dta.second != dtb.second:
#             self.assertLessThanEqual(
#                 abs(dta.second - dtb.second), 1, "datetimes are not one second apart!"
#             )
#             dta = dta.replace(second=0)
#             dtb = dtb.replace(second=0)
# 
#         self.assertEqual(dta, dtb)
# 
# Path: baleen/models.py
# class Feed(me.DynamicDocument):
# 
#     version   = me.StringField(choices=FEEDTYPES)
#     etag      = me.StringField()
#     modified  = me.StringField()
#     title     = me.StringField(max_length=256)
#     link      = me.URLField(required=True, unique=True)
#     urls      = me.DictField()
#     category  = me.StringField(required=True)
#     active    = me.BooleanField(default=True)
#     fetched   = me.DateTimeField(default=None)
#     created   = me.DateTimeField(default=datetime.now, required=True)
#     updated   = me.DateTimeField(default=datetime.now, required=True)
# 
#     @classmethod
#     def pre_save(cls, sender, document, **kwargs):
#         document.updated = datetime.now()
# 
#     meta      = {
#         'collection': 'feeds',
#     }
# 
#     @property
#     def xmlurl(self):
#         return self.link
# 
#     @property
#     def htmlurl(self):
#         return self.urls.get('htmlUrl')
# 
#     def count_posts(self):
#         """
#         Count the number of associated posts
# 
#         TODO: This is very, very slow on Mongo (fix and make better).
#         """
#         return Post.objects(feed=self).count()
# 
#     def __unicode__(self):
#         return self.title if self.title else self.link
# 
# class Post(me.DynamicDocument):
# 
#     feed      = me.ReferenceField(Feed)
#     title     = me.StringField( max_length=512 )
#     url       = me.URLField( required=True, unique=True )
#     pubdate   = me.DateTimeField()
#     content   = me.StringField( required=True )
#     tags      = me.ListField(me.StringField(max_length=256))
#     signature = me.StringField( required=True, max_length=64, min_length=64, unique=True )
#     created   = me.DateTimeField(default=datetime.now, required=True)
#     updated   = me.DateTimeField(default=datetime.now, required=True)
# 
#     @classmethod
#     def pre_save(cls, sender, document, **kwargs):
#         document.updated   = datetime.now()
#         document.signature = document.hash()
# 
#     meta      = {
#         'collection': 'posts',
#     }
# 
#     def hash(self):
#         """
#         Returns the SHA256 hash of the content.
#         """
#         sha = hashlib.sha256()
#         sha.update(self.content.encode('UTF-8'))
#         return sha.hexdigest()
# 
#     def htmlize(self):
#         """
#         Returns an HTML string of the content of the Post.
#         In the future we may use bleach to do sanitization or other simple
#         sanity checks to ensure that things are going ok, which is why this
#         method stub exists. 
#         """
#         return self.content
# 
#     def __unicode__(self):
#         return self.title if self.title else self.url


import os
import pickle
import unittest
    import mock
from .test_models import MongoTestMixin
    from unittest import mock
from baleen.wrangle import *
from baleen.exceptions import *
from baleen.models import Feed, Post
            with self.assertRaises(WranglingError) as cm:
                wrangle.wrangle()
            self.assertEqual(Post.objects.count(), 10)

    def test_is_wrangled(self):
        """
        Test the wrangling detection
        """
        wrangle = PostWrangler(self.entries[0])
        self.assertFalse(wrangle.is_wrangled())
        wrangle.wrangle()
        self.assertTrue(wrangle.is_wrangled())

    def test_save_not_save(self):
        """
        Test the wrangle interaction with the database
        """
        self.assertEqual(Post.objects.count(), 0)
        wrangle = PostWrangler(self.entries[0])

        # Don't save the wrangle
        wrangle.wrangle(False)
        self.assertEqual(Post.objects.count(), 0)

        # We've already wrangled so nothing should happen!
        wrangle.wrangle()
        self.assertEqual(Post.objects.count(), 0)

        # Try making something happen directly
        wrangle.wrangle().save()

# Path: tests/test_models.py
# class MongoTestMixin(object):
# 
#     def setUp(self):
#         """
#         Create the mongomock connection
#         """
#         self.conn = connect(host='mongomock://localhost')
#         assert isinstance(self.conn, MockMongoClient)
# 
#         # Clear out the database
#         for feed in Feed.objects(): feed.delete()
#         for post in Post.objects(): post.delete()
# 
#     def tearDown(self):
#         """
#         Drop the mongomock connection
#         """
#         assert isinstance(self.conn, MockMongoClient)
#         self.conn = None
# 
#     def assertDateTimeEqual(self, dta, dtb):
#         """
#         Assert that two datetimes are within 1 second of each other
#         """
#         dta = dta.replace(microsecond=0)
#         dtb = dta.replace(microsecond=0)
# 
#         if dta.second != dtb.second:
#             self.assertLessThanEqual(
#                 abs(dta.second - dtb.second), 1, "datetimes are not one second apart!"
#             )
#             dta = dta.replace(second=0)
#             dtb = dtb.replace(second=0)
# 
#         self.assertEqual(dta, dtb)
# 
# Path: baleen/models.py
# class Feed(me.DynamicDocument):
# 
#     version   = me.StringField(choices=FEEDTYPES)
#     etag      = me.StringField()
#     modified  = me.StringField()
#     title     = me.StringField(max_length=256)
#     link      = me.URLField(required=True, unique=True)
#     urls      = me.DictField()
#     category  = me.StringField(required=True)
#     active    = me.BooleanField(default=True)
#     fetched   = me.DateTimeField(default=None)
#     created   = me.DateTimeField(default=datetime.now, required=True)
#     updated   = me.DateTimeField(default=datetime.now, required=True)
# 
#     @classmethod
#     def pre_save(cls, sender, document, **kwargs):
#         document.updated = datetime.now()
# 
#     meta      = {
#         'collection': 'feeds',
#     }
# 
#     @property
#     def xmlurl(self):
#         return self.link
# 
#     @property
#     def htmlurl(self):
#         return self.urls.get('htmlUrl')
# 
#     def count_posts(self):
#         """
#         Count the number of associated posts
# 
#         TODO: This is very, very slow on Mongo (fix and make better).
#         """
#         return Post.objects(feed=self).count()
# 
#     def __unicode__(self):
#         return self.title if self.title else self.link
# 
# class Post(me.DynamicDocument):
# 
#     feed      = me.ReferenceField(Feed)
#     title     = me.StringField( max_length=512 )
#     url       = me.URLField( required=True, unique=True )
#     pubdate   = me.DateTimeField()
#     content   = me.StringField( required=True )
#     tags      = me.ListField(me.StringField(max_length=256))
#     signature = me.StringField( required=True, max_length=64, min_length=64, unique=True )
#     created   = me.DateTimeField(default=datetime.now, required=True)
#     updated   = me.DateTimeField(default=datetime.now, required=True)
# 
#     @classmethod
#     def pre_save(cls, sender, document, **kwargs):
#         document.updated   = datetime.now()
#         document.signature = document.hash()
# 
#     meta      = {
#         'collection': 'posts',
#     }
# 
#     def hash(self):
#         """
#         Returns the SHA256 hash of the content.
#         """
#         sha = hashlib.sha256()
#         sha.update(self.content.encode('UTF-8'))
#         return sha.hexdigest()
# 
#     def htmlize(self):
#         """
#         Returns an HTML string of the content of the Post.
#         In the future we may use bleach to do sanitization or other simple
#         sanity checks to ensure that things are going ok, which is why this
#         method stub exists. 
#         """
#         return self.content
# 
#     def __unicode__(self):
#         return self.title if self.title else self.url


# Path: tests/test_wrangle.py
import os
import pickle
import unittest
    import mock
from .test_models import MongoTestMixin
    from unittest import mock
from baleen.wrangle import *
from baleen.exceptions import *
from baleen.models import Feed, Post

            with self.assertRaises(WranglingError) as cm:
                wrangle.wrangle()
            self.assertEqual(Post.objects.count(), 10)

    def test_is_wrangled(self):
        """
        Test the wrangling detection
        """
        wrangle = PostWrangler(self.entries[0])
        self.assertFalse(wrangle.is_wrangled())
        wrangle.wrangle()
        self.assertTrue(wrangle.is_wrangled())

    def test_save_not_save(self):
        """
        Test the wrangle interaction with the database
        """
        self.assertEqual(Post.objects.count(), 0)
        wrangle = PostWrangler(self.entries[0])

        # Don't save the wrangle
        wrangle.wrangle(False)
        self.assertEqual(Post.objects.count(), 0)

        # We've already wrangled so nothing should happen!
        wrangle.wrangle()
        self.assertEqual(Post.objects.count(), 0)

        # Try making something happen directly
        wrangle.wrangle().save()

        self.assertEqual(Post.objects.count(), 1)