| 1 | import * as GCP from "@/GCP"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import * as bigquery from "@distilled.cloud/gcp/bigquery_v2"; |
| 4 | import { expect } from "alchemy-test"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | |
| 9 | const { test } = Test.make({ providers: GCP.providers() }); |
| 10 | |
| 11 | const logLevel = Effect.provideService( |
| 12 | MinimumLogLevel, |
| 13 | process.env.DEBUG ? "Debug" : "Info", |
| 14 | ); |
| 15 | |
| 16 | const waitUntilGone = ( |
| 17 | projectId: string, |
| 18 | datasetId: string, |
| 19 | tableId: string, |
| 20 | policyId: string, |
| 21 | ) => |
| 22 | bigquery |
| 23 | .getRowAccessPolicies({ |
| 24 | projectId, |
| 25 | datasetId, |
| 26 | tableId, |
| 27 | policyId, |
| 28 | }) |
| 29 | .pipe( |
| 30 | Effect.as("found" as const), |
| 31 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("1 second"), |
| 34 | until: (status) => status === "gone", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider( |
| 40 | "create, update, and delete a row access policy", |
| 41 | (stack) => |
| 42 | Effect.gen(function* () { |
| 43 | yield* stack.destroy(); |
| 44 | |
| 45 | const created = yield* stack.deploy( |
| 46 | Effect.gen(function* () { |
| 47 | const dataset = yield* GCP.BigQuery.Dataset("Analytics", { |
| 48 | location: "US-CENTRAL1", |
| 49 | forceDestroy: true, |
| 50 | }); |
| 51 | const table = yield* GCP.BigQuery.Table("Events", { |
| 52 | datasetId: dataset.datasetId, |
| 53 | schema: [{ name: "nullable_field", type: "STRING" }], |
| 54 | }); |
| 55 | return yield* GCP.BigQuery.RowAccessPolicy("Visible", { |
| 56 | datasetId: dataset.datasetId, |
| 57 | tableId: table.tableId, |
| 58 | filterPredicate: "nullable_field IS NOT NULL", |
| 59 | }); |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | expect(created.policyId).toEqual(expect.any(String)); |
| 64 | expect(created.policyId).toMatch(/^[a-zA-Z0-9_]+$/); |
| 65 | expect(created.datasetId).toEqual(expect.any(String)); |
| 66 | expect(created.tableId).toEqual(expect.any(String)); |
| 67 | expect(created.project).toEqual(expect.any(String)); |
| 68 | expect(created.filterPredicate).toEqual("nullable_field IS NOT NULL"); |
| 69 | |
| 70 | const fetched = yield* bigquery.getRowAccessPolicies({ |
| 71 | projectId: created.project, |
| 72 | datasetId: created.datasetId, |
| 73 | tableId: created.tableId, |
| 74 | policyId: created.policyId, |
| 75 | }); |
| 76 | expect(fetched.rowAccessPolicyReference?.policyId).toEqual( |
| 77 | created.policyId, |
| 78 | ); |
| 79 | // The predicate is sent verbatim — no ownership tautology. |
| 80 | expect(fetched.filterPredicate).toEqual("nullable_field IS NOT NULL"); |
| 81 | |
| 82 | const updated = yield* stack.deploy( |
| 83 | Effect.gen(function* () { |
| 84 | const dataset = yield* GCP.BigQuery.Dataset("Analytics", { |
| 85 | datasetId: created.datasetId, |
| 86 | location: "US-CENTRAL1", |
| 87 | forceDestroy: true, |
| 88 | }); |
| 89 | const table = yield* GCP.BigQuery.Table("Events", { |
| 90 | datasetId: dataset.datasetId, |
| 91 | tableId: created.tableId, |
| 92 | schema: [{ name: "nullable_field", type: "STRING" }], |
| 93 | }); |
| 94 | return yield* GCP.BigQuery.RowAccessPolicy("Visible", { |
| 95 | datasetId: dataset.datasetId, |
| 96 | tableId: table.tableId, |
| 97 | policyId: created.policyId, |
| 98 | filterPredicate: "TRUE", |
| 99 | }); |
| 100 | }), |
| 101 | ); |
| 102 | |
| 103 | expect(updated.name).toEqual(created.name); |
| 104 | expect(updated.policyId).toEqual(created.policyId); |
| 105 | expect(updated.filterPredicate).toEqual("TRUE"); |
| 106 | |
| 107 | const fetchedUpdate = yield* bigquery.getRowAccessPolicies({ |
| 108 | projectId: created.project, |
| 109 | datasetId: created.datasetId, |
| 110 | tableId: created.tableId, |
| 111 | policyId: created.policyId, |
| 112 | }); |
| 113 | expect(fetchedUpdate.filterPredicate).toEqual("TRUE"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone( |
| 118 | created.project, |
| 119 | created.datasetId, |
| 120 | created.tableId, |
| 121 | created.policyId, |
| 122 | ); |
| 123 | expect(gone).toEqual("gone"); |
| 124 | }).pipe(logLevel), |
| 125 | { tags: ["provider:gcp", "provider:gcp:bigquery", "live"], timeout: 90_000 }, |
| 126 | ); |