<commit_before>package principal

import (
	"database/sql"
	"github.com/fritzpay/paymentd/pkg/testutil"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestPrincipalSQL(t *testing.T) {
	Convey("Given a principal DB connection", t, testutil.WithPrincipalDB(t, func(db *sql.DB) {
		Reset(func() {
			db.Close()
		})
		Convey("When requesting a nonexistent principal", func() {
			principal, err := PrincipalByNameDB(db, "nonexistent")

			Convey("It should return an empty principal", func() {
				So(principal.Empty(), ShouldBeTrue)
			})
			Convey("It should return an error not found", func() {
				So(err, ShouldEqual, ErrPrincipalNotFound)
			})
		})

	}))
}<commit_msg>add principal tests<commit_after>package principal

import (
	"database/sql"
	"github.com/fritzpay/paymentd/pkg/testutil"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
	"time"
)

func WithPrincipal(db *sql.DB, f func(pr *Principal)) func() {
	return func() {
		pr := &Principal{
			Created:   time.Now(),
			CreatedBy: "test",
			Name:      "test_principal",
		}
		err := InsertPrincipalDB(db, pr)
		So(err, ShouldBeNil)
		So(pr.Empty(), ShouldBeFalse)

		Reset(func() {
			_, err := db.Exec("delete from principal where name = 'test_principal'")
			So(err, ShouldBeNil)
		})

		f(pr)
	}
}

func TestPrincipalSQL(t *testing.T) {
	Convey("Given a principal DB connection", t, testutil.WithPrincipalDB(t, func(db *sql.DB) {
		Reset(func() {
			db.Close()
		})
		Convey("When requesting a nonexistent principal", func() {
			principal, err := PrincipalByNameDB(db, "nonexistent")

			Convey("It should return an empty principal", func() {
				So(principal.Empty(), ShouldBeTrue)
			})
			Convey("It should return an error not found", func() {
				So(err, ShouldEqual, ErrPrincipalNotFound)
			})
		})

		Convey("Given a principal", WithPrincipal(db, func(pr *Principal) {

			Convey("When selecting a principal by name", func() {
				selPr, err := PrincipalByNameDB(db, pr.Name)

				Convey("It should succeed", func() {
					So(err, ShouldBeNil)
					So(selPr.Empty(), ShouldBeFalse)
					Convey("It should match", func() {
						So(selPr.ID, ShouldEqual, pr.ID)
					})
				})
			})
		}))
	}))
}
{'fraction_non_alphanumeric': 0.15216400911161732, 'fraction_numerical': 0.0, 'mean_word_length': 3.9237668161434978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5982228', 'n_tokens_mistral': 881, 'n_tokens_neox': 754, 'n_words': 172}