|
| 1 | +import { |
| 2 | + BigQuery, |
| 3 | + Dataset, |
| 4 | + TableMetadata, |
| 5 | + Table, |
| 6 | +} from "@google-cloud/bigquery"; |
| 7 | +import { firestore } from "firebase-admin"; |
| 8 | +import { RawChangelogViewSchema } from "../../../bigquery/schema"; |
| 9 | +import { initializeLatestMaterializedView } from "../../../bigquery/initializeLatestMaterializedView"; |
| 10 | +import { |
| 11 | + changeTracker, |
| 12 | + changeTrackerEvent, |
| 13 | +} from "../../fixtures/changeTracker"; |
| 14 | +import { deleteTable } from "../../fixtures/clearTables"; |
| 15 | +import * as logs from "../../../logs"; |
| 16 | + |
| 17 | +jest.mock("../../../logs"); |
| 18 | +// jest.mock("sql-formatter"); |
| 19 | + |
| 20 | +describe("initializeLatestMaterializedView", () => { |
| 21 | + const projectId = "dev-extensions-testing"; |
| 22 | + const bq = new BigQuery({ projectId }); |
| 23 | + |
| 24 | + let dataset: Dataset; |
| 25 | + let table: Table; |
| 26 | + let testConfig: { |
| 27 | + datasetId: string; |
| 28 | + tableId: string; |
| 29 | + tableIdRaw: string; |
| 30 | + viewIdRaw: string; |
| 31 | + }; |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + const randomId = (Math.random() + 1).toString(36).substring(7); |
| 35 | + testConfig = { |
| 36 | + datasetId: `dataset_${randomId}`, |
| 37 | + tableId: `table_${randomId}`, |
| 38 | + tableIdRaw: `table_${randomId}_raw_changelog`, |
| 39 | + viewIdRaw: `table_${randomId}_raw_latest`, |
| 40 | + }; |
| 41 | + dataset = bq.dataset(testConfig.datasetId); |
| 42 | + table = dataset.table(testConfig.tableIdRaw); |
| 43 | + |
| 44 | + await dataset.create(); |
| 45 | + |
| 46 | + await table.create({ schema: RawChangelogViewSchema }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await deleteTable({ datasetId: testConfig.datasetId }); |
| 51 | + }); |
| 52 | + |
| 53 | + test("creates a new materialized view when view does not exist", async () => { |
| 54 | + const view = dataset.table(testConfig.viewIdRaw); |
| 55 | + const config = { |
| 56 | + datasetId: testConfig.datasetId, |
| 57 | + tableId: testConfig.tableId, |
| 58 | + useMaterializedView: true, |
| 59 | + useIncrementalMaterializedView: false, |
| 60 | + maxStaleness: `INTERVAL "4:0:0" HOUR TO SECOND`, |
| 61 | + refreshIntervalMinutes: 5, |
| 62 | + clustering: null, |
| 63 | + }; |
| 64 | + |
| 65 | + await initializeLatestMaterializedView({ |
| 66 | + bq, |
| 67 | + changeTrackerConfig: config, |
| 68 | + view, |
| 69 | + viewExists: false, |
| 70 | + rawChangeLogTableName: testConfig.tableIdRaw, |
| 71 | + rawLatestViewName: testConfig.viewIdRaw, |
| 72 | + schema: RawChangelogViewSchema, |
| 73 | + }); |
| 74 | + |
| 75 | + const [metadata] = (await view.getMetadata()) as unknown as [TableMetadata]; |
| 76 | + expect(metadata.materializedView).toBeDefined(); |
| 77 | + expect(metadata.materializedView?.enableRefresh).toBe(true); |
| 78 | + expect( |
| 79 | + metadata.materializedView?.allowNonIncrementalDefinition |
| 80 | + ).toBeDefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + test("does not recreate view if configuration matches", async () => { |
| 84 | + const event = changeTrackerEvent({ |
| 85 | + data: { end_date: firestore.Timestamp.now() }, |
| 86 | + eventId: "testing2", |
| 87 | + }); |
| 88 | + |
| 89 | + await changeTracker({ |
| 90 | + datasetId: testConfig.datasetId, |
| 91 | + tableId: testConfig.tableId, |
| 92 | + useMaterializedView: true, |
| 93 | + useIncrementalMaterializedView: true, |
| 94 | + }).record([event]); |
| 95 | + |
| 96 | + const view = dataset.table(testConfig.viewIdRaw); |
| 97 | + const config = { |
| 98 | + datasetId: testConfig.datasetId, |
| 99 | + tableId: testConfig.tableId, |
| 100 | + useMaterializedView: true, |
| 101 | + useIncrementalMaterializedView: true, |
| 102 | + clustering: null, |
| 103 | + }; |
| 104 | + |
| 105 | + const [initialMetadata] = (await view.getMetadata()) as unknown as [ |
| 106 | + TableMetadata |
| 107 | + ]; |
| 108 | + |
| 109 | + await initializeLatestMaterializedView({ |
| 110 | + bq, |
| 111 | + changeTrackerConfig: config, |
| 112 | + view, |
| 113 | + viewExists: true, |
| 114 | + rawChangeLogTableName: testConfig.tableIdRaw, |
| 115 | + rawLatestViewName: testConfig.viewIdRaw, |
| 116 | + schema: RawChangelogViewSchema, |
| 117 | + }); |
| 118 | + |
| 119 | + const [finalMetadata] = (await view.getMetadata()) as unknown as [ |
| 120 | + TableMetadata |
| 121 | + ]; |
| 122 | + expect(finalMetadata).toEqual(initialMetadata); |
| 123 | + }); |
| 124 | + |
| 125 | + test("recreates view when switching from incremental to non-incremental", async () => { |
| 126 | + const event = changeTrackerEvent({ |
| 127 | + data: { end_date: firestore.Timestamp.now() }, |
| 128 | + eventId: "testing3", |
| 129 | + }); |
| 130 | + |
| 131 | + await changeTracker({ |
| 132 | + datasetId: testConfig.datasetId, |
| 133 | + tableId: testConfig.tableId, |
| 134 | + useMaterializedView: true, |
| 135 | + useIncrementalMaterializedView: true, |
| 136 | + }).record([event]); |
| 137 | + |
| 138 | + const view = dataset.table(testConfig.viewIdRaw); |
| 139 | + const newConfig = { |
| 140 | + datasetId: testConfig.datasetId, |
| 141 | + tableId: testConfig.tableId, |
| 142 | + useMaterializedView: true, |
| 143 | + maxStaleness: `INTERVAL "4:0:0" HOUR TO SECOND`, |
| 144 | + refreshIntervalMinutes: 5, |
| 145 | + clustering: null, |
| 146 | + }; |
| 147 | + |
| 148 | + const [initialMetadata] = (await view.getMetadata()) as unknown as [ |
| 149 | + TableMetadata |
| 150 | + ]; |
| 151 | + expect( |
| 152 | + initialMetadata.materializedView?.allowNonIncrementalDefinition |
| 153 | + ).toBeUndefined(); |
| 154 | + |
| 155 | + await initializeLatestMaterializedView({ |
| 156 | + bq, |
| 157 | + changeTrackerConfig: newConfig, |
| 158 | + view, |
| 159 | + viewExists: true, |
| 160 | + rawChangeLogTableName: testConfig.tableIdRaw, |
| 161 | + rawLatestViewName: testConfig.viewIdRaw, |
| 162 | + schema: RawChangelogViewSchema, |
| 163 | + }); |
| 164 | + |
| 165 | + const [finalMetadata] = (await view.getMetadata()) as unknown as [ |
| 166 | + TableMetadata |
| 167 | + ]; |
| 168 | + expect( |
| 169 | + finalMetadata.materializedView?.allowNonIncrementalDefinition |
| 170 | + ).toBeDefined(); |
| 171 | + }); |
| 172 | + |
| 173 | + test("handles view creation errors", async () => { |
| 174 | + const view = dataset.table(testConfig.viewIdRaw); |
| 175 | + const invalidConfig = { |
| 176 | + datasetId: testConfig.datasetId, |
| 177 | + tableId: testConfig.tableId, |
| 178 | + useMaterializedView: true, |
| 179 | + maxStaleness: "invalid", |
| 180 | + clustering: null, |
| 181 | + }; |
| 182 | + |
| 183 | + await expect( |
| 184 | + initializeLatestMaterializedView({ |
| 185 | + bq, |
| 186 | + changeTrackerConfig: invalidConfig, |
| 187 | + view, |
| 188 | + viewExists: false, |
| 189 | + rawChangeLogTableName: testConfig.tableIdRaw, |
| 190 | + rawLatestViewName: testConfig.viewIdRaw, |
| 191 | + schema: RawChangelogViewSchema, |
| 192 | + }) |
| 193 | + ).rejects.toThrow(); |
| 194 | + |
| 195 | + expect(logs.tableCreationError).toHaveBeenCalled(); |
| 196 | + }); |
| 197 | +}); |
0 commit comments