summaryrefslogtreecommitdiffstats
path: root/tests/test_lps_gen.py
blob: bb8c519e29c05aaa0b0307200bb5500c90a632ac (plain) (blame)
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# -*- coding: utf-8 -*-
#
#   SPDX-License-Identifier: CC0-1.0
#
#   This file is part of lpschedule-generator.
#


import json
import os

import mistune
import mock

from collections import OrderedDict
from os import path
from io import StringIO

from bs4 import BeautifulSoup
from icalendar import vCalAddress, vText, vDatetime
from nose.tools import *
from pytz import timezone

from lps_gen import (read_file, write_file, json_write, json_read,
                     json_write, template_read, LPiCal, LPSRenderer,
                     LPSpeakersRenderer, LPSMarkdown,
                     LPSpeakersMarkdown, RenderHTML)


class TestJSONUtils(object):
    """Class that tests json utils in `lps_gen` module.
    """
    @classmethod
    def setup_class(self):
        """Runs before running any tests in this class."""
        self.speakers_ids = OrderedDict({
            'Daniel Kahn Gillmor': 'gillmor',
            'Edward Snowden': 'snowden',
            'Richard Stallman': 'stallman',
            'Clara Snowden': 'clara_snowden',
            'Ludovic Courtès': 'courtes',
            'Jonas Öberg': 'aberg',
        })
        self.ids_filename = 'speakers.ids'

        self.speakers_noids = [
            'Daniel Kahn Gillmor',
            'Richard Stallman',
            'Ludovic Courtès',
            'Jonas Öberg',
        ]
        self.noids_filename = 'speakers.noids'

        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')


    def setup(self):
        """Runs before each test in this class."""
        pass


    def test_json_write(self):
        """Testing json_write function."""
        json_write(self.ids_filename, self.speakers_ids)
        assert_equal(json.loads(read_file(self.ids_filename),
                                object_pairs_hook=OrderedDict),
                     self.speakers_ids)

        json_write(self.noids_filename, self.speakers_noids)
        assert_equal(json.loads(read_file(self.noids_filename),
                                object_pairs_hook=OrderedDict),
                     self.speakers_noids)


    def test_json_read(self):
        """Testing json_read function."""
        write_file(self.ids_filename, json.dumps(self.speakers_ids,
                                             indent=4))
        assert_equal(json_read(self.ids_filename), self.speakers_ids)

        write_file(self.noids_filename, json.dumps(self.speakers_noids,
                                             indent=4))
        assert_equal(json_read(self.noids_filename), self.speakers_noids)


    def teardown(self):
        """Cleans up things after each test in this class."""
        # Remove `speakers.ids` file if it exists.
        if path.isfile(self.ids_filename):
            os.remove(self.ids_filename)

        # Remove `speakers.noids` file if it exists.
        if path.isfile(self.noids_filename):
            os.remove(self.noids_filename)


    @classmethod
    def teardown_class(self):
        """Clean up the mess created by this test."""
        # Change back to the old cwd
        os.chdir(self.old_cwd)


class TestTemplates(object):
    """Petty tests for lp templates.
    """

    def test_read(self):
        t = template_read('schedule')
        assert type(t) is str
        assert len(t) > 0

        t = template_read('speakers')
        assert type(t) is str
        assert len(t) > 0

        with mock.patch('sys.stderr', new_callable=StringIO) as out:
            t = template_read('nonexistent')
            assert t is None


class TestLPiCal(object):
    """
    Testing LPiCal class.
    """

    @classmethod
    def setup_class(self):
        """Setting up things for Testing LPiCal class.
        """

        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')

        self.MD_FILE = path.join('files', 'lp-sch.md')
        self.MD_FILE_CONTENT = read_file(self.MD_FILE)

        self.MD_FILE_S_ONLY = path.join('files', 'lp-sch-sessions-only.md')
        self.MD_FILE_S_ONLY_CONTENT = read_file(self.MD_FILE_S_ONLY)

        self.markdown = LPSMarkdown()
        self.lps_dict = self.markdown(self.MD_FILE_CONTENT)
        self.lps_dict_s_only = self.markdown(self.MD_FILE_S_ONLY_CONTENT)
        self.purge_list = ['speakers.noids']


    def setup(self):
        """Setting up things for a new test.
        """
        self.lp_ical = LPiCal(self.lps_dict, '2019')


    def test_init(self):
        """Testing LPiCal.__init__.
        """
        lp_ical = LPiCal(self.lps_dict, '2019')

        assert_equal(lp_ical.lp_year, '2019')
        assert_equal(lp_ical.cal.get('x-wr-calname'),
                     'LibrePlanet 2019')
        assert_equal(lp_ical.ucounter, 0)


    def test_gen_uid(self):
        """Testing LPiCal.gen_uid.
        """

        uid_fmt = ''.join(['{id}@LP', self.lp_ical.lp_year,
                           '@libreplanet.org'])

        for i in range(40):
            assert_equals(self.lp_ical.gen_uid(),
                          uid_fmt.format(id=i+1))


    def test_get_timeslot(self):
        """
        Testing LPiCal.get_timeslot.
        """

        timeslots = {
            '09:00-09:45: Registration and Breakfast':
            ['09:00', '09:45', 'Registration and Breakfast'],
            '  09:45 - 10:45: Opening Keynote':
            ['09:45', '10:45', 'Opening Keynote'],
            '10:5 - 10:55: Break':
            ['10:5', '10:55', 'Break'],
            ' 10:55 - 11:40: Session Block 1A':
            ['10:55', '11:40', 'Session Block 1A'],
            '    11:40 - 11:50: Break':
            ['11:40', '11:50', 'Break'],
            '9:45 - 10:30: Keynote ':
            ['9:45', '10:30', 'Keynote'],
            '16:55 - 17:40:Session Block 6B':
            ['16:55', '17:40', 'Session Block 6B'],
            '17:50 - 18:35: Closing keynote':
            ['17:50', '18:35', 'Closing keynote'],
            '':
            [None, None, None],
            '\t\t\t':
            [None, None, None],
            '                  ':
            [None, None, None],
            '10:00 - 10:45 - Keynote':
            ['10:00', '10:45', 'Keynote'],
            '16:20 - 17:05':
            ['16:20', '17:05', ''],
            '16:25-17:25':
            ['16:25', '17:25', ''],
            '17:05-17:15 - Break':
            ['17:05', '17:15', 'Break']
        }

        for string, timeslot in timeslots.items():
            start, end, name = self.lp_ical.get_timeslot(string)
            assert_equal(start, timeslot[0])
            assert_equal(end, timeslot[1])
            assert_equal(name, timeslot[2])


    def test_get_month_day(self):
        """Testing LPiCal.get_month_day.
        """

        month_days = {
            'Sunday, March 20': ['March', '20'],
            'Saturday, March 19': ['March', '19'],
            'Monday,March 20 ': ['March', '20'],
            'Tuesday,March21': ['March', '21'],
            '   Wednesday, March 22': ['March', '22'],
            'Thursday, March 23  ': ['March', '23'],
            '': [None, None],
            '\t\t': [None, None],
            '       ': [None, None],
        }

        for string, month_day in month_days.items():
            month, day  = self.lp_ical.get_month_day(string)
            assert_equal(month, month_day[0])
            assert_equal(day, month_day[1])


    def test_mk_datetime(self):
        """Testing LPiCal.mk_datetime
        """

        datetimes = [
            {
                'params': ['February', '28','08:00'],
                'datetime': '2019-02-28 08:00:00',
            },
            {
                'params': ['March', '21', '9:0'],
                'datetime': '2019-03-21 09:00:00',
            },
            {
                'params': ['March', '23', '15:30'],
                'datetime': '2019-03-23 15:30:00',
            },
        ]

        for test in datetimes:
            month = test['params'][0]
            day = test['params'][1]
            time = test['params'][2]

            dt_obj = self.lp_ical.mk_datetime(month, day, time)

            assert str(dt_obj.dt.tzinfo) == 'US/Eastern'
            assert str(dt_obj.dt)[:-6] == test['datetime']


    def test_mk_attendee(self):
        """Testing LPiCal.mk_attendee
        """
        speakers = [
            'Richard Stallman',
            'ginger coons',
            '<a href="speakers.htmll#corvellec">Marianne Corvellec</a>',
            '<a href="speakers.html#le-lous">Jonathan Le Lous</a>',
            'Jonas \xc3\x96berg',
            ]

        for speaker in speakers:
            attendee = self.lp_ical.mk_attendee(speaker)
            assert str(attendee) == 'invalid:nomail'
            assert attendee.params.get('cn') == BeautifulSoup(
                speaker, 'html.parser').get_text()
            assert attendee.params.get('ROLE') == 'REQ-PARTICIPANT'
            assert attendee.params.get('CUTYPE') == 'INDIVIDUAL'


    def test_add_event(self):
        """Testing LPiCal.add_event
        """
        uids = []

        for day_str, timeslots in self.lps_dict.items():
            month, day = self.lp_ical.get_month_day(day_str)
            for timeslot_str, sessions in timeslots.items():
                t_start, t_end, t_name = self.lp_ical.get_timeslot(timeslot_str)
                for session, session_info in sessions.items():
                    event = self.lp_ical.add_event(month, day,
                                            t_start, t_end, t_name,
                                            session, session_info)
                    assert event['uid'] not in uids
                    uids.append(event['uid'])

                    assert event['dtstamp'] == self.lp_ical.dtstamp
                    assert event['class'] == 'PUBLIC'
                    assert event['status'] == 'CONFIRMED'
                    assert event['method'] == 'PUBLISH'

                    if session == 'st-from-ts':
                        assert event['summary'] == t_name
                    else:
                        assert event['summary'] == session

                    assert event['location'] == session_info['room']
                    assert event['description'] == BeautifulSoup(' '.join(
                        session_info['desc']).replace(
                            '\n',' '), 'html.parser').get_text()

                    if type(event['attendee']) is list:
                        for attendee in event['attendee']:
                            assert isinstance(attendee, vCalAddress)
                    else:
                        assert isinstance(event['attendee'], vCalAddress)

                    assert isinstance(event['dtstart'], vDatetime)
                    assert isinstance(event['dtend'], vDatetime)


    def test_gen_ical(self):
        """Testing LPiCal.gen_ical.
        """
        print(self.lp_ical.gen_ical())


    def test_gen_ical_sessions_only(self):
        """Testing LPiCal.gen_ical with sessions only schedule.
        """
        print(LPiCal(self.lps_dict_s_only, '2019').gen_ical())


    def test_to_ical(self):
        """Testing LPiCal.to_ical.
        """
        filename = self.lp_ical.to_ical()
        assert_equal(filename, 'lp2019-schedule.ics')

        self.purge_list.append(filename)


    @classmethod
    def teardown_class(self):
        """
        Tearing down the mess created by Testing LPiCal class.
        """

        # remove files in the purge_list.
        for f in self.purge_list:
            if path.isfile(f):
                os.remove(f)

        # Change back to the old cwd
        os.chdir(self.old_cwd)


