Coverage for tests/test_utils.py: 43%

33 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 02:40 -0700

1# This file is part of felis. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import os 

23import unittest 

24from collections.abc import MutableMapping 

25from typing import Any 

26 

27import yaml 

28 

29from felis import DEFAULT_FRAME 

30from felis.utils import ReorderingVisitor 

31 

32TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

33TEST_YAML = os.path.join(TESTDIR, "data", "test.yml") 

34 

35 

36class VisitorTestCase(unittest.TestCase): 

37 """Tests for ReorderingVisitor class.""" 

38 

39 schema_obj: MutableMapping[str, Any] = {} 

40 

41 def setUp(self) -> None: 

42 """Load data from test file.""" 

43 with open(TEST_YAML) as test_yaml: 

44 self.schema_obj = yaml.load(test_yaml, Loader=yaml.SafeLoader) 

45 self.schema_obj.update(DEFAULT_FRAME) 

46 

47 def test_reordering(self) -> None: 

48 """Check for attribute ordering.""" 

49 visitor = ReorderingVisitor() 

50 schema = visitor.visit_schema(self.schema_obj) 

51 self.assertEqual( 

52 list(schema.keys()), ["@context", "name", "@id", "@type", "description", "tables", "version"] 

53 ) 

54 

55 table = schema["tables"][0] 

56 self.assertEqual(list(table.keys())[:5], ["name", "@id", "description", "columns", "primaryKey"]) 

57 

58 column = table["columns"][0] 

59 self.assertEqual(list(column.keys())[:4], ["name", "@id", "description", "datatype"]) 

60 

61 def test_add_type(self) -> None: 

62 """Check for attribute ordering with add_type.""" 

63 visitor = ReorderingVisitor(add_type=True) 

64 schema = visitor.visit_schema(self.schema_obj) 

65 self.assertEqual( 

66 list(schema.keys()), ["@context", "name", "@id", "@type", "description", "tables", "version"] 

67 ) 

68 

69 table = schema["tables"][0] 

70 self.assertEqual( 

71 list(table.keys())[:6], ["name", "@id", "@type", "description", "columns", "primaryKey"] 

72 ) 

73 

74 column = table["columns"][0] 

75 self.assertEqual(list(column.keys())[:5], ["name", "@id", "@type", "description", "datatype"]) 

76 

77 

78if __name__ == "__main__": 78 ↛ 79line 78 didn't jump to line 79, because the condition on line 78 was never true

79 unittest.main()