From 6ede87a142d30df2efe941c9a0d23e6b766cb065 Mon Sep 17 00:00:00 2001 From: hovsep Date: Mon, 16 Sep 2024 23:39:14 +0300 Subject: [PATCH] Add integration tests closes https://github.com/hovsep/fmesh/issues/37 --- examples/basic.go | 55 --------------- .../computation/basic_math_test.go | 68 +++++++++++++++++++ 2 files changed, 68 insertions(+), 55 deletions(-) delete mode 100644 examples/basic.go create mode 100644 integration_tests/computation/basic_math_test.go diff --git a/examples/basic.go b/examples/basic.go deleted file mode 100644 index 939e2db..0000000 --- a/examples/basic.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "github.com/hovsep/fmesh" - "github.com/hovsep/fmesh/component" - "github.com/hovsep/fmesh/port" - "github.com/hovsep/fmesh/signal" - "os" -) - -// This example shows a very basic program written with FMesh -// All it does is passes an integer into a simple f-mesh which consists of 2 components, the first one adds 2 to the -// initial number, and the second one doubles the result. (result must be 102) -func main() { - c1 := component.NewComponent("adder"). - WithDescription("adds 2 to the input"). - WithInputs("num"). - WithOutputs("res"). - WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { - num := inputs.ByName("num").Signal().Payload().(int) - outputs.ByName("res").PutSignal(signal.New(num + 2)) - return nil - }) - - c2 := component.NewComponent("multiplier"). - WithDescription("multiplies by 3"). - WithInputs("num"). - WithOutputs("res"). - WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { - num := inputs.ByName("num").Signal().Payload().(int) - outputs.ByName("res").PutSignal(signal.New(num * 3)) - return nil - }) - - c1.Outputs().ByName("res").PipeTo(c2.Inputs().ByName("num")) - - fm := fmesh.New("basic fmesh").WithComponents(c1, c2).WithErrorHandlingStrategy(fmesh.StopOnFirstError) - - c1.Inputs().ByName("num").PutSignal(signal.New(32)) - - cycles, err := fm.Run() - - fmt.Println("f-mesh stopped after ", len(cycles), " cycles") - - if err != nil { - fmt.Println("Error:", err) - os.Exit(1) - } - - res := c2.Outputs().ByName("res").Signal().Payload() - - fmt.Println("calculation result:", res) - -} diff --git a/integration_tests/computation/basic_math_test.go b/integration_tests/computation/basic_math_test.go new file mode 100644 index 0000000..b0ef6ed --- /dev/null +++ b/integration_tests/computation/basic_math_test.go @@ -0,0 +1,68 @@ +package integration_tests + +import ( + "github.com/hovsep/fmesh" + "github.com/hovsep/fmesh/component" + "github.com/hovsep/fmesh/cycle" + "github.com/hovsep/fmesh/port" + "github.com/hovsep/fmesh/signal" + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_BasicMath(t *testing.T) { + tests := []struct { + name string + setupFM func() *fmesh.FMesh + setInputs func(fm *fmesh.FMesh) + assertions func(t *testing.T, fm *fmesh.FMesh, cycles cycle.Collection, err error) + }{ + { + name: "add and multiply", + setupFM: func() *fmesh.FMesh { + c1 := component.NewComponent("c1"). + WithDescription("adds 2 to the input"). + WithInputs("num"). + WithOutputs("res"). + WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { + num := inputs.ByName("num").Signal().Payload().(int) + outputs.ByName("res").PutSignal(signal.New(num + 2)) + return nil + }) + + c2 := component.NewComponent("c2"). + WithDescription("multiplies by 3"). + WithInputs("num"). + WithOutputs("res"). + WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { + num := inputs.ByName("num").Signal().Payload().(int) + outputs.ByName("res").PutSignal(signal.New(num * 3)) + return nil + }) + + c1.Outputs().ByName("res").PipeTo(c2.Inputs().ByName("num")) + + return fmesh.New("fm").WithComponents(c1, c2).WithErrorHandlingStrategy(fmesh.StopOnFirstError) + }, + setInputs: func(fm *fmesh.FMesh) { + fm.Components().ByName("c1").Inputs().ByName("num").PutSignal(signal.New(32)) + }, + assertions: func(t *testing.T, fm *fmesh.FMesh, cycles cycle.Collection, err error) { + assert.NoError(t, err) + assert.Len(t, cycles, 3) + + resultSignal := fm.Components().ByName("c2").Outputs().ByName("res").Signal() + assert.Len(t, resultSignal.Payloads(), 1) + assert.Equal(t, 102, resultSignal.Payload().(int)) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fm := tt.setupFM() + tt.setInputs(fm) + cycles, err := fm.Run() + tt.assertions(t, fm, cycles, err) + }) + } +}