Skip to content

Commit

Permalink
Added integration test for hasura.
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuwei committed Apr 25, 2023
1 parent 98c3b0c commit 48af04a
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Dependency directories (remove the comment below to include it)
vendor/
.env*
.env
script.py
dist/
node_modules/
14 changes: 14 additions & 0 deletions build/dipdup.testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ hasura:
isolation_level: read-committed
rest: true

hasura:
url: http://${HASURA_HOST:-hasura}:${HASURA_PORT:-8080}
admin_secret: ${ADMIN_SECRET:-changeme}
select_limit: 100
allow_aggregation: false
# source: default
add_source: true
source:
name: default
db_host: ${HASURA_POSTGRES_HOST:-none}
use_prepared_statements: true
isolation_level: read-committed
rest: true

prometheus:
url: ${PROMETHEUS_BIND:-0.0.0.0:2112}

Expand Down
55 changes: 55 additions & 0 deletions build/expected_metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
tables:
-
name: activate_account
columns:
- created_at
- updated_at
- network
- hash
- branch
- status
- kind
- signature
- protocol
- level
- errors
- expiration_level
- raw
- pkh
- secret
-
name: ballots
columns:
- created_at
- updated_at
- network
- hash
- branch
- status
- kind
- signature
- protocol
- level
- errors
- expiration_level
- raw
- period
- ballot
-


# Mempool Operation

# - created_at
# - updated_at
# - network
# - hash
# - branch
# - status
# - kind
# - signature
# - protocol
# - level
# - errors
# - expiration_level
# - raw
85 changes: 85 additions & 0 deletions cmd/mempool/hasura_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"context"
libCfg "github.com/dipdup-net/go-lib/config"
"github.com/dipdup-net/go-lib/hasura"
"github.com/dipdup-net/mempool/cmd/mempool/config"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"testing"
)

func TestIntegration_HasuraMetadata(t *testing.T) {
// read config
configPath := "../../build/dipdup.testnet.yml" // todo: Fix paths
var cfg config.Config
if err := libCfg.Parse(configPath, &cfg); err != nil {
log.Err(err).Msg("") // or fail
return
}

integrationHelpers := hasura.NewIntegrationHelpers(cfg.Hasura)

ctx, _ := context.WithCancel(context.Background())

metadata, err := integrationHelpers.GetMetadata(ctx)
if err != nil {
t.Fatalf("Error with getting hasura metadata %e", err)
}

expectedMetadataDefinition := "../../build/expected_metadata.yml" // todo: Fix paths
expectedMetadata, err := integrationHelpers.ParseExpectedMetadata(expectedMetadataDefinition)
if err != nil {
t.Fatalf("Error with parsing expected metadata: %e", err)
}

// Go through `expectedMetadata` and assert that each object
// in that array is in `metadata` with corresponding columns.
for _, expectedTable := range expectedMetadata.Tables {
metadataTable, err := getTableColumns(metadata, expectedTable.Name, "user") // todo: read role from config
if err != nil {
t.Fatalf("Erro with searching expectedTable in metadata: %e", err)
}

if !elementsMatch(expectedTable, metadataTable) {
t.Errorf("Table columns do not match: %s", expectedTable.Name)
}
}
}

func elementsMatch(expectedTable hasura.ExpectedTable, metadataTable hasura.Columns) bool {
if len(expectedTable.Columns) != len(metadataTable) {
return false
}

hasuraColumns := make(map[string]int)

for _, columnName := range metadataTable {
hasuraColumns[columnName] = 0
}

for _, expectedColumn := range expectedTable.Columns {
if _, ok := hasuraColumns[expectedColumn]; !ok {
return false
}
}

return true
}

func getTableColumns(metadata hasura.Metadata, tableName string, role string) (hasura.Columns, error) {
for _, source := range metadata.Sources {
for _, table := range source.Tables {
if table.Schema.Name == tableName {
for _, selectPermission := range table.SelectPermissions {
if selectPermission.Role == role {
return selectPermission.Permission.Columns, nil
}
}
}
}
}

return nil, errors.Errorf("Table %s for role %s was not found", tableName, role)
}
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ version: "3.6"
services:
indexer:
image: dipdup/mempool:latest
command: "-c dipdup.mainnet.yml"
command: "-c dipdup.testnet.yml"
# command: "-c dipdup.mainnet.yml"
build:
dockerfile: build/Dockerfile
context: .
depends_on:
- db
- hasura
restart: always
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
Expand All @@ -32,7 +34,9 @@ services:
retries: 5

hasura:
image: hasura/graphql-engine:v2.0.1
# image: fedormelexin/graphql-engine-arm64:v2.0.1
# image: hasura/graphql-engine:v2.0.1
image: hasura/graphql-engine:v2.20.0
ports:
- 127.0.0.1:${HASURA_PORT:-22000}:8080
depends_on:
Expand Down

0 comments on commit 48af04a

Please sign in to comment.