1# Lint as: python3
2# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16# pylint: disable=g-bad-import-order
17
18"""Test for data_load.py."""
19
20from __future__ import absolute_import
21from __future__ import division
22from __future__ import print_function
23
24import unittest
25from data_load import DataLoader
26
27import tensorflow as tf
28
29
30class TestLoad(unittest.TestCase):
31
32    def setUp(self):    # pylint: disable=g-missing-super-call
33        self.loader = DataLoader(
34                "./data/train", "./data/valid", "./data/test", seq_length=512)
35
36    def test_get_data(self):
37        self.assertIsInstance(self.loader.train_data, list)
38        self.assertIsInstance(self.loader.train_label, list)
39        self.assertIsInstance(self.loader.valid_data, list)
40        self.assertIsInstance(self.loader.valid_label, list)
41        self.assertIsInstance(self.loader.test_data, list)
42        self.assertIsInstance(self.loader.test_label, list)
43        self.assertEqual(self.loader.train_len, len(self.loader.train_data))
44        self.assertEqual(self.loader.train_len, len(self.loader.train_label))
45        self.assertEqual(self.loader.valid_len, len(self.loader.valid_data))
46        self.assertEqual(self.loader.valid_len, len(self.loader.valid_label))
47        self.assertEqual(self.loader.test_len, len(self.loader.test_data))
48        self.assertEqual(self.loader.test_len, len(self.loader.test_label))
49
50    def test_pad(self):
51        original_data1 = [[2, 3], [1, 1]]
52        expected_data1_0 = [[2, 3], [2, 3], [2, 3], [2, 3], [1, 1]]
53        expected_data1_1 = [[2, 3], [1, 1], [1, 1], [1, 1], [1, 1]]
54        original_data2 = [[-2, 3], [-77, -681], [5, 6], [9, -7], [22, 3333],
55                                            [9, 99], [-100, 0]]
56        expected_data2 = [[-2, 3], [-77, -681], [5, 6], [9, -7], [22, 3333]]
57        padding_data1 = self.loader.pad(original_data1, seq_length=5, dim=2)
58        padding_data2 = self.loader.pad(original_data2, seq_length=5, dim=2)
59        for i in range(len(padding_data1[0])):
60            for j in range(len(padding_data1[0].tolist()[0])):
61                self.assertLess(
62                        abs(padding_data1[0].tolist()[i][j] - expected_data1_0[i][j]),
63                        10.001)
64        for i in range(len(padding_data1[1])):
65            for j in range(len(padding_data1[1].tolist()[0])):
66                self.assertLess(
67                        abs(padding_data1[1].tolist()[i][j] - expected_data1_1[i][j]),
68                        10.001)
69        self.assertEqual(padding_data2[0].tolist(), expected_data2)
70        self.assertEqual(padding_data2[1].tolist(), expected_data2)
71
72    def test_format(self):
73        self.loader.format()
74        expected_train_label = int(self.loader.label2id[self.loader.train_label[0]])
75        expected_valid_label = int(self.loader.label2id[self.loader.valid_label[0]])
76        expected_test_label = int(self.loader.label2id[self.loader.test_label[0]])
77        for feature, label in self.loader.train_data:    # pylint: disable=unused-variable
78            format_train_label = label.numpy()
79            break
80        for feature, label in self.loader.valid_data:
81            format_valid_label = label.numpy()
82            break
83        for feature, label in self.loader.test_data:
84            format_test_label = label.numpy()
85            break
86        self.assertEqual(expected_train_label, format_train_label)
87        self.assertEqual(expected_valid_label, format_valid_label)
88        self.assertEqual(expected_test_label, format_test_label)
89        self.assertIsInstance(self.loader.train_data, tf.data.Dataset)
90        self.assertIsInstance(self.loader.valid_data, tf.data.Dataset)
91        self.assertIsInstance(self.loader.test_data, tf.data.Dataset)
92
93
94if __name__ == "__main__":
95    unittest.main()
96