1#!/usr/bin/env python3
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""Unit tests for the data_frame.py script."""
19
20from data_frame import DataFrame
21
22def test_data_frame():
23  # trivial empty data frame
24  df = DataFrame()
25  assert df.headers == []
26  assert df.data_table == []
27  assert df.data_table_transposed == []
28
29  # common case, same number of values in each place.
30  df = DataFrame({'TotalTime_ms': [1, 2, 3], 'Displayed_ms': [4, 5, 6]})
31  assert df.headers == ['TotalTime_ms', 'Displayed_ms']
32  assert df.data_table == [[1, 4], [2, 5], [3, 6]]
33  assert df.data_table_transposed == [(1, 2, 3), (4, 5, 6)]
34
35  # varying num values.
36  df = DataFrame({'many': [1, 2], 'none': []})
37  assert df.headers == ['many', 'none']
38  assert df.data_table == [[1, None], [2, None]]
39  assert df.data_table_transposed == [(1, 2), (None, None)]
40
41  df = DataFrame({'many': [], 'none': [1, 2]})
42  assert df.headers == ['many', 'none']
43  assert df.data_table == [[None, 1], [None, 2]]
44  assert df.data_table_transposed == [(None, None), (1, 2)]
45
46  # merge multiple data frames
47  df = DataFrame()
48  df.concat_rows(DataFrame())
49  assert df.headers == []
50  assert df.data_table == []
51  assert df.data_table_transposed == []
52
53  df = DataFrame()
54  df2 = DataFrame({'TotalTime_ms': [1, 2, 3], 'Displayed_ms': [4, 5, 6]})
55
56  df.concat_rows(df2)
57  assert df.headers == ['TotalTime_ms', 'Displayed_ms']
58  assert df.data_table == [[1, 4], [2, 5], [3, 6]]
59  assert df.data_table_transposed == [(1, 2, 3), (4, 5, 6)]
60
61  df = DataFrame({'TotalTime_ms': [1, 2]})
62  df2 = DataFrame({'Displayed_ms': [4, 5]})
63
64  df.concat_rows(df2)
65  assert df.headers == ['TotalTime_ms', 'Displayed_ms']
66  assert df.data_table == [[1, None], [2, None], [None, 4], [None, 5]]
67
68  df = DataFrame({'TotalTime_ms': [1, 2]})
69  df2 = DataFrame({'TotalTime_ms': [3, 4], 'Displayed_ms': [5, 6]})
70
71  df.concat_rows(df2)
72  assert df.headers == ['TotalTime_ms', 'Displayed_ms']
73  assert df.data_table == [[1, None], [2, None], [3, 5], [4, 6]]
74
75  # data_row_at
76  df = DataFrame({'TotalTime_ms': [1, 2, 3], 'Displayed_ms': [4, 5, 6]})
77  assert df.data_row_at(-1) == [3, 6]
78  assert df.data_row_at(2) == [3, 6]
79  assert df.data_row_at(1) == [2, 5]
80
81  # repeat
82  df = DataFrame({'TotalTime_ms': [1], 'Displayed_ms': [4]})
83  df2 = DataFrame({'TotalTime_ms': [1, 1, 1], 'Displayed_ms': [4, 4, 4]})
84  assert df.repeat(3) == df2
85
86  # repeat
87  df = DataFrame({'TotalTime_ms': [1, 1, 1], 'Displayed_ms': [4, 4, 4]})
88  assert df.data_row_len == 3
89  df = DataFrame({'TotalTime_ms': [1, 1]})
90  assert df.data_row_len == 2
91
92  # repeat
93  df = DataFrame({'TotalTime_ms': [1, 1, 1], 'Displayed_ms': [4, 4, 4]})
94  assert df.data_row_len == 3
95  df = DataFrame({'TotalTime_ms': [1, 1]})
96  assert df.data_row_len == 2
97
98  # data_row_reduce
99  df = DataFrame({'TotalTime_ms': [1, 1, 1], 'Displayed_ms': [4, 4, 4]})
100  df_sum = DataFrame({'TotalTime_ms': [3], 'Displayed_ms': [12]})
101  assert df.data_row_reduce(sum) == df_sum
102
103  # merge_data_columns
104  df = DataFrame({'TotalTime_ms': [1, 2, 3]})
105  df2 = DataFrame({'Displayed_ms': [3, 4, 5, 6]})
106
107  df.merge_data_columns(df2)
108  assert df == DataFrame(
109    {'TotalTime_ms': [1, 2, 3], 'Displayed_ms': [3, 4, 5, 6]})
110
111  df = DataFrame({'TotalTime_ms': [1, 2, 3]})
112  df2 = DataFrame({'Displayed_ms': [3, 4]})
113
114  df.merge_data_columns(df2)
115  assert df == DataFrame(
116    {'TotalTime_ms': [1, 2, 3], 'Displayed_ms': [3, 4]})
117
118  df = DataFrame({'TotalTime_ms': [1, 2, 3]})
119  df2 = DataFrame({'TotalTime_ms': [10, 11]})
120
121  df.merge_data_columns(df2)
122  assert df == DataFrame({'TotalTime_ms': [10, 11, 3]})
123
124  df = DataFrame({'TotalTime_ms': []})
125  df2 = DataFrame({'TotalTime_ms': [10, 11]})
126
127  df.merge_data_columns(df2)
128  assert df == DataFrame({'TotalTime_ms': [10, 11]})
129