~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to lib/test/schematest.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import datetime
 
2
import os
 
3
import time
 
4
import unittest
 
5
 
 
6
from miro import schema
 
7
from miro.schema import (SchemaString, SchemaInt, SchemaFloat, SchemaBool,
 
8
                         SchemaDateTime, SchemaList, SchemaDict, SchemaObject,
 
9
                         SchemaReprContainer, ValidationError)
 
10
from miro.test.framework import MiroTestCase
 
11
 
 
12
class TestValidation(MiroTestCase):
 
13
    def test_module_variables_defined(self):
 
14
        self.assert_(hasattr(schema, 'VERSION'))
 
15
        self.assert_(hasattr(schema, 'object_schemas'))
 
16
 
 
17
    def test_none_validation(self):
 
18
        self.assertRaises(ValidationError, SchemaInt(noneOk=False).validate,
 
19
                          None)
 
20
        self.assertRaises(ValidationError, SchemaInt().validate, None)
 
21
        SchemaInt(noneOk=True).validate(None)
 
22
 
 
23
    def test_bool_validation(self):
 
24
        schemabool = SchemaBool()
 
25
        self.assertRaises(ValidationError, schemabool.validate, 1)
 
26
        self.assertRaises(ValidationError, schemabool.validate, 0)
 
27
        self.assertRaises(ValidationError, schemabool.validate, "True")
 
28
        self.assertRaises(ValidationError, schemabool.validate, None)
 
29
        schemabool.validate(True)
 
30
        schemabool.validate(False)
 
31
 
 
32
    def test_date_time_validation(self):
 
33
        schemadatetime = SchemaDateTime()
 
34
        self.assertRaises(ValidationError, schemadatetime.validate, 1)
 
35
        self.assertRaises(ValidationError, schemadatetime.validate, 0)
 
36
        delta = datetime.timedelta(days=40)
 
37
        self.assertRaises(ValidationError, schemadatetime.validate, delta)
 
38
        schemadatetime.validate(datetime.datetime(1980, 8, 1))
 
39
 
 
40
    def test_int_validation(self):
 
41
        schemaint = SchemaInt()
 
42
        self.assertRaises(ValidationError, schemaint.validate, "One")
 
43
        self.assertRaises(ValidationError, schemaint.validate, 1.4)
 
44
        schemaint.validate(1)
 
45
        schemaint.validate(1L)
 
46
 
 
47
    def test_float_validation(self):
 
48
        schemafloat = SchemaFloat()
 
49
        self.assertRaises(ValidationError, schemafloat.validate, "One half")
 
50
        self.assertRaises(ValidationError, schemafloat.validate, 1)
 
51
        schemafloat.validate(1.4)
 
52
 
 
53
    def test_string_validation(self):
 
54
        schemastring = SchemaString()
 
55
        self.assertRaises(ValidationError, schemastring.validate, 10123)
 
56
        self.assertRaises(ValidationError, schemastring.validate, "10123")
 
57
        schemastring.validate(u"10123")
 
58
 
 
59
    def test_repr_container_validatoin(self):
 
60
        schemasimple = SchemaReprContainer()
 
61
        schemasimple.validate({1: u"Ben", u"pie": 3.1415})
 
62
        schemasimple.validate([1, 1, u"two", u"three", 5])
 
63
        schemasimple.validate({u'y2k': datetime.datetime(2000, 1, 1),
 
64
                               'now': time.localtime()})
 
65
        schemasimple.validate({
 
66
            'fib': (1, 1, u"two", u"three", 5),
 
67
            'square': (1, 4, u"nine", 16),
 
68
            'fact': (1, 2.0, 6, u"twenty-four"),
 
69
            })
 
70
        # make sure circular references doesn't screw it up
 
71
        l = []
 
72
        d = {}
 
73
        l.extend([l, d])
 
74
        d['list'] = l
 
75
        schemasimple.validate(l)
 
76
        schemasimple.validate(d)
 
77
 
 
78
        class TestObject(object):
 
79
            pass
 
80
        self.assertRaises(ValidationError, schemasimple.validate,
 
81
                          TestObject())
 
82
        self.assertRaises(ValidationError, schemasimple.validate,
 
83
                          [TestObject()])
 
84
        self.assertRaises(ValidationError, schemasimple.validate, 
 
85
                          {'object': TestObject()})
 
86
 
 
87
    def test_list_validation(self):
 
88
        schemalist = SchemaList(SchemaInt())
 
89
        self.assertRaises(ValidationError, schemalist.validate,
 
90
                          1234)
 
91
        schemalist.validate([1, 2, 3, 4])
 
92
 
 
93
    def test_dict_validation(self):
 
94
        schemadict = SchemaDict(SchemaInt(), SchemaString())
 
95
        self.assertRaises(ValidationError, schemadict.validate,
 
96
                1234)
 
97
        schemadict.validate({12: u"Buckle my shoe"})
 
98
 
 
99
    def test_object_validation(self):
 
100
        class TestObject(object):
 
101
            pass
 
102
        class ChildObject(TestObject):
 
103
            pass
 
104
 
 
105
        schemaobject = SchemaObject(TestObject)
 
106
        self.assertRaises(ValidationError, schemaobject.validate, 1234)
 
107
        schemaobject.validate(TestObject())
 
108
        # child objects should work
 
109
        schemaobject.validate(ChildObject())
 
110
        # the actual class object shouldn't
 
111
        self.assertRaises(ValidationError, schemaobject.validate, TestObject)
 
112
 
 
113
if __name__ == '__main__':
 
114
    unittest.main()