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

This commit is contained in:
Di Wu 2016-01-17 23:37:08 -08:00
parent 29df1be80c
commit fe72792fd2
28 changed files with 130 additions and 170 deletions

View File

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

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.profile = Profile(user=self.alice)
self.profile.token = make_password("secret-token")
self.profile.save()

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,8 +58,7 @@ class Command(BaseCommand):
help = 'Ensures triggers exist in database'
def handle(self, *args, **options):
cursor = connection.cursor()
with connection.cursor() as cursor:
if connection.vendor == "postgresql":
_pg(cursor)
return "Created PostgreSQL trigger"

View File

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

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

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()

View File

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

View File

@ -1,24 +1,23 @@
from datetime import datetime
from datetime import timedelta
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 django.utils import timezone
from mock import patch
from hc.api.management.commands.sendalerts import Command
from hc.api.models import Check
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()

View File

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

View File

@ -1,19 +1,11 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.utils import override_settings
from hc.api.models import Channel
from hc.test import BaseTestCase
class AddChannelTestCase(TestCase):
def setUp(self):
super(AddChannelTestCase, self).setUp()
self.alice = User(username="alice", email="alice@example.org")
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/"

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
class AddCheckTestCase(BaseTestCase):
def test_it_works(self):
url = "/checks/add/"

View File

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

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "alice@example.org"
self.channel.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice, name="Alice Was Here")
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "alice@example.org"
self.channel.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.check = Check(user=self.alice)
self.check.save()

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
self.channel = Channel(user=self.alice, kind="email")
self.channel.value = "alice@example.org"
self.channel.save()

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="alice@example.org")
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"

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="alice@example.org")
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"

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="alice@example.org")
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. """

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
class GetClientTokenTestCase(BaseTestCase):
@patch("hc.payments.views.braintree")
def test_it_works(self, mock_braintree):

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="alice@example.org")
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"

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="alice@example.org")
self.alice.set_password("password")
self.alice.save()
class PricingTestCase(BaseTestCase):
def test_anonymous(self):
r = self.client.get("/pricing/")

11
hc/test.py Normal file
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="alice@example.org")
self.alice.set_password("password")
self.alice.save()