~abyxcos/sqlh

09e5df297e87aba57148a9a0c95c86a4949951d9 — abyxcos 2 years ago 73e069a
Get some basic tests going and do the module thing to hush golang.
5 files changed, 79 insertions(+), 0 deletions(-)

A .gitignore
D .sqlh.go.swp
A go.mod
A go.sum
A sqlh_test.go
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
*.swp

D .sqlh.go.swp => .sqlh.go.swp +0 -0
A go.mod => go.mod +5 -0
@@ 0,0 1,5 @@
module git.sr.ht/~abyxcos/sqlh

go 1.17

require github.com/iancoleman/strcase v0.2.0

A go.sum => go.sum +2 -0
@@ 0,0 1,2 @@
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=

A sqlh_test.go => sqlh_test.go +71 -0
@@ 0,0 1,71 @@
package sqlh

import (
	"testing"
)

type Test2 struct {
	A     string
	B     int
	C     string
}

type Test struct {
	Name  string
	unexportedInt  int
	Count int
	Blah  Test2
	Test2 string   `db:"TEST3"`
	DB    int
	ID    int
	APITestFunc string
}

func makeTestVar() Test {
	return Test{
		Name: "Test",
		unexportedInt: 12,
		Count: 5,
		Test2: "Hello",
		APITestFunc: "1234",
		ID:   14,
		Blah: Test2{
			A: "123",
			B: 4,
			C: "567",
		},
	}
}

func TestBuildInsert(t *testing.T) {
	FormatChar = "$"
	FormatCounter = true
	SkipFields = "ID, C,Name"

	t.Log("Testing Insert")
	columns, params, _ := BuildInsert(makeTestVar())

	if columns != "count, a, b, TEST3, db, api_test_func" {
		t.Log("Columns are wrong")
		t.Fail()
	}

	if params != "$1, $2, $3, $4, $5, $6" {
		t.Log("Parameters are wrong")
		t.Fail()
	}
}

func TestBuildUpdate(t *testing.T) {
	FormatChar = "$"
	FormatCounter = true
	SkipFields = "ID, C,Name"

	t.Log("Testing Update")
	columns, _ := BuildUpdate(makeTestVar())

	if columns != "count=$1, a=$2, b=$3, TEST3=$4, db=$5, api_test_func=$6" {
		t.Log("Columns are wrong")
		t.Fail()
	}
}