github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/report/report_test.go (about)

     1  package report
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/covergates/covergates/config"
     8  	"github.com/covergates/covergates/core"
     9  	"github.com/covergates/covergates/mock"
    10  	"github.com/golang/mock/gomock"
    11  	"github.com/google/go-cmp/cmp"
    12  )
    13  
    14  const expectMarkdown = `### [Coverage: 50.0%](http://localhost/report/github/space/name?ref=commit)
    15  
    16  ||File|Coverage|
    17  |--|--|--------|
    18  |:arrow_up_small:|A|1.00|
    19  |:arrow_down_small:|B|0.00|
    20  ||C|0.50|
    21  `
    22  
    23  const expectMarkdownNoTarget = `### [Coverage: 50.0%](http://localhost/report/github/space/name?ref=commit)
    24  
    25  ||File|Coverage|
    26  |--|--|--------|
    27  |:arrow_up_small:|A|1.00|
    28  ||B|0.00|
    29  |:arrow_up_small:|C|0.50|
    30  `
    31  
    32  func TestMarkdownReport(t *testing.T) {
    33  	source := &core.Report{
    34  		ReportID: "report_id",
    35  		Commit:   "commit",
    36  		Coverages: []*core.CoverageReport{
    37  			{
    38  				StatementCoverage: 0.8,
    39  				Files: []*core.File{
    40  					{
    41  						Name:              "A",
    42  						StatementCoverage: 1.0,
    43  					},
    44  					{
    45  						Name:              "B",
    46  						StatementCoverage: 0,
    47  					},
    48  					{
    49  						Name:              "C",
    50  						StatementCoverage: 0.5,
    51  					},
    52  				},
    53  			},
    54  		},
    55  	}
    56  
    57  	target := &core.Report{
    58  		Coverages: []*core.CoverageReport{
    59  			{
    60  				StatementCoverage: 0.8,
    61  				Files: []*core.File{
    62  					{
    63  						Name:              "A",
    64  						StatementCoverage: 0.8,
    65  					},
    66  					{
    67  						Name:              "B",
    68  						StatementCoverage: 0.8,
    69  					},
    70  					{
    71  						Name:              "C",
    72  						StatementCoverage: 0.5,
    73  					},
    74  				},
    75  			},
    76  		},
    77  	}
    78  
    79  	ctrl := gomock.NewController(t)
    80  	defer ctrl.Finish()
    81  
    82  	mockRepo := mock.NewMockRepoStore(ctrl)
    83  	mockRepo.EXPECT().Find(gomock.Eq(
    84  		&core.Repo{ReportID: source.ReportID},
    85  	)).AnyTimes().Return(&core.Repo{
    86  		Name:      "name",
    87  		NameSpace: "space",
    88  		SCM:       core.Github,
    89  	}, nil)
    90  
    91  	service := &Service{
    92  		Config: &config.Config{
    93  			Server: config.Server{
    94  				Addr: "http://localhost",
    95  			},
    96  		},
    97  		RepoStore: mockRepo,
    98  	}
    99  
   100  	reader, err := service.MarkdownReport(source, target)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	data, err := ioutil.ReadAll(reader)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	if diff := cmp.Diff(expectMarkdown, string(data)); diff != "" {
   109  		t.Log(diff)
   110  		t.Fail()
   111  	}
   112  
   113  	reader, err = service.MarkdownReport(source, &core.Report{})
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	data, err = ioutil.ReadAll(reader)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	if diff := cmp.Diff(expectMarkdownNoTarget, string(data)); diff != "" {
   122  		t.Log(diff)
   123  		t.Fail()
   124  	}
   125  }