github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/tools/test_monitor/common/level2.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/utils/unittest"
    11  )
    12  
    13  // Level2Summary models full level 2 summary of multiple tests from 1 or more level 1 test runs
    14  type Level2Summary struct {
    15  	TestResultsMap map[string]*Level2TestResult `json:"tests"`
    16  }
    17  
    18  // Level2TestResult models all results from a specific test over many (level 1) test runs
    19  type Level2TestResult struct {
    20  	Test            string    `json:"test"`
    21  	Package         string    `json:"package"`
    22  	Runs            int       `json:"runs"`
    23  	Passed          int       `json:"passed"`
    24  	Failed          int       `json:"failed"`
    25  	Skipped         int       `json:"skipped"`
    26  	Exceptions      int       `json:"exceptions"`
    27  	FailureRate     float64   `json:"failure_rate"`
    28  	AverageDuration float64   `json:"average_duration"`
    29  	Durations       []float64 `json:"durations"`
    30  }
    31  
    32  // AssertLevel2TestResults checks that 2 Level2TestResult structs are equal, doing a deep comparison.
    33  // This is needed for multiple test files (level 2 and 3) so it's extracted as a helper function here.
    34  func AssertLevel2TestResults(t *testing.T, expectedLevel2TestResult, actualLevel2TestResult Level2TestResult) {
    35  	require.Equal(t, expectedLevel2TestResult.Package, actualLevel2TestResult.Package)
    36  	require.Equal(t, expectedLevel2TestResult.Test, actualLevel2TestResult.Test)
    37  
    38  	require.Equal(t, expectedLevel2TestResult.Runs, actualLevel2TestResult.Runs, "runs not equal; test: "+expectedLevel2TestResult.Test)
    39  	require.Equal(t, expectedLevel2TestResult.Passed, actualLevel2TestResult.Passed, "passed not equal; test: "+expectedLevel2TestResult.Test)
    40  	require.Equal(t, expectedLevel2TestResult.Failed, actualLevel2TestResult.Failed, "failed not equal; test: "+expectedLevel2TestResult.Test)
    41  	require.Equal(t, expectedLevel2TestResult.Skipped, actualLevel2TestResult.Skipped, "skipped not equal; test: "+expectedLevel2TestResult.Test)
    42  	require.Equal(t, expectedLevel2TestResult.Exceptions, actualLevel2TestResult.Exceptions, "exceptions not equal; test: "+expectedLevel2TestResult.Test)
    43  
    44  	unittest.AssertFloatEqual(t, expectedLevel2TestResult.FailureRate, actualLevel2TestResult.FailureRate, "failure rate not equal; test: "+expectedLevel2TestResult.Test)
    45  	unittest.AssertFloatEqual(t, expectedLevel2TestResult.AverageDuration, actualLevel2TestResult.AverageDuration, "avg duration not equal; test: "+expectedLevel2TestResult.Test)
    46  	require.Equal(t, len(expectedLevel2TestResult.Durations), len(actualLevel2TestResult.Durations), PrintLevel2TestResult(&actualLevel2TestResult, "duration list sizes don't match"))
    47  	// skip checking all individual durations because
    48  	// a. require.Contains() seems to have a bug for checking float values in a list
    49  	// b. durations might be removed from level 2 in the future since they're not necessary
    50  	// c. the main metrics involving duration is average duration and test runs which are both checked
    51  }
    52  
    53  func PrintLevel2TestResult(level2TestResult *Level2TestResult, message string) string {
    54  	builder := strings.Builder{}
    55  	builder.WriteString("*** Level 2 Test Result (not found) " + message + "***")
    56  	builder.WriteString("\nTest: " + level2TestResult.Test)
    57  	builder.WriteString("\nPackage: " + level2TestResult.Package)
    58  	builder.WriteString("\nRuns: " + fmt.Sprintf("%d", level2TestResult.Runs))
    59  	builder.WriteString("\nPassed: " + fmt.Sprintf("%d", level2TestResult.Passed))
    60  	builder.WriteString("\nFailed: " + fmt.Sprintf("%d", level2TestResult.Failed))
    61  	builder.WriteString("\nExceptions: " + fmt.Sprintf("%d", level2TestResult.Exceptions))
    62  	builder.WriteString("\nSkipped: " + fmt.Sprintf("%d", level2TestResult.Skipped))
    63  	builder.WriteString("\nAvg Duration: " + fmt.Sprintf("%f", level2TestResult.AverageDuration))
    64  
    65  	for i, duration := range level2TestResult.Durations {
    66  		builder.WriteString("\nDurations[" + fmt.Sprintf("%d", i) + "]" + fmt.Sprintf("%f", duration))
    67  	}
    68  
    69  	return builder.String()
    70  }