2017年8月26日 星期六

[Linux 常見問題] How to print a file, excluding comments and blank lines, using grep/sed?

Source From Here 
Question 
As title with text file as below: 
- test.txt 
  1. This is line1  
  2. # This is the line being commented  
  3.   
  4. This is line2  
The expected output is: 
This is line1
This is line2

How-To 
Try command egrep or grep with argument '-E': 
// -E: Interpret PATTERN as an extended regular expression
// -v, --invert-match: Invert the sense of matching, to select non-matching lines.

# grep -E -v '^$|^\s*#' test.txt
This is line1
This is line2


# egrep -v '^$|^\s*#' test.txt
This is line1
This is line2

2017年8月25日 星期五

[ Python 常見問題 ] Mocking a function to raise an Exception to test an except block

Source From Here 
Question 
I have a function (foo) which calls another function (bar). If invoking bar() raises an HttpError, I want to handle it specially if the status code is 404, otherwise re-raise. I am trying to write some unit tests around this foo function, mocking out the call to bar(). Unfortunately, I am unable to get the mocked call to bar() to raise an Exception which is caught by my except block. 

Here is my code which illustrates my problem: 
- test.py 
  1. import unittest  
  2. from unittest import mock  
  3.   
  4. class HttpError(Exception):  
  5.     def __init__(self, result, msg):  
  6.         super(HttpError, self).__init__(msg)  
  7.         self.result = result  
  8.   
  9. class FooTests(unittest.TestCase):  
  10.     @mock.patch('test.bar')  
  11.     def test_foo_shouldReturnResultOfBar_whenBarSucceeds(self, barMock):  
  12.         barMock.return_value = True  
  13.         result = foo()  
  14.         self.assertTrue(result)  # passes  
  15.   
  16.     @mock.patch('test.bar')  
  17.     def test_foo_shouldReturnNone_whenBarRaiseHttpError404(self, barMock):  
  18.         barMock.side_effect = HttpError(mock.Mock(return_value={'status': 404}), 'not found')  
  19.         result = foo()  
  20.         self.assertIsNone(result)  # fails, test raises HttpError  
  21.   
  22.     @mock.patch('test.bar')  
  23.     def test_foo_shouldRaiseHttpError_whenBarRaiseHttpErrorNot404(self, barMock):  
  24.         barMock.side_effect = HttpError(mock.Mock(return_value={'status': 500}), 'error')  
  25.         with self.assertRaises(HttpError):  # passes  
  26.             foo()  
  27.   
  28. def foo():  
  29.     try:  
  30.         result = bar()  
  31.         return result  
  32.     except HttpError as error:  
  33.         if error.resp.status == 404:  
  34.             print('404 - %s' % error.message)  
  35.             return None  
  36.         raise  
  37.   
  38. def bar():  
  39.     raise NotImplementedError()  
Execution output: 
# pytest -s -v test.py 
... 
collected 3 items 

test.py::FooTests::test_foo_shouldRaiseHttpError_whenBarRaiseHttpErrorNot404 FAILED 
test.py::FooTests::test_foo_shouldReturnNone_whenBarRaiseHttpError404 FAILED 
test.py::FooTests::test_foo_shouldReturnResultOfBar_whenBarSucceeds PASSED 
...

I followed the Mock docs which say that you should set the side_effect of a Mock instance to an Exception class to have the mocked function raise the error. I also looked at some other related StackOverflow Q&As, and it looks like I am doing the same thing they are doing to cause and Exception to be raised by their mock. 

Why is setting the side_effect of bar Mock not causing the expected Exception to be raised? If I am doing something weird, how should I go about testing logic in my except block? 

How-To 
Your mock is raising the exception just fine, but the error.resp.status value is missing. Rather than use return_value, just tell Mock that status is an attribute: 
  1. barMock.side_effect = HttpError(mock.Mock(status=404), 'not found')  
Additional keyword arguments to Mock() are set as attributes on the resulting object. The modified test.py will look like: 
  1. import unittest  
  2. from unittest import mock  
  3.   
  4. class HttpError(Exception):  
  5.     def __init__(self, resp, msg):  
  6.         super(HttpError, self).__init__(msg)  
  7.         self.resp = resp  
  8.         self.message = msg  
  9.   
  10. class FooTests(unittest.TestCase):  
  11.     @mock.patch('test.bar')  
  12.     def test_foo_shouldReturnResultOfBar_whenBarSucceeds(self, barMock):  
  13.         barMock.return_value = True  
  14.         result = foo()  
  15.         self.assertTrue(result)  # passes  
  16.   
  17.     @mock.patch('test.bar')  
  18.     def test_foo_shouldReturnNone_whenBarRaiseHttpError404(self, barMock):  
  19.         barMock.side_effect = HttpError(mock.Mock(status=404), 'not found')  
  20.         result = foo()  
  21.         self.assertIsNone(result)  # fails, test raises HttpError  
  22.   
  23.     @mock.patch('test.bar')  
  24.     def test_foo_shouldRaiseHttpError_whenBarRaiseHttpErrorNot404(self, barMock):  
  25.         barMock.side_effect = HttpError(mock.Mock(status=500), 'error')  
  26.         with self.assertRaises(HttpError):  # passes  
  27.             foo()  
  28.   
  29. def foo():  
  30.     try:  
  31.         result = bar()  
  32.         return result  
  33.     except HttpError as error:  
  34.         if error.resp.status == 404:  
  35.             print('404 - %s' % error.message)  
  36.             return None  
  37.         raise  
  38.   
  39. def bar():  
  40.     raise NotImplementedError()  
The execution output: 
# pytest -s -v test.py 
... 
test.py::FooTests::test_foo_shouldRaiseHttpError_whenBarRaiseHttpErrorNot404 PASSED 
test.py::FooTests::test_foo_shouldReturnNone_whenBarRaiseHttpError404 404 - not found 
PASSED 
test.py::FooTests::test_foo_shouldReturnResultOfBar_whenBarSucceeds PASSED 
...


[ Python 常見問題 ] Using mock patch to mock an instance method

Source From Here 
Question 
I'm trying to mock something while testing a Django app using the imaginatively named Mock testing library. I can't seem to quite get it to work, I'm trying to do this: 
- models.py 
  1. #!/usr/bin/env python3  
  2. class Promotion(object):  
  3.     def __init__(self):  
  4.         pass  
  5.   
  6.     def bar(self):  
  7.         return "Do something I don't want!"  
How do I mock the method bar of the creating object of class Promotion

How-To 
Check below sample code: 
- test.py 
  1. import models  
  2. from unittest import TestCase  
  3. from unittest.mock import patch  
  4.   
  5. class ViewsDoSomething(TestCase):  
  6.     def setUp(self):  
  7.         self.mockMsg = 'Do what I want'  
  8.   
  9.   
  10.     @patch.object(models.Promotion, 'bar')  
  11.     def test_enter_promotion(self, mockObj):  
  12.         mockObj.return_value = self.mockMsg  
  13.   
  14.         p = models.Promotion()  
  15.         self.assertTrue(p.bar() == self.mockMsg)  
Then you can test it this way: 
# pytest -v test.py
...
test.py::ViewsDoSomething::test_enter_promotion PASSED
...


Supplement 
Python 文章收集 - 用 Mock 來做 Python Unit Test 
Python 文章收集 - pytest introduction

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...