# Path: staticgen/models.py
# class Page(models.Model):
#     PUBLISHER_STATE_DEFAULT = 0
#     PUBLISHER_STATE_CHANGED = 1
#     PUBLISHER_STATE_PUBLISHED = 2
#     PUBLISHER_STATE_ERROR = 3
#     PUBLISHER_STATE_DELETE = 4
# 
#     PUBLISHER_STATES = (
#         (PUBLISHER_STATE_DEFAULT, _('Pending')),
#         (PUBLISHER_STATE_CHANGED, _('Changed')),
#         (PUBLISHER_STATE_PUBLISHED, _('Published')),
#         (PUBLISHER_STATE_ERROR, _('Error')),
#         (PUBLISHER_STATE_DELETE, _('Deleted'))
#     )
# 
#     site = models.ForeignKey(Site, verbose_name=_('Site'))
#     path = models.CharField(_('URL'), max_length=255)
#     publisher_state = models.SmallIntegerField(
#         _('State'), choices=PUBLISHER_STATES, default=PUBLISHER_STATE_DEFAULT,
#         editable=False, db_index=True)
#     updated_at = models.DateTimeField(auto_now=True)
#     objects = PageManager()
# 
#     class Meta:
#         verbose_name = _('page')
#         verbose_name_plural = _('pages')
#         ordering = ('path', )
#         unique_together = ('site', 'path')
# 
#     def __str__(self):
#         return self.path
# 
#     @property
#     def has_changed(self):
#         return self.publisher_state == self.PUBLISHER_STATE_CHANGED
# 
# Path: staticgen/tasks.py
# @shared_task
# def publish_pages():
#     message = _('Received task to publish/update all registered pages.')
#     logger.info(force_text(message))
#     LogEntry.objects.log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(sync=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published or updated all registered pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     logger.info(message)
# 
# @shared_task
# def publish_pending():
#     message = _('Received task to publish all pending pages.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(pending=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published all pending pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)
# 
# @shared_task()
# def sync_pages():
#     message = _('Received task to sync all registered pages.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     Page.objects.sync()
# 
#     elapsed_time = time.time() - start_time
#     message = _('Synced all registered pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)
# 
# @shared_task
# def publish_changed():
#     message = _('Received task to publish all pages marked as changed.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(changed=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published all pages marked as changed successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)


from django.conf import settings
from django.test import TestCase
from boto import connect_s3
from moto import mock_s3
from staticgen.models import Page
from staticgen.tasks import publish_pages, publish_pending, sync_pages, publish_changed
    @mock_s3
    def test_publish_pending(self):
        # setUp
        connection = connect_s3()
        connection.create_bucket(settings.AWS_STORAGE_BUCKET_NAME)

        Page.objects.sync()

        # there should be 16 pending publish pages in the database
        self.assertEqual(Page.objects.pending().count(), 16)

        # actual test - this should publish all 16  pages
        publish_pending()

        # there should no more pending pages.
        self.assertEqual(Page.objects.pending().count(), 0)


    @mock_s3
    def test_publish_changed(self):
        # setUp
        connection = connect_s3()
        connection.create_bucket(settings.AWS_STORAGE_BUCKET_NAME)
        Page.objects.sync()  # adds 16 pages to database

        # mark 5 pages as changed.
        page_ids = Page.objects.values_list('pk', flat=True)[:5]
        Page.objects.filter(pk__in=page_ids).update(publisher_state=Page.PUBLISHER_STATE_CHANGED)

        # there should be 5 pages - marked as changed

# Path: staticgen/models.py
# class Page(models.Model):
#     PUBLISHER_STATE_DEFAULT = 0
#     PUBLISHER_STATE_CHANGED = 1
#     PUBLISHER_STATE_PUBLISHED = 2
#     PUBLISHER_STATE_ERROR = 3
#     PUBLISHER_STATE_DELETE = 4
# 
#     PUBLISHER_STATES = (
#         (PUBLISHER_STATE_DEFAULT, _('Pending')),
#         (PUBLISHER_STATE_CHANGED, _('Changed')),
#         (PUBLISHER_STATE_PUBLISHED, _('Published')),
#         (PUBLISHER_STATE_ERROR, _('Error')),
#         (PUBLISHER_STATE_DELETE, _('Deleted'))
#     )
# 
#     site = models.ForeignKey(Site, verbose_name=_('Site'))
#     path = models.CharField(_('URL'), max_length=255)
#     publisher_state = models.SmallIntegerField(
#         _('State'), choices=PUBLISHER_STATES, default=PUBLISHER_STATE_DEFAULT,
#         editable=False, db_index=True)
#     updated_at = models.DateTimeField(auto_now=True)
#     objects = PageManager()
# 
#     class Meta:
#         verbose_name = _('page')
#         verbose_name_plural = _('pages')
#         ordering = ('path', )
#         unique_together = ('site', 'path')
# 
#     def __str__(self):
#         return self.path
# 
#     @property
#     def has_changed(self):
#         return self.publisher_state == self.PUBLISHER_STATE_CHANGED
# 
# Path: staticgen/tasks.py
# @shared_task
# def publish_pages():
#     message = _('Received task to publish/update all registered pages.')
#     logger.info(force_text(message))
#     LogEntry.objects.log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(sync=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published or updated all registered pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     logger.info(message)
# 
# @shared_task
# def publish_pending():
#     message = _('Received task to publish all pending pages.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(pending=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published all pending pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)
# 
# @shared_task()
# def sync_pages():
#     message = _('Received task to sync all registered pages.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     Page.objects.sync()
# 
#     elapsed_time = time.time() - start_time
#     message = _('Synced all registered pages successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)
# 
# @shared_task
# def publish_changed():
#     message = _('Received task to publish all pages marked as changed.')
#     log_action(message)
# 
#     start_time = time.time()
# 
#     publisher = StaticgenPublisher()
#     publisher.publish(changed=True)
# 
#     elapsed_time = time.time() - start_time
#     message = _('Published all pages marked as changed successfully, '
#                 'in {elapsed_time:.2f} seconds.').format(elapsed_time=elapsed_time)
#     log_action(message)


# Path: tests/test_tasks.py
from django.conf import settings
from django.test import TestCase
from boto import connect_s3
from moto import mock_s3
from staticgen.models import Page
from staticgen.tasks import publish_pages, publish_pending, sync_pages, publish_changed

    @mock_s3
    def test_publish_pending(self):
        # setUp
        connection = connect_s3()
        connection.create_bucket(settings.AWS_STORAGE_BUCKET_NAME)

        Page.objects.sync()

        # there should be 16 pending publish pages in the database
        self.assertEqual(Page.objects.pending().count(), 16)

        # actual test - this should publish all 16  pages
        publish_pending()

        # there should no more pending pages.
        self.assertEqual(Page.objects.pending().count(), 0)


    @mock_s3
    def test_publish_changed(self):
        # setUp
        connection = connect_s3()
        connection.create_bucket(settings.AWS_STORAGE_BUCKET_NAME)
        Page.objects.sync()  # adds 16 pages to database

        # mark 5 pages as changed.
        page_ids = Page.objects.values_list('pk', flat=True)[:5]
        Page.objects.filter(pk__in=page_ids).update(publisher_state=Page.PUBLISHER_STATE_CHANGED)

        # there should be 5 pages - marked as changed

        self.assertEqual(Page.objects.changed().count(), 5)