|
| 1 | +# Copyright 2024 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import tempfile |
| 17 | +import unittest |
| 18 | +from official.projects.waste_identification_ml.Triton_TF_Cloud_Deployment.client import utils |
| 19 | + |
| 20 | + |
| 21 | +class TestLoadLabels(unittest.TestCase): |
| 22 | + |
| 23 | + def test_load_labels(self): |
| 24 | + # Create a temporary CSV file within the test |
| 25 | + with tempfile.NamedTemporaryFile(mode='w+', delete=False) as temp_csv: |
| 26 | + temp_csv.write('Label\nBottle\nCan\nCup\n') |
| 27 | + temp_csv_path = temp_csv.name |
| 28 | + |
| 29 | + try: |
| 30 | + # Call the function under test |
| 31 | + category_indices, category_index = utils.load_labels(temp_csv_path) |
| 32 | + |
| 33 | + # Expected results |
| 34 | + expected_list = ['Label', 'Bottle', 'Can', 'Cup'] |
| 35 | + expected_dict = { |
| 36 | + 1: {'id': 1, 'name': 'Label', 'supercategory': 'objects'}, |
| 37 | + 2: {'id': 2, 'name': 'Bottle', 'supercategory': 'objects'}, |
| 38 | + 3: {'id': 3, 'name': 'Can', 'supercategory': 'objects'}, |
| 39 | + 4: {'id': 4, 'name': 'Cup', 'supercategory': 'objects'}, |
| 40 | + } |
| 41 | + |
| 42 | + self.assertEqual(category_indices, expected_list) |
| 43 | + self.assertEqual(category_index, expected_dict) |
| 44 | + |
| 45 | + finally: |
| 46 | + # Ensure the temporary file is deleted even if assertions fail |
| 47 | + os.remove(temp_csv_path) |
| 48 | + |
| 49 | + def test_files_paths_with_images(self): |
| 50 | + # Create a temporary directory |
| 51 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 52 | + # Create some image and non-image files |
| 53 | + filenames = ['img2.jpg', 'img1.png', 'doc1.txt', 'photo.gif'] |
| 54 | + for filename in filenames: |
| 55 | + open(os.path.join(temp_dir, filename), 'a').close() |
| 56 | + |
| 57 | + # Call the function under test |
| 58 | + result = utils.files_paths(temp_dir) |
| 59 | + |
| 60 | + # Expected image files sorted naturally |
| 61 | + expected = [ |
| 62 | + os.path.join(temp_dir, 'img1.png'), |
| 63 | + os.path.join(temp_dir, 'img2.jpg'), |
| 64 | + os.path.join(temp_dir, 'photo.gif'), |
| 65 | + ] |
| 66 | + |
| 67 | + self.assertEqual(result, expected) |
| 68 | + |
| 69 | + def test_files_paths_with_no_images(self): |
| 70 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 71 | + # Create only non-image files |
| 72 | + filenames = ['doc1.txt', 'readme.md'] |
| 73 | + for filename in filenames: |
| 74 | + open(os.path.join(temp_dir, filename), 'a').close() |
| 75 | + |
| 76 | + result = utils.files_paths(temp_dir) |
| 77 | + self.assertEqual(result, []) # Should return an empty list |
| 78 | + |
| 79 | + def test_files_paths_empty_folder(self): |
| 80 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 81 | + result = utils.files_paths(temp_dir) |
| 82 | + self.assertEqual(result, []) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + unittest.main() |
0 commit comments