|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + parser "github.com/kaschula/call-event-parser" |
| 8 | + . "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestItReturnsAnErrorWhenNoEventDateTimeGiven(t *testing.T) { |
| 12 | + rawCsv := [][]string{ |
| 13 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 14 | + []string{""}, |
| 15 | + } |
| 16 | + |
| 17 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 18 | + |
| 19 | + Equal(t, 0, len(callFile.ValidData())) |
| 20 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'eventDatetime' value")) |
| 21 | +} |
| 22 | + |
| 23 | +func TestItReturnsAnErrorWhenDateFormatIsIncorrect(t *testing.T) { |
| 24 | + rawCsv := [][]string{ |
| 25 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 26 | + []string{"2012-01-02 12-01-30"}, |
| 27 | + } |
| 28 | + |
| 29 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 30 | + |
| 31 | + Equal(t, 0, len(callFile.ValidData())) |
| 32 | + True(t, strings.Contains(callFile.RecordErrors()[0], "date format must be yyyy-mm-dd hh:mm:ss")) |
| 33 | +} |
| 34 | + |
| 35 | +func TestItReturnsAnErrorWhenEventActionIsMissing(t *testing.T) { |
| 36 | + rawCsv := [][]string{ |
| 37 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 38 | + []string{"2012-01-02 12:01:30", ""}, |
| 39 | + } |
| 40 | + |
| 41 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 42 | + |
| 43 | + Equal(t, 0, len(callFile.ValidData())) |
| 44 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'eventAction' value")) |
| 45 | +} |
| 46 | + |
| 47 | +func TestItReturnsAnErrorWhenEventActionIsMoreThan20CharactersLong(t *testing.T) { |
| 48 | + rawCsv := [][]string{ |
| 49 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 50 | + []string{"2012-01-02 12:01:30", "abcdefghijklmnopqrstu"}, |
| 51 | + } |
| 52 | + |
| 53 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 54 | + |
| 55 | + Equal(t, 0, len(callFile.ValidData())) |
| 56 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'eventAction' to be between 1 - 20 in length")) |
| 57 | +} |
| 58 | + |
| 59 | +func TestItReturnsAnErrorWhenCallRefIsMissing(t *testing.T) { |
| 60 | + rawCsv := [][]string{ |
| 61 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 62 | + []string{"2012-01-02 12:01:30", "sale", ""}, |
| 63 | + } |
| 64 | + |
| 65 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 66 | + |
| 67 | + Equal(t, 0, len(callFile.ValidData())) |
| 68 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'callRef' value")) |
| 69 | +} |
| 70 | + |
| 71 | +func TestItReturnsAnErrorWhenCallRefIsNotAValidInteger(t *testing.T) { |
| 72 | + rawCsv := [][]string{ |
| 73 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 74 | + []string{"2012-01-02 12:01:30", "sale", "notAnInt"}, |
| 75 | + } |
| 76 | + |
| 77 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 78 | + |
| 79 | + Equal(t, 0, len(callFile.ValidData())) |
| 80 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'callRef' to be a valid integer")) |
| 81 | +} |
| 82 | + |
| 83 | +func TestItReturnsAnErrorWhenEventValueIsNoteAValidFloat(t *testing.T) { |
| 84 | + rawCsv := [][]string{ |
| 85 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 86 | + []string{"2012-01-02 12:01:30", "sale", "1234", "2.1a"}, |
| 87 | + } |
| 88 | + |
| 89 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 90 | + |
| 91 | + Equal(t, 0, len(callFile.ValidData())) |
| 92 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'eventValue' to be a valid float")) |
| 93 | +} |
| 94 | + |
| 95 | +func TestItReturnsAnErrorWhenEventCurrencyCodeIsNotSetAndEventValueIsMoreThanZero(t *testing.T) { |
| 96 | + rawCsv := [][]string{ |
| 97 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 98 | + []string{"2012-01-02 12:01:30", "sale", "1234", "1.0", ""}, |
| 99 | + } |
| 100 | + |
| 101 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 102 | + |
| 103 | + Equal(t, 0, len(callFile.ValidData())) |
| 104 | + True(t, strings.Contains(callFile.RecordErrors()[0], "requires 'eventCurrencyCode' if event value is more than 0.0")) |
| 105 | +} |
| 106 | + |
| 107 | +func TestItDoesNotReturnAnErrorWhenEventValueIsZeroAndNoCurrencyCodeSet(t *testing.T) { |
| 108 | + rawCsv := [][]string{ |
| 109 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 110 | + []string{"2012-01-02 12:01:30", "sale", "1234", "0.0", ""}, |
| 111 | + } |
| 112 | + |
| 113 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 114 | + |
| 115 | + Equal(t, 0, len(callFile.RecordErrors())) |
| 116 | + Equal(t, 1, len(callFile.ValidData())) |
| 117 | +} |
| 118 | + |
| 119 | +func TestItReturnsItSetsTheEventValueToZeroFloatIfEmpty(t *testing.T) { |
| 120 | + rawCsv := [][]string{ |
| 121 | + []string{"eventDatetime", "eventAction", "callRef", "eventValue", "eventCurrencyCode"}, |
| 122 | + []string{"2012-01-02 12:01:30", "sale", "1234", "", "GBP"}, |
| 123 | + } |
| 124 | + |
| 125 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file") |
| 126 | + |
| 127 | + Equal(t, 1, len(callFile.ValidData())) |
| 128 | + Equal(t, "0.00", callFile.ValidData()[0][parser.COL_EVENT_VAL]) |
| 129 | +} |
| 130 | + |
| 131 | +func TestItReturnsTheFileName(t *testing.T) { |
| 132 | + rawCsv := [][]string{} |
| 133 | + |
| 134 | + callFile := parser.CreateCallEventFileFromRaw(rawCsv, "path/to/file.txt") |
| 135 | + |
| 136 | + Equal(t, "file.txt", callFile.GetFilename()) |
| 137 | +} |
0 commit comments