Add test for yaml_file loader 31/28631/2
authorYujun Zhang <zhang.yujunz@zte.com.cn>
Tue, 14 Feb 2017 07:20:16 +0000 (15:20 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Tue, 14 Feb 2017 22:54:40 +0000 (06:54 +0800)
Change-Id: I997ceeed17bd35889f6b0f3465bbe75df256b71e
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
qtip/base/error.py
qtip/loader/plan.py
qtip/loader/yaml_file.py
tests/data/yaml/invalid.yaml [new file with mode: 0644]
tests/data/yaml/with_name.yaml [new file with mode: 0644]
tests/data/yaml/without_name.yaml [new file with mode: 0644]
tests/unit/loader/yaml_file_test.py [new file with mode: 0644]

index 01a7f7a..a055aa8 100644 (file)
@@ -8,22 +8,23 @@
 ##############################################################################
 
 
-class QtipError(Exception):
+class BaseError(Exception):
     pass
 
 
-class InvalidFormat(QtipError):
-    def __init__(self, filename):
+class InvalidContent(BaseError):
+    def __init__(self, filename, excinfo=None):
         self.filename = filename
+        self.excinfo = excinfo
 
 
-class NotFound(QtipError):
+class NotFound(BaseError):
     def __init__(self, module, package='qtip'):
         self.package = package
         self.module = module
 
 
-class ToBeDoneError(QtipError):
+class ToBeDoneError(BaseError):
     """something still to be done"""
     def __init__(self, method, module):
         self.method = method
index 6f1764e..9b5546e 100644 (file)
@@ -26,7 +26,6 @@ class Plan(YamlFileLoader):
 
         self.qpis = [QPISpec(qpi, paths=paths)
                      for qpi in self.content[PlanProp.QPIS]]
-        self.info = self.content[PlanProp.INFO]
         _config = self.content[PlanProp.CONFIG]
 
         # TODO(yujunz) create collector by name
index f1cd461..ccaee8d 100644 (file)
@@ -7,11 +7,10 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-from collections import defaultdict
 from os import path
 import yaml
 
-from qtip.base.error import InvalidFormat
+from qtip.base.error import InvalidContent
 from qtip.base.constant import BaseProp
 from qtip.loader.file import FileLoader
 
@@ -21,13 +20,9 @@ class YamlFileLoader(FileLoader):
 
     def __init__(self, name, paths=None):
         super(YamlFileLoader, self).__init__(name, paths)
-        content = defaultdict(lambda: None)
-
-        try:
-            content.update(yaml.safe_load(file(self._abspath)))
-        except yaml.YAMLError:
-            # TODO(yujunz) log yaml error
-            raise InvalidFormat(self._abspath)
-
-        self.name = content[BaseProp.NAME] or path.splitext(name)[0]
-        self.content = content
+        with open(self._abspath, 'r') as stream:
+            content = yaml.safe_load(stream)
+            if not isinstance(content, dict):
+                raise InvalidContent(self._abspath)
+            self.content = content
+            self.name = content.get(BaseProp.NAME, path.splitext(name)[0])
diff --git a/tests/data/yaml/invalid.yaml b/tests/data/yaml/invalid.yaml
new file mode 100644 (file)
index 0000000..22e31ed
--- /dev/null
@@ -0,0 +1 @@
+invalid - yaml
\ No newline at end of file
diff --git a/tests/data/yaml/with_name.yaml b/tests/data/yaml/with_name.yaml
new file mode 100644 (file)
index 0000000..25f7f83
--- /dev/null
@@ -0,0 +1 @@
+name: name in content
\ No newline at end of file
diff --git a/tests/data/yaml/without_name.yaml b/tests/data/yaml/without_name.yaml
new file mode 100644 (file)
index 0000000..bd234bd
--- /dev/null
@@ -0,0 +1 @@
+no_name: yaml file without name
\ No newline at end of file
diff --git a/tests/unit/loader/yaml_file_test.py b/tests/unit/loader/yaml_file_test.py
new file mode 100644 (file)
index 0000000..1783694
--- /dev/null
@@ -0,0 +1,33 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import os
+import pytest
+
+from qtip.base.error import InvalidContent
+from qtip.loader.yaml_file import YamlFileLoader
+
+
+@pytest.fixture
+def yaml_root(data_root):
+    return os.path.join(data_root, 'yaml')
+
+
+@pytest.mark.parametrize('filename, expected', [
+    ('with_name.yaml', 'name in content'),
+    ('without_name.yaml', 'without_name')])
+def test_init(yaml_root, filename, expected):
+    loader = YamlFileLoader(filename, [yaml_root])
+    assert loader.name == expected
+
+
+def test_invalid_content(yaml_root):
+    with pytest.raises(InvalidContent) as excinfo:
+        YamlFileLoader('invalid.yaml', [yaml_root])
+    assert 'invalid.yaml' in excinfo.value.filename