Commit 5d70fbb3 authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

Add tests and run them with tox

parent 9ed63873
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
import sys
import django
from django.conf import settings
from django.test.utils import get_runner
if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests",])
sys.exit(bool(failures))
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from django.db import models
from model_journal.models import Journal
class RelatedTestModel(models.Model):
"""
Model which is used as ForeignKey during testing.
"""
char_field = models.CharField(max_length=16)
class TestModel(models.Model):
char_field = models.CharField(max_length=16)
int_field = models.IntegerField()
bool_field = models.BooleanField()
foreign_key = models.ForeignKey(RelatedTestModel)
date_time_field = models.DateTimeField(auto_now_add=True)
# The `created` field will be excluded from the journal for testing
created = models.DateTimeField(auto_now_add=True, editable=False)
journal = Journal(exclude=['created'])
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from django import VERSION as DJANGO_VERSION
DATABASES = {
'default': {'ENGINE': 'django.db.backends.sqlite3'}
}
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'model_journal',
'tests',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'model_journal.middleware.ModelJournalMiddleware',
]
# Django < 1.10 needs old-style middleware
if DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] < 10:
MIDDLEWARE_CLASSES = MIDDLEWARE
STATIC_URL = "/static/"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
SECRET_KEY = 'fake-key'
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from django.test import TransactionTestCase
from model_journal.models import JournalEntry
from .models import RelatedTestModel, TestModel
class JournalEntryTestCase(TransactionTestCase):
def setUp(self):
self.related_object = RelatedTestModel.objects.create(char_field="Foo")
def _create_test_model(self):
obj = TestModel()
obj.char_field = "test"
obj.int_field = 1
obj.bool_field = True
obj.foreign_key = self.related_object
obj.save()
return obj
def test_journal_entry_api(self):
# We'll start with an empty Journal
self.assertEqual(JournalEntry.objects.count(), 0)
# Then we create a new TestModel
obj = self._create_test_model()
# And now we should have one JournalEntry record
self.assertEqual(obj.journal.count(), 1)
# The JournalEntry operation should be "add"
entry = JournalEntry.objects.first()
self.assertEqual(entry.operation, JournalEntry.OPERATION_ADD)
# Test if all the required attributes are logged correctly
attrs = entry.attrs
self.assertIn('char_field', attrs)
self.assertIn('int_field', attrs)
self.assertIn('bool_field', attrs)
self.assertIn('foreign_key_id', attrs)
self.assertNotIn('created', attrs)
# Test JSON types
self.assertEqual(attrs['char_field'], obj.char_field)
self.assertEqual(attrs['int_field'], obj.int_field)
self.assertEqual(attrs['bool_field'], obj.bool_field)
# @attention: ForeignKeys are only logged as ids
self.assertEqual(attrs['foreign_key_id'], obj.foreign_key_id)
# Now we'll modify two attributes and see if they show up in the diff
obj.char_field = "test2"
obj.int_field = 2
obj.save()
self.assertEqual(obj.journal.count(), 2)
updated_entry = JournalEntry.objects.last()
self.assertEqual(updated_entry.operation, JournalEntry.OPERATION_CHANGE)
# We've only modified `char_field` and `int_field`, so this is the diff which *should* be computed
# by `get_diff()`:
expected_diff = {
'char_field': ("test", "test2"),
'int_field': (1, 2)
}
self.assertEqual(updated_entry.get_diff(), expected_diff)
[tox]
envlist = py{27,36}-django{18,110,111}-jsonfield{0,1,2}
[testenv]
commands = {envpython} runtests.py
deps =
jsonfield0: jsonfield < 1
jsonfield1: jsonfield >= 1, < 2
jsonfield2: jsonfield >= 2
django18: Django >= 1.8, < 1.9
django110: Django >= 1.10, < 1.11
django111: Django >= 1.11, < 2
[testenv:py36]
basepython=/usr/bin/python3.6
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment