github.com/andrewrech/ih-abstract@v0.0.0-20210322142951-2fec1c8d0f38/records_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 ) 8 9 func TestAddCheckRecords(t *testing.T) { 10 var r Records 11 r.Store = make(Store) 12 13 l := []string{ 14 "10000001", 15 "ZZZ, ZZZ", 16 "CBC", 17 "100", 18 } 19 20 t.Run("Add record", func(t *testing.T) { 21 err := r.Add(&l) 22 if err != nil { 23 t.Fatal(err) 24 } 25 }) 26 27 t.Run("Check record", func(t *testing.T) { 28 exists, err := r.Check(&l) 29 if !exists || err != nil { 30 t.Fatal("failed to check record") 31 } 32 }) 33 } 34 35 func TestExistingRecords(t *testing.T) { 36 f := TestFile 37 38 r := Existing(&f) 39 40 t.Run("existing", func(t *testing.T) { 41 diff := cmp.Diff(int(12), len(r.Store)) 42 if diff != "" { 43 t.Fatalf(diff) 44 } 45 }) 46 } 47 48 func BenchmarkExistingRecords(b *testing.B) { 49 f := TestFilePhi 50 51 var r *Records 52 53 for i := 0; i < b.N; i++ { 54 r = Existing(&f) 55 } 56 57 _ = r 58 } 59 60 func TestNewRecords(t *testing.T) { 61 f := TestFileOld 62 r := Existing(&f) 63 64 in := helperTestReader(TestFile) 65 66 header := helperCorrectHeader() 67 68 var buf int64 = 2e7 69 out := make(chan []string, buf) 70 done := make(chan struct{}) 71 72 New(r, header, in, out, done) 73 74 for l := range out { 75 _ = l 76 } 77 78 <-done 79 80 t.Run("Detect new data", func(t *testing.T) { 81 lines := helperCsvLines("new-ids.txt") 82 83 diff := cmp.Diff(int64(11), lines) 84 if diff != "" { 85 t.Fatalf(diff) 86 } 87 }) 88 } 89 90 func TestRecordID(t *testing.T) { 91 header := []string{"MRN", "MRNFacilityID", "UID"} 92 headerBad := []string{"NoID", "InThis", "Header"} 93 94 id, _ := RecordID(header) 95 96 t.Run("Get record ID data column name", func(t *testing.T) { 97 diff := cmp.Diff("MRN", id) 98 if diff != "" { 99 t.Fatalf(diff) 100 } 101 }) 102 103 _, err := RecordID(headerBad) 104 105 t.Run("Error if no record ID data column name", func(t *testing.T) { 106 diff := cmp.Diff(err.Error(), string("cannot identify patient instance column name")) 107 if diff != "" { 108 t.Fatalf(diff) 109 } 110 }) 111 } 112 113 func BenchmarkNewRecords(b *testing.B) { 114 for i := 0; i < b.N; i++ { 115 f := TestFilePhi 116 r := Existing(&f) 117 118 in := helperTestReader(TestFile) 119 120 header := helperCorrectHeader() 121 122 var buf int64 = 2e7 123 out := make(chan []string, buf) 124 done := make(chan struct{}) 125 126 New(r, header, in, out, done) 127 128 for l := range out { 129 _ = l 130 } 131 132 <-done 133 } 134 }