test_models.py 1.59 KB
Newer Older
1
from django.test import TestCase
2 3 4 5 6 7

from mock import Mock

from ..models import (
    InstanceTemplate, Instance, pre_state_changed, post_state_changed
)
8 9 10


class TemplateTestCase(TestCase):
11

12
    def test_template_creation(self):
13 14
        template = InstanceTemplate(name='My first template',
                                    access_method='ssh', )
Őry Máté committed
15
        template.clean()
16
        # TODO add images & net
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53


class InstanceTestCase(TestCase):

    def test_pre_state_changed_w_exception(self):
        """Signal handler of pre_state_changed prevents save with Exception."""
        def callback(sender, new_state, **kwargs):
            if new_state == 'invalid value':
                raise Exception()
        pre_state_changed.connect(callback)
        i = Instance(state='NOSTATE')
        i.save = Mock()
        i.state_changed('invalid value')
        assert i.state == 'NOSTATE'
        assert not i.save.called

    def test_pre_state_changed_wo_exception(self):
        """Signal handler of pre_state_changed allows save."""
        mock = Mock()
        pre_state_changed.connect(mock)
        i = Instance(state='NOSTATE')
        i.save = Mock()
        i.state_changed('RUNNING')
        assert i.state == 'RUNNING'
        assert mock.called
        assert i.save.called

    def test_post_state_changed(self):
        """Signal handler of post_state_changed runs."""
        mock = Mock()
        post_state_changed.connect(mock)
        i = Instance(state='NOSTATE')
        i.save = Mock()
        i.state_changed('RUNNING')
        assert mock.called
        assert i.save.called
        assert i.state == 'RUNNING'