Skip to content

Commit

Permalink
test(i): Extend mutation tests with col.Update and Create (sourcenetw…
Browse files Browse the repository at this point in the history
…ork#1838)

## Relevant issue(s)

Resolves sourcenetwork#1832

## Description

Extends mutation tests with collection.Update and Create calls for the
corresponding actions. Adds another github workflow to run the tests.
  • Loading branch information
AndrewSisley authored Aug 31, 2023
1 parent 8a8582b commit 6bd4ee5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/test-collection-named.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2023 Democratized Data Foundation
#
# Use of this software is governed by the Business Source License
# included in the file licenses/BSL.txt.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0, included in the file
# licenses/APL.txt.

name: Run Collection Named Mutations Tests Workflow

# This workflow runs the test suite with any supporting mutation test actions
# running their mutations via their corresponding named [Collection] call.
#
# For example, CreateDoc will call [Collection.Create], and
# UpdateDoc will call [Collection.Update].

on:
pull_request:
branches:
- master
- develop

push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
branches:
- master
- develop

jobs:
test-collection-named-mutations:
name: Test Collection Named Mutations job

runs-on: ubuntu-latest

steps:
- name: Checkout code into the directory
uses: actions/checkout@v3

- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.20"
check-latest: true

- name: Build dependencies
run: |
make deps:modules
make deps:test
- name: Run tests with Collection Named mutations
run: make test:ci-col-named-mutations
7 changes: 0 additions & 7 deletions .github/workflows/test-gql-mutations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,3 @@ jobs:
- name: Run tests with gql mutations
run: make test:ci-gql-mutations

## Uncomment to enable ability to SSH into the runner.
#- name: Setup upterm ssh session for debugging
# uses: lhotari/action-upterm@v1
# with:
# limit-access-to-actor: true
# limit-access-to-users: shahzadlone
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ test\:ci-gql-mutations:
test\:gql-mutations:
DEFRA_MUTATION_TYPE=gql DEFRA_BADGER_MEMORY=true gotestsum --format pkgname -- $(DEFAULT_TEST_DIRECTORIES)

# This action and the test:col-named-mutations (below) runs the test suite with any supporting mutation test
# actions running their mutations via their corresponding named [Collection] call.
#
# For example, CreateDoc will call [Collection.Create], and
# UpdateDoc will call [Collection.Update].
.PHONY: test\:ci-col-named-mutations
test\:ci-col-named-mutations:
DEFRA_MUTATION_TYPE=collection-named DEFRA_BADGER_MEMORY=true $(MAKE) test:all

.PHONY: test\:col-named-mutations
test\:col-named-mutations:
DEFRA_MUTATION_TYPE=collection-named DEFRA_BADGER_MEMORY=true gotestsum --format pkgname -- $(DEFAULT_TEST_DIRECTORIES)

.PHONY: test\:go
test\:go:
go test $(DEFAULT_TEST_DIRECTORIES) $(TEST_FLAGS)
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ const (
// to run their mutations via [Collection.Save].
CollectionSaveMutationType MutationType = "collection-save"

// CollectionNamedMutationType will cause all supporting actions
// to run their mutations via their corresponding named [Collection]
// call.
//
// For example, CreateDoc will call [Collection.Create], and
// UpdateDoc will call [Collection.Update].
CollectionNamedMutationType MutationType = "collection-named"

// GQLRequestMutationType will cause all supporting actions to
// run their mutations using GQL requests, typically these will
// include a `id` parameter to target the specified document.
Expand Down Expand Up @@ -1040,6 +1048,8 @@ func createDoc(
switch mutationType {
case CollectionSaveMutationType:
mutation = createDocViaColSave
case CollectionNamedMutationType:
mutation = createDocViaColCreate
case GQLRequestMutationType:
mutation = createDocViaGQL
default:
Expand Down Expand Up @@ -1086,6 +1096,21 @@ func createDocViaColSave(
return doc, collections[action.CollectionID].Save(s.ctx, doc)
}

func createDocViaColCreate(
s *state,
action CreateDoc,
node *net.Node,
collections []client.Collection,
) (*client.Document, error) {
var err error
doc, err := client.NewDocFromJSON([]byte(action.Doc))
if err != nil {
return nil, err
}

return doc, collections[action.CollectionID].Create(s.ctx, doc)
}

func createDocViaGQL(
s *state,
action CreateDoc,
Expand Down Expand Up @@ -1166,6 +1191,8 @@ func updateDoc(
switch mutationType {
case CollectionSaveMutationType:
mutation = updateDocViaColSave
case CollectionNamedMutationType:
mutation = updateDocViaColUpdate
case GQLRequestMutationType:
mutation = updateDocViaGQL
default:
Expand Down Expand Up @@ -1202,6 +1229,22 @@ func updateDocViaColSave(
return collections[action.CollectionID].Save(s.ctx, doc)
}

func updateDocViaColUpdate(
s *state,
action UpdateDoc,
node *net.Node,
collections []client.Collection,
) error {
doc := s.documents[action.CollectionID][action.DocID]

err := doc.SetWithJSON([]byte(action.Doc))
if err != nil {
return err
}

return collections[action.CollectionID].Update(s.ctx, doc)
}

func updateDocViaGQL(
s *state,
action UpdateDoc,
Expand Down

0 comments on commit 6bd4ee5

Please sign in to comment.