|
| 1 | +// Copyright 2025 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package action |
| 12 | + |
| 13 | +import ( |
| 14 | + "github.com/sourcenetwork/immutable" |
| 15 | + |
| 16 | + "github.com/sourcenetwork/defradb/client" |
| 17 | + "github.com/sourcenetwork/defradb/internal/db" |
| 18 | + "github.com/sourcenetwork/defradb/tests/state" |
| 19 | +) |
| 20 | + |
| 21 | +// GetCollections is an action that fetches collections using the provided options. |
| 22 | +// |
| 23 | +// ID, RootID and CollectionVersionID will only be asserted on if an expected value is provided. |
| 24 | +type GetCollections struct { |
| 25 | + stateful |
| 26 | + |
| 27 | + // NodeID may hold the ID (index) of a node to get collections from. |
| 28 | + // |
| 29 | + // If a value is not provided collections will be gotten from all nodes. |
| 30 | + NodeID immutable.Option[int] |
| 31 | + |
| 32 | + // Used to identify the transaction for this to run against. Optional. |
| 33 | + TransactionID immutable.Option[int] |
| 34 | + |
| 35 | + // The identity of this request. Optional. |
| 36 | + // |
| 37 | + // If node acp is enabled, identity will be used to check if this operation can be performed. |
| 38 | + Identity immutable.Option[state.Identity] |
| 39 | + |
| 40 | + // The expected results. |
| 41 | + // |
| 42 | + // Each item will be compared individually, if CollectionID, VersionID, or FieldIDs on the |
| 43 | + // expected item are default they will not be compared with the actual. |
| 44 | + // |
| 45 | + // Assertions on Indexes and Sources will not distinguish between nil and empty (in order |
| 46 | + // to allow their omission in most cases). |
| 47 | + ExpectedResults []client.CollectionVersion |
| 48 | + |
| 49 | + // An optional set of fetch options for the collections. |
| 50 | + FilterOptions client.CollectionFetchOptions |
| 51 | + |
| 52 | + // Any error expected from the action. Optional. |
| 53 | + // |
| 54 | + // String can be a partial, and the test will pass if an error is returned that |
| 55 | + // contains this string. |
| 56 | + ExpectedError string |
| 57 | +} |
| 58 | + |
| 59 | +var _ Action = (*GetCollections)(nil) |
| 60 | +var _ Stateful = (*GetCollections)(nil) |
| 61 | + |
| 62 | +// Execute executes the get collections action. |
| 63 | +func (a *GetCollections) Execute() { |
| 64 | + // Collect transform strings from expected results for lens ID replacement |
| 65 | + transformSet := []string{} |
| 66 | + for _, col := range a.ExpectedResults { |
| 67 | + if col.PreviousVersion.HasValue() && col.PreviousVersion.Value().Transform.HasValue() { |
| 68 | + transformSet = append(transformSet, col.PreviousVersion.Value().Transform.Value()) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // The lens IDs are consistent across nodes, so we can patch once for all nodes. |
| 73 | + // This will need to change if patches want to replace more than just lens IDs. |
| 74 | + if len(transformSet) > 0 { |
| 75 | + transformMap := replaceMap(a.s, 0, transformSet) |
| 76 | + |
| 77 | + for i, col := range a.ExpectedResults { |
| 78 | + if col.PreviousVersion.HasValue() && col.PreviousVersion.Value().Transform.HasValue() { |
| 79 | + a.ExpectedResults[i].PreviousVersion = immutable.Some( |
| 80 | + client.CollectionSource{ |
| 81 | + SourceCollectionID: a.ExpectedResults[i].PreviousVersion.Value().SourceCollectionID, |
| 82 | + Transform: immutable.Some(transformMap[col.PreviousVersion.Value().Transform.Value()]), |
| 83 | + }, |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + nodeIDs, nodes := getNodesWithIDs(a.NodeID, a.s.Nodes) |
| 90 | + for index, node := range nodes { |
| 91 | + nodeID := nodeIDs[index] |
| 92 | + txn, err := a.s.GetTransaction(node, a.TransactionID) |
| 93 | + if assertError(a.s.T, err, a.ExpectedError) { |
| 94 | + return |
| 95 | + } |
| 96 | + ctx := db.InitContext(a.s.Ctx, txn) |
| 97 | + ctx = getContextWithIdentity(ctx, a.s, a.Identity, nodeID) |
| 98 | + |
| 99 | + results, err := node.GetCollections(ctx, a.FilterOptions) |
| 100 | + resultDescriptions := make([]client.CollectionVersion, len(results)) |
| 101 | + for i, col := range results { |
| 102 | + resultDescriptions[i] = col.Version() |
| 103 | + } |
| 104 | + |
| 105 | + expectedErrorRaised := assertError(a.s.T, err, a.ExpectedError) |
| 106 | + assertExpectedErrorRaised(a.s.T, a.ExpectedError, expectedErrorRaised) |
| 107 | + |
| 108 | + if !expectedErrorRaised { |
| 109 | + assertCollectionVersions(a.s, a.ExpectedResults, resultDescriptions) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments