Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
test make me happy :)
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxMaSk committed Oct 20, 2016
1 parent 0043072 commit 59b0cc3
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 246 deletions.
35 changes: 17 additions & 18 deletions django_th/publishing_limit.py
Expand Up @@ -31,23 +31,22 @@ def get_data(service, cache_data, trigger_id):

cache = caches[service]

if 'publishing_limit' in settings.DJANGO_TH:
limit = settings.DJANGO_TH['publishing_limit']

# publishing of all the data
if limit == 0:
return cache_data
# or just a set of them
if cache_data is not None and len(cache_data) > limit:
for data in cache_data[limit:]:
service_str = ''.join((service, '_',
str(trigger_id)))
# put that data in a version 2 of the cache
cache.set(service_str, data, version=2)
# delete data from cache version=1
# https://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
cache.delete_pattern(service_str)
# put in cache unpublished data
cache_data = cache_data[:limit]
limit = settings.DJANGO_TH.get('publishing_limit', 0)

# publishing of all the data
if limit == 0:
return cache_data
# or just a set of them
if cache_data is not None and len(cache_data) > limit:
for data in cache_data[limit:]:
service_str = ''.join((service, '_',
str(trigger_id)))
# put that data in a version 2 of the cache
cache.set(service_str, data, version=2)
# delete data from cache version=1
# https://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
cache.delete_pattern(service_str)
# put in cache unpublished data
cache_data = cache_data[:limit]

return cache_data
3 changes: 2 additions & 1 deletion django_th/recycle.py
Expand Up @@ -3,6 +3,7 @@
from __future__ import absolute_import

# django
from django.conf import settings
from django.core.cache import caches
from django.utils.log import getLogger

Expand All @@ -20,7 +21,7 @@ def recycle():
logger = getLogger('django_th.trigger_happy')
all_packages = MyService.all_packages()
for package in all_packages:
if package == 'th_instapush':
if package in settings.DJANGO_TH.get('services_wo_cache'):
continue
cache = caches[package]
# http://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk
Expand Down
77 changes: 4 additions & 73 deletions django_th/settings.py
Expand Up @@ -339,79 +339,10 @@
},
}

DJANGO_TH = {
# paginating
'paginate_by': 5,

# this permits to avoid "flood" effect when publishing
# to the target service - when limit is reached
# the cache is kept until next time
# set it to 0 to drop that limit
'publishing_limit': 5,
# number of process to spawn from multiprocessing.Pool
'processes': 5,
}

TH_SERVICES = (
# uncomment the lines to enable the service you need
# 'th_evernote.my_evernote.ServiceEvernote',
# 'th_github.my_github.ServiceGithub',
# 'th_instapush.my_instapush.ServiceInstapush',
# 'th_pelican.my_pelican.ServicePelican',
# 'th_pocket.my_pocket.ServicePocket',
# 'th_pushbullet.my_pushbullet.ServicePushbullet',
'th_rss.my_rss.ServiceRss',
# 'th_todoist.my_todoist.ServiceTodoist',
# 'th_trello.my_trello.ServiceTrello',
# 'th_twitter.my_twitter.ServiceTwitter',
'th_wallabag.my_wallabag.ServiceWallabag',
)


TH_EVERNOTE = {
# get your credential by subscribing to http://dev.evernote.com/
# for testing purpose set sandbox to True
# for production purpose set sandbox to False
'sandbox': False,
'consumer_key': '<your evernote key>',
'consumer_secret': '<your evernote secret>',
}


TH_GITHUB = {
'username': 'username',
'password': 'password',
'consumer_key': 'my key',
'consumer_secret': 'my secret'
}


TH_POCKET = {
# get your credential by subscribing to http://getpocket.com/developer/
'consumer_key': '<your pocket key>',
}

TH_PUSHBULLET = {
'client_id': '<your pushbullet key>',
'client_secret': '<your pushbullet secret>',
}

TH_TODOIST = {
'client_id': '<your todoist key>',
'client_secret': '<your todoist secret>',
}

TH_TRELLO = {
'consumer_key': '<your trello key>',
'consumer_secret': '<your trello secret>',
}

