|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + mockServices, |
| 19 | + TestDatabaseId, |
| 20 | + TestDatabases, |
| 21 | +} from '@backstage/backend-test-utils'; |
| 22 | +import { DatabaseMetricValuesStore } from './DatabaseMetricValuesStore'; |
| 23 | +import { DbMetricValue } from './MetricValuesStore'; |
| 24 | +import { migrate } from './migration'; |
| 25 | + |
| 26 | +const metricValues: Omit<DbMetricValue, 'id'>[] = [ |
| 27 | + { |
| 28 | + catalog_entity_ref: 'component:default/test-service', |
| 29 | + metric_id: 'github.metric1', |
| 30 | + value: 41, |
| 31 | + timestamp: new Date('2023-01-01T00:00:00Z'), |
| 32 | + error_message: undefined, |
| 33 | + }, |
| 34 | + { |
| 35 | + catalog_entity_ref: 'component:default/another-service', |
| 36 | + metric_id: 'github.metric1', |
| 37 | + value: 25, |
| 38 | + timestamp: new Date('2023-01-01T00:00:00Z'), |
| 39 | + error_message: undefined, |
| 40 | + }, |
| 41 | + { |
| 42 | + catalog_entity_ref: 'component:default/another-service', |
| 43 | + metric_id: 'github.metric2', |
| 44 | + value: undefined, |
| 45 | + timestamp: new Date('2023-01-01T00:00:00Z'), |
| 46 | + error_message: 'Failed to fetch metric', |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +describe('DatabaseMetricValuesStore', () => { |
| 51 | + const databases = TestDatabases.create({ |
| 52 | + ids: ['POSTGRES_13', 'SQLITE_3'], |
| 53 | + }); |
| 54 | + |
| 55 | + async function createDatabase(databaseId: TestDatabaseId) { |
| 56 | + const knex = await databases.init(databaseId); |
| 57 | + const mockDatabaseService = mockServices.database.mock({ |
| 58 | + getClient: async () => knex, |
| 59 | + migrations: { skip: false }, |
| 60 | + }); |
| 61 | + |
| 62 | + await migrate(mockDatabaseService); |
| 63 | + return { |
| 64 | + knex, |
| 65 | + db: new DatabaseMetricValuesStore(knex), |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + describe('createMetricValues', () => { |
| 70 | + it.each(databases.eachSupportedId())( |
| 71 | + 'should successfully insert metric values - %p', |
| 72 | + async databaseId => { |
| 73 | + const { knex, db } = await createDatabase(databaseId); |
| 74 | + |
| 75 | + await expect( |
| 76 | + db.createMetricValues(metricValues), |
| 77 | + ).resolves.not.toThrow(); |
| 78 | + |
| 79 | + const insertedValues = await knex('metric_values').select('*'); |
| 80 | + expect(insertedValues).toHaveLength(3); |
| 81 | + expect(insertedValues[0].catalog_entity_ref).toBe( |
| 82 | + 'component:default/test-service', |
| 83 | + ); |
| 84 | + expect(insertedValues[0].metric_id).toBe('github.metric1'); |
| 85 | + expect(insertedValues[0].value).toBe(41); |
| 86 | + expect(insertedValues[0].error_message).toBe(null); |
| 87 | + expect(insertedValues[1].value).toBe(25); |
| 88 | + expect(insertedValues[2].metric_id).toBe('github.metric2'); |
| 89 | + expect(insertedValues[2].value).toBe(null); |
| 90 | + expect(insertedValues[2].error_message).toBe('Failed to fetch metric'); |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + describe('readLatestEntityMetricValues', () => { |
| 95 | + it.each(databases.eachSupportedId())( |
| 96 | + 'should return latest metric values for entity and metrics - %p', |
| 97 | + async databaseId => { |
| 98 | + const { knex, db } = await createDatabase(databaseId); |
| 99 | + |
| 100 | + const baseTime = new Date('2023-01-01T00:00:00Z'); |
| 101 | + const laterTime = new Date('2023-01-01T01:00:00Z'); |
| 102 | + |
| 103 | + await knex('metric_values').insert([ |
| 104 | + { |
| 105 | + ...metricValues[0], |
| 106 | + timestamp: baseTime, // older time |
| 107 | + }, |
| 108 | + { |
| 109 | + ...metricValues[1], |
| 110 | + timestamp: laterTime, // newer time, value should be returned |
| 111 | + }, |
| 112 | + { |
| 113 | + ...metricValues[2], |
| 114 | + timestamp: laterTime, // newer time, different entity |
| 115 | + }, |
| 116 | + { |
| 117 | + catalog_entity_ref: 'component:default/test-service', |
| 118 | + metric_id: 'github.metric2', |
| 119 | + value: undefined, |
| 120 | + timestamp: baseTime, |
| 121 | + error_message: 'Failed to fetch metric', |
| 122 | + }, |
| 123 | + ]); |
| 124 | + |
| 125 | + const result = await db.readLatestEntityMetricValues( |
| 126 | + 'component:default/test-service', |
| 127 | + ['github.metric1', 'github.metric2'], |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result).toHaveLength(2); |
| 131 | + |
| 132 | + const metric1Result = result.find( |
| 133 | + r => r.metric_id === 'github.metric1', |
| 134 | + ); |
| 135 | + const metric2Result = result.find( |
| 136 | + r => r.metric_id === 'github.metric2', |
| 137 | + ); |
| 138 | + |
| 139 | + expect(metric1Result).toBeDefined(); |
| 140 | + expect(metric1Result!.value).toBe(41); |
| 141 | + expect(metric1Result!.catalog_entity_ref).toBe( |
| 142 | + 'component:default/test-service', |
| 143 | + ); |
| 144 | + |
| 145 | + expect(metric2Result).toBeDefined(); |
| 146 | + expect(metric2Result!.value).toBe(null); |
| 147 | + expect(metric2Result!.catalog_entity_ref).toBe( |
| 148 | + 'component:default/test-service', |
| 149 | + ); |
| 150 | + }, |
| 151 | + ); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments