Source

milky / test_milky.py

Full commit
Surgo 2481b6d 








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-

import logging
import datetime
import unittest

import milky
import test_configs as configs

RTM_API_KEY = configs.RTM_API_KEY
RTM_SHARED_SECRET = configs.RTM_SHARED_SECRET
PERMS = milky.PERMS_DELETE

"""Define test configurations in test_configs.py
RTM_API_KEY = '<your api key>'
RTM_SHARED_SECRET = '<your api sig>'

TESTUSER_MAIN = {
    u'fullname': u'<main user fullname>',
    u'username': u'<main user name>',
    u'id': u'<main user id>',
    u'frob': u'',
    u'token': u'',
}
TESTUSER_1 = {
    u'username': u'<some user name>', }
TESTUSER_2 = {
    u'username': u'<some user name>', }
"""

class TestAuthFrob(unittest.TestCase):
    def setUp(self):
        self.rtm = milky.API(RTM_API_KEY, RTM_SHARED_SECRET, 
                PERMS)
        self.assertEqual(self.rtm.frob, None)

    def test_get_auth_url(self):
        import webbrowser
        auth_url = self.rtm.get_auth_url()
        self.assertTrue(self.rtm.frob is not None)
        logging.info('Frob: %s' % self.rtm.frob)
        logging.info('Auth url: %s' % auth_url)
        webbrowser.open(auth_url)

class TestAuthToken(unittest.TestCase):
    def setUp(self):
        self.rtm = milky.API(RTM_API_KEY, RTM_SHARED_SECRET, 
                PERMS, frob=configs.TESTUSER_MAIN['frob'])
        self.token = self.rtm.get_token()
        logging.info('Token: %s' % self.token)

    def test_checkToken(self):
        """rtm.auth.checkToken"""

        auth = self.rtm.auth.checkToken(auth_token=self.rtm.token)
        logging.info('Auth token: %s' % auth.token)
        self.assertEqual(auth.token, self.token)
        logging.info('Auth perms: %s' % auth.perms)
        self.assertEqual(auth.perms, PERMS)
        logging.info('Auth user ID: %s' % auth.user.id)
        self.assertEqual(auth.user.id, configs.TESTUSER_MAIN['id'])
        logging.info('Auth username: %s' % auth.user.username)
        self.assertEqual(auth.user.username, configs.TESTUSER_MAIN['username'])
        logging.info('Auth fullname: %s' % auth.user.fullname)
        self.assertEqual(auth.user.fullname, configs.TESTUSER_MAIN['fullname'])

class TestBase(unittest.TestCase):
    def setUp(self):
        self.rtm = milky.API(RTM_API_KEY, RTM_SHARED_SECRET, 
                PERMS, token=configs.TESTUSER_MAIN['token'])

        # Test account (devtest)
        self.testuser_main = configs.TESTUSER_MAIN

        self.testusers = [
                configs.TESTUSER_1,
                configs.TESTUSER_2]

        # Test groups
        self.groupnames = [u'test', u'テスト', ]
        # Test lists
        self.test_list = u'Milky テスト'
        # Test locations
        self.locationnames = [u'home', u'office']

class TestTest(TestBase):
    def test_echo(self):
        """rtm.test.echo"""

        stat = self.rtm.test.echo()
        logging.info('Echo : %s' % stat)
        self.assertEqual(stat, True)

    def test_login(self):
        """rtm.test.login"""

        user = self.rtm.test.login()
        logging.info('User ID: %s' % user.id)
        self.assertEqual(user.id, self.testuser_main['id'])
        logging.info('Username: %s' % user.username)
        self.assertEqual(user.username, self.testuser_main['username'])

class TestReflection(TestBase):
    def test_get_method_info(self):
        """rtm.reflection.getMethodInfo"""

        method_name = u'rtm.test.login'
        method = self.rtm.reflection.getMethodInfo(method_name=method_name)
        logging.info('Method info: %s' % method.name)
        logging.info('Method description: %s' % method.description)
        self.assertEqual(method.name, method_name)
        logging.info('Method need login: %s' % method.needslogin)
        self.assertEqual(method.needslogin, True)
        logging.info('Method need signing: %s' % method.needssigning)
        self.assertEqual(method.needssigning, True)
        logging.info('Method required perms: %s' % method.requiredperms)
        self.assertEqual(method.requiredperms, True)
        logging.info('Method response: %s' % method.response)
        for argument in method.arguments:
            logging.info('Argument: %s: %s (%s)' % (
                argument.name, argument.description, argument.optional))
        for error in method.errors:
            logging.info('Error: %s %s - %s' % (
                error.code, error.message, error.description))

    def test_get_methods(self):
        """rtm.test.getMethods"""

        methods = self.rtm.reflection.getMethods()
        for method in methods:
            logging.info('Method: %s' % method)
        self.assertTrue(u'rtm.auth.checkToken' in methods)
        self.assertTrue(u'rtm.auth.getFrob' in methods)
        self.assertTrue(u'rtm.auth.getToken' in methods)

