github.com/andrewrech/ih-abstract@v0.0.0-20210322142951-2fec1c8d0f38/write_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  )
    10  
    11  func TestRowWriter(t *testing.T) {
    12  	mock := strings.NewReader(`MRN,MRNFacility,MedViewPatientID,PatientName,DOB,Sex,DrawnDate,DiagServiceID,AccessionNumber,HNAMOrderID,OrderTypeLocalID,OrderTypeMnemonic,TestTypeLocalID,TestTypeMnemonic,ResultDate,Value
    13  "1000000001","UID",1111111111,"ZZZ, ZZZ",1950006-1600:00:00.000,M,2020-11-1505:28:00.000,GL,00000111111111,1111111111,1111111111,CMV,1111111111111,WBC,2014-11-1505:37:58.000,Test removal on basis of Order
    14  `)
    15  
    16  	r := readCSV(mock)
    17  
    18  	<-r.done
    19  
    20  	t.Run("write CSV rows", func(t *testing.T) {
    21  		writeDone := make(chan struct{})
    22  
    23  		f := "test-write.csv"
    24  
    25  		WriteRows(r.out, f, r.header, writeDone)
    26  		defer os.Remove("test-write.csv")
    27  
    28  		<-writeDone
    29  
    30  		lines := helperCsvLines(f)
    31  
    32  		diff := cmp.Diff(int64(2), lines)
    33  
    34  		if diff != "" {
    35  			t.Fatalf(diff)
    36  		}
    37  	})
    38  }
    39  
    40  func TestWrite(t *testing.T) {
    41  	header := []string{"MRN", "MRNFacility", "MedViewPatientID", "PatientName", "DOB", "Sex", "DrawnDate", "DiagServiceID", "AccessionNumber", "HNAMOrderID", "OrderTypeLocalID", "OrderTypeMnemonic", "TestTypeLocalID", "TestTypeMnemonic", "ResultDate", "Value"}
    42  	testStr := []string{"1000000001", "UID", "1111111111", "ZZZ, ZZZ", "1950006-1600:00:00.000", "M", "2020-11-1505:28:00.000", "GL", "00000111111111", "1111111111", "1111111111", "CMV", "1111111111111", "WBC", "2014-11-1505:37:58.000", "Test removal on basis of Order"}
    43  
    44  	var buf int64 = 2e7
    45  	in := make(chan []string, buf)
    46  
    47  	channels := make(map[string](chan []string))
    48  	channels["test-write"] = in
    49  
    50  	in <- testStr
    51  	close(in)
    52  
    53  	t.Run("write CSV file from channel", func(t *testing.T) {
    54  		defer os.Remove("test-write.csv")
    55  		done := Write(header, channels)
    56  		<-done
    57  
    58  		lines := helperCsvLines("test-write.csv")
    59  		diff := cmp.Diff(int64(2), lines)
    60  
    61  		if diff != "" {
    62  			t.Fatalf(diff)
    63  		}
    64  	})
    65  }