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 17"""Test for data_prepare.py.""" 18 19from __future__ import absolute_import 20from __future__ import division 21from __future__ import print_function 22 23import csv 24import json 25import os 26import unittest 27from data_prepare import generate_negative_data 28from data_prepare import prepare_original_data 29from data_prepare import write_data 30 31 32class TestPrepare(unittest.TestCase): 33 34 def setUp(self): # pylint: disable=g-missing-super-call 35 self.file = "./%s/output_%s_%s.txt" % (folders[0], folders[0], names[0]) # pylint: disable=undefined-variable 36 self.data = [] 37 prepare_original_data(folders[0], names[0], self.data, self.file) # pylint: disable=undefined-variable 38 39 def test_prepare_data(self): 40 num = 0 41 with open(self.file, "r") as f: 42 lines = csv.reader(f) 43 for idx, line in enumerate(lines): # pylint: disable=unused-variable 44 if len(line) == 3 and line[2] == "-": 45 num += 1 46 self.assertEqual(len(self.data), num) 47 self.assertIsInstance(self.data, list) 48 self.assertIsInstance(self.data[0], dict) 49 self.assertEqual(list(self.data[-1]), ["gesture", "accel_ms2_xyz", "name"]) 50 self.assertEqual(self.data[0]["name"], names[0]) # pylint: disable=undefined-variable 51 52 def test_generate_negative(self): 53 original_len = len(self.data) 54 generate_negative_data(self.data) 55 self.assertEqual(original_len + 300, len(self.data)) 56 generated_num = 0 57 for idx, data in enumerate(self.data): # pylint: disable=unused-variable 58 if data["name"] == "negative6" or data["name"] == "negative7" or data[ 59 "name"] == "negative8": 60 generated_num += 1 61 self.assertEqual(generated_num, 300) 62 63 def test_write_data(self): 64 data_path_test = "./data/data0" 65 write_data(self.data, data_path_test) 66 with open(data_path_test, "r") as f: 67 lines = f.readlines() 68 self.assertEqual(len(lines), len(self.data)) 69 self.assertEqual(json.loads(lines[0]), self.data[0]) 70 self.assertEqual(json.loads(lines[-1]), self.data[-1]) 71 os.remove(data_path_test) 72 73 74if __name__ == "__main__": 75 unittest.main() 76