From 5b6370f13ea84abea960daf77b99f93dc88ad85c Mon Sep 17 00:00:00 2001 From: fujinhua Date: Tue, 14 Aug 2018 12:05:29 +0800 Subject: Add unit test for enumutil Change-Id: Ib117576543097e9c2b0ed7d702f5a841be3ebd70 Issue-ID: VFC-1009 Signed-off-by: fujinhua --- lcm/pub/utils/tests.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lcm/pub/utils/tests.py diff --git a/lcm/pub/utils/tests.py b/lcm/pub/utils/tests.py new file mode 100644 index 00000000..00fafdee --- /dev/null +++ b/lcm/pub/utils/tests.py @@ -0,0 +1,29 @@ +# Copyright 2018 ZTE Corporation. +# +# 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 enumutil + + +class UtilsTest(unittest.TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_enum(self): + MY_TYPE = enumutil.enum(SAMLL=0, LARGE=1) + self.assertEqual(0, MY_TYPE.SAMLL) + self.assertEqual(1, MY_TYPE.LARGE) -- cgit 1.2.3-korg From 68d6f7f81784780aff58f096870bf8ab53f9963e Mon Sep 17 00:00:00 2001 From: fujinhua Date: Tue, 14 Aug 2018 14:38:16 +0800 Subject: Add unit test for fileutil Change-Id: Ida68d6281350a6e5226493eef7f81a7e8c9664b4 Issue-ID: VFC-1009 Signed-off-by: fujinhua --- lcm/pub/utils/tests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lcm/pub/utils/tests.py b/lcm/pub/utils/tests.py index 00fafdee..706b28bd 100644 --- a/lcm/pub/utils/tests.py +++ b/lcm/pub/utils/tests.py @@ -13,7 +13,18 @@ # limitations under the License. import unittest +import mock import enumutil +import fileutil +import urllib2 + + +class MockReq(): + def read(self): + return "1" + + def close(self): + pass class UtilsTest(unittest.TestCase): @@ -27,3 +38,18 @@ class UtilsTest(unittest.TestCase): MY_TYPE = enumutil.enum(SAMLL=0, LARGE=1) self.assertEqual(0, MY_TYPE.SAMLL) self.assertEqual(1, MY_TYPE.LARGE) + + def test_create_and_delete_dir(self): + dirs = "abc/def/hij" + fileutil.make_dirs(dirs) + fileutil.make_dirs(dirs) + fileutil.delete_dirs(dirs) + + @mock.patch.object(urllib2, 'urlopen') + def test_download_file_from_http(self, mock_urlopen): + mock_urlopen.return_value = MockReq() + fileutil.delete_dirs("abc") + is_ok, f_name = fileutil.download_file_from_http("1", "abc", "1.txt") + self.assertTrue(is_ok) + self.assertTrue(f_name.endswith("abc/1.txt")) + fileutil.delete_dirs("abc") -- cgit 1.2.3-korg From 63a969df1fdf9ccd895769cd9b297b85705b8947 Mon Sep 17 00:00:00 2001 From: fujinhua Date: Tue, 14 Aug 2018 15:51:29 +0800 Subject: Add unit test for jobutil Change-Id: I3c63ae508eec95f7471a53f0d961816a9bb49341 Issue-ID: VFC-1009 Signed-off-by: fujinhua --- lcm/pub/utils/tests.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lcm/pub/utils/tests.py b/lcm/pub/utils/tests.py index 706b28bd..232acf05 100644 --- a/lcm/pub/utils/tests.py +++ b/lcm/pub/utils/tests.py @@ -18,6 +18,9 @@ import enumutil import fileutil import urllib2 +from lcm.pub.database.models import JobStatusModel +from lcm.pub.utils.jobutil import JobUtil + class MockReq(): def read(self): @@ -53,3 +56,32 @@ class UtilsTest(unittest.TestCase): self.assertTrue(is_ok) self.assertTrue(f_name.endswith("abc/1.txt")) fileutil.delete_dirs("abc") + + def test_query_job_status(self): + job_id = "1" + JobStatusModel.objects.filter().delete() + JobStatusModel( + indexid=1, + jobid=job_id, + status="success", + progress=10 + ).save() + JobStatusModel( + indexid=2, + jobid=job_id, + status="success", + progress=50 + ).save() + JobStatusModel( + indexid=3, + jobid=job_id, + status="success", + progress=100 + ).save() + jobs = JobUtil.query_job_status(job_id) + self.assertEqual(1, len(jobs)) + self.assertEqual(3, jobs[0].indexid) + jobs = JobUtil.query_job_status(job_id, 1) + self.assertEqual(2, len(jobs)) + self.assertEqual(3, jobs[0].indexid) + self.assertEqual(2, jobs[1].indexid) -- cgit 1.2.3-korg