Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor unittests #166

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 52 additions & 59 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions docker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ amqp==2.5.1
atomicwrites==1.3.0
billiard==3.6.1.0
celery[redis]==4.3.0
certifi==2019.6.16
certifi==2019.9.11
chardet==3.0.4
defusedxml==0.6.0 ; python_version >= '3.0'
dj-database-url==0.5.0
Expand All @@ -12,8 +12,8 @@ djangorestframework==3.9.4
envdir==1.0.1
gunicorn==19.9.0
idna==2.8
importlib-metadata==0.19
kombu==4.6.4
importlib-metadata==0.23
kombu==4.6.5
oauthlib==3.1.0
prometheus-client==0.7.0
pyjwt==1.7.1
Expand All @@ -29,7 +29,7 @@ six==1.12.0
social-auth-app-django==3.1.0
social-auth-core==3.2.0
sqlparse==0.3.0
urllib3==1.25.3
urllib3==1.25.6
vine==1.3.0
whitenoise==4.1.2
zipp==0.5.2
zipp==0.6.0
25 changes: 8 additions & 17 deletions promgen/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import yaml

from django.contrib.auth.models import Permission
from django.contrib.auth.models import Permission, User
from django.test import TestCase


Expand All @@ -26,27 +26,18 @@ def data(cls, *args):
with open(os.path.join(os.path.dirname(__file__), *args)) as fp:
return fp.read()

def factory(self, klass, name):
from promgen import models
if klass == models.Project:
shard = models.Shard.objects.create(
name='Shard ' + name)
service = models.Service.objects.create(
name='Service ' + name,
shard=shard,
)
return models.Project.objects.create(
name=name,
service=service,
)

def assertRoute(self, response, view, status=200):
self.assertEqual(response.status_code, status)
def assertRoute(self, response, view, status=200, msg=None):
self.assertEqual(response.status_code, status, msg)
self.assertEqual(response.resolver_match.func.__name__, view.as_view().__name__)

def assertCount(self, model, count, msg=None):
self.assertEqual(model.objects.count(), count, msg)

def add_force_login(self, **kwargs):
user = User.objects.create_user(**kwargs)
self.client.force_login(user, "django.contrib.auth.backends.ModelBackend")
return user

def add_user_permissions(self, *args, user=None):
codenames = [p.split(".")[1] for p in args]
permissions = Permission.objects.filter(
Expand Down
11 changes: 6 additions & 5 deletions promgen/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# These sources are released under the terms of the MIT license: see LICENSE


from promgen import models, views
from promgen.tests import PromgenTest

from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen import models, views

from promgen.tests import PromgenTest

Expand All @@ -21,7 +24,5 @@ def test_alert_blackhole(self):
response = self.client.post(
reverse('alert'), data=TEST_HEARTBEAT, content_type='application/json'
)
self.assertEqual(response.status_code, 202)
self.assertEqual(
models.Alert.objects.count(), 0, 'Heartbeat alert should be deleted'
)
self.assertRoute(response, views.Alert, 202)
self.assertCount(models.Alert, 0, "Heartbeat alert should be deleted")
19 changes: 4 additions & 15 deletions promgen/tests/test_host_add.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from unittest import mock

import factory.django
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_save
from django.test import override_settings
from django.urls import reverse

from promgen import models
from promgen.tests import PromgenTest

TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml')
TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json')
TEST_IMPORT = PromgenTest.data('examples', 'import.json')
TEST_REPLACE = PromgenTest.data('examples', 'replace.json')
from django.urls import reverse


class RouteTests(PromgenTest):
longMessage = True

@factory.django.mute_signals(pre_save, post_save)
def setUp(self):
self.client.force_login(User.objects.create_user(id=999, username="Foo"), 'django.contrib.auth.backends.ModelBackend')
self.add_force_login(id=999, username="Foo")

def test_newline(self):
farm = models.Farm.objects.create(name='Foo')
self.client.post(reverse('hosts-add', args=[farm.pk]), {
'hosts': "\naaa\nbbb\nccc \n"
}, follow=False)
self.assertEqual(models.Host.objects.count(), 3, 'Expected 3 hosts')
self.assertCount(models.Host, 3, "Expected 3 hosts")

def test_comma(self):
farm = models.Farm.objects.create(name='Foo')
self.client.post(reverse('hosts-add', args=[farm.pk]), {
'hosts': ",,aaa, bbb,ccc,"
}, follow=False)
self.assertEqual(models.Host.objects.count(), 3, 'Expected 3 hosts')
self.assertCount(models.Host, 3, "Expected 3 hosts")
Loading