|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Tests for `caso.extract.manager` module. |
| 17 | +""" |
| 18 | + |
| 19 | +import uuid |
| 20 | + |
| 21 | +import dateutil.parser |
| 22 | +import mock |
| 23 | + |
| 24 | +from caso.extract import manager |
| 25 | +from caso.tests import base |
| 26 | + |
| 27 | + |
| 28 | +class TestCasoManager(base.TestCase): |
| 29 | + def setUp(self): |
| 30 | + super(TestCasoManager, self).setUp() |
| 31 | + manager.SUPPORTED_EXTRACTORS = {"foo": "foo.Bar"} |
| 32 | + self.flags(extractor="foo") |
| 33 | + self.p_extractor = mock.patch.dict('sys.modules', |
| 34 | + {'foo': mock.MagicMock()}) |
| 35 | + self.p_extractor.start() |
| 36 | + self.manager = manager.Manager() |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + self.p_extractor.stop() |
| 40 | + |
| 41 | + super(TestCasoManager, self).tearDown() |
| 42 | + |
| 43 | + def test_extract_empty_tenants(self): |
| 44 | + self.flags(tenants=[]) |
| 45 | + |
| 46 | + with mock.patch.object(self.manager.extractor, |
| 47 | + "extract_for_tenant") as m: |
| 48 | + self.manager._extract("1999-12-19") |
| 49 | + self.assertFalse(m.called) |
| 50 | + self.assertEqual({}, self.manager.records) |
| 51 | + |
| 52 | + def test_extract(self): |
| 53 | + records = {uuid.uuid4().hex: None} |
| 54 | + self.flags(tenants=["bazonk"]) |
| 55 | + |
| 56 | + with mock.patch.object(self.manager.extractor, |
| 57 | + "extract_for_tenant") as m: |
| 58 | + m.return_value = records |
| 59 | + self.manager._extract("1999-12-19") |
| 60 | + m.assert_called_once_with("bazonk", "1999-12-19") |
| 61 | + self.assertEqual(records, self.manager.records) |
| 62 | + |
| 63 | + def test_get_records_wrong_extract_from(self): |
| 64 | + self.flags(extract_from="1999-12-99") |
| 65 | + self.assertRaises(ValueError, |
| 66 | + self.manager.get_records) |
| 67 | + |
| 68 | + def test_get_records_wrong_lastrun(self): |
| 69 | + self.assertRaises(ValueError, |
| 70 | + self.manager.get_records, |
| 71 | + lastrun="1999-12-99") |
| 72 | + |
| 73 | + def test_get_records_with_extract_from(self): |
| 74 | + date = "1999-12-11" |
| 75 | + dt = dateutil.parser.parse(date) |
| 76 | + self.flags(extract_from=date) |
| 77 | + with mock.patch.object(self.manager, "_extract") as m: |
| 78 | + self.manager.get_records() |
| 79 | + m.assert_called_with(dt) |
| 80 | + |
| 81 | + def test_get_records_with_lastrun(self): |
| 82 | + date = "1999-12-11" |
| 83 | + dt = dateutil.parser.parse(date) |
| 84 | + with mock.patch.object(self.manager, "_extract") as m: |
| 85 | + self.manager.get_records(lastrun=date) |
| 86 | + m.assert_called_with(dt) |
0 commit comments