class TestTimezones(TestBase):
    def test_getlist(self):
        """rtm.timezones.getList"""
        
        timezones = self.rtm.timezones.getList()
        settings = self.rtm.settings.getList()

        for timezone in timezones:
            logging.info('Timezone: %s %s %s %s %s' % (
                timezone.name, timezone.id, timezone.dst, 
                timezone.offset, timezone.current_offset))
        self.assertTrue(
                settings.timezone in [timezone.name for timezone in timezones])

class TestTime(TestBase):
    def setUp(self):
        super(TestTime, self).setUp()

        timezones = self.rtm.timezones.getList()
        settings = self.rtm.settings.getList()

        for timezone in timezones:
            if timezone.name == settings.timezone:
                self.timezone = timezone

    def test_convert(self):
        utc_now = datetime.datetime.utcnow()
        locale_now = utc_now + datetime.timedelta(0, int(self.timezone.current_offset))

        logging.info('UTC: %s, Locale: %s' % (utc_now, locale_now))
        time = self.rtm.time.convert(to_timezone=self.timezone.name, from_timezone='UTC', 
                time=utc_now.isoformat())
        self.assertEqual(time.timezone, self.timezone.name)
        self.assertEqual(time.time, locale_now.strftime('%Y-%m-%dT%H:%M:%S'))

class TestLocations(TestBase):
    def test_get_locations(self):
        locations = self.rtm.locations.getList()
        for location in self.locationnames:
            self.assertTrue(location in \
                    [location.name for location in locations])

        for location in locations:
            logging.info('Location: %s %d %d %d %.3f %.3f' % (location.name, location.id, 
                location.zoom, location.viewable,
                location.longitude, location.latitude))

class TestSettings(TestBase):
    def test_getlist(self):
        """rtm.settings.getList"""

        settings = self.rtm.settings.getList()
        logging.info('Language: %s' % settings.language)
        self.assertEqual(settings.language, u"ja")
        logging.info('Default list: %s' % settings.defaultlist)
        self.assertTrue(hasattr(settings, "defaultlist"))
        logging.info('Timezone: %s' % settings.timezone)
        self.assertEqual(settings.timezone, u"Asia/Tokyo")
        logging.info('Date format: %s' % settings.dateformat)
        self.assertEqual(settings.dateformat, 1)
        logging.info('Time format: %s' % settings.timeformat)
        self.assertEqual(settings.timeformat, 1)

class TestGroupUser(TestBase):
    def add_contacts(self):
        """rtm.contacts.add"""

        for username in [user['username'] for user in self.testusers]:
            timeline = self.rtm.timelines.create()
            logging.debug(timeline)
            contact = self.rtm.contacts.add(contact=username, timeline=timeline)
            self.assertEqual(contact.username, username)

            logging.info('Contact: %s %s %s' % (
                contact.username, contact.id, contact.fullname))
        self.contacts = self.rtm.contacts.getList()

        self.contacts = self.rtm.contacts.getList()
        for username in [user['username'] for user in self.testusers]:
            self.assertTrue(username in \
                    [contact.username for contact in self.contacts])

    def delete_contacts(self):
        """rtm.contacts.delete"""

        self.contacts = self.rtm.contacts.getList()
        for username in [user['username'] for user in self.testusers]:
            for contact in self.contacts:
                if username == contact.username:
                    logging.info('Delete contact: %s %s' % (
                        username, contact.id))
                    timeline = self.rtm.timelines.create()
                    logging.debug(timeline)
                    self.assertTrue(self.rtm.contacts.delete(
                        contact_id=contact.id, timeline=timeline))

        self.contacts = self.rtm.contacts.getList()
        for username in [user['username'] for user in self.testusers]:
            self.assertTrue(username not in \
                    [contact.username for contact in self.contacts])

    def add_groups(self):
        """rtm.groups.add"""

        for groupname in self.groupnames:
            timeline = self.rtm.timelines.create()
            logging.debug(timeline)
            group = self.rtm.groups.add(group=groupname, timeline=timeline)
            self.assertEqual(group.name, groupname)

            logging.info('Group: %s %s' % (
                group.name, group.id))

        self.groups = self.rtm.groups.getList()
        for groupname in self.groupnames:
            self.assertTrue(groupname in [group.name for group in self.groups])

    def delete_groups(self):
        """rtm.groups.delete"""

        self.groups = self.rtm.groups.getList()
        for groupname in self.groupnames:
            for group in self.groups:
                if groupname == group.name:
                    logging.info('Delete group: %s %s' % (
                        groupname, group.id))
                    timeline = self.rtm.timelines.create()
                    logging.debug(timeline)
                    self.assertTrue(self.rtm.groups.delete(
                        group_id=group.id, timeline=timeline))

        self.groups = self.rtm.groups.getList()
        for groupname in self.groupnames:
            self.assertTrue(groupname not in [group.name for group in self.groups])

    def add_contact_to_group(self):
        """rtm.groups.addContact"""

        group = self.groups[0]
        for contact in self.contacts:
            timeline = self.rtm.timelines.create()
            logging.debug(timeline)
            self.assertTrue(self.rtm.groups.addContact(
                group_id=group.id, contact_id=contact.id,
                timeline=timeline))

        self.groups = self.rtm.groups.getList()
        group = self.groups[0]
        for contact in self.contacts:
            self.assertTrue(contact.id in \
                    [contact.id for contact in group.contacts])

    def remove_contact_from_group(self):
        """rtm.groups.removeContact"""

        group = self.groups[0]
        for contact in self.contacts:
            timeline = self.rtm.timelines.create()
            logging.debug(timeline)
            self.assertTrue(self.rtm.groups.removeContact(
                group_id=group.id, contact_id=contact.id,
                timeline=timeline))

        self.groups = self.rtm.groups.getList()
        group = self.groups[0]
        for contact in self.contacts:
            self.assertTrue(contact.id not in \
                    [contact.id for contact in group.contacts])

    def test(self):
        """rtm.groups, rtm.contacts"""

        # Initialize
        self.delete_contacts()
        self.delete_groups()

        # Add contact & group
        self.add_contacts()
        self.add_groups()

        # Add contact to group
        self.add_contact_to_group()

        # Remove contact from group
        self.remove_contact_from_group()

        # Initialize
        self.delete_contacts()
        self.delete_groups()

class TestListsTasksNotes(TestBase):

    def get_lists(self):
        self.lists = self.rtm.lists.getList()
        for _list in self.lists:
            logging.info('List: %s %s %s %s %s %s %s %s' % (
                _list.name, 
                _list.id, 
                _list.locked, 
                _list.deleted, 
                _list.archived, 
                _list.position, 
                _list.sort_order, 
                _list.smart))

    def add_list(self):
        self.assertTrue(len([_list.id for _list in self.lists \
                if _list.name == self.test_list and not _list.deleted]) == 0)

        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self._list = self.rtm.lists.add(name=self.test_list,
                timeline=timeline)
        logging.debug('Add list %s %s' % (self._list.name, self._list.id))

        # Refresh
        self.lists = self.rtm.lists.getList()
        self.assertTrue(len([_list.id for _list in self.lists \
                if _list.name == self.test_list and not _list.deleted]) == 1)
        self.assertEqual(self._list.name, self.test_list)
        self.assertEqual(self._list.deleted, False)
        self.assertEqual(self._list.archived, False)
        self.assertEqual(self._list.locked, False)

    def delete_list(self):
        for _list in self.lists:
            if _list.name == self.test_list:
                timeline = self.rtm.timelines.create()
                logging.debug(timeline)
                self._list = self.rtm.lists.delete(list_id=_list.id,
                        timeline=timeline)
                logging.debug('Delete list %s %s' % (_list.name, _list.id))
                self.assertEqual(self._list.name, self.test_list)
                self.assertEqual(self._list.deleted, True)

        self.lists = self.rtm.lists.getList()
        self.assertTrue(len([_list.id for _list in self.lists \
                if _list.name == self.test_list and not _list.deleted]) == 0)

    def get_list_id_by_name(self, name):
        for _list in self.lists:
            if _list.name == name:
                return _list.id
        return None

    def default_list(self):
        # Get default list ID from settings.
        settings = self.rtm.settings.getList()
        if not settings.defaultlist:
            self.defaultlist_bakcup = self.get_list_id_by_name(u'Inbox')
        else:
            self.defaultlist_bakcup = settings.defaultlist
        logging.info('Default list: %s' % self.defaultlist_bakcup)

        # Change default list
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.rtm.lists.setDefaultList(list_id=self._list.id,
                timeline=timeline)
        settings = self.rtm.settings.getList()
        self.assertEqual(settings.defaultlist, self._list.id)
        logging.info('Default list: %s' % settings.defaultlist)

        # Back to default list
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.rtm.lists.setDefaultList(list_id=self.defaultlist_bakcup,
                timeline=timeline)
        settings = self.rtm.settings.getList()
        self.assertEqual(settings.defaultlist, self.defaultlist_bakcup)
        logging.info('Default list: %s' % settings.defaultlist)

    def archive_list(self):
        self.assertEqual(self._list.archived, False)

        # Archive list
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self._list = self.rtm.lists.archive(list_id=self._list.id,
                timeline=timeline)
        self.assertEqual(self._list.archived, True)

        # Unarchive list
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self._list = self.rtm.lists.unarchive(list_id=self._list.id,
                timeline=timeline)
        self.assertEqual(self._list.archived, False)

    def set_name_list(self):
        new_test_list = u'New Milky テスト'
        self.assertEqual(self._list.name, self.test_list)
        logging.info('List name: %s' % self._list.name)

        # Set new name
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self._list = self.rtm.lists.setName(list_id=self._list.id,
                name=new_test_list,
                timeline=timeline)
        self.assertEqual(self._list.name, new_test_list)
        logging.info('List name: %s' % self._list.name)

        # Set back old name
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self._list = self.rtm.lists.setName(list_id=self._list.id,
                name=self.test_list,
                timeline=timeline)
        self.assertEqual(self._list.name, self.test_list)
        logging.info('List name: %s' % self._list.name)

    def get_tasks(self):
        self.tasks = self.rtm.tasks.getList()

        logging.info('Tasks: %s' % self.tasks.rev)
        for tasklist in self.tasks.lists:
            logging.info('List: %s' % tasklist.id)
            for taskseries in tasklist.taskseries:
                logging.info('TaskSeries: %s %s %s %s %s %s %s' % (
                    taskseries.name, taskseries.id, 
                    taskseries.url,
                    taskseries.source,
                    taskseries.location_id, 
                    taskseries.created, taskseries.modified))
                for note in taskseries.notes:
                    logging.debug('Note: %s %s %s %s %s' % (
                        note.title, note.text, note.id, 
                        note.created, note.modified))
                for participant in taskseries.participants:
                    logging.debug('Participant: %s %s %s' % (
                        participant.fullname, 
                        participant.id, participant.username))
                logging.debug('Tags: %s' % ', '.join(taskseries.tags))

                for task in taskseries.task:
                    logging.debug('Task: %s %s %s %s %s %s %s %s %s' % (
                        task.id, task.due, task.has_due_time, 
                        task.added, task.deleted, task.completed, 
                        task.priority, task.postponed, task.estimate))

    def add_task(self):
        self.list_tasks = self.rtm.tasks.getList(list_id=self._list.id)
        self.assertEqual(len(self.list_tasks.lists[0].taskseries), 0)
        # Test task 1
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.add(
                list_id=self._list.id,
                name=u'テスト 1 (Normal)',
                timeline=timeline)

        # Test task 2 (Smart Add)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_2 = self.rtm.tasks.add(
                list_id=self._list.id,
                name=u'テスト 2 (Smart Add) ^tomorrow !1 #test @office *weekly =10 min http://twitter.com/Surgo',
                parse=1,
                timeline=timeline)
        self.list_tasks = self.rtm.tasks.getList(list_id=self._list.id)
        self.assertEqual(len(self.list_tasks.lists[0].taskseries), 2)

    def listing_task(self):
        logging.info('List: %s' % self.test_task_1.id)
        self.assertEqual(self.test_task_1.id, self._list.id)

        # Move list to default_list (Maybe Inbox).
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.moveTo(
                from_list_id=self._list.id,
                to_list_id=self.defaultlist_bakcup,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                timeline=timeline)
        logging.info('List: %s' % self.test_task_1.id)
        self.assertEqual(self.test_task_1.id, self.defaultlist_bakcup)

        # Move list to Milky テスト.
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.moveTo(
                from_list_id=self.defaultlist_bakcup,
                to_list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                timeline=timeline)
        logging.info('List: %s' % self.test_task_1.id)
        self.assertEqual(self.test_task_1.id, self._list.id)

    def tagging_task(self):
        # Add tags
        tags = [u'テスト', u'漢字', u'English', u'A', ]
        logging.info('Tags: %s' % ', '.join(self.test_task_1.taskseries[0].tags))
        [self.assertTrue(tag.lower() not in self.test_task_1.taskseries[0].tags) \
                for tag in tags]
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.addTags(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                tags=','.join(tags),
                timeline=timeline)
        logging.info('Tags: %s' % ', '.join(self.test_task_1.taskseries[0].tags))
        [self.assertTrue(tag.lower() in self.test_task_1.taskseries[0].tags) \
                for tag in tags]

        # Remove tags
        tags = [u'漢字', u'A', ]
        remove_tags = [u'テスト', u'English', ]
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.removeTags(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                tags=','.join(remove_tags),
                timeline=timeline)
        logging.info('Tags: %s' % ', '.join(self.test_task_1.taskseries[0].tags))
        [self.assertTrue(tag.lower() not in self.test_task_1.taskseries[0].tags) \
                for tag in remove_tags]
        [self.assertTrue(tag.lower() in self.test_task_1.taskseries[0].tags) \
                for tag in tags]

        # Reset tags
        replace_tags = [u'置換', u'replace', ]
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setTags(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                tags=','.join(replace_tags),
                timeline=timeline)
        logging.info('Tags: %s' % ', '.join(self.test_task_1.taskseries[0].tags))
        [self.assertTrue(tag.lower() not in self.test_task_1.taskseries[0].tags) \
                for tag in tags]
        [self.assertTrue(tag.lower() in self.test_task_1.taskseries[0].tags) \
                for tag in replace_tags]

    def prioritize_task(self):
        # Set priority 3
        logging.info('Priority: %s' % self.test_task_1.taskseries[0].task[0].priority)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].priority is None)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setPriority(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                priority=3,
                timeline=timeline)
        logging.info('Priority: %s' % self.test_task_1.taskseries[0].task[0].priority)
        self.assertEqual(self.test_task_1.taskseries[0].task[0].priority, 3)
        # Move priority 2
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.movePriority(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                direction=u'up',
                timeline=timeline)
        logging.info('Priority: %s' % self.test_task_1.taskseries[0].task[0].priority)
        self.assertEqual(self.test_task_1.taskseries[0].task[0].priority, 2)
        # Move priority 3
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.movePriority(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                direction=u'down',
                timeline=timeline)
        logging.info('Priority: %s' % self.test_task_1.taskseries[0].task[0].priority)
        self.assertEqual(self.test_task_1.taskseries[0].task[0].priority, 3)
        # Unset priority
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setPriority(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                priority=u'N',
                timeline=timeline)
        logging.info('Priority: %s' % self.test_task_1.taskseries[0].task[0].priority)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].priority is None)

    def addinfo_task(self):
        # Change name
        new_name = u'テスト !"#$%&\'()=-~|^\\{}`:*+;_?/>.<,'
        logging.info('Name: %s' % self.test_task_1.taskseries[0].name)
        self.assertEqual(self.test_task_1.taskseries[0].name, u'テスト 1 (Normal)')
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setName(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                name=new_name,
                timeline=timeline)
        logging.info('Name: %s' % self.test_task_1.taskseries[0].name)
        self.assertEqual(self.test_task_1.taskseries[0].name, new_name)

        # Set location
        location = self.rtm.locations.getList()[0]
        logging.info('Set location to: %s (%s)' % (location.name, location.id))
        logging.info('Location: %s' % self.test_task_1.taskseries[0].location_id)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setLocation(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                location_id=location.id,
                timeline=timeline)
        logging.info('Location: %s' % self.test_task_1.taskseries[0].location_id)
        self.assertEqual(self.test_task_1.taskseries[0].location_id, location.id)

        # Set url
        url = u'http://www.rememberthemilk.com/'
        logging.info('Set url to: %s' % url)
        logging.info('URL: %s' % self.test_task_1.taskseries[0].url)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setURL(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                url=url,
                timeline=timeline)
        logging.info('URL: %s' % self.test_task_1.taskseries[0].url)
        self.assertEqual(self.test_task_1.taskseries[0].url, url)

    def scheduling_task(self):
        logging.info('Due date: %s' % self.test_task_1.taskseries[0].task[0].due)
        logging.info('Has due time: %s' % self.test_task_1.taskseries[0].task[0].has_due_time)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].due is None)
        self.assertTrue(not self.test_task_1.taskseries[0].task[0].has_due_time)

        # Set due date
        today = datetime.date.today()
        due_date = datetime.datetime(today.year, today.month, today.day, 0, 0) + datetime.timedelta(days=1)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setDueDate(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                due=due_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                timeline=timeline)
        logging.info('Due date: %s' % self.test_task_1.taskseries[0].task[0].due)
        logging.info('Has due time: %s' % self.test_task_1.taskseries[0].task[0].has_due_time)
        self.assertEqual(due_date, self.test_task_1.taskseries[0].task[0].due)
        self.assertTrue(not self.test_task_1.taskseries[0].task[0].has_due_time)

        # Set due date with due time
        due_date = datetime.datetime(due_date.year, due_date.month, due_date.day, 12, 30)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setDueDate(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                due=due_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                has_due_time=milky.HAS_DUE_TIME,
                timeline=timeline)
        logging.info('Due date: %s' % self.test_task_1.taskseries[0].task[0].due)
        logging.info('Has due time: %s' % self.test_task_1.taskseries[0].task[0].has_due_time)
        self.assertEqual(due_date, self.test_task_1.taskseries[0].task[0].due)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].has_due_time)

        # Postpone
        due_date = due_date + datetime.timedelta(days=1)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.postpone(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                timeline=timeline)
        logging.info('Due date: %s' % self.test_task_1.taskseries[0].task[0].due)
        logging.info('Has due time: %s' % self.test_task_1.taskseries[0].task[0].has_due_time)
        self.assertEqual(due_date, self.test_task_1.taskseries[0].task[0].due)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].has_due_time)

        # Estimate (所要時間)
        logging.info('Estimate: %s' % self.test_task_1.taskseries[0].task[0].estimate)
        estimate='0.5 days'
        delta = datetime.timedelta(days=0.5)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].estimate is None)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setEstimate(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                estimate=estimate,
                timeline=timeline)
        logging.info('Estimate: %s' % self.test_task_1.taskseries[0].task[0].estimate)
        self.assertEqual(delta, self.test_task_1.taskseries[0].task[0].estimate)

        # Recurrence (再起)
        """Recurrence examples:

            Every Tuesday - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1;BYDAY=TU"}
            Every Monday and Wednesday - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE"}
            Every weekday - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR"}
            Every day - {"every":"1","$t":"FREQ=DAILY;INTERVAL=1"}
            Every week - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1"}
            Every 2 weeks - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=2"}
            Every month - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=1"}
            Every 6 months - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=6"}
            Every year - {"every":"1","$t":"FREQ=YEARLY;INTERVAL=1"}
            Every month on the 4th - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=4"}
            Every month on the 3rd Tuesday - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=1;BYDAY=3TU"}
            Every month on the last Monday - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=1;BYDAY=-1MO"}
            Every month on the 2nd last Friday - {"every":"1","$t":"FREQ=MONTHLY;INTERVAL=1;BYDAY=-2FR"}
            Every week until 1/1/2007 - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1;UNTIL=20070101T000000"}
            Every week for 20 times - {"every":"1","$t":"FREQ=WEEKLY;INTERVAL=1;COUNT=20"}
            Every year on the last Monday - Contain bags.
        """
        logging.info('Recurrence: %s' % self.test_task_1.taskseries[0].rrule)
        self.assertTrue(self.test_task_1.taskseries[0].rrule is None)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setRecurrence(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                repeat='Every week for 20 times',
                timeline=timeline)
        logging.info('Recurrence: %s %s %s %s %s %s %s' % (
            self.test_task_1.taskseries[0].rrule.every,
            self.test_task_1.taskseries[0].rrule.freq,
            self.test_task_1.taskseries[0].rrule.interval,
            self.test_task_1.taskseries[0].rrule.byday,
            self.test_task_1.taskseries[0].rrule.bymonthday,
            self.test_task_1.taskseries[0].rrule.until,
            self.test_task_1.taskseries[0].rrule.count,))
        self.assertEqual(self.test_task_1.taskseries[0].rrule.every, 1)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.freq, milky.FREQ_WEEKLY)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.interval, 1)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.byday, None)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.bymonthday, None)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.until, None)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.count, 20)

        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.setRecurrence(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                repeat='Every month on the 3rd Tuesday',
                timeline=timeline)
        logging.info('Recurrence: %s %s %s %s %s %s %s' % (
            self.test_task_1.taskseries[0].rrule.every,
            self.test_task_1.taskseries[0].rrule.freq,
            self.test_task_1.taskseries[0].rrule.interval,
            self.test_task_1.taskseries[0].rrule.byday,
            self.test_task_1.taskseries[0].rrule.bymonthday,
            self.test_task_1.taskseries[0].rrule.until,
            self.test_task_1.taskseries[0].rrule.count,))
        self.assertEqual(self.test_task_1.taskseries[0].rrule.every, 1)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.freq, milky.FREQ_MONTHLY)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.interval, 1)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.byday, u'3TU')
        self.assertEqual(self.test_task_1.taskseries[0].rrule.bymonthday, None)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.until, None)
        self.assertEqual(self.test_task_1.taskseries[0].rrule.count, None)

    def notes_task(self):
        logging.info('Note: %s' % self.test_task_1.taskseries[0].notes)
        self.assertTrue(len(self.test_task_1.taskseries[0].notes) == 0)

        # Add note
        note_title = u"Note test"
        note_text = u"ABC\nDEF\nGHI"
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        note = self.rtm.tasksnotes.add(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                note_title=note_title,
                note_text=note_text,
                timeline=timeline)
        logging.info('Note: %s %s %s %s %s' % (
            note.title,
            note.text,
            note.id,
            note.created,
            note.modified))
        self.assertEqual(note.title, note_title)
        self.assertEqual(note.text, note_text)

        # Edit note
        note_title = u"ノートテスト"
        note_text = u"あいうえお\nかきくけこ\nさしすせそ"
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        note = self.rtm.tasksnotes.edit(
                note_id=note.id,
                note_title=note_title,
                note_text=note_text,
                timeline=timeline)
        print note
        logging.info('Note: %s %s %s %s %s' % (
            note.title,
            note.text,
            note.id,
            note.created,
            note.modified))
        self.assertEqual(note.title, note_title)
        self.assertEqual(note.text, note_text)

        # Delete note
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.assertTrue(self.rtm.tasksnotes.delete(
                note_id=note.id,
                timeline=timeline))

    def completion_task(self):
        # Complete
        logging.info('Completed: %s' % self.test_task_1.taskseries[0].task[0].completed)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].completed is None)
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.complete(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                timeline=timeline)
        logging.info('Completed: %s' % self.test_task_1.taskseries[0].task[0].completed)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].completed is not None)

        # Uncomplete
        timeline = self.rtm.timelines.create()
        logging.debug(timeline)
        self.test_task_1 = self.rtm.tasks.uncomplete(
                list_id=self._list.id,
                taskseries_id=self.test_task_1.taskseries[0].id,
                task_id=self.test_task_1.taskseries[0].task[0].id,
                timeline=timeline)
        logging.info('Completed: %s' % self.test_task_1.taskseries[0].task[0].completed)
        self.assertTrue(self.test_task_1.taskseries[0].task[0].completed is None)

    def delete_tasks(self):
        pass

    def test(self):
        # Get lists and tasks
        self.get_lists()
        self.get_tasks()

        # Initialize
        self.delete_list()
        self.delete_tasks()

        # For list
        self.add_list()
        self.default_list()
        self.archive_list()
        self.set_name_list()

        # For tasks
        self.add_task()
        self.listing_task()
        self.tagging_task()
        self.prioritize_task()
        self.addinfo_task()
        self.scheduling_task()
        self.completion_task()
        self.notes_task()

        # Initialize
        self.delete_tasks()
        self.delete_list()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO, filename='test.log')

    TEST_CASES = [
            # TestAuthFrob,
            # TestAuthToken,
            TestTest,
            TestReflection,
            TestTimezones,
            # TestTime, # Not worked?
            TestLocations,
            TestSettings,
            TestGroupUser,
            TestListsTasksNotes,
            ]
    for test_cls in TEST_CASES:
        testsuite = unittest.TestLoader().loadTestsFromTestCase(test_cls)
        unittest.TextTestRunner(verbosity=2).run(testsuite)