X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=cmframework%2Ftest%2Fcmdependencysort_test.py;fp=cmframework%2Ftest%2Fcmdependencysort_test.py;h=b42fdfa19ddf4cb1776eb23210936580a1cf0c46;hb=c389bdee7b3845b55f443dbf04c0ce4083a55886;hp=0000000000000000000000000000000000000000;hpb=5030f0c004701dd422c78c71c014ef60f48139fc;p=ta%2Fconfig-manager.git diff --git a/cmframework/test/cmdependencysort_test.py b/cmframework/test/cmdependencysort_test.py new file mode 100644 index 0000000..b42fdfa --- /dev/null +++ b/cmframework/test/cmdependencysort_test.py @@ -0,0 +1,117 @@ +# Copyright 2019 Nokia + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.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. + +import unittest +import mock +from mock import call + +from cmframework.utils.cmdependencysort import CMDependencySort as Sort +from cmframework.apis.cmerror import CMError + + +class CMDependencySortTest(unittest.TestCase): + def test_empty(self): + sort = Sort() + sorted_list = sort.sort() + assert sorted_list == [] + + def test_one_no_deps(self): + sort = Sort({'a': []}) + sorted_list = sort.sort() + assert sorted_list == ['a'] + + sort = Sort(None, {'a': []}) + sorted_list = sort.sort() + assert sorted_list == ['a'] + + def test_both_no_deps(self): + sort = Sort({'a': []}, {'a': []}) + sorted_list = sort.sort() + assert sorted_list == ['a'] + + sort = Sort({'a': [], 'b': []}, {'a': [], 'b': []}) + sorted_list = sort.sort() + + assert len(sorted_list) == 2 + assert 'a' in sorted_list + assert 'b' in sorted_list + + @staticmethod + def _assert_orderings(after, before, sorted_list): + for entry, deps in after.iteritems(): + for dep in deps: + assert sorted_list.index(entry) > sorted_list.index(dep) + + for entry, deps in before.iteritems(): + for dep in deps: + assert sorted_list.index(entry) < sorted_list.index(dep) + + def test_mandbc(self): + after = {'a': ['m'], 'b': ['d']} + before = {'a': ['b', 'c', 'd', 'n'], 'b': ['c']} + sort = Sort(after, before) + sorted_list = sort.sort() + + CMDependencySortTest._assert_orderings(after, before, sorted_list) + + def test_simple_only_after(self): + after = {'a': ['b'], 'b': ['c']} + before = {} + sort = Sort(after) + sorted_list = sort.sort() + + CMDependencySortTest._assert_orderings(after, before, sorted_list) + + def test_simple_only_before(self): + after = {} + before = {'a': ['b'], 'b': ['c']} + sort = Sort(None, before) + sorted_list = sort.sort() + + CMDependencySortTest._assert_orderings(after, before, sorted_list) + + def test_simple(self): + after = {'b': ['a'], 'c': ['b']} + before = {'a': ['b'], 'b': ['c']} + sort = Sort(after, before) + sorted_list = sort.sort() + + CMDependencySortTest._assert_orderings(after, before, sorted_list) + + def test_cycle_before_after(self): + after = {'b': ['a']} + before = {'b': ['a']} + sort = Sort(after, before) + + with self.assertRaises(CMError) as context: + sorted_list = sort.sort() + + def test_cycle_only_before(self): + after = {} + before = {'b': ['a'], 'a': ['b']} + sort = Sort(after, before) + + with self.assertRaises(CMError) as context: + sorted_list = sort.sort() + + def test_cycle_only_after(self): + after = {'b': ['a'], 'a': ['b']} + before = {} + sort = Sort(after, before) + + with self.assertRaises(CMError) as context: + sorted_list = sort.sort() + +if __name__ == '__main__': + unittest.main()