func TestNewSecuritySymbol(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := "AAPL" if security.Symbol != expected { t.Errorf(errorMessage, expected, security.Symbol) } }
func TestNewSecurityTestIssue(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := false if security.TestIssue != expected { t.Errorf(errorMessage, expected, security.TestIssue) } }
func TestNewSecurityRoundLotSize(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := 100 if security.RoundLotSize != expected { t.Errorf(errorMessage, expected, security.RoundLotSize) } }
func TestNewSecurityMarketCategory(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := "Q" if security.MarketCategory != expected { t.Errorf(errorMessage, expected, security.MarketCategory) } }
func TestNewSecurityFinancialStatus(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := "N" if security.FinancialStatus != expected { t.Errorf(errorMessage, expected, security.FinancialStatus) } }
func TestNewSecurityExchangeId(t *testing.T) { security := models.NewSecurity(2, "N", "Q", "Apple Inc. - Common Stock", 100, "AAPL", false) expected := 2 if security.ExchangeId != expected { t.Errorf(errorMessage, expected, security.ExchangeId) } }
func importSecurities(buffer *bytes.Buffer) error { db, err := models.NewDB(os.Getenv("DB_TYPE"), connectionString()) if err != nil { log.Panic(err) return err } reader := csv.NewReader(buffer) reader.Comma = '|' reader.FieldsPerRecord = 6 currentLine := 0 numSecurities := 0 for { currentLine++ row, err := reader.Read() if currentLine == 1 { log.Println("Skipping first line (header)") continue } match, _ := regexp.MatchString("^File Creation", row[0]) if match { log.Println("Done, skipping last line (file creation)") break } if err != nil { if err == io.EOF { err = nil } else { log.Println("err: ", err) } } exchangeId := parseExchangeId() roundLotSize := parseRoundLotSize(row[roundLotSizeIndex]) testIssue := parseTestIssue(row[testIssueIndex]) security := models.NewSecurity( exchangeId, row[financialStatusIndex], row[marketCategoryIndex], row[securityNameIndex], roundLotSize, row[symbolIndex], testIssue, ) db.Create(security) numSecurities++ } if numSecurities != currentLine-2 { err = errors.New("The number of securities imported does not match") return err } log.Println("Imported securities:", numSecurities) return nil }