<commit_before>package freetds

import ("testing"; "os";"fmt")

const CREATE_DB_SCRIPT = `
if exists(select * from sys.tables where name = 'freetds_types')
drop table freetds_types
;
create table freetds_types (
  int int null,
  long bigint null,
  smallint smallint null,
  tinyint tinyint null,
  varchar varchar(255) null,
  nvarchar nvarchar(255) null,
  char char(255) null,
  nchar nchar(255) null,
  text text null,
  ntext ntext null,
  datetime datetime null,
  smalldatetime smalldatetime null,
  money money null,
  smallmoney smallmoney null,
  real real null,
  float float(53) null,
  bit bit null,
  timestamp timestamp null,
  binary binary(10) null
)
;

insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (2147483647,   9223372036854775807, 32767, 255, 'išo medo u dućan   ','išo medo u dućan    2','išo medo u dućan    3','išo medo u dućan    4','išo medo u dućan    5','išo medo u dućan    6', '1972-08-08T10:11:12','1972-08-08T10:11:12', 1234.5678,   1234.5678,  1234.5678,  1234.5678, 0, 0x123567890)

insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (-2147483648, -9223372036854775808, -32768,  0, 'nije reko dobar dan','nije reko dobar dan 2','nije reko dobar dan 3','nije reko dobar dan 4','nije reko dobar dan 5','nije reko dobar dan 6', '1998-10-10T16:17:18','1998-10-10T16:17:18', -1234.5678, -1234.5678, -1234.5678, -1234.5678, 1, 0x0987654321abcd)

insert into freetds_types (int) values (3)
;

`

func ConnectToTestDb(t *testing.T) (*Conn) {
  db   := os.Getenv("GOFREETDS_DB")
  user := os.Getenv("GOFREETDS_USER")
  pwd  := <PASSWORD>("<PASSWORD>")
  host := os.Getenv("GOFREETDS_HOST")
  conn, err := Connect(user, pwd, host, db)
  if err != nil {
    t.Errorf("can't connect to the test database")
    return nil
  }
  return conn
}

func TestConnect(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn != nil {
    defer conn.Close()
  }
}

func TestCreateTable(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn == nil {
    return
  }
  defer conn.Close()
  _, err := conn.Exec(CREATE_DB_SCRIPT)
  if err != nil {
    t.Error(err)
  }
}

func TestReading(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn == nil {
    return
  }
  defer conn.Close()

  results, err := conn.Exec("select * from freetds_types")
  if err != nil {
    fmt.Printf("error: %s\n%s\n%s", err, conn.Message, conn.Error)
    return
  }
  fmt.Printf("results %v", results)
  for _, r := range results {
    fmt.Printf("\n\nColums:\n")<commit_msg>testing stored procedure return value<commit_after>package freetds

import ("testing"; "os";"fmt")

var CREATE_DB_SCRIPTS = [...]string{`
if exists(select * from sys.tables where name = 'freetds_types')
drop table freetds_types
;
create table freetds_types (
  int int null,
  long bigint null,
  smallint smallint null,
  tinyint tinyint null,
  varchar varchar(255) null,
  nvarchar nvarchar(255) null,
  char char(255) null,
  nchar nchar(255) null,
  text text null,
  ntext ntext null,
  datetime datetime null,
  smalldatetime smalldatetime null,
  money money null,
  smallmoney smallmoney null,
  real real null,
  float float(53) null,
  bit bit null,
  timestamp timestamp null,
  binary binary(10) null
)
;

insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (2147483647,   9223372036854775807, 32767, 255, 'išo medo u dućan   ','išo medo u dućan    2','išo medo u dućan    3','išo medo u dućan    4','išo medo u dućan    5','išo medo u dućan    6', '1972-08-08T10:11:12','1972-08-08T10:11:12', 1234.5678,   1234.5678,  1234.5678,  1234.5678, 0, 0x123567890)

insert into freetds_types (int, long, smallint, tinyint, varchar, nvarchar, char, nchar, text, ntext, datetime, smalldatetime, money, smallmoney, real, float, bit, binary)
values (-2147483648, -9223372036854775808, -32768,  0, 'nije reko dobar dan','nije reko dobar dan 2','nije reko dobar dan 3','nije reko dobar dan 4','nije reko dobar dan 5','nije reko dobar dan 6', '1998-10-10T16:17:18','1998-10-10T16:17:18', -1234.5678, -1234.5678, -1234.5678, -1234.5678, 1, 0x0987654321abcd)

insert into freetds_types (int) values (3)
`,`
if exists(select * from sys.procedures where name = 'freetds_return_value')
  drop procedure freetds_return_value
`,`
create procedure freetds_return_value as
  return -5`}

func ConnectToTestDb(t *testing.T) (*Conn) {
  db   := os.Getenv("GOFREETDS_DB")
  user := os.Getenv("GOFREETDS_USER")
  pwd  := <PASSWORD>("<PASSWORD>")
  host := os.Getenv("GOFREETDS_HOST")
  conn, err := Connect(user, pwd, host, db)
  if err != nil {
    t.Errorf("can't connect to the test database")
    return nil
  }
  return conn
}

func TestConnect(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn != nil {
    defer conn.Close()
  }
}

func TestCreateTable(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn == nil {
    return
  }
  defer conn.Close()
  for _, s := range CREATE_DB_SCRIPTS {
    _, err := conn.Exec(s)
    if err != nil {
      t.Error(err)
    }
  }
}

func TestStoredProcedureReturnValue(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn == nil {
    return
  }
  defer conn.Close()
  results, err := conn.Exec("exec freetds_return_value")
  if err != nil {
    t.Error(err)
  }
  if results[0].ReturnValue != -5 {
    t.Errorf("expected return value was -5 got %d", results[0].ReturnValue)
  }
}

func TestReading(t *testing.T) {
  conn := ConnectToTestDb(t)
  if conn == nil {
    return
  }
  defer conn.Close()

  results, err := conn.Exec("select * from freetds_types")
  if err != nil {
    fmt.Printf("error: %s\n%s\n%s", err, conn.Message, conn.Error)
    return
  }
  fmt.Printf("results %v", results)
  for _, r := range results {
    fmt.Printf("\n\nColums:\n")
{'fraction_non_alphanumeric': 0.13522896346503555, 'fraction_numerical': 0.08083980823276575, 'mean_word_length': 3.886914378029079, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6765021', 'n_tokens_mistral': 2650, 'n_tokens_neox': 2341, 'n_words': 632}