~zfilenet/zfilenet/trunk

« back to all changes in this revision

Viewing changes to pinet/tests.py

  • Committer: Ross Light
  • Date: 2008-11-09 22:36:41 UTC
  • mfrom: (187.1.5 more-unittests)
  • Revision ID: rlight2@gmail.com-20081109223641-fxir6vuczwh5byvg
Added more unit tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
#   tests.py
 
4
#   pinet
 
5
#
 
6
#   Copyright (C) 2008 Ross Light
 
7
#
 
8
#   This file is part of zfilenet.
 
9
#
 
10
#   zfilenet is free software: you can redistribute it and/or modify
 
11
#   it under the terms of the GNU General Public License as published by
 
12
#   the Free Software Foundation, either version 3 of the License, or
 
13
#   (at your option) any later version.
 
14
#
 
15
#   zfilenet is distributed in the hope that it will be useful,
 
16
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
#   GNU General Public License for more details.
 
19
#
 
20
#   You should have received a copy of the GNU General Public License
 
21
#   along with zfilenet.  If not, see <http://www.gnu.org/licenses/>.
 
22
#
 
23
 
 
24
"""pinet unit tests"""
 
25
 
 
26
from __future__ import absolute_import
 
27
 
 
28
from . import dispatch
 
29
 
 
30
__author__ = 'Ross Light'
 
31
__date__ = 'November 9, 2007'
 
32
__all__ = []
 
33
 
 
34
class Handler(object):
 
35
    def __init__(self):
 
36
        self.data = []
 
37
    
 
38
    def my_private_method(self):
 
39
        pass
 
40
    
 
41
    @dispatch.expose()
 
42
    def spam(self, eggs):
 
43
        self.data.append(eggs)
 
44
        return eggs
 
45
 
 
46
def test_method_exposure():
 
47
    """Method publicity test"""
 
48
    handler = Handler()
 
49
    result = dispatch.get_exposed_methods(handler)
 
50
    assert result == {'spam': handler.spam}, "Invalid exposure dictionary"
 
51
 
 
52
def test_dispatcher():
 
53
    """Dispatcher accuracy test"""
 
54
    def _my_great_func(a, b):
 
55
        return a + b
 
56
    handler = Handler()
 
57
    dispatcher = dispatch.Dispatcher()
 
58
    dispatcher.register_function(_my_great_func, 'add')
 
59
    dispatcher.register_object(handler, 'ns')
 
60
    ideal = {'add': (_my_great_func, False),
 
61
             'ns.spam': (handler.spam, False),}
 
62
    assert dispatcher.functions == ideal, "Wrong functions"
 
63
    assert dispatcher.dispatch(None, 'add', (2, 2)) == 4, "Adding invalid"
 
64
    assert dispatcher.dispatch(None, 'add', {'a': 2, 'b': 2}) == 4, \
 
65
        "Keywords invalid"
 
66
    assert dispatcher.dispatch(None, 'ns.spam', ('foo',)) == 'foo', \
 
67
        "Handler not returning"
 
68
    assert handler.data == ['foo'], "Handler not called"
 
69
    try:
 
70
        dispatcher.dispatch(None, 'subtract', ())
 
71
    except dispatch.MethodNotFound:
 
72
        pass
 
73
    else:
 
74
        raise AssertionError("Calling fake methods")