TH_TWITTER = {
# get your credential by subscribing to
# https://dev.twitter.com/
'consumer_key': '<your twitter key>',
'consumer_secret': '<your twitter secret>',
}
try:
from .th_settings import *
except ImportError:
raise ImportError("you should create a th_settings.py with everything related to TriggerHappy, see th_settings_sample.py")

SECRET_KEY = 'to be defined :P'

Expand Down
12 changes: 12 additions & 0 deletions django_th/tests/test_main.py
Expand Up @@ -40,3 +40,15 @@ def create_triggerservice(self, date_created="20130610",
date_created=date_created,
description=description,
status=status)


def setup_view(view, request, *args, **kwargs):
"""Mimic as_view() returned callable, but returns view instance.
args and kwargs are the same you would pass to ``reverse()``
"""
view.request = request
view.args = args
view.kwargs = kwargs
return view
12 changes: 2 additions & 10 deletions django_th/tests/test_models_and_services.py
@@ -1,26 +1,18 @@
# coding: utf-8
from django.test import TestCase
from django.contrib.auth.models import User

from django_th.models import TriggerService
from django_th.models import UserService, ServicesActivated, update_result
from django_th.forms.base import TriggerServiceForm
from django_th.forms.base import UserServiceForm
from django_th.tests.test_main import MainTest


class UserServiceTest(TestCase):
class UserServiceTest(MainTest):

"""
UserService Model
"""

def setUp(self):
try:
self.user = User.objects.get(username='john')
except User.DoesNotExist:
self.user = User.objects.create_user(
username='john', email='john@doe.info', password='doe')

def create_userservice(self, token="AZERTY12345"):
user = self.user
name = ServicesActivated.objects.create(name='ServiceEvernote',
Expand Down
2 changes: 2 additions & 0 deletions django_th/tests/test_my_conf.py
Expand Up @@ -16,9 +16,11 @@ def test_get_config_th(self):
self.assertTrue(settings.DJANGO_TH)
self.assertIn('paginate_by', settings.DJANGO_TH)
self.assertIn('publishing_limit', settings.DJANGO_TH)
self.assertIn('services_wo_cache', settings.DJANGO_TH)
self.assertIs(type(settings.DJANGO_TH.get('paginate_by')), int)
self.assertIs(type(settings.DJANGO_TH.get('publishing_limit')), int)
self.assertIs(type(settings.DJANGO_TH.get('processes')), int)
self.assertIs(type(settings.DJANGO_TH.get('services_wo_cache')), list)
self.assertIn('django_th', settings.CACHES)

def test_get_services_list(self):
Expand Down
7 changes: 1 addition & 6 deletions django_th/tests/test_services.py
@@ -1,6 +1,5 @@
# coding: utf-8
from django.test import Client
from django.contrib.auth.models import User

try:
from django.apps import apps
Expand All @@ -15,18 +14,14 @@
class ServicesMgrTestCase(MainTest):

def setUp(self):
super(ServicesMgrTestCase, self).setUp()
arg = ''
self.service = ServicesMgr(arg)
self.service.consumer_key = 'azerty'
self.service.consumer_secret = 'qsdfghjk'

self.oauth = 'oauth1'
self.request = Client()
try:
self.user = User.objects.get(username='john')
except User.DoesNotExist:
self.user = User.objects.create_user(
username='john', email='john@doe.info', password='doe')

def test_set_title(self):
data = {'title': 'foobar'}
Expand Down
23 changes: 3 additions & 20 deletions django_th/tests/test_views.py
@@ -1,13 +1,12 @@
# coding: utf-8
import unittest
from django.test import RequestFactory, Client
from django.contrib.auth.models import User

from django_th.views import TriggerEditedTemplateView
from django_th.views import TriggerDeletedTemplateView, TriggerListView
from django_th.views_fbv import can_modify_trigger, trigger_on_off
from django_th.models import TriggerService
from django_th.tests.test_main import MainTest
from django_th.tests.test_main import MainTest, setup_view


class TriggerEditedTemplateViewTestCase(unittest.TestCase):
Expand Down Expand Up @@ -40,16 +39,12 @@ def test_get(self):
'triggers/deleted_thanks_trigger.html')


