Skip to content

Commit

Permalink
Fix the Pipeline.get_name method and add unit tests #91
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Feb 12, 2021
1 parent f54ea82 commit 8b2a16c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 26 deletions.
2 changes: 1 addition & 1 deletion scanpipe/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_name(cls):
"""
Return the name declared on the class or the name of the class itself.
"""
return cls.name or cls.__class__.__name__
return cls.name or cls.__name__

@classmethod
def get_doc(cls):
Expand Down
3 changes: 2 additions & 1 deletion scanpipe/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

scanpipe_app_config = apps.get_app_config("scanpipe")

scanpipe_app_config.register_pipeline("raise_exception", RaiseException)
scanpipe_app_config.register_pipeline("do_nothing", DoNothing)
scanpipe_app_config.register_pipeline("pretty_name", DoNothing)
scanpipe_app_config.register_pipeline("raise_exception", RaiseException)


mocked_now = mock.Mock(now=lambda: datetime(2010, 10, 10, 10, 10, 10))
Expand Down
6 changes: 6 additions & 0 deletions scanpipe/tests/pipelines/do_nothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ class DoNothing(Pipeline):
"""

def step1(self):
"""
Step1 doc.
"""
pass

def step2(self):
"""
Step2 doc.
"""
pass

steps = (
Expand Down
37 changes: 37 additions & 0 deletions scanpipe/tests/pipelines/pretty_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

from scanpipe.pipelines import Pipeline


class PrettyPipeline(Pipeline):
"""
Doc from docstring.
"""

name = "Pretty name"
doc = "Doc from attribute"

def step1(self):
pass

steps = (step1,)
70 changes: 46 additions & 24 deletions scanpipe/tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,52 @@
from scanpipe.models import Project
from scanpipe.pipelines import Pipeline
from scanpipe.pipelines import is_pipeline_subclass
from scanpipe.pipelines.docker import Docker
from scanpipe.pipelines.load_inventory import LoadInventory
from scanpipe.pipelines.root_filesystems import RootFS
from scanpipe.tests.pipelines.do_nothing import DoNothing
from scanpipe.tests.pipelines.pretty_name import PrettyPipeline


class ScanPipePipelinesTest(TestCase):
def test_scanpipe_pipeline_class_get_doc(self):
expected = "A pipeline to analyze a Docker image."
self.assertEqual(expected, Docker.get_doc())
def test_scanpipe_pipeline_class_get_name(self):
project1 = Project.objects.create(name="Analysis")
run = project1.add_pipeline("do_nothing")

def test_scanpipe_pipeline_class_pipeline_get_name(self):
pipeline_class = DoNothing
pipeline_instance = DoNothing(run)
self.assertIsNone(pipeline_class.name)
self.assertIsNone(pipeline_instance.name)
self.assertEqual("DoNothing", pipeline_class.get_name())
self.assertEqual("DoNothing", pipeline_instance.get_name())

pipeline_class = PrettyPipeline
pipeline_instance = PrettyPipeline(run)
self.assertEqual("Pretty name", pipeline_class.name)
self.assertEqual("Pretty name", pipeline_instance.name)
self.assertEqual("Pretty name", pipeline_class.get_name())
self.assertEqual("Pretty name", pipeline_instance.get_name())

def test_scanpipe_pipelines_class_get_doc(self):
project1 = Project.objects.create(name="Analysis")
run = project1.add_pipeline("docker")
pipeline = run.make_pipeline_instance()
self.assertEqual("Docker", pipeline.get_name())
run = project1.add_pipeline("do_nothing")
pipeline_class = DoNothing
pipeline_instance = DoNothing(run)

expected = "A pipeline that does nothing, in 2 steps."
self.assertIsNone(pipeline_class.doc)
self.assertIsNone(pipeline_instance.doc)
self.assertEqual(expected, pipeline_class.get_doc())
self.assertEqual(expected, pipeline_instance.get_doc())

pipeline_class = PrettyPipeline
pipeline_instance = PrettyPipeline(run)
expected = "Doc from attribute"
self.assertEqual(expected, pipeline_class.doc)
self.assertEqual(expected, pipeline_instance.doc)
self.assertEqual(expected, pipeline_class.get_doc())
self.assertEqual(expected, pipeline_instance.get_doc())

def test_scanpipe_pipeline_class_log(self):
project1 = Project.objects.create(name="Analysis")
run = project1.add_pipeline("docker")
run = project1.add_pipeline("do_nothing")
pipeline = run.make_pipeline_instance()
pipeline.log("Event1")
pipeline.log("Event2")
Expand Down Expand Up @@ -90,33 +117,28 @@ def test_scanpipe_pipeline_class_execute_with_exception(self):

def test_scanpipe_pipeline_class_save_errors_context_manager(self):
project1 = Project.objects.create(name="Analysis")
run = project1.add_pipeline("docker")
run = project1.add_pipeline("do_nothing")
pipeline = run.make_pipeline_instance()
self.assertEqual(project1, pipeline.project)

with pipeline.save_errors(Exception):
raise Exception("Error message")

error = project1.projecterrors.get()
self.assertEqual("Docker", error.model)
self.assertEqual("DoNothing", error.model)
self.assertEqual({}, error.details)
self.assertEqual("Error message", error.message)
self.assertIn('raise Exception("Error message")', error.traceback)

def test_scanpipe_pipelines_is_pipeline_subclass(self):
self.assertFalse(is_pipeline_subclass(None))
self.assertFalse(is_pipeline_subclass(Pipeline))
self.assertTrue(is_pipeline_subclass(Docker))
self.assertTrue(is_pipeline_subclass(RootFS))
self.assertTrue(is_pipeline_subclass(LoadInventory))

def test_scanpipe_pipelines_pipeline_class_get_doc(self):
self.assertEqual("A pipeline to analyze a Docker image.", Docker.get_doc())
self.assertTrue(is_pipeline_subclass(DoNothing))
self.assertTrue(is_pipeline_subclass(PrettyPipeline))

def test_scanpipe_pipelines_pipeline_class_get_graph(self):
graph = Docker.get_graph()
def test_scanpipe_pipelines_class_get_graph(self):
expected = [
{"name": "extract_images", "doc": "Extract the images from tarballs."},
{"name": "extract_layers", "doc": "Extract layers from images."},
{"doc": "Step1 doc.", "name": "step1"},
{"doc": "Step2 doc.", "name": "step2"},
]
self.assertEqual(expected, graph[0:2])
self.assertEqual(expected, DoNothing.get_graph())

0 comments on commit 8b2a16c

Please sign in to comment.