Imported Upstream version 0.48
[platform/upstream/libical.git] / src / python / test.py
1 #!/usr/bin/env python 
2 # -*- Mode: python -*-
3 #======================================================================
4 # FILE: test.py
5 # CREATOR: eric 
6 #
7 # DESCRIPTION:
8 #   
9 #
10 #  $Id: test.py,v 1.24 2002-10-24 13:44:31 acampi Exp $
11 #  $Locker:  $
12 #
13 # (C) COPYRIGHT 2001, Eric Busboom <eric@softwarestudio.org>
14 # (C) COPYRIGHT 2001, Patrick Lewis <plewis@inetarena.com>  
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of either: 
18 #
19 #    The LGPL as published by the Free Software Foundation, version
20 #    2.1, available at: http://www.fsf.org/copyleft/lesser.html
21 #
22 #  Or:
23 #
24 #    The Mozilla Public License Version 1.0. You may obtain a copy of
25 #    the License at http://www.mozilla.org/MPL/
26 #======================================================================
27
28 import LibicalWrap
29 from Libical import *
30
31 def error_type():
32     error = icalerror_perror()
33     return error[:index(error,':')]
34
35 comp_str = """
36 BEGIN:VCALENDAR
37 VERSION:2.0
38 PRODID:-//ABC Corporation//NONSGML My Product//EN
39 METHOD:REQUEST
40 BEGIN:VEVENT
41 ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com
42 COMMENT: When in the course of writting comments and nonsense text\, it 
43  becomes necessary to insert a newline
44 DTSTART:19972512T120000
45 DTSTART:19970101T120000Z
46 DTSTART:19970101
47 DURATION:P3DT4H25M
48 FREEBUSY:19970101T120000/19970101T120000
49 FREEBUSY:19970101T120000/PT3H
50 FREEBUSY:19970101T120000/PT3H
51 END:VEVENT
52 END:VCALENDAR"""
53
54
55 def test_property():
56
57     print "--------------------------- Test Property ----------------------"
58
59
60     liw = LibicalWrap
61     icalprop = liw.icalproperty_new_from_string("ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;CUTYPE=GROUP:MAILTO:employee-A@host.com")
62
63     print liw.icalproperty_as_ical_string(icalprop)
64
65     p = Property(ref=icalprop)
66
67     print p.name()
68     print
69     print "Parameters:"
70     for param in p.parameters():
71             print "  ", param, " = ", p[param]
72     print
73     print p['ROLE']
74     
75     p['ROLE'] = 'INDIVIDUAL'
76
77     print p['ROLE']
78
79     p['ROLE'] = 'GROFROMBLATZ'
80
81     print p['ROLE']
82
83     print
84
85     p['X-MAN-FAVOURITE'] = 'Wolverine'
86     p['X-FILES-FAVOURITE'] = 'Mulder'
87
88     print p['X-MAN-FAVOURITE']
89
90     assert(p['X-MAN-FAVOURITE'] == 'Wolverine')
91     assert(p['X-FILES-FAVOURITE'] == 'Mulder')
92     assert(p['X-FILES-FAVOURITE'] != 'Scully')
93
94     print p.value()
95     p.value("mailto:Bob@bob.com")
96     print p.value()
97
98
99     print p.as_ical_string()
100     del p['ROLE']
101     del p['X-MAN-FAVOURITE']
102     
103     print p.as_ical_string()
104
105
106     try:
107         p = Property()
108     except Property.ConstructorFailedError:
109         pass
110     else:
111         assert(0)
112
113     # X Property
114     p = Property("X-COMMENT")
115
116     p.value("This is a sentence, with punctuation; indeed: it is")
117     print p
118
119     p.value("This is not approved by the Ministry of Silly Walks")
120     print p
121
122
123     assert(test_enum('METHOD','PUBLISH'))
124     assert(not test_enum('METHOD','FOO'))
125
126     assert(test_enum('ACTION','AUDIO'))
127     assert(not test_enum('ACTION','OPAQUE'))
128
129 def test_time():
130     "Test routine"
131
132     print"-------------------Test Time  --------------------------------"
133
134     t = Time("19970325T123010Z",'DTSTART')
135     
136     assert(t.year == 1997)
137     assert(t.month == 3)
138     assert(t.day == 25)
139     assert(t.hour == 12)
140     assert(t.minute == 30)
141     assert(t.second == 10)
142     assert(t.is_utc())
143     assert(not t.is_date())
144     
145     print t
146
147     t.timezone("America/Los_Angeles")
148     print str(t)
149     print t.timezone()
150     #assert(str(t)=='DTSTART;TZID=America/Los_Angeles:19970325T123010')
151     assert(str(t)=='DTSTART;TZID=/freeassociation.sourceforge.net/Tzfile/America/Los_Angeles:19970325T053010')
152
153     t.second = t.second+80
154
155     t.timezone("UTC")
156     print t.minute, t.second
157     assert(t.minute == 31)
158     assert(t.second == 30)
159
160     d = Duration(3600,"DURATION")
161     t2 = t + d
162
163     print t2
164     assert(t2.hour == 13)
165
166     t2 = t - d
167
168     print t2
169     assert(isinstance(t2,Time))
170     assert(t2.hour == 11)
171
172     # test int args
173     t = Time(2)
174     print t
175  
176     # test float args
177     t = Time(2.5)
178     print t
179
180 def test_period():    
181
182     print"-------------------Test Period--------------------------------"
183
184     p = Period("19970101T180000Z/19970101T233000Z")
185
186     print p
187     
188     assert(str(p) == 'FREEBUSY:19970101T180000Z/19970101T233000Z')
189
190     print p.start()
191     assert(str(p.start()) == 'DTSTART:19970101T180000Z')
192
193     print p.end()
194     assert(str(p.end()) == 'DTEND:19970101T233000Z')
195
196     print p.duration()
197     assert(str(p.duration()) == 'DURATION:PT5H30M')
198     p = None
199
200     p = Period("19970101T180000Z/PT5H30M")
201     print p
202
203     print p.start()
204     assert(str(p.start()) == 'DTSTART:19970101T180000Z')
205
206     print p.end()
207     assert(str(p.end()) == 'DTEND:19970101T233000Z')
208
209     print p.duration()
210     assert(str(p.duration()) == 'DURATION:PT5H30M')
211
212
213 def test_duration():
214
215     print "-------------- Test Duration ----------------"
216
217     # Ical string
218
219     d = Duration("P3DT4H25M")
220
221     print str(d)
222
223     assert(str(d) == "DURATION:P3DT4H25M")
224
225     print d.seconds()
226
227     assert(d.seconds() == 275100)
228
229     # seconds
230
231     d = Duration(-275100)
232            
233     print str(d)
234
235     assert(str(d) == "DURATION:-P3DT4H25M")
236
237     print d.seconds()
238
239     assert(d.seconds() == -275100)
240
241     #error
242
243     try:
244         d = Duration("P10WT7M")
245         print str(d)
246         assert(0)
247     except: pass
248
249     try:
250         d = Duration("Pgiberish")
251         print str(d)
252         assert(0)
253     except:
254         pass
255
256
257
258 def test_attach():
259
260     file = open('littlefile.txt')
261     attachProp = Attach(file)
262     file.close()
263     attachProp.fmttype('text/ascii')
264     print "\n" + attachProp.name()
265     print attachProp.value_type()
266     print attachProp.fmttype()
267     attachProp['fmttype']=None
268     print "Calling value()"
269     print attachProp.value()
270     print "Calling asIcalString()"
271     print attachProp.as_ical_string()
272
273
274 def test_component():
275
276     print "------------------- Test Component ----------------------"
277
278
279     c = NewComponent(comp_str);
280
281     props = c.properties()
282     
283     for p in props: 
284         print p.as_ical_string()
285     
286     inner = c.components()[0]
287
288     print inner
289     print type(inner)
290
291
292     props = inner.properties()
293     
294     for p in props: 
295         print p.as_ical_string()
296         
297     dtstart = inner.properties('DTSTART')[0]
298         
299     print dtstart
300     
301     print "\n Orig hour: ", dtstart.hour
302     assert(dtstart.hour == 12)
303
304     dtstart.hour = dtstart.hour + 5
305
306     print "\n New hour: ", dtstart.hour
307     assert(dtstart.hour == 17)
308
309     attendee = inner.properties('ATTENDEE')[0]
310     
311     print attendee
312
313     t = Time("20011111T123030")
314     t.name('DTEND')
315
316     inner.add_property(t)
317
318
319     print c
320
321     dtstart1 = inner.properties('DTSTART')[0]
322     dtstart2 = inner.properties('DTSTART')[0]
323     dtstart3 = inner.property('DTSTART')
324
325     assert(dtstart1 is dtstart2)
326     assert(dtstart1 == dtstart2)
327
328     assert(dtstart1 is dtstart3)
329     assert(dtstart1 == dtstart3)
330
331
332     p = Property(type="SUMMARY");
333     p.value("This is a summary")
334
335     inner.properties().append(p)
336
337     print inner.as_ical_string()
338
339     p = inner.properties("SUMMARY")[0]
340     assert(p!=None);
341     print str(p)
342     assert(str(p) == "SUMMARY:This is a summary")
343
344     inner.properties()[:] = [p]
345
346     print inner.as_ical_string()
347
348     # test sequence 
349     event = Event()
350
351     try:
352        event.sequence("foo")
353     except TypeError:
354        pass
355
356     event.sequence(-1)
357     print event.sequence()
358
359     event.sequence(1)
360     event.sequence(88)
361     print event.sequence()
362     
363 def test_event():
364     print "------------ Event Class ----------------------"
365
366     event = Event()
367
368     event.method('REQUEST')
369     event.version('2.0')
370
371     event.created("20010313T123000Z")
372     print "created =", event.created()
373     assert (event.created() == Time("20010313T123000Z"))
374
375     event.organizer("MAILTO:j_doe@nowhere.com")
376     org = event.organizer()
377     print org.cn()
378     org.cn('Jane Doe')
379     assert (isinstance(org, Organizer))
380     print "organizer =", event.organizer()
381     assert (event.organizer().value() == "MAILTO:j_doe@nowhere.com")
382
383     event.dtstart("20010401T183000Z")
384     print "dtstart =", event.dtstart()
385     assert (event.dtstart()== Time("20010401T183000Z"))
386
387     dtend = Time('20010401T190000Z', 'DTEND')
388     event.dtend(dtend)
389     assert (event.dtend() ==dtend )
390     assert (event.dtend() == Time('20010401T190000Z'))
391     
392     att = Attendee()
393     att.value('jsmith@nothere.com')
394     event.attendees(('ef_hutton@listenup.com', att))
395     
396     event.x_properties('X-TEST',('foo', 'bar'))
397     event.x_properties('X-TEST2',('foo, biz', 'bar, biz'))
398
399     inner = event.components()[0]
400     for e in inner.properties('X-TEST'):
401         print " ", e.as_ical_string()
402
403     assert(len(event.x_properties('X-TEST'))==2)
404
405     event.description("A short description.  Longer ones break things. Really. What does it break. The code is supposed to handle realy long lines, longer, in fact, than any sane person would create except by writting a random text generator or by excerpting text from a less sane person. Actually, it did \"break\" and I had to remove an \n assert to fix it.")
406     event.status('TeNtAtIvE')
407     
408     print event.as_ical_string()
409
410     
411 def test_derivedprop():
412     
413     print "------------ Derived Properties -----------------"
414     
415     p = RDate("20011111T123030")
416
417     print str(p)
418
419
420     p = RDate("19970101T120000/19970101T123000")
421
422     print str(p)
423
424     try:
425         p = RDate("P3DT4H25M")
426         print str(p)
427         assert(0)
428     except: pass
429
430     
431     p = Trigger("P3DT4H25M")
432
433     print str(p)
434
435     p = Trigger("20011111T123030")
436
437     print str(p)
438
439     try:
440         p = Trigger("19970101T120000/19970101T123000")
441         print str(p)
442         assert(0)
443     except: pass
444
445 def test_gauge():
446     print "------------ Gauge -----------------"
447     event = Event()
448
449     event.method('REQUEST')
450     event.version('2.0')
451     event.created("20010313T123000Z")
452     event.organizer("MAILTO:j_doe@nowhere.com")
453     org = event.organizer()
454     org.cn('Jane Doe')
455     event.dtstart("20010401T183000Z")
456     dtend = Time('20010401T190000Z', 'DTEND')
457     event.dtend(dtend)
458     event.description("A short description.")
459     event.status('TeNtAtIvE')
460     
461     print event.as_ical_string()
462
463     gauge = Gauge(sql="SELECT * FROM VEVENT WHERE DTSTART > '20010401T180000Z'")
464
465     assert(gauge.compare(event) == 1)
466
467     gauge = Gauge(sql="SELECT * FROM VEVENT WHERE DTSTART > '20010401T190000Z'")
468
469     assert(gauge.compare(event) == 0)
470
471 def do_test_store(storeobj=None, *args):
472     assert(storeobj != None)
473     store = storeobj(*args)
474     assert(store != None)
475
476     print ">------------ ",
477     print store.__class__,
478     print "Store -----------------"
479
480
481     # create fileset
482
483     event = Event()
484
485     event.method('REQUEST')
486     event.version('2.0')
487     event.created("20010313T123000Z")
488     event.organizer("MAILTO:j_doe@nowhere.com")
489     event.dtstart("20010401T183000Z")
490     event.duration('PT3H')
491
492     event.description("A short description.")
493
494     # for i = 1 to 10
495     #  copy event
496     #  munge uid and increment month
497     for i in range(1,11):
498         newevent = event.clone()
499         newevent.uid("%d@localhost" % (i,))
500         newevent.dtstart().month = newevent.dtstart().month + i
501
502         #print ne
503         store.add_component(newevent)
504
505     # commit
506     store.commit()
507     assert(store.count_components("VCALENDAR") == 10)
508     # free
509     del(store)
510
511     # open again
512     store = storeobj(*args)
513     # assert count of components = 10
514     assert(store.count_components("VCALENDAR") == 10)
515
516     # print them out
517     # fetch by uid
518     n7 = store.fetch("7@localhost")
519     print n7
520     # fetch by match
521
522     n7m = store.fetchMatch(n7)
523     assert(str(n7) == str(n7m))
524
525     # modify in memory
526     n7.uid("42@localhost")
527     del(store)
528     del(n7)
529
530     store = storeobj(*args)
531     assert(store.fetch("42@localhost") == None)
532     n7 = store.fetch("7@localhost")
533     n7.uid("42@localhost")
534     store.mark()
535     store.commit()
536     del(store)
537     store = storeobj(*args)
538     assert(store.fetch("7@localhost") == None)
539
540     # fetch by gauge
541
542     gauge = Gauge(sql="SELECT * FROM VEVENT WHERE DTSTART > '20010601T000000Z' AND DTSTART < '20010901T000000Z'")
543
544     store.select(gauge)
545
546     count = 0
547
548     c = store.first_component()
549     while c != None:
550             print c.uid()
551             print c.dtstart()
552             print
553             count = count + 1
554             c = store.next_component()
555
556     store.clearSelect()
557
558     assert(count == 3)
559
560     # remove all of them
561     c = store.first_component()
562     while c != None:
563             print c.uid()
564             store.remove_component(c)
565             c = store.first_component()
566
567     assert(store.count_components("VCALENDAR") == 0)
568     store.commit()
569     assert(store.count_components("VCALENDAR") == 0)
570     # print them out
571     # assert count of components = 0
572
573
574 def test_store():
575     print "------------ Store -----------------"
576     do_test_store(FileStore,"filesetout.ics")
577
578 def run_tests():
579     print "Running unit tests for:", ICAL_PACKAGE, ICAL_VERSION
580
581     test_property()
582
583     test_time()
584
585     test_period()
586
587     test_component()
588
589     test_duration()
590
591     test_derivedprop()
592
593     test_event()
594
595     #test_attach()
596
597     test_gauge()
598     
599     test_store()
600
601
602
603
604 if __name__ == "__main__":
605     run_tests()
606