"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/tests/db/test_fingerprint.py" (13 May 2020, 2734 Bytes) of package /linux/misc/openstack/monasca-api-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_fingerprint.py": 3.1.0_vs_4.0.0.

    1 # Copyright 2018 StackHPC Ltd.
    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 import hashlib
   16 from unittest import mock
   17 
   18 import monasca_api.db.fingerprint as fingerprint
   19 from monasca_api.tests import base
   20 
   21 
   22 class TestFingerprint(base.BaseTestCase):
   23 
   24     @mock.patch('monasca_api.db.fingerprint.Fingerprint._get_metadata')
   25     @mock.patch('monasca_api.db.fingerprint.Fingerprint._get_schema_raw')
   26     def test_get_schema_raw_pre_alembic(self, mock_schema_raw, mock_metadata):
   27         mock_schema_raw.return_value = 'dummy_schema_raw'
   28 
   29         tables = mock.PropertyMock = {
   30             'dummy_table': 'dummy_columns'
   31         }
   32         mock_metadata.return_value.tables = tables
   33 
   34         # No Alembic revision ID exists in the DB so we look it up from the
   35         # table of fingerprints. Since we use a dummy schema, we insert a dummy
   36         # entry into the lookup table.
   37         fingerprint._REVS[
   38             hashlib.sha1(b'dummy_schema_raw').hexdigest()] = 'dummy_revision'
   39 
   40         f = fingerprint.Fingerprint('mock_engine')
   41         self.assertEqual(f.schema_raw, 'dummy_schema_raw')
   42         self.assertEqual(f.sha1, hashlib.sha1(b'dummy_schema_raw').hexdigest())
   43         self.assertEqual(f.revision, 'dummy_revision')
   44 
   45     @mock.patch('monasca_api.db.fingerprint.Fingerprint._get_db_session')
   46     @mock.patch('monasca_api.db.fingerprint.Fingerprint._get_metadata')
   47     @mock.patch('monasca_api.db.fingerprint.Fingerprint._get_schema_raw')
   48     def test_get_schema_raw_post_alembic(
   49             self, mock_schema_raw, mock_metadata, mock_db_session):
   50         mock_schema_raw.return_value = 'dummy_schema_raw'
   51 
   52         tables = mock.PropertyMock = {
   53             'alembic_version': 'dummy_version',
   54             'dummy_table': 'dummy_columns'
   55         }
   56         mock_metadata.return_value.tables = tables
   57 
   58         # Alembic sets the version in the DB, so we look it up from there
   59         mock_db_session.return_value.query.return_value.one.return_value = (
   60             'dummy_revision',)
   61 
   62         f = fingerprint.Fingerprint('mock_engine')
   63         self.assertEqual(f.schema_raw, 'dummy_schema_raw')
   64         self.assertEqual(f.sha1, hashlib.sha1(b'dummy_schema_raw').hexdigest())
   65         self.assertEqual(f.revision, 'dummy_revision')