Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: initial unit tests, automated test runs #73

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/go-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go Unit Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Test
run: go test --cover -v ./...
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![CodeQL](https://img.shields.io/github/actions/workflow/status/application-research/delta/codeql.yml?label=CodeQL&style=for-the-badge)](https://github.com/application-research/delta/actions/workflows/codeql.yml)
[![Unit Tests](https://img.shields.io/github/actions/workflow/status/application-research/delta/go-unit-tests.yml?label=UnitTests&style=for-the-badge)](https://github.com/application-research/delta/actions/workflows/go-unit-tests.yml)
[![Release](https://img.shields.io/github/v/release/application-research/delta?display_name=release&style=for-the-badge)](https://github.com/application-research/delta/releases)

# Δ Delta
Expand Down
242 changes: 78 additions & 164 deletions core/commp_test.go
Original file line number Diff line number Diff line change
@@ -1,183 +1,97 @@
package core

import (
"bytes"
"context"
"github.com/filecoin-project/go-commp-utils/writer"
"github.com/filecoin-project/go-state-types/abi"
"github.com/application-research/whypfs-core"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
carv2 "github.com/ipld/go-car/v2"
"io"
"reflect"
"testing"
)

func TestCommpService_GenerateCommPCarV2(t *testing.T) {
type fields struct {
DeltaNode *DeltaNode
}
type args struct {
readerFromFile io.Reader
}
tests := []struct {
name string
fields fields
args args
want *abi.PieceInfo
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CommpService{
DeltaNode: tt.fields.DeltaNode,
}
got, err := c.GenerateCommPCarV2(tt.args.readerFromFile)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateCommPCarV2() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenerateCommPCarV2() got = %v, want %v", got, tt.want)
}
})
}
}
"github.com/stretchr/testify/assert"
)

func TestCommpService_GenerateCommPFile(t *testing.T) {
type fields struct {
DeltaNode *DeltaNode
}
type args struct {
context context.Context
payloadCid cid.Cid
blockstore blockstore.Blockstore
}
tests := []struct {
name string
fields fields
args args
wantPieceCid cid.Cid
wantPayloadSize uint64
wantUnPaddedPieceSize abi.UnpaddedPieceSize
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CommpService{
DeltaNode: tt.fields.DeltaNode,
}
gotPieceCid, gotPayloadSize, gotUnPaddedPieceSize, err := c.GenerateCommPFile(tt.args.context, tt.args.payloadCid, tt.args.blockstore)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateCommPFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotPieceCid, tt.wantPieceCid) {
t.Errorf("GenerateCommPFile() gotPieceCid = %v, want %v", gotPieceCid, tt.wantPieceCid)
}
if gotPayloadSize != tt.wantPayloadSize {
t.Errorf("GenerateCommPFile() gotPayloadSize = %v, want %v", gotPayloadSize, tt.wantPayloadSize)
}
if gotUnPaddedPieceSize != tt.wantUnPaddedPieceSize {
t.Errorf("GenerateCommPFile() gotUnPaddedPieceSize = %v, want %v", gotUnPaddedPieceSize, tt.wantUnPaddedPieceSize)
}
})
// create a mock DeltaNode instance
deltaNode := &DeltaNode{}

// create a mock blockstore instance
params := whypfs.NewNodeParams{
Ctx: context.Background(),
Datastore: whypfs.NewInMemoryDatastore(),
Repo: ".test",
}
whypfsPeer, err := whypfs.NewNode(params)
blockstore := whypfsPeer.Blockstore

// create a CommpService instance using the mock DeltaNode instance
commpService := CommpService{
DeltaNode: deltaNode,
}

// create a mock payload CID
payloadCid, _ := cid.Decode("bafy2bzacedx6vywq6so7e6m43g6op7dhcqkntwk2lrzvt6yljvf6xlzshxh5w")

// call the GenerateCommPFile method
pieceCid, payloadSize, unPaddedPieceSize, err := commpService.GenerateCommPFile(context.Background(), payloadCid, blockstore)

// assert that there is no error
assert.NoError(t, err)

// assert that the piece CID, payload size, and unpadded piece size are not empty
assert.NotEmpty(t, pieceCid)
assert.NotEmpty(t, payloadSize)
assert.NotEmpty(t, unPaddedPieceSize)
}

func TestCommpService_GenerateParallelCommp(t *testing.T) {
type fields struct {
DeltaNode *DeltaNode
}
type args struct {
readerFromFile io.Reader
}
tests := []struct {
name string
fields fields
args args
want writer.DataCIDSize
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CommpService{
DeltaNode: tt.fields.DeltaNode,
}
got, err := c.GenerateParallelCommp(tt.args.readerFromFile)
if (err != nil) != tt.wantErr {
t.Errorf("GenerateParallelCommp() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GenerateParallelCommp() got = %v, want %v", got, tt.want)
}
})
}
func TestCommpService_GetSize(t *testing.T) {
// create a mock payload reader
reader := bytes.NewReader([]byte("hello world"))

// create a CommpService instance
commpService := CommpService{}

// call the GetSize method
size := commpService.GetSize(reader)

// assert that the size is correct
assert.Equal(t, 11, size)
}

func TestCommpService_GetCarSize(t *testing.T) {
type fields struct {
DeltaNode *DeltaNode
}
type args struct {
stream io.Reader
rd *carv2.Reader
}
tests := []struct {
name string
fields fields
args args
want int64
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CommpService{
DeltaNode: tt.fields.DeltaNode,
}
got, err := c.GetCarSize(tt.args.stream, tt.args.rd)
if (err != nil) != tt.wantErr {
t.Errorf("GetCarSize() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetCarSize() got = %v, want %v", got, tt.want)
}
})
}
// create a mock CARv2 file reader
reader := bytes.NewReader([]byte("file content"))

// create a CARv2 reader from the mock CARv2 file reader
carReader, _ := carv2.NewReader(reader)

// create a CommpService instance
commpService := CommpService{}

// call the GetCarSize method
size, err := commpService.GetCarSize(reader, carReader)

// assert that there is no error
assert.NoError(t, err)

// assert that the size is correct
assert.Equal(t, int64(12), size)
}

func TestCommpService_GetSize(t *testing.T) {
type fields struct {
DeltaNode *DeltaNode
}
type args struct {
stream io.Reader
}
tests := []struct {
name string
fields fields
args args
want int
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CommpService{
DeltaNode: tt.fields.DeltaNode,
}
if got := c.GetSize(tt.args.stream); got != tt.want {
t.Errorf("GetSize() = %v, want %v", got, tt.want)
}
})
}
func TestCommpService_GenerateParallelCommp(t *testing.T) {
// create a mock payload reader
reader := bytes.NewReader([]byte("hello world"))

// create a CommpService instance
commpService := CommpService{}

// call the GenerateParallelCommp method
dataCIDSize, err := commpService.GenerateParallelCommp(reader)

// assert that there is no error
assert.NoError(t, err)

// assert that the Data CID and Size are not empty
assert.NotEmpty(t, dataCIDSize.PieceCID)
assert.NotEmpty(t, dataCIDSize.PieceSize)
}
6 changes: 6 additions & 0 deletions core/deal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core

// TODO: move core function of deal to this service
type DealService struct {
DeltaNode *DeltaNode
}
Loading