forked from PuloV/ics-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
515 lines (400 loc) · 13.7 KB
/
parse_test.go
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
package ics_test
import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
"github.com/PuloV/ics-golang"
)
func TestLoadCalendar(t *testing.T) {
parser := ics.New()
calBytes, err := ioutil.ReadFile("testCalendars/2eventsCal.ics")
if err != nil {
t.Errorf("Failed to read calendar file ( %s )", err)
}
parser.Load(string(calBytes))
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s )", err)
}
for i, pErr := range parseErrors {
t.Errorf("Parsing Error №%d: %s", i, pErr)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s )", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar, found %d calendars", len(calendars))
}
}
func TestNewParser(t *testing.T) {
parser := ics.New()
rType := fmt.Sprintf("%v", reflect.TypeOf(parser))
if rType != "*ics.Parser" {
t.Errorf("Failed to create a Parser !")
}
}
func TestNewParserChans(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
output := parser.GetOutputChan()
rType := fmt.Sprintf("%v", reflect.TypeOf(input))
if rType != "chan string" {
t.Errorf("Failed to create a input chan ! Received : Type %s Value %s", rType, input)
}
rType = fmt.Sprintf("%v", reflect.TypeOf(output))
if rType != "chan *ics.Event" {
t.Errorf("Failed to create a output chan! Received : Type %s Value %s", rType, output)
}
}
func TestParsing0Calendars(t *testing.T) {
parser := ics.New()
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
for i, pErr := range parseErrors {
t.Errorf("Parsing Error №%d : %s \n", i, pErr)
}
}
func TestParsing1Calendars(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/2eventsCal.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
for i, pErr := range parseErrors {
t.Errorf("Parsing Error №%d : %s \n", i, pErr)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
}
}
func TestParsing2Calendars(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/2eventsCal.ics"
input <- "testCalendars/3eventsNoAttendee.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
for i, pErr := range parseErrors {
t.Errorf("Parsing Error №%d : %s \n", i, pErr)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 2 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
}
}
func TestParsingNotExistingCalendar(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/notFound.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 1 {
t.Errorf("Expected 1 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
}
func TestParsingNotExistingAndExistingCalendars(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/3eventsNoAttendee.ics"
input <- "testCalendars/notFound.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 1 {
t.Errorf("Expected 1 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
}
}
func TestParsingWrongCalendarUrls(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "http://localhost/goTestFails"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 1 {
t.Errorf("Expected 1 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 0 {
t.Errorf("Expected 0 calendar , found %d calendars \n", len(calendars))
}
}
func TestCreatingTempDir(t *testing.T) {
ics.FilePath = "testingTempDir/"
parser := ics.New()
input := parser.GetInputChan()
input <- "https://www.google.com/calendar/ical/yordanpulov%40gmail.com/private-81525ac0eb14cdc2e858c15e1b296a1c/basic.ics"
parser.Wait()
_, err := os.Stat(ics.FilePath)
if err != nil {
t.Errorf("Failed to create %s \n", ics.FilePath)
}
// remove the new dir
os.Remove(ics.FilePath)
// return the var to default
ics.FilePath = "tmp/"
}
func TestCalendarInfo(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/2eventsCal.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 0 {
t.Errorf("Expected 0 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
return
}
calendar := calendars[0]
if calendar.GetName() != "2 Events Cal" {
t.Errorf("Expected name '%s' calendar , got '%s' calendars \n", "2 Events Cal", calendar.GetName())
}
if calendar.GetDesc() != "The cal has 2 events(1st with attendees and second without)" {
t.Errorf("Expected description '%s' calendar , got '%s' calendars \n", "The cal has 2 events(1st with attendees and second without)", calendar.GetDesc())
}
if calendar.GetVersion() != 2.0 {
t.Errorf("Expected version %s calendar , got %s calendars \n", 2.0, calendar.GetVersion())
}
events := calendar.GetEvents()
if len(events) != 2 {
t.Errorf("Expected %s events in calendar , got %s events \n", 2, len(events))
}
eventsByDates := calendar.GetEventsByDates()
if len(eventsByDates) != 2 {
t.Errorf("Expected %s events in calendar , got %s events \n", 2, len(eventsByDates))
}
geometryExamIcsFormat, errICS := time.Parse(ics.IcsFormat, "20140616T060000Z")
if err != nil {
t.Errorf("(ics time format) Unexpected error %s \n", errICS)
}
geometryExamYmdHis, errYMD := time.Parse(ics.YmdHis, "2014-06-16 06:00:00")
if err != nil {
t.Errorf("(YmdHis time format) Unexpected error %s \n", errYMD)
}
eventsByDate, err := calendar.GetEventsByDate(geometryExamIcsFormat)
if err != nil {
t.Errorf("(ics time format) Unexpected error %s \n", err)
}
if len(eventsByDate) != 1 {
t.Errorf("(ics time format) Expected %s events in calendar for the date 2014-06-16 , got %s events \n", 1, len(eventsByDate))
}
eventsByDate, err = calendar.GetEventsByDate(geometryExamYmdHis)
if err != nil {
t.Errorf("(YmdHis time format) Unexpected error %s \n", err)
}
if len(eventsByDate) != 1 {
t.Errorf("(YmdHis time format) Expected %s events in calendar for the date 2014-06-16 , got %s events \n", 1, len(eventsByDate))
}
}
func TestCalendarEvents(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/2eventsCal.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 0 {
t.Errorf("Expected 0 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
return
}
calendar := calendars[0]
event, err := calendar.GetEventByImportedID("[email protected]")
if err != nil {
t.Errorf("Failed to get event by id with error %s \n", err)
}
// event must have
start, _ := time.Parse(ics.IcsFormat, "20140714T100000Z")
end, _ := time.Parse(ics.IcsFormat, "20140714T110000Z")
created, _ := time.Parse(ics.IcsFormat, "20140515T075711Z")
modified, _ := time.Parse(ics.IcsFormat, "20141125T074253Z")
location := "In The Office"
desc := "1. Report on previous weekly tasks. \\n2. Plan of the present weekly tasks."
seq := 1
status := "CONFIRMED"
summary := "General Operative Meeting"
rrule := ""
attendeesCount := 3
org := new(ics.Attendee)
org.SetName("[email protected]")
org.SetEmail("[email protected]")
if event.GetStart() != start {
t.Errorf("Expected start %s , found %s \n", start, event.GetStart())
}
if event.GetEnd() != end {
t.Errorf("Expected end %s , found %s \n", end, event.GetEnd())
}
if event.GetCreated() != created {
t.Errorf("Expected created %s , found %s \n", created, event.GetCreated())
}
if event.GetLastModified() != modified {
t.Errorf("Expected modified %s , found %s \n", modified, event.GetLastModified())
}
if event.GetLocation() != location {
t.Errorf("Expected location %s , found %s \n", location, event.GetLocation())
}
if event.GetDescription() != desc {
t.Errorf("Expected description %s , found %s \n", desc, event.GetDescription())
}
if event.GetSequence() != seq {
t.Errorf("Expected sequence %s , found %s \n", seq, event.GetSequence())
}
if event.GetStatus() != status {
t.Errorf("Expected status %s , found %s \n", status, event.GetStatus())
}
if event.GetSummary() != summary {
t.Errorf("Expected status %s , found %s \n", summary, event.GetSummary())
}
if event.GetRRule() != rrule {
t.Errorf("Expected rrule %s , found %s \n", rrule, event.GetRRule())
}
if len(event.GetAttendees()) != attendeesCount {
t.Errorf("Expected attendeesCount %s , found %s \n", attendeesCount, len(event.GetAttendees()))
}
eventOrg := event.GetOrganizer()
if *eventOrg != *org {
t.Errorf("Expected organizer %s , found %s \n", org, event.GetOrganizer())
}
// SECOND EVENT WITHOUT ATTENDEES AND ORGANIZER
eventNoAttendees, errNoAttendees := calendar.GetEventByImportedID("[email protected]")
attendeesCount = 0
org = new(ics.Attendee)
if errNoAttendees != nil {
t.Errorf("Failed to get event by id with error %s \n", errNoAttendees)
}
if len(eventNoAttendees.GetAttendees()) != attendeesCount {
t.Errorf("Expected attendeesCount %s , found %s \n", attendeesCount, len(event.GetAttendees()))
}
if eventNoAttendees.GetOrganizer() != nil {
t.Errorf("Expected organizer %s , found %s \n", org, eventNoAttendees.GetOrganizer())
}
}
func TestCalendarEventAttendees(t *testing.T) {
parser := ics.New()
input := parser.GetInputChan()
input <- "testCalendars/2eventsCal.ics"
parser.Wait()
parseErrors, err := parser.GetErrors()
if err != nil {
t.Errorf("Failed to wait the parse of the calendars ( %s ) \n", err)
}
if len(parseErrors) != 0 {
t.Errorf("Expected 0 error , found %d in :\n %#v \n", len(parseErrors), parseErrors)
}
calendars, errCal := parser.GetCalendars()
if errCal != nil {
t.Errorf("Failed to get calendars ( %s ) \n", errCal)
}
if len(calendars) != 1 {
t.Errorf("Expected 1 calendar , found %d calendars \n", len(calendars))
return
}
calendar := calendars[0]
event, err := calendar.GetEventByImportedID("[email protected]")
if err != nil {
t.Errorf("Failed to get event by id with error %s \n", err)
}
attendees := event.GetAttendees()
attendeesCount := 3
if len(attendees) != attendeesCount {
t.Errorf("Expected attendeesCount %s , found %s \n", attendeesCount, len(attendees))
return
}
john := attendees[0]
sue := attendees[1]
travis := attendees[2]
// check name
if john.GetName() != "John Smith" {
t.Errorf("Expected attendee name %s , found %s \n", "John Smith", john.GetName())
}
if sue.GetName() != "Sue Zimmermann" {
t.Errorf("Expected attendee name %s , found %s \n", "Sue Zimmermann", sue.GetName())
}
if travis.GetName() != "Travis M. Vollmer" {
t.Errorf("Expected attendee name %s , found %s \n", "Travis M. Vollmer", travis.GetName())
}
// check email
if john.GetEmail() != "[email protected]" {
t.Errorf("Expected attendee email %s , found %s \n", "[email protected]", john.GetEmail())
}
if sue.GetEmail() != "[email protected]" {
t.Errorf("Expected attendee email %s , found %s \n", "[email protected]", sue.GetEmail())
}
if travis.GetEmail() != "[email protected]" {
t.Errorf("Expected attendee email %s , found %s \n", "[email protected]", travis.GetEmail())
}
// check status
if john.GetStatus() != "ACCEPTED" {
t.Errorf("Expected attendee status %s , found %s \n", "ACCEPTED", john.GetStatus())
}
if sue.GetStatus() != "NEEDS-ACTION" {
t.Errorf("Expected attendee status %s , found %s \n", "NEEDS-ACTION", sue.GetStatus())
}
if travis.GetStatus() != "NEEDS-ACTION" {
t.Errorf("Expected attendee status %s , found %s \n", "NEEDS-ACTION", travis.GetStatus())
}
// check role
if john.GetRole() != "REQ-PARTICIPANT" {
t.Errorf("Expected attendee status %s , found %s \n", "REQ-PARTICIPANT", john.GetRole())
}
if sue.GetRole() != "REQ-PARTICIPANT" {
t.Errorf("Expected attendee status %s , found %s \n", "REQ-PARTICIPANT", sue.GetRole())
}
if travis.GetRole() != "REQ-PARTICIPANT" {
t.Errorf("Expected attendee status %s , found %s \n", "REQ-PARTICIPANT", travis.GetRole())
}
}