class TriggerListViewTestCase(unittest.TestCase):
class TriggerListViewTestCase(MainTest):

def setUp(self):
super(TriggerListViewTestCase, self).setUp()
# Every test needs access to the request factory.
self.factory = RequestFactory()
try:
self.user = User.objects.get(username='john')
except User.DoesNotExist:
self.user = User.objects.create_user(
username='john', email='john@doe.info', password='doe')

def test_context_data(self):
"""
Expand Down Expand Up @@ -104,15 +99,3 @@ def test_trigger_on_off(self):

response = trigger_on_off(request=c, trigger_id=s.id)
self.assertTrue(response.status_code, 200)


def setup_view(view, request, *args, **kwargs):
"""Mimic as_view() returned callable, but returns view instance.
args and kwargs are the same you would pass to ``reverse()``
"""
view.request = request
view.args = args
view.kwargs = kwargs
return view
12 changes: 4 additions & 8 deletions django_th/tests/test_views_userservices.py
@@ -1,22 +1,18 @@
import unittest
# coding: utf-8
from django.test import RequestFactory
from django.contrib.auth.models import User

from django_th.views_userservices import UserServiceListView
from django_th.models import UserService
from django_th.tests.test_views import setup_view
from django_th.tests.test_main import MainTest


class UserServiceListViewTestCase(unittest.TestCase):
class UserServiceListViewTestCase(MainTest):

def setUp(self):
super(UserServiceListViewTestCase, self).setUp()
# Every test needs access to the request factory.
self.factory = RequestFactory()
try:
self.user = User.objects.get(username='john')
except User.DoesNotExist:
self.user = User.objects.create_user(
username='john', email='john@doe.info', password='doe')

def test_context_data(self):
# Setup request and view
Expand Down
74 changes: 74 additions & 0 deletions django_th/th_settings.py
@@ -0,0 +1,74 @@
DJANGO_TH = {
# paginating
'paginate_by': 5,

# this permits to avoid "flood" effect when publishing
# to the target service - when limit is reached
# the cache is kept until next time
# set it to 0 to drop that limit
'publishing_limit': 5,
# number of process to spawn from multiprocessing.Pool
'processes': 5,
'services_wo_cache': ['th_instapush',]
}

TH_SERVICES = (
# uncomment the lines to enable the service you need
# 'th_evernote.my_evernote.ServiceEvernote',
# 'th_github.my_github.ServiceGithub',
# 'th_instapush.my_instapush.ServiceInstapush',
# 'th_pelican.my_pelican.ServicePelican',
# 'th_pocket.my_pocket.ServicePocket',
# 'th_pushbullet.my_pushbullet.ServicePushbullet',
'th_rss.my_rss.ServiceRss',
# 'th_todoist.my_todoist.ServiceTodoist',
# 'th_trello.my_trello.ServiceTrello',
# 'th_twitter.my_twitter.ServiceTwitter',
'th_wallabag.my_wallabag.ServiceWallabag',
)


TH_EVERNOTE = {
# get your credential by subscribing to http://dev.evernote.com/
# for testing purpose set sandbox to True
# for production purpose set sandbox to False
'sandbox': False,
'consumer_key': '<your evernote key>',
'consumer_secret': '<your evernote secret>',
}


TH_GITHUB = {
'username': 'username',
'password': 'password',
'consumer_key': 'my key',
'consumer_secret': 'my secret'
}


TH_POCKET = {
# get your credential by subscribing to http://getpocket.com/developer/
'consumer_key': '<your pocket key>',
}

TH_PUSHBULLET = {
'client_id': '<your pushbullet key>',
'client_secret': '<your pushbullet secret>',
}

TH_TODOIST = {
'client_id': '<your todoist key>',
'client_secret': '<your todoist secret>',
}

TH_TRELLO = {
'consumer_key': '<your trello key>',
'consumer_secret': '<your trello secret>',
}

TH_TWITTER = {
# get your credential by subscribing to
# https://dev.twitter.com/
'consumer_key': '<your twitter key>',
'consumer_secret': '<your twitter secret>',
}

0 comments on commit 59b0cc3

Please sign in to comment.