class TestLPS(object):
    """
    Class that tests everything related LP Schedule.
    """
    @classmethod
    def setup_class(self):
        """Runs before running any tests in this class."""

        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')

        self.MD_FILE = path.join('files', 'lp-sch.md')
        self.MD_FILE_CONTENT = read_file(self.MD_FILE)

        self.markdown = LPSMarkdown()
        self.lps_dict = self.markdown(self.MD_FILE_CONTENT)

    def setup(self):
        """Runs before each test in this class."""
        pass


    def test_LPSMarkdown_day(self):
        """
        Testing `LPSMarkdown` class - Day.
        """
        days = ['Saturday, March 19',
                'Sunday, March 20']
        i = 0
        for day in self.lps_dict.keys():
            assert_equal(day, days[i])
            i = i + 1


    def test_LPSMarkdown_timeslot(self):
        """
        Testing `LPSMarkdown` class - Timeslot.
        """
        timeslots = [
            '09:00 - 09:45: Registration and Breakfast',
            '09:45 - 10:45: Opening Keynote: Richard Stallman',
            '10:55 - 11:40: Session Block 1A',
            '11:40 - 11:50: Break',
            '11:50 - 12:35: Session Block 2A',
            '09:00 - 09:45: Registration and breakfast',
            '09:45 - 10:30: Keynote: Access without empowerment',
            '10:30 - 10:40: Break',
            '10:40 - 11:25: Session Block 1B',
            ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for timeslot in lps_timeslots.keys():
                assert_equal(timeslot, timeslots[i])
                i = i + 1


    def test_LPSMarkdown_session(self):
        """
        Testing `LPSMarkdown` class - Session.
        """
        sessions = [
            'Free software, free hardware, and other things',
            'Federation and GNU',
            'Dr. Hyde and Mr. Jekyll: advocating for free software in nonfree academic contexts',
            'TAFTA, CETA, TISA: traps and threats to Free Software Everywhere',
            'Let\'s encrypt!',
            'Attribution revolution -- turning copyright upside-down',
            'st-from-ts',
            'Fork and ignore: fighting a GPL violation by coding instead',
            'Who did this? Just wait until your father gets home',
            ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session in lps_sessions.keys():
                    assert_equal(session, sessions[i])
                    i = i + 1


    def test_LPSMarkdown_speaker(self):
        """
        Testing `LPSMarkdown` class - Speaker
        """
        speakers = [
            ['Richard Stallman'],
            ['<a href="http://dustycloud.org">Christopher Webber</a>'],
            ['ginger coons'],
            ['<a href="/2015/program/speakers.html#corvellec">Marianne Corvellec</a>',
             '<a href="/2015/program/speakers.html#le-lous">Jonathan Le Lous</a>'],
            ['Seth Schoen'],
            ['Jonas Öberg'],
            ['Benjamin Mako Hill'],
            ['Bradley Kuhn'],
            ['Ken Starks'],
            ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    assert_equal(session_info['speakers'], speakers[i])
                    i = i + 1


    def test_LPSMarkdown_room(self):
        """
        Testing `LPSMarkdown` class - Room
        """
        rooms = [
            'Room 32-123',
            'Room 32-123',
            'Room 32-141',
            'Room 32-155',
            'Room 32-123',
            'Room 32-141',
            'Room 32-123',
            'Room 32-123',
            'Room 32-141',
            ]
        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    assert_equal(session_info['room'], rooms[i])
                    i = i + 1


    def test_LPSMarkdown_video(self):
        """Testing `LPSMarkdown` class - Video
        """

        videos = [
            'https://media.libre.planet/rms-free-everything',
            'https://media.libre.planet/gnu-fed',
            'VideoTBA',
            'https://media.libre.planet/tafta-ceta-tisa',
            'https://media.libre.planet/letsencrypt',
            'VideoTBA',
            'https://media.libre.planet/mako-keynote',
            'https://media.libre.planet/fork-ignore',
            'VideoTBA',
            ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    assert_equal(session_info['video'], videos[i])
                    i = i + 1


    def test_LPSMarkdown_desc(self):
        """Testing `LPSMarkdown` class - Video
        """
        descriptions = [
            'Preceded by a welcome address from',
            'The effort to re-decentralize the web has',
            'What if the classic horror trope of the',
            'TAFTA, CETA, and TISA are far-reaching',
            'This year a robotic certificate authority will',
            'Reusing works licensed under free licenses seems',
            'In order to relate effectively to the digital works',
            'The free software movement has twin',
            'Typically, GPL enforcement activity',
            'While traditional enforcement is often',
            'Recently, Software Freedom Conservancy',
            'This talk discusses which scenarios make this remedy',
            'What\'s going on in here? Computer parts',
        ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    for desc in session_info['desc']:
                        assert_true(desc.startswith(descriptions[i]))
                        i = i + 1


    def test_RenderHTML(self):
        """Testing `RenderHTML` function with LP schedule
        """
        lps_html = RenderHTML(self.lps_dict, 'schedule')
        print(lps_html) # TODO: Scrape and test html output


    def test_RenderHTML_sessions_only(self):
        """Testing `RenderHTML` function - LP schedule - sessions only
        """
        md_content = read_file(path.join('files',
                                         'lp-sch-sessions-only.md'))

        lps_html = RenderHTML(self.markdown(md_content),
                              'schedule')
        print(lps_html) # TODO: Scrape and test html output

    @raises(SystemExit)
    def test_RenderHTML_nonexistent_template(self):
        """Testing `RenderHTML` function - LP schedule - ith non-existent template
        """
        with mock.patch('sys.stderr', new_callable=StringIO) as out:
            lps_html = RenderHTML(self.lps_dict, 'nonexistent')


    def teardown(self):
        """Cleans up things after each test in this class."""
        pass


    @classmethod
    def teardown_class(self):
        """Clean up the mess created by this test."""

        # Remove `speakers.noids` file if it exists.
        if path.isfile('speakers.noids'):
            os.remove('speakers.noids')

        # Change back to the old cwd
        os.chdir(self.old_cwd)


class TestLPSTBA(object):
    """Class tests TBAs in the LP schedule.

    """

    @classmethod
    def setup_class(self):
        """Runs before running any tests in this class.

        """
        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')

        self.MD_FILE = path.join('files', 'lp-sch-tba.md')
        self.MD_FILE_CONTENT = read_file(self.MD_FILE)

        self.markdown = LPSMarkdown()
        self.lps_dict = self.markdown(self.MD_FILE_CONTENT)


    def setup(self):
        """Runs before each test in this class.

        """
        lp_html = RenderHTML(self.lps_dict, 'schedule')
        self.soup = BeautifulSoup(lp_html, 'html.parser')


    def cleanup_speaker(self, sp):
        return ' '.join([s.strip() for s in sp.string.split('\n')
                        if len(s.strip())])


    def cleanup_desc(self, desc):
        return desc.replace('\n', '').strip()


    def test_LP_speakers(self):
        """Tests the non-existence of `SpeakerTBA` in gen. HTML.

        """
        speakers = [
            'Paige Peterson, MaidSoft',
            'George Chriss and others, Kat Walsh (moderator)',
            'Andrew Seeder, Dudley Street Neighborhood Initiative',
            'Marina Zhurakhinskaya, Red Hat',
            'Marianne Corvellec, April and Jonathan Le Lous, April',
            'Scott Dexter and Evan Misshula, CUNY, and Erin Glass, UCSD',
            'Michaela R. Brown',
        ]

        for sp in self.soup.find_all(class_='program-session-speaker'):
            sp_block = self.cleanup_speaker(sp)
            assert_equal(sp_block, speakers.pop(0))


    def test_LP_room(self):
        """Tests the non-existence of `RoomTBA` in gen. HTML.

        """
        rooms = [
            'Room 32-141',
            'Room 32-144',
            'Room 31-123',
            'Room 32-144',
            'Room 42-042',
        ]

        for sp in self.soup.find_all(class_='room'):
            room_block = sp.string
            assert_equal(room_block, rooms.pop(0))


    def test_LP_description(self):
        """Tests the non-existence of `DescTBA` in gen. HTML.
        """
        descriptions = [
            'Your workplace can exert a lot of control over how',
            'Free software developers and users tend to be most',
            'This talk will help you gather information, frame',
            'A look back at free software history',
            'Academic Institutions and their researchers',
            'At CUNY, we have taken steps to change this',
            'Being a free software user isn\'t easy,',
            'In this session, I\'ll give students tips',
        ]

        for descs in self.soup.find_all(class_='session-desc'):
            for desc in descs.strings:
                desc = self.cleanup_desc(desc)
                if desc:
                    assert desc.startswith(descriptions.pop(0))


    def teardown(self):
        """Cleans up things after each test in this class.

        """
        # Remove `speakers.noids` file if it exists.
        if path.isfile('speakers.noids'):
            os.remove('speakers.noids')


    @classmethod
    def teardown_class(self):
        """Cleans up the mess after running all tests in this class.
        """
        # Change back to the old cwd
        os.chdir(self.old_cwd)


class TestLPSpeakers(object):
    """
    Class that tests everything related LP Speakers
    """

    @classmethod
    def setup_class(self):
        """Runs before running any tests in this class."""

        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')

        self.MD_FILE = path.join('files', 'lp-speakers.md')
        self.MD_FILE_CONTENT = read_file(self.MD_FILE)

        self.markdown = LPSpeakersMarkdown()
        self.lpspeakers_dict = self.markdown(self.MD_FILE_CONTENT)


    def setup(self):
        """Runs before each test in this class."""
        pass


    def test_speakers_id_file_exists(self):
        """
        Testing if LPSpeakersMardown created speakers.ids file.
        """
        speakers_ids = self.markdown.speakers_renderer.speakers_ids

        assert path.isfile('speakers.ids')
        assert_equal(json_read('speakers.ids'), speakers_ids)


    def test_LPSpeakersMarkdown_keynotespeakers_name(self):
        """Testing LPSpeakersMarkdown keynote speakers' names.

        """
        keynote_speakers = ['Daniel Kahn Gillmor',
                            'Edward Snowden',
                            'Richard Stallman',
                            'Clara Snowden',
                            'Ludovic Courtès']

        i = 0
        for kspeaker in self.lpspeakers_dict['keynote-speakers']:
            assert_equal(kspeaker['speaker'], keynote_speakers[i])
            i = i + 1


    def test_LPSpeakersMarkdown_keynotespeakers_id(self):
        """Testing LPSpeakersMarkdown keynote speakers' id.

        """
        keynote_speaker_ids = ['gillmor',
                               'snowden',
                               'stallman',
                               'clara_snowden',
                               'courtes']


        i = 0
        for kspeaker in self.lpspeakers_dict['keynote-speakers']:
            assert_equal(kspeaker['id'], keynote_speaker_ids[i])
            i = i + 1


    def test_LPSpeakersMarkdown_keynotespeakers_imgurl(self):
        """Testing LPSpeakersMarkdown keynote speakers' image url.

        """
        keynote_speaker_img_urls = [
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/dkg.jpg',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/snowden.jpg',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/stallman.jpg',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/c_snowden.jpg'
        ]



        i = 0
        for kspeaker in self.lpspeakers_dict['keynote-speakers']:
            if 'img_url' in kspeaker:
                assert_equal(kspeaker['img_url'],
                             keynote_speaker_img_urls[i])
            i = i + 1


    def test_LPSpeakersMarkdown_keynotespeakers_imgalt(self):
        """Testing LPSpeakersMarkdown keynote speakers' image alt text.

        """
        keynote_speaker_img_alts = ['Daniel Kahn Gillmor - Photo',
                                    'Edward Snowden - Photo',
                                    'Richard Stallman - Photo',
                                    '']



        i = 0
        for kspeaker in self.lpspeakers_dict['keynote-speakers']:
            if 'img_alt' in kspeaker:
                assert_equal(kspeaker['img_alt'],
                             keynote_speaker_img_alts[i])
            i = i + 1


    def test_LPSpeakersMarkdown_keynotespeakers_bio(self):
        """Testing LPSpeakersMarkdown keynote speakers' bio.

        """
        keynote_speaker_bios = [
            ['Daniel Kahn Gillmor is a technologist with the ACLU\'s Speech, Privacy'],
            ['Edward Snowden is a former intelligence officer who served the CIA,'],
            ['Richard is a software developer and software freedom activist. In 1983',
             'Since the mid-1990s, Richard has spent most of his time in political',],
            [],
            ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam',
             'Ut turpis felis, pulvinar a semper sed, adipiscing id']
        ]

        i = 0
        for kspeaker in self.lpspeakers_dict['keynote-speakers']:
            if 'bio' in kspeaker:
                j = 0
                for p in kspeaker['bio']:
                    p.startswith(keynote_speaker_bios[i][j])
                    j = j + 1

            i = i + 1


    def test_LPSpeakersMarkdown_speakers_name(self):
        """Testing LPSpeakersMarkdown speakers' names.

        """
        speakers = ['Emmanuel',
                    'George Chriss',
                    'Marianne Corvellec',
                    'Richard Fontana',
                    'Mike Gerwitz',
                    'Bassam Kurdali',
                    'Jonathan Le Lous',
                    'M. C. McGrath',
                    'Deb Nicholson',
                    'Stefano Zacchiroli']

        i = 0
        for kspeaker in self.lpspeakers_dict['speakers']:
            assert_equal(kspeaker['speaker'], speakers[i])
            i = i + 1


    def test_LPSpeakersMarkdown_speakers_id(self):
        """Testing LPSpeakersMarkdown speakers' id.

        """
        speaker_ids = ['emmanuel',
                       'chriss',
                       'corvellec',
                       'fontana',
                       'gerwitz',
                       'kurdali',
                       'lous',
                       'mcgrath',
                       'nicholson',
                       'zacchiroli']

        i = 0
        for kspeaker in self.lpspeakers_dict['speakers']:
            assert_equal(kspeaker['id'], speaker_ids[i])
            i = i + 1


    def test_LPSpeakersMarkdown_speakers_imgurl(self):
        """Testing LPSpeakersMarkdown speakers' image url.

        """
        speaker_img_urls = [
            '', '',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/corvellec.jpg',
            '', '',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/kurdali.png',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/lelous.jpg',
            '',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/nicholson.jpg',
            '//static.fsf.org/nosvn/libreplanet/speaker-pics/zacchiroli.jpg'
        ]

        i = 0
        for kspeaker in self.lpspeakers_dict['speakers']:
            if 'img_url' in kspeaker:
                assert_equal(kspeaker['img_url'],
                             speaker_img_urls[i])
            i = i + 1


    def test_LPSpeakersMarkdown_speakers_imgalt(self):
        """Testing LPSpeakersMarkdown speakers' image alt text.

        """
        speaker_img_alts = [
            '', '',
            'Marianne Corvellec - Photo',
            '', '',
            'Bassam Kurdali - Photo',
            'Jonathan Le Lous - Photo',
            '',
            'Deb Nicholson - Photo',
            'Stefano Zacchiroli - Photo']

        i = 0
        for kspeaker in self.lpspeakers_dict['speakers']:
            if 'img_alt' in kspeaker:
                assert_equal(kspeaker['img_alt'],
                             speaker_img_alts[i])
            i = i + 1


    def test_LPSpeakersMarkdown_speakers_bio(self):
        """Testing LPSpeakersMarkdown speakers' bio.

        """
        speaker_bios = [
            ['Emmanuel is a Division III student at Hampshire College, studying how'],
            [],
            ['Marianne Corvellec has been a Free Software activist with April'],
            ['Richard Fontana is a lawyer at Red Hat. He leads support for Red Hat\'s'],
            [],
            ['Bassam is a 3D animator/filmmaker whose 2006 short, Elephants Dream,'],
            ['Jonathan has been involved with the Free Software Movement for ten'],
            ['M. C. is the founder of Transparency Toolkit, a free software project'],
            [],
            ['Stefano Zacchiroli is Associate Professor of Computer Science at']
        ]

        i = 0
        for kspeaker in self.lpspeakers_dict['speakers']:
            if 'bio' in kspeaker:
                j = 0
                for p in kspeaker['bio']:
                    p.startswith(speaker_bios[i][j])
                    j = j + 1

            i = i + 1


    def test_RenderHTML(self):
        """Testing `RenderHTML` function with LP speakers
        """
        lps_html = RenderHTML(self.lpspeakers_dict, 'speakers')
        print(lps_html) # TODO: Scrape and test html output.


    def teardown(self):
        """Cleans up things after each test in this class."""
        pass


    @classmethod
    def teardown_class(self):
        """Purge the mess created by this test."""

        # Remove `speakers.ids` file if it exists.
        if path.isfile('speakers.ids'):
            os.remove('speakers.ids')

        # Change back to the old cwd
        os.chdir(self.old_cwd)


class TestSpeakersAutoLinking(object):
    """Class tests autolinking of speakers in sessions MD.
    """
    @classmethod
    def setup_class(self):
        """Runs before running any tests in this class."""

        # Change current working directory to the tests directory.
        self.old_cwd = os.getcwd()
        os.chdir('tests')

        self.ids_filename = 'speakers.ids'
        self.noids_filename = 'speakers.noids'

        self.SPEAKERS_MD = path.join('files', 'lp-speakers-autolink.md')
        self.SPEAKERS_MD_CONTENT = read_file(self.SPEAKERS_MD)

        self.SESSIONS_MD = path.join('files', 'lp-sessions-autolink.md')
        self.SESSIONS_MD_CONTENT = read_file(self.SESSIONS_MD)


    def setup(self):
        """Runs before each test in this class."""
        pass


    def test_sessions_autolinking(self):
        """Testing autolinking of speakers in sessions. """
        self.speakers_markdown = LPSpeakersMarkdown()
        self.lpspeakers_dict = self.speakers_markdown(
            self.SPEAKERS_MD_CONTENT)

        assert (path.isfile(self.ids_filename) and
                json.loads(read_file(self.ids_filename)))

        self.sessions_markdown = LPSMarkdown()
        self.lps_dict = self.sessions_markdown(self.SESSIONS_MD_CONTENT)

        assert (path.isfile(self.noids_filename) and
                json.loads(read_file(self.noids_filename)))

        speakers = [
            [
                '<a href="speakers.html#snowden">Edward Snowden</a>',
                '<a href="speakers.html#gillmor">Daniel Kahn Gillmor</a>',
            ],
            [
                '<a href="speakers.html#nicholson">Deb Nicholson</a>',
                '<a href="speakers.html#fontana">Richard Fontana</a>',
            ],
            [
                'Paige Peterson', 'MaidSoft'
            ],
            [
                'George Chriss',
                'Kat Walsh (moderator)',
            ],
            [
                '<a href="speakers.html#zacchiroli">Stefano Zacchiroli</a>',
                'Debian', 'OSI', 'IRILL'
            ],
            [
                '<a href="speakers.html#corvellec">Marianne Corvellec</a>',
                'April and Jonathan Le Lous',
                'April'
            ],
            [
                '<a href="speakers.html#brown">Michaela R. Brown</a>',
            ],
            [
                '<a href="speakers.html#gott">Molly Gott</a>'
            ],
            [
                'Christopher Webber',
                '<a href="speakers.html#thompson">David Thompson</a>',
                'Ludovic Courtès',
            ],
        ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    assert_equal(session_info['speakers'], speakers[i])
                    i = i + 1

        speakers_noids = [
            'Paige Peterson',
            'George Chriss',
            'Kat Walsh',
            'Jonathan Le Lous',
            'Christopher Webber',
            'Ludovic Courtès',
        ]
        assert_equal(json_read(self.noids_filename), speakers_noids)


    def test_sessions_autolinking_nospeakerids(self):
        """Testing autolinked speakrs in sessions MD when speakers.id not available. """

        assert not path.isfile(self.ids_filename)

        self.sessions_markdown = LPSMarkdown()
        self.lps_dict = self.sessions_markdown(self.SESSIONS_MD_CONTENT)

        assert (path.isfile(self.noids_filename) and
                json.loads(read_file(self.noids_filename)))

        speakers = [
            [
                'Edward Snowden',
                'Daniel Kahn Gillmor',
            ],
            [
                'Deb Nicholson',
                'Richard Fontana',
            ],
            [
                'Paige Peterson', 'MaidSoft'
            ],
            [
                'George Chriss',
                'Kat Walsh (moderator)',
            ],
            [
                'Stefano Zacchiroli',
                'Debian', 'OSI', 'IRILL'
            ],
            [
                'Marianne Corvellec',
                'April and Jonathan Le Lous',
                'April'
            ],
            [
                'Michaela R. Brown',
            ],
            [
                'Molly Gott'
            ],
            [
                'Christopher Webber',
                'David Thompson',
                'Ludovic Courtès',
            ],
        ]

        i = 0
        for lps_timeslots in self.lps_dict.values():
            for lps_sessions in lps_timeslots.values():
                for session_info in lps_sessions.values():
                    assert_equal(session_info['speakers'], speakers[i])
                    i = i + 1

        speakers_noids = [
            'Edward Snowden',
            'Daniel Kahn Gillmor',
            'Deb Nicholson',
            'Richard Fontana',
            'Paige Peterson',
            'George Chriss',
            'Kat Walsh',
            'Stefano Zacchiroli',
            'Marianne Corvellec',
            'Jonathan Le Lous',
            'Michaela R. Brown',
            'Molly Gott',
            'Christopher Webber',
            'David Thompson',
            'Ludovic Courtès',
        ]

        assert_equal(json_read(self.noids_filename), speakers_noids)


    def teardown(self):
        """Cleans up things after each test in this class."""
        # Remove `speakers.ids` file if it exists.
        if path.isfile(self.ids_filename):
           os.remove(self.ids_filename)

        # Remove `speakers.noids` file if it exists.
        if path.isfile(self.noids_filename):
           os.remove(self.noids_filename)


    @classmethod
    def teardown_class(self):
        """Clean up the mess created by this test class"""
        # Change back to the old cwd
        os.chdir(self.old_cwd)