2019年2月11日 星期一

how to write python unit test mock

1. mock function
@patch('file.module.function', Mock(return_value=True)
def test_xxx(self):
self.assertEqual(testFun(), True)

2 mock class instance
如果bar()回傳的是Class Bar的instance的話, 就使用magicmock, magicmock會自動mock class的method

myMock = mock.MagicMock()
@patch('file.module.bar', Mock(return_value=myMock)
def test_xxx(self):
self.assertEqual(testFun(), True)

3.
假設Bar有一個method toString()
def foo():
x = Bar()
x.toString()

myMock = mock.MagicMock()
myMock.toString.return_value = 'bar'
@patch('file.module.bar', Mock(return_value=myMock)
def test_xxx(self):
self.assertEqual(testFun(), True)

4. patch dict

class FooTest(unittest.testcase):
   def setUp(self):
        patches = {
              'app.a.bar', Mock(),
              'app.b.bar', Mock(),
       }
        [mock.patch(func, mock).start() for func, mock in patches.items()]


5 fixture
>>>> conftest.py <<<<
import pytest
@pytest.fixture(scope=session)
def init_test_env():
    os.append()


2018年7月3日 星期二

多重github帳號

1. 產生兩把rsa key並設定ssh config
~/.ssh/config

#gmail account
HOST github.com-gmail
    HostName github.com
    User git
    IdentityFile ~/.ssh/gmail_rsa
#hotmail account
HOST github.com-hotmail
        HostName github.com
        User git
        IdentityFile ~/.ssh/hotmail_rsa

2. 修改/.git/config
將URL從 github.com改成 github.com-gmail or github.com-hotmail

2015年10月20日 星期二

Color of QTextEdit

Palett = QtGui.QPalette()
Palett.setColor(QtGui.QPalette.Base,  QtCore.Qt.black)
Palett.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
self.setPalette(Palett)

QT use palett to set color of widget, and Base mean base color of widget
this is different with background

PyQt5 install

Install PyQt5.5 on Macos

Macos: 10.10.5
Python: 3.4.3
PyQt: 5.5
https://riverbankcomputing.com/software/pyqt/download5
download source
http://sourceforge.net/projects/pyqt/files/sip/sip-4.16.9/sip-4.16.9.tar.gz/download

install Qt5
add qmake path
sudo vi /etc/paths
add '/Users/xxx/Qt5.5.0/5.5/clang_64/bin

cd Path/of/PyQt
python3 configure.py
make
make install
(install to /Library/Frameworks/Python.framework)


#! /Library/Frameworks/Python.framework/Versions/3.4/bin/python3
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Editor(QMainWindow):
    def __init__(self, parent=None):
        super(Editor, self).__init__(parent)
        self.text = QTextEdit(self)
        self.setCentralWidget(self.text)
        self.setWindowTitle("PyEditor")

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    mainWindow = Editor()
    mainWindow.show()

    sys.exit(app.exec_())

2013年12月22日 星期日

mount disk permission problem

today, I mount a disk by user 1, and the other user cann't rw this disk,

so, i modify permission and ownship (chmod and chown), but it's did work, the value was change, but i still didn't read/write disk.

finally, i restart computer, then the new configure have update.


1 auto mount
1.1 find out uuid of disk
     #sudo blkid
1.2 mount by uuid

other condition is modify permission and ownship can pass, but the value didn't change, this may the format of File system not support group, in the other word, it is not a file system of Linux system, it may be FAT or NTFS, in this case, you should umount disk and mount system in new parameter.

2013年6月24日 星期一

Quartus 11.1, 13.0sp1 on Ubuntu 64bit



sudo apt-get upgrade
sudo apt-get install ia32-libs
./11.1_173_quartus_linux.sh
bash /11.1_173_quartus_linux/setup

error 1: ./setup: Syntax error: "&" unexpected
出現這個錯誤是因為quartus xxxx.sh被設定成/bin/sh,但其實是用bash寫的,所以只需改成bash執行即可。

最近Altera 更新了Quartus 13.0 sp1, 我也順便更新下我的系統 時發現 Altera終於修正了 Quartus 11的問題,所以現在只用無腦的雙擊setup.sh(改名了) Quartus 13就能順利的安裝。

2013年4月15日 星期一

SVN on Ubuntu 12.10


使用svn co https://xxx.xxx/xxx時出現SSL handshake failed: SSL error: Key usage violation in certificate has been detected.


原來是ssl版本的問題,
$sudo apt-get install libneon27
$sudo mv /usr/lib/libneon-gnutls.so.27 /usr/lib/libneon-gnutls.so.27.old
$sudo ln -s /usr/lib/libneon.so.27 /usr/lib/libneon-gnutls.so.27 

Reference
http://stackoverflow.com/questions/10062095/svn-on-ubuntu-sslhandshake-failed