<commit_before>package graph

import (
	"errors"
	"fmt"
	"testing"

	"github.com/pachyderm/pachyderm/src/pps"
	"github.com/pachyderm/pachyderm/src/pps/parse"
	"github.com/stretchr/testify/require"
)

func TestGetNameToNodeInfo(t *testing.T) {
	pipeline, err := parse.NewParser().ParsePipeline("../parse/testdata/basic", "")
	require.NoError(t, err)
	nodes := pps.GetNameToNode(pipeline)
	nodeInfos, err := GetNameToNodeInfo(nodes)
	require.NoError(t, err)
	require.Equal(t, []string{"bar-node"}, nodeInfos["baz-node-bar-in-bar-out-in"].Parents)
}

func TestBuild(t *testing.T) {
	intC := make(chan int, 8)
	nameToNodeInfo := map[string]*NodeInfo{
		"1": &NodeInfo{
			Parents: []string{},
		},
		"2": &NodeInfo{
			Parents: []string{},
		},
		"3-1": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"3-2": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"3-3": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"4-1": &NodeInfo{
			Parents: []string{
				"3-1",
				"3-2",
				"3-3",
			},
		},
		"4-2": &NodeInfo{
			Parents: []string{
				"3-1",
				"3-2",
				"3-3",
			},
		},
		"5": &NodeInfo{
			Parents: []string{
				"4-1",
				"4-2",
			},
		},
	}
	nameToNodeFunc := map[string]func() error{
		"1":   testNodeFunc(intC, "1", 1, ""),
		"2":   testNodeFunc(intC, "2", 2, ""),
		"3-1": testNodeFunc(intC, "3-1", 3, ""),
		"3-2": testNodeFunc(intC, "3-2", 4, ""),
		"3-3": testNodeFunc(intC, "3-3", 5, ""),
		"4-1": testNodeFunc(intC, "4-1", 6, ""),
		"4-2": testNodeFunc(intC, "4-2", 7, ""),
		"5":   testNodeFunc(intC, "5", 8, ""),
	}

	run, err := build(newTestNodeErrorRecorder(), nameToNodeInfo, nameToNodeFunc)
	require.NoError(t, err)
	run.Do()

	i := <-intC
	require.True(t, i == 1 || i == 2)
	i = <-intC
	require.True(t, i == 1 || i == 2)
	i = <-intC
	require.True(t, i == 3 || i == 4 || i == 5)
}

func testNodeFunc(intC chan int, nodeName string, i int, errString string) func() error {
	var err error
	if errString != "" {
		err = errors.New(errString)
	}
	return func() error {
		intC <- i
		fmt.Printf("ran %s, sent %d, returning %v\n", nodeName, i, err)
		return err
	}
}

type testNodeErrorRecorder struct {
	stringC chan string
}<commit_msg>finish initial graph test<commit_after>package graph

import (
	"errors"
	"fmt"
	"testing"

	"github.com/pachyderm/pachyderm/src/pps"
	"github.com/pachyderm/pachyderm/src/pps/parse"
	"github.com/stretchr/testify/require"
)

func TestGetNameToNodeInfo(t *testing.T) {
	pipeline, err := parse.NewParser().ParsePipeline("../parse/testdata/basic", "")
	require.NoError(t, err)
	nodes := pps.GetNameToNode(pipeline)
	nodeInfos, err := GetNameToNodeInfo(nodes)
	require.NoError(t, err)
	require.Equal(t, []string{"bar-node"}, nodeInfos["baz-node-bar-in-bar-out-in"].Parents)
}

func TestBuild(t *testing.T) {
	intC := make(chan int, 8)
	nameToNodeInfo := map[string]*NodeInfo{
		"1": &NodeInfo{
			Parents: []string{},
		},
		"2": &NodeInfo{
			Parents: []string{},
		},
		"3-1": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"3-2": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"3-3": &NodeInfo{
			Parents: []string{
				"1",
				"2",
			},
		},
		"4-1": &NodeInfo{
			Parents: []string{
				"3-1",
				"3-2",
				"3-3",
			},
		},
		"4-2": &NodeInfo{
			Parents: []string{
				"3-1",
				"3-2",
				"3-3",
			},
		},
		"5": &NodeInfo{
			Parents: []string{
				"4-1",
				"4-2",
			},
		},
	}
	nameToNodeFunc := map[string]func() error{
		"1":   testNodeFunc(intC, "1", 1, ""),
		"2":   testNodeFunc(intC, "2", 2, ""),
		"3-1": testNodeFunc(intC, "3-1", 3, ""),
		"3-2": testNodeFunc(intC, "3-2", 4, ""),
		"3-3": testNodeFunc(intC, "3-3", 5, ""),
		"4-1": testNodeFunc(intC, "4-1", 6, ""),
		"4-2": testNodeFunc(intC, "4-2", 7, ""),
		"5":   testNodeFunc(intC, "5", 8, ""),
	}

	run, err := build(newTestNodeErrorRecorder(), nameToNodeInfo, nameToNodeFunc)
	require.NoError(t, err)
	run.Do()

	i := <-intC
	require.True(t, i == 1 || i == 2)
	i = <-intC
	require.True(t, i == 1 || i == 2)
	i = <-intC
	require.True(t, i == 3 || i == 4 || i == 5)
	i = <-intC
	require.True(t, i == 3 || i == 4 || i == 5)
	i = <-intC
	require.True(t, i == 3 || i == 4 || i == 5)
	i = <-intC
	require.True(t, i == 6 || i == 7)
	i = <-intC
	require.True(t, i == 6 || i == 7)
	i = <-intC
	require.True(t, i == 8)
}

func testNodeFunc(intC chan int, nodeName string, i int, errString string) func() error {
	var err error
	if errString != "" {
		err = errors.New(errString)
	}
	return func() error {
		intC <- i
		fmt.Printf("ran %s, sent %d, returning %v\n", nodeName, i, err)
		return err
	}
}

type testNodeErrorRecorder struct {
	stringC chan string
}
{'fraction_non_alphanumeric': 0.22790498609030602, 'fraction_numerical': 0.035309223197089665, 'mean_word_length': 3.1399468556244465, 'pattern_counts': {'":': 32, '<': 16, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4597430', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2063, 'n_words': 418}