-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDataFrameCsvReaderTest.class.st
More file actions
130 lines (111 loc) · 4.22 KB
/
DataFrameCsvReaderTest.class.st
File metadata and controls
130 lines (111 loc) · 4.22 KB
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
Class {
#name : #DataFrameCsvReaderTest,
#superclass : #TestCase,
#instVars : [
'directory',
'commaCsvFile',
'tabCsvFile',
'emptyCsvFile',
'expectedDataFrame',
'dataFrameWithoutRowNames',
'dataFrameWithRowNames'
],
#category : #'DataFrame-IO-Tests-Core'
}
{ #category : #running }
DataFrameCsvReaderTest >> createFile: aFileReference withContents: aString [
| stream |
stream := aFileReference writeStream.
stream nextPutAll: aString.
stream close
]
{ #category : #running }
DataFrameCsvReaderTest >> setUp [
super setUp.
directory := FileSystem memory workingDirectory / 'testData'.
directory createDirectory.
commaCsvFile := directory / 'comma.csv'.
tabCsvFile := directory / 'tab.csv'.
emptyCsvFile := directory / 'empty.csv'.
self createFile: commaCsvFile withContents: TestCsvStrings commaCsvString.
self createFile: tabCsvFile withContents: TestCsvStrings tabCsvString.
self createFile: emptyCsvFile withContents: TestCsvStrings emptyCsvString.
dataFrameWithRowNames := DataFrame withRows: #( #( 2.4 true 'rain' ) #( 0.5 true 'rain' ) #( -1.2 true 'snow' ) #( -2.3 false '-' ) #( 3.2 true 'rain' ) ).
dataFrameWithRowNames columnNames: #( temperature precipitation type ).
dataFrameWithRowNames rowNames: (#( '01:10' '01:30' '01:50' '02:10' '02:30' ) collect: #asTime).
dataFrameWithoutRowNames := DataFrame withRows: {
{
'01:10' asTime.
2.4.
true.
'rain' }.
{
'01:30' asTime.
0.5.
true.
'rain' }.
{
'01:50' asTime.
-1.2.
true.
'snow' }.
{
'02:10' asTime.
-2.3.
false.
'-' }.
{
'02:30' asTime.
3.2.
true.
'rain' } }.
dataFrameWithoutRowNames columnNames: #( nil temperature precipitation type )
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadCsv [
| actualDataFrame |
actualDataFrame := DataFrame readFromCsv: commaCsvFile.
self assert: actualDataFrame equals: dataFrameWithoutRowNames
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadCsvWithRowNames [
| actualDataFrame |
actualDataFrame := DataFrame readFromCsvWithRowNames: commaCsvFile.
self assert: actualDataFrame equals: dataFrameWithRowNames
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadCsvWithRowNamesWithSeparatorTab [
| actualDataFrame |
actualDataFrame := DataFrame readFromCsvWithRowNames: tabCsvFile separator: Character tab.
self assert: actualDataFrame equals: dataFrameWithRowNames
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadCsvWithSeparatorTab [
| actualDataFrame |
actualDataFrame := DataFrame readFromCsv: tabCsvFile withSeparator: Character tab.
self assert: actualDataFrame equals: dataFrameWithoutRowNames
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadFromString [
| actualDataFrame |
actualDataFrame := DataFrameCsvReader new readFromString: TestCsvStrings commaCsvString.
self
assertCollection: actualDataFrame columns first
hasSameElements: #('1:10 am' '1:30 am' '1:50 am' '2:10 am' '2:30 am').
self
assertCollection: actualDataFrame columns last
hasSameElements: #('rain' 'rain' 'snow' '-' 'rain')
]
{ #category : #tests }
DataFrameCsvReaderTest >> testReadFromStringWithSeparatorWithHeader [
| df |
df := DataFrameCsvReader new
readFromString: TestCsvStrings dollarSignCsvString
withSeparator: $$
withHeader: false.
self assert: df dimensions equals: 6 @ 13.
self assertCollection: df columnNames hasSameElements: (1 to: 13) asArray.
self
assertCollection: df first values
hasSameElements: #('Given' 'a' 'text' 'file' 'of' 'many' 'lines,' 'where' 'fields' 'within' 'a' 'line' nil)
]