1
1
import unittest
2
- import sys
2
+
3
3
from datetime import datetime as dt , date , time
4
+
4
5
from odml import Property , Section , Document
5
6
from odml .tools .xmlparser import XMLReader , XMLWriter
6
7
8
+ try :
9
+ unicode = unicode
10
+ except NameError :
11
+ unicode = str
12
+
7
13
8
14
class TestInferType (unittest .TestCase ):
9
15
10
16
def test_string (self ):
11
- p = Property ("test" , value = "somestring" )
12
- assert (p .dtype == "string" )
13
- if sys .version_info < (3 , 0 ):
14
- assert isinstance (p .values [0 ], unicode )
15
- else :
16
- assert isinstance (p .values [0 ], str )
17
+ prop = Property ("test" , value = "some_string" )
18
+ self .assertEqual (prop .dtype , "string" )
19
+ self .assertIsInstance (prop .values [0 ], unicode )
17
20
18
21
def test_text (self ):
19
- p = Property ("test" , value = "some\n string" )
20
- assert (p .dtype == "text" )
21
- if sys .version_info < (3 , 0 ):
22
- assert isinstance (p .values [0 ], unicode )
23
- else :
24
- assert isinstance (p .values [0 ], str )
22
+ prop = Property ("test" , value = "some\n string" )
23
+ self .assertEqual (prop .dtype , "text" )
24
+ self .assertIsInstance (prop .values [0 ], unicode )
25
25
26
26
def test_int (self ):
27
- p = Property ("test" , value = 111 )
28
- assert ( p .dtype == "int" )
29
- assert isinstance ( p .values [0 ], int )
27
+ prop = Property ("test" , value = 111 )
28
+ self . assertEqual ( prop .dtype , "int" )
29
+ self . assertIsInstance ( prop .values [0 ], int )
30
30
31
31
def test_float (self ):
32
- p = Property ("test" , value = 3.14 )
33
- assert ( p .dtype == "float" )
34
- assert isinstance ( p .values [0 ], float )
32
+ prop = Property ("test" , value = 3.14 )
33
+ self . assertEqual ( prop .dtype , "float" )
34
+ self . assertIsInstance ( prop .values [0 ], float )
35
35
36
36
def test_datetime (self ):
37
- p = Property ("test" , value = dt .now ())
38
- assert ( p .dtype == "datetime" )
39
- assert isinstance ( p .values [0 ], dt )
37
+ prop = Property ("test" , value = dt .now ())
38
+ self . assertEqual ( prop .dtype , "datetime" )
39
+ self . assertIsInstance ( prop .values [0 ], dt )
40
40
41
41
def test_date (self ):
42
- p = Property ("test" , dt .now ().date ())
43
- assert ( p .dtype == "date" )
44
- assert isinstance ( p .values [0 ], date )
42
+ prop = Property ("test" , dt .now ().date ())
43
+ self . assertEqual ( prop .dtype , "date" )
44
+ self . assertIsInstance ( prop .values [0 ], date )
45
45
46
46
def test_time (self ):
47
- p = Property ("test" , value = dt .now ().time ())
48
- assert ( p .dtype == "time" )
49
- assert isinstance ( p .values [0 ], time )
47
+ prop = Property ("test" , value = dt .now ().time ())
48
+ self . assertEqual ( prop .dtype , "time" )
49
+ self . assertIsInstance ( prop .values [0 ], time )
50
50
51
51
def test_boolean (self ):
52
- p = Property ("test" , True )
53
- assert ( p .dtype == "boolean" )
54
- assert isinstance ( p .values [0 ], bool )
52
+ prop = Property ("test" , True )
53
+ self . assertEqual ( prop .dtype , "boolean" )
54
+ self . assertIsInstance ( prop .values [0 ], bool )
55
55
56
- p = Property ("test" , False )
57
- assert ( p .dtype == "boolean" )
58
- assert isinstance ( p .values [0 ], bool )
56
+ prop = Property ("test" , False )
57
+ self . assertEqual ( prop .dtype , "boolean" )
58
+ self . assertIsInstance ( prop .values [0 ], bool )
59
59
60
60
def test_read_write (self ):
61
61
doc = Document ("author" )
62
- sec = Section ("name" , "type" )
63
- doc . append ( sec )
62
+ sec = Section ("name" , "type" , parent = doc )
63
+
64
64
sec .append (Property ("strprop" , "somestring" ))
65
65
sec .append (Property ("txtprop" , "some\n text" ))
66
66
sec .append (Property ("intprop" , 200 ))
@@ -69,47 +69,40 @@ def test_read_write(self):
69
69
sec .append (Property ("dateprop" , dt .now ().date ()))
70
70
sec .append (Property ("timeprop" , dt .now ().time ()))
71
71
sec .append (Property ("boolprop" , True ))
72
- if sys .version_info < (3 , 0 ):
73
- str_doc = unicode (XMLWriter (doc ))
74
- else :
75
- str_doc = str (XMLWriter (doc ))
72
+
73
+ str_doc = unicode (XMLWriter (doc ))
74
+
76
75
new_doc = XMLReader ().from_string (str_doc )
77
76
new_sec = new_doc .sections [0 ]
78
77
79
- p = new_sec .properties ["strprop" ]
80
- assert (p .dtype == "string" )
81
- if sys .version_info < (3 , 0 ):
82
- assert isinstance (p .values [0 ], unicode )
83
- else :
84
- assert isinstance (p .values [0 ], str )
85
-
86
- p = new_sec .properties ["txtprop" ]
87
- assert (p .dtype == "text" )
88
- if sys .version_info < (3 , 0 ):
89
- assert isinstance (p .values [0 ], unicode )
90
- else :
91
- assert isinstance (p .values [0 ], str )
92
-
93
- p = new_sec .properties ["intprop" ]
94
- assert (p .dtype == "int" )
95
- assert isinstance (p .values [0 ], int )
96
-
97
- p = new_sec .properties ["floatprop" ]
98
- assert (p .dtype == "float" )
99
- assert isinstance (p .values [0 ], float )
100
-
101
- p = new_sec .properties ["datetimeprop" ]
102
- assert (p .dtype == "datetime" )
103
- assert isinstance (p .values [0 ], dt )
104
-
105
- p = new_sec .properties ["dateprop" ]
106
- assert (p .dtype == "date" )
107
- assert isinstance (p .values [0 ], date )
108
-
109
- p = new_sec .properties ["timeprop" ]
110
- assert (p .dtype == "time" )
111
- assert isinstance (p .values [0 ], time )
112
-
113
- p = new_sec .properties ["boolprop" ]
114
- assert (p .dtype == "boolean" )
115
- assert isinstance (p .values [0 ], bool )
78
+ prop = new_sec .properties ["strprop" ]
79
+ self .assertEqual (prop .dtype , "string" )
80
+ self .assertIsInstance (prop .values [0 ], unicode )
81
+
82
+ prop = new_sec .properties ["txtprop" ]
83
+ self .assertEqual (prop .dtype , "text" )
84
+ self .assertIsInstance (prop .values [0 ], unicode )
85
+
86
+ prop = new_sec .properties ["intprop" ]
87
+ self .assertEqual (prop .dtype , "int" )
88
+ self .assertIsInstance (prop .values [0 ], int )
89
+
90
+ prop = new_sec .properties ["floatprop" ]
91
+ self .assertEqual (prop .dtype , "float" )
92
+ self .assertIsInstance (prop .values [0 ], float )
93
+
94
+ prop = new_sec .properties ["datetimeprop" ]
95
+ self .assertEqual (prop .dtype , "datetime" )
96
+ self .assertIsInstance (prop .values [0 ], dt )
97
+
98
+ prop = new_sec .properties ["dateprop" ]
99
+ self .assertEqual (prop .dtype , "date" )
100
+ self .assertIsInstance (prop .values [0 ], date )
101
+
102
+ prop = new_sec .properties ["timeprop" ]
103
+ self .assertEqual (prop .dtype , "time" )
104
+ self .assertIsInstance (prop .values [0 ], time )
105
+
106
+ prop = new_sec .properties ["boolprop" ]
107
+ self .assertEqual (prop .dtype , "boolean" )
108
+ self .assertIsInstance (prop .values [0 ], bool )
0 commit comments