Matthew Treinish | 9b4d588 | 2013-10-24 20:00:11 +0000 | [diff] [blame] | 1 | # Copyright 2010 United States Government as represented by the |
| 2 | # Administrator of the National Aeronautics and Space Administration. |
| 3 | # Copyright 2013 Hewlett-Packard Development Company, L.P. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import fixtures |
| 19 | import mock |
| 20 | |
| 21 | |
| 22 | class PatchObject(fixtures.Fixture): |
| 23 | """Deal with code around mock.""" |
| 24 | |
Mauro S. M. Rodrigues | 7990598 | 2014-01-13 18:32:46 +0000 | [diff] [blame] | 25 | def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs): |
Matthew Treinish | 9b4d588 | 2013-10-24 20:00:11 +0000 | [diff] [blame] | 26 | self.obj = obj |
| 27 | self.attr = attr |
| 28 | self.kwargs = kwargs |
Mauro S. M. Rodrigues | 7990598 | 2014-01-13 18:32:46 +0000 | [diff] [blame] | 29 | self.new = new |
Matthew Treinish | 9b4d588 | 2013-10-24 20:00:11 +0000 | [diff] [blame] | 30 | |
| 31 | def setUp(self): |
| 32 | super(PatchObject, self).setUp() |
Mauro S. M. Rodrigues | 7990598 | 2014-01-13 18:32:46 +0000 | [diff] [blame] | 33 | _p = mock.patch.object(self.obj, self.attr, self.new, **self.kwargs) |
Matthew Treinish | 9b4d588 | 2013-10-24 20:00:11 +0000 | [diff] [blame] | 34 | self.mock = _p.start() |
| 35 | self.addCleanup(_p.stop) |
| 36 | |
| 37 | |
| 38 | class Patch(fixtures.Fixture): |
| 39 | |
| 40 | """Deal with code around mock.patch.""" |
| 41 | |
| 42 | def __init__(self, obj, **kwargs): |
| 43 | self.obj = obj |
| 44 | self.kwargs = kwargs |
| 45 | |
| 46 | def setUp(self): |
| 47 | super(Patch, self).setUp() |
| 48 | _p = mock.patch(self.obj, **self.kwargs) |
| 49 | self.mock = _p.start() |
| 50 | self.addCleanup(_p.stop) |