Browse Source

use basetestcase for most test cases that require predefined user, add test for ensuretriggers, add travis cache

pull/30/head
Di Wu 9 years ago
parent
commit
fe72792fd2
28 changed files with 129 additions and 169 deletions
  1. +1
    -0
      .travis.yml
  2. +2
    -9
      hc/accounts/tests/test_check_token.py
  3. +18
    -13
      hc/api/management/commands/ensuretriggers.py
  4. +1
    -0
      hc/api/tests/test_email_webhook.py
  5. +27
    -0
      hc/api/tests/test_ensuretriggers.py
  6. +4
    -7
      hc/api/tests/test_notify.py
  7. +1
    -0
      hc/api/tests/test_ping.py
  8. +9
    -10
      hc/api/tests/test_sendalerts.py
  9. +1
    -0
      hc/api/tests/test_status.py
  10. +5
    -13
      hc/front/tests/test_add_channel.py
  11. +2
    -9
      hc/front/tests/test_add_check.py
  12. +1
    -0
      hc/front/tests/test_basics.py
  13. +3
    -6
      hc/front/tests/test_channel_checks.py
  14. +3
    -6
      hc/front/tests/test_log.py
  15. +2
    -7
      hc/front/tests/test_my_checks.py
  16. +3
    -6
      hc/front/tests/test_remove_channel.py
  17. +3
    -6
      hc/front/tests/test_remove_check.py
  18. +3
    -6
      hc/front/tests/test_update_channel.py
  19. +3
    -6
      hc/front/tests/test_update_name.py
  20. +3
    -6
      hc/front/tests/test_update_timeout.py
  21. +2
    -7
      hc/front/tests/test_verify_email.py
  22. +4
    -8
      hc/payments/tests/test_billing.py
  23. +4
    -8
      hc/payments/tests/test_cancel_plan.py
  24. +4
    -10
      hc/payments/tests/test_create_plan.py
  25. +3
    -9
      hc/payments/tests/test_get_client_token.py
  26. +4
    -8
      hc/payments/tests/test_invoice.py
  27. +2
    -9
      hc/payments/tests/test_pricing.py
  28. +11
    -0
      hc/test.py

+ 1
- 0
.travis.yml View File

@ -14,3 +14,4 @@ addons:
script:
- coverage run --source=hc manage.py test
after_success: coveralls
cache: pip

+ 2
- 9
hc/accounts/tests/test_check_token.py View File

@ -1,19 +1,12 @@
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.test import TestCase
from hc.accounts.models import Profile
from hc.test import BaseTestCase
class CheckTokenTestCase(TestCase):
class CheckTokenTestCase(BaseTestCase):
def setUp(self):
super(CheckTokenTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.profile = Profile(user=self.alice)
self.profile.token = make_password("secret-token")
self.profile.save()


+ 18
- 13
hc/api/management/commands/ensuretriggers.py View File

@ -25,10 +25,14 @@ def _pg(cursor):
def _mysql(cursor):
cursor.execute("""
DROP TRIGGER IF EXISTS update_alert_after;
""")
cursor.execute("""
CREATE TRIGGER update_alert_after
BEFORE UPDATE ON api_check
FOR EACH ROW SET NEW.alert_after = NEW.last_ping + INTERVAL (NEW.timeout + NEW.grace) MICROSECOND;
FOR EACH ROW SET
NEW.alert_after =
NEW.last_ping + INTERVAL (NEW.timeout + NEW.grace) MICROSECOND;
""")
@ -42,7 +46,9 @@ def _sqlite(cursor):
AFTER UPDATE OF last_ping, timeout, grace ON api_check
FOR EACH ROW BEGIN
UPDATE api_check
SET alert_after = datetime(strftime('%s', last_ping) + timeout/1000000 + grace/1000000, 'unixepoch')
SET alert_after =
datetime(strftime('%s', last_ping) +
timeout/1000000 + grace/1000000, 'unixepoch')
WHERE id = OLD.id;
END;
""")
@ -52,14 +58,13 @@ class Command(BaseCommand):
help = 'Ensures triggers exist in database'
def handle(self, *args, **options):
cursor = connection.cursor()
if connection.vendor == "postgresql":
_pg(cursor)
return "Created PostgreSQL trigger"
if connection.vendor == "mysql":
_mysql(cursor)
return "Created MySQL trigger"
if connection.vendor == "sqlite":
_sqlite(cursor)
return "Created SQLite trigger"
with connection.cursor() as cursor:
if connection.vendor == "postgresql":
_pg(cursor)
return "Created PostgreSQL trigger"
if connection.vendor == "mysql":
_mysql(cursor)
return "Created MySQL trigger"
if connection.vendor == "sqlite":
_sqlite(cursor)
return "Created SQLite trigger"

+ 1
- 0
hc/api/tests/test_email_webhook.py View File

@ -1,6 +1,7 @@
import json
from django.test import TestCase
from hc.api.models import Check, Ping


+ 27
- 0
hc/api/tests/test_ensuretriggers.py View File

@ -0,0 +1,27 @@
from datetime import timedelta
from django.test import TestCase
from django.utils import timezone
from hc.api.management.commands.ensuretriggers import Command
from hc.api.models import Check
class EnsureTriggersTestCase(TestCase):
def test_ensure_triggers(self):
Command().handle()
check = Check.objects.create()
assert check.alert_after is None
check.last_ping = timezone.now()
check.save()
check.refresh_from_db()
assert check.alert_after is not None
alert_after = check.alert_after
check.last_ping += timedelta(days=1)
check.save()
check.refresh_from_db()
assert check.alert_after > alert_after

+ 4
- 7
hc/api/tests/test_notify.py View File

@ -1,17 +1,14 @@
from django.contrib.auth.models import User
from django.core import mail
from django.test import TestCase
from hc.api.models import Channel, Check, Notification
from mock import patch
from requests.exceptions import ReadTimeout
from hc.api.models import Channel, Check, Notification
from hc.test import BaseTestCase
class NotifyTestCase(TestCase):
class NotifyTestCase(BaseTestCase):
def _setup_data(self, channel_kind, channel_value, email_verified=True):
self.alice = User(username="alice")
self.alice.save()
self.check = Check()
self.check.status = "down"
self.check.save()


+ 1
- 0
hc/api/tests/test_ping.py View File

@ -1,4 +1,5 @@
from django.test import Client, TestCase
from hc.api.models import Check, Ping


+ 9
- 10
hc/api/tests/test_sendalerts.py View File

@ -1,24 +1,23 @@
from datetime import datetime
from datetime import timedelta
from django.utils import timezone
from mock import patch
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.management.commands.sendalerts import Command
from hc.api.models import Check
from mock import patch
from hc.test import BaseTestCase
class SendAlertsTestCase(TestCase):
class SendAlertsTestCase(BaseTestCase):
@patch("hc.api.management.commands.sendalerts.Command.handle_one")
def test_it_handles_few(self, mock):
alice = User(username="alice")
alice.save()
yesterday = timezone.now() - timedelta(days=1)
names = ["Check %d" % d for d in range(0, 10)]
for name in names:
check = Check(user=alice, name=name)
check.alert_after = datetime(2000, 1, 1)
check = Check(user=self.alice, name=name)
check.alert_after = yesterday
check.status = "up"
check.save()


+ 1
- 0
hc/api/tests/test_status.py View File

@ -1,4 +1,5 @@
from django.test import TestCase
from hc.api.models import Check


+ 5
- 13
hc/front/tests/test_add_channel.py View File

@ -1,19 +1,11 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel
from django.test.utils import override_settings
class AddChannelTestCase(TestCase):
from hc.api.models import Channel
from hc.test import BaseTestCase
def setUp(self):
super(AddChannelTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
settings.PUSHOVER_API_TOKEN = "bogus_token"
settings.PUSHOVER_SUBSCRIPTION_URL = "bogus_url"
@override_settings(PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="url")
class AddChannelTestCase(BaseTestCase):
def test_it_works(self):
url = "/integrations/add/"


+ 2
- 9
hc/front/tests/test_add_check.py View File

@ -1,15 +1,8 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
from hc.test import BaseTestCase
class AddCheckTestCase(TestCase):
def setUp(self):
super(AddCheckTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
class AddCheckTestCase(BaseTestCase):
def test_it_works(self):
url = "/checks/add/"


+ 1
- 0
hc/front/tests/test_basics.py View File

@ -1,4 +1,5 @@
from django.test import TestCase
from hc.api.models import Check


+ 3
- 6
hc/front/tests/test_channel_checks.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel
from hc.test import BaseTestCase
class ChannelChecksTestCase(TestCase):
class ChannelChecksTestCase(BaseTestCase):
def setUp(self):
super(ChannelChecksTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "[email protected]"
self.channel.save()


+ 3
- 6
hc/front/tests/test_log.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
class LogTestCase(TestCase):
class LogTestCase(BaseTestCase):
def setUp(self):
super(LogTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()


+ 2
- 7
hc/front/tests/test_my_checks.py View File

@ -1,16 +1,11 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
from hc.test import BaseTestCase
class MyChecksTestCase(TestCase):
class MyChecksTestCase(BaseTestCase):
def setUp(self):
super(MyChecksTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice, name="Alice Was Here")
self.check.save()


+ 3
- 6
hc/front/tests/test_remove_channel.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel
from hc.test import BaseTestCase
class RemoveChannelTestCase(TestCase):
class RemoveChannelTestCase(BaseTestCase):
def setUp(self):
super(RemoveChannelTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "[email protected]"
self.channel.save()


+ 3
- 6
hc/front/tests/test_remove_check.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
from hc.test import BaseTestCase
class RemoveCheckTestCase(TestCase):
class RemoveCheckTestCase(BaseTestCase):
def setUp(self):
super(RemoveCheckTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()


+ 3
- 6
hc/front/tests/test_update_channel.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel, Check
from hc.test import BaseTestCase
class UpdateChannelTestCase(TestCase):
class UpdateChannelTestCase(BaseTestCase):
def setUp(self):
super(UpdateChannelTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()


+ 3
- 6
hc/front/tests/test_update_name.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
from hc.test import BaseTestCase
class UpdateNameTestCase(TestCase):
class UpdateNameTestCase(BaseTestCase):
def setUp(self):
super(UpdateNameTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()


+ 3
- 6
hc/front/tests/test_update_timeout.py View File

@ -1,16 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Check
from hc.test import BaseTestCase
class UpdateTimeoutTestCase(TestCase):
class UpdateTimeoutTestCase(BaseTestCase):
def setUp(self):
super(UpdateTimeoutTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()


+ 2
- 7
hc/front/tests/test_verify_email.py View File

@ -1,16 +1,11 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.api.models import Channel
from hc.test import BaseTestCase
class VerifyEmailTestCase(TestCase):
class VerifyEmailTestCase(BaseTestCase):
def setUp(self):
super(VerifyEmailTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "[email protected]"
self.channel.save()


+ 4
- 8
hc/payments/tests/test_billing.py View File

@ -1,17 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.payments.models import Subscription
from mock import Mock, patch
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class BillingTestCase(TestCase):
class BillingTestCase(BaseTestCase):
def setUp(self):
super(BillingTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.sub = Subscription(user=self.alice)
self.sub.subscription_id = "test-id"
self.sub.customer_id = "test-customer-id"


+ 4
- 8
hc/payments/tests/test_cancel_plan.py View File

@ -1,17 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.payments.models import Subscription
from mock import patch
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class CancelPlanTestCase(TestCase):
class CancelPlanTestCase(BaseTestCase):
def setUp(self):
super(CancelPlanTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.sub = Subscription(user=self.alice)
self.sub.subscription_id = "test-id"
self.sub.plan_id = "P5"


+ 4
- 10
hc/payments/tests/test_create_plan.py View File

@ -1,17 +1,11 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.accounts.models import Profile
from hc.payments.models import Subscription
from mock import patch
from hc.accounts.models import Profile
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class CreatePlanTestCase(TestCase):
def setUp(self):
super(CreatePlanTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
class CreatePlanTestCase(BaseTestCase):
def _setup_mock(self, mock):
""" Set up Braintree calls that the controller will use. """


+ 3
- 9
hc/payments/tests/test_get_client_token.py View File

@ -1,16 +1,10 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.payments.models import Subscription
from mock import patch
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class GetClientTokenTestCase(TestCase):
def setUp(self):
super(GetClientTokenTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
class GetClientTokenTestCase(BaseTestCase):
@patch("hc.payments.views.braintree")
def test_it_works(self, mock_braintree):


+ 4
- 8
hc/payments/tests/test_invoice.py View File

@ -1,17 +1,13 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.payments.models import Subscription
from mock import Mock, patch
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class InvoiceTestCase(TestCase):
class InvoiceTestCase(BaseTestCase):
def setUp(self):
super(InvoiceTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
self.sub = Subscription(user=self.alice)
self.sub.subscription_id = "test-id"
self.sub.customer_id = "test-customer-id"


+ 2
- 9
hc/payments/tests/test_pricing.py View File

@ -1,15 +1,8 @@
from django.contrib.auth.models import User
from django.test import TestCase
from hc.payments.models import Subscription
from hc.test import BaseTestCase
class PricingTestCase(TestCase):
def setUp(self):
super(PricingTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()
class PricingTestCase(BaseTestCase):
def test_anonymous(self):
r = self.client.get("/pricing/")


+ 11
- 0
hc/test.py View File

@ -0,0 +1,11 @@
from django.contrib.auth.models import User
from django.test import TestCase
class BaseTestCase(TestCase):
def setUp(self):
super(BaseTestCase, self).setUp()
self.alice = User(username="alice", email="[email protected]")
self.alice.set_password("password")
self.alice.save()

Loading…
Cancel
Save