diff --git a/database/repo.go b/database/repo.go index 5c7c799f..d8a7def8 100644 --- a/database/repo.go +++ b/database/repo.go @@ -16,6 +16,10 @@ var ( // Repo type has an empty FullName field provided. ErrEmptyRepoFullName = errors.New("empty repo full_name provided") + // ErrEmptyRepoHash defines the error type when a + // Repo type has an empty Hash field provided. + ErrEmptyRepoHash = errors.New("empty repo hash provided") + // ErrEmptyRepoName defines the error type when a // Repo type has an empty Name field provided. ErrEmptyRepoName = errors.New("empty repo name provided") @@ -33,6 +37,7 @@ var ( type Repo struct { ID sql.NullInt64 `sql:"id"` UserID sql.NullInt64 `sql:"user_id"` + Hash sql.NullString `sql:"hash"` Org sql.NullString `sql:"org"` Name sql.NullString `sql:"name"` FullName sql.NullString `sql:"full_name"` @@ -71,6 +76,11 @@ func (r *Repo) Nullify() *Repo { r.UserID.Valid = false } + // check if the Hash field should be false + if len(r.Hash.String) == 0 { + r.Hash.Valid = false + } + // check if the Org field should be false if len(r.Org.String) == 0 { r.Org.Valid = false @@ -121,6 +131,7 @@ func (r *Repo) ToLibrary() *library.Repo { repo.SetID(r.ID.Int64) repo.SetUserID(r.UserID.Int64) + repo.SetHash(r.Hash.String) repo.SetOrg(r.Org.String) repo.SetName(r.Name.String) repo.SetFullName(r.FullName.String) @@ -148,6 +159,11 @@ func (r *Repo) Validate() error { return ErrEmptyRepoUserID } + // verify the Hash field is populated + if len(r.Hash.String) == 0 { + return ErrEmptyRepoHash + } + // verify the Org field is populated if len(r.Org.String) == 0 { return ErrEmptyRepoOrg @@ -172,6 +188,7 @@ func RepoFromLibrary(r *library.Repo) *Repo { repo := &Repo{ ID: sql.NullInt64{Int64: r.GetID(), Valid: true}, UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true}, + Hash: sql.NullString{String: r.GetHash(), Valid: true}, Org: sql.NullString{String: r.GetOrg(), Valid: true}, Name: sql.NullString{String: r.GetName(), Valid: true}, FullName: sql.NullString{String: r.GetFullName(), Valid: true}, diff --git a/database/repo_test.go b/database/repo_test.go index cbb51cfd..83e91799 100644 --- a/database/repo_test.go +++ b/database/repo_test.go @@ -17,6 +17,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 0, Valid: true}, UserID: sql.NullInt64{Int64: 0, Valid: true}, + Hash: sql.NullString{String: "", Valid: true}, Org: sql.NullString{String: "", Valid: true}, Name: sql.NullString{String: "", Valid: true}, FullName: sql.NullString{String: "", Valid: true}, @@ -36,6 +37,7 @@ func TestDatabase_Repo_Nullify(t *testing.T) { want := &Repo{ ID: sql.NullInt64{Int64: 0, Valid: false}, UserID: sql.NullInt64{Int64: 0, Valid: false}, + Hash: sql.NullString{String: "", Valid: false}, Org: sql.NullString{String: "", Valid: false}, Name: sql.NullString{String: "", Valid: false}, FullName: sql.NullString{String: "", Valid: false}, @@ -82,6 +84,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) { want := &library.Repo{ ID: &num64, UserID: &num64, + Hash: &str, Org: &str, Name: &str, FullName: &str, @@ -101,6 +104,7 @@ func TestDatabase_Repo_ToLibrary(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: num64, Valid: true}, UserID: sql.NullInt64{Int64: num64, Valid: true}, + Hash: sql.NullString{String: str, Valid: true}, Org: sql.NullString{String: str, Valid: true}, Name: sql.NullString{String: str, Valid: true}, FullName: sql.NullString{String: str, Valid: true}, @@ -131,6 +135,7 @@ func TestDatabase_Repo_Validate(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 1, Valid: true}, UserID: sql.NullInt64{Int64: 1, Valid: true}, + Hash: sql.NullString{String: "baz", Valid: true}, Org: sql.NullString{String: "foo", Valid: true}, Name: sql.NullString{String: "bar", Valid: true}, FullName: sql.NullString{String: "foo/bar", Valid: true}, @@ -149,6 +154,25 @@ func TestDatabase_Repo_Validate_NoUserID(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 1, Valid: true}, Org: sql.NullString{String: "foo", Valid: true}, + Hash: sql.NullString{String: "baz", Valid: true}, + Name: sql.NullString{String: "bar", Valid: true}, + FullName: sql.NullString{String: "foo/bar", Valid: true}, + } + + // run test + err := r.Validate() + + if err == nil { + t.Errorf("Validate should have returned err") + } +} + +func TestDatabase_Repo_Validate_NoHash(t *testing.T) { + // setup types + r := &Repo{ + ID: sql.NullInt64{Int64: 1, Valid: true}, + UserID: sql.NullInt64{Int64: 1, Valid: true}, + Org: sql.NullString{String: "foo", Valid: true}, Name: sql.NullString{String: "bar", Valid: true}, FullName: sql.NullString{String: "foo/bar", Valid: true}, } @@ -166,6 +190,7 @@ func TestDatabase_Repo_Validate_NoOrg(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 1, Valid: true}, UserID: sql.NullInt64{Int64: 1, Valid: true}, + Hash: sql.NullString{String: "baz", Valid: true}, Name: sql.NullString{String: "bar", Valid: true}, FullName: sql.NullString{String: "foo/bar", Valid: true}, } @@ -183,6 +208,7 @@ func TestDatabase_Repo_Validate_NoName(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 1, Valid: true}, UserID: sql.NullInt64{Int64: 1, Valid: true}, + Hash: sql.NullString{String: "baz", Valid: true}, Org: sql.NullString{String: "foo", Valid: true}, FullName: sql.NullString{String: "foo/bar", Valid: true}, } @@ -199,6 +225,7 @@ func TestDatabase_Repo_Validate_NoFullName(t *testing.T) { r := &Repo{ ID: sql.NullInt64{Int64: 1, Valid: true}, UserID: sql.NullInt64{Int64: 1, Valid: true}, + Hash: sql.NullString{String: "baz", Valid: true}, Org: sql.NullString{String: "foo", Valid: true}, Name: sql.NullString{String: "bar", Valid: true}, } @@ -219,6 +246,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { want := &Repo{ ID: sql.NullInt64{Int64: num64, Valid: true}, UserID: sql.NullInt64{Int64: num64, Valid: true}, + Hash: sql.NullString{String: str, Valid: true}, Org: sql.NullString{String: str, Valid: true}, Name: sql.NullString{String: str, Valid: true}, FullName: sql.NullString{String: str, Valid: true}, @@ -238,6 +266,7 @@ func TestDatabase_RepoFromLibrary(t *testing.T) { r := &library.Repo{ ID: &num64, UserID: &num64, + Hash: &str, Org: &str, Name: &str, FullName: &str, diff --git a/library/repo.go b/library/repo.go index 1ac187db..bd7ca7bc 100644 --- a/library/repo.go +++ b/library/repo.go @@ -10,6 +10,7 @@ import "fmt" type Repo struct { ID *int64 `json:"id,omitempty"` UserID *int64 `json:"user_id,omitempty"` + Hash *string `json:"-"` Org *string `json:"org,omitempty"` Name *string `json:"name,omitempty"` FullName *string `json:"full_name,omitempty"` @@ -53,6 +54,19 @@ func (r *Repo) GetUserID() int64 { return *r.UserID } +// GetHash returns the Hash field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetHash() string { + // return zero value if Repo type or Hash field is nil + if r == nil || r.Hash == nil { + return "" + } + + return *r.Hash +} + // GetOrg returns the Org field. // // When the provided Repo type is nil, or the field within @@ -274,6 +288,19 @@ func (r *Repo) SetUserID(v int64) { r.UserID = &v } +// SetHash sets the Hash field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetHash(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Hash = &v +} + // SetOrg sets the Org field. // // When the provided Repo type is nil, it diff --git a/library/repo_test.go b/library/repo_test.go index 3873d8d5..46b5a9b5 100644 --- a/library/repo_test.go +++ b/library/repo_test.go @@ -19,6 +19,7 @@ func TestLibrary_Repo_Getters(t *testing.T) { r := &Repo{ ID: &num64, UserID: &num64, + Hash: &str, Org: &str, Name: &str, FullName: &str, @@ -37,6 +38,7 @@ func TestLibrary_Repo_Getters(t *testing.T) { } wantID := num64 wantUserID := num64 + wantHash := str wantOrg := str wantName := str wantFullName := str @@ -56,6 +58,7 @@ func TestLibrary_Repo_Getters(t *testing.T) { // run test gotID := r.GetID() gotUserID := r.GetUserID() + gotHash := r.GetHash() gotOrg := r.GetOrg() gotName := r.GetName() gotFullName := r.GetFullName() @@ -75,51 +78,71 @@ func TestLibrary_Repo_Getters(t *testing.T) { if gotID != wantID { t.Errorf("GetID is %v, want %v", gotID, wantID) } + if gotUserID != wantUserID { t.Errorf("GetUserID is %v, want %v", gotUserID, wantUserID) } + + if gotHash != wantHash { + t.Errorf("GetHash is %v, want %v", gotHash, wantHash) + } + if gotOrg != wantOrg { t.Errorf("GetOrg is %v, want %v", gotOrg, wantOrg) } + if gotName != wantName { t.Errorf("GetName is %v, want %v", gotName, wantName) } + if gotFullName != wantFullName { t.Errorf("GetFullName is %v, want %v", gotFullName, wantFullName) } + if gotLink != wantLink { t.Errorf("GetLink is %v, want %v", gotLink, wantLink) } + if gotClone != wantClone { t.Errorf("GetClone is %v, want %v", gotClone, wantClone) } + if gotBranch != wantBranch { t.Errorf("GetBranch is %v, want %v", gotBranch, wantBranch) } + if gotTimeout != wantTimeout { t.Errorf("GetTimeout is %v, want %v", gotTimeout, wantTimeout) } + if gotVisibility != wantVisibility { t.Errorf("GetVisibility is %v, want %v", gotVisibility, wantVisibility) } + if gotPrivate != wantPrivate { t.Errorf("GetPrivate is %v, want %v", gotPrivate, wantPrivate) } + if gotTrusted != wantTrusted { t.Errorf("GetTrusted is %v, want %v", gotTrusted, wantTrusted) } + if gotActive != wantActive { t.Errorf("GetActive is %v, want %v", gotActive, wantActive) } + if gotAllowPull != wantAllowPull { t.Errorf("GetAllowPull is %v, want %v", gotAllowPull, wantAllowPull) } + if gotAllowPush != wantAllowPush { t.Errorf("GetAllowPush is %v, want %v", gotAllowPush, wantAllowPush) } + if gotAllowDeploy != wantAllowDeploy { t.Errorf("GetAllowDeploy is %v, want %v", gotAllowDeploy, wantAllowDeploy) } + if gotAllowTag != wantAllowTag { t.Errorf("GetAllowTag is %v, want %v", gotAllowTag, wantAllowTag) } @@ -132,6 +155,7 @@ func TestLibrary_Repo_Getters_Empty(t *testing.T) { // run test gotID := r.GetID() gotUserID := r.GetUserID() + gotHash := r.GetHash() gotOrg := r.GetOrg() gotName := r.GetName() gotFullName := r.GetFullName() @@ -151,51 +175,71 @@ func TestLibrary_Repo_Getters_Empty(t *testing.T) { if gotID != 0 { t.Errorf("GetID is %v, want 0", gotID) } + if gotUserID != 0 { t.Errorf("GetUserID is %v, want 0", gotUserID) } + + if gotHash != "" { + t.Errorf("GetHash is %v, want \"\"", gotHash) + } + if gotOrg != "" { t.Errorf("GetOrg is %v, want \"\"", gotOrg) } + if gotName != "" { t.Errorf("GetName is %v, want \"\"", gotName) } + if gotFullName != "" { t.Errorf("GetFullName is %v, want \"\"", gotFullName) } + if gotLink != "" { t.Errorf("GetLink is %v, want \"\"", gotLink) } + if gotClone != "" { t.Errorf("GetClone is %v, want \"\"", gotClone) } + if gotBranch != "" { t.Errorf("GetBranch is %v, want \"\"", gotBranch) } + if gotTimeout != 0 { t.Errorf("GetTimeout is %v, want 0", gotTimeout) } + if gotVisibility != "" { t.Errorf("GetVisibility is %v, want \"\"", gotVisibility) } + if gotPrivate != false { t.Errorf("GetPrivate is %v, want false", gotPrivate) } + if gotTrusted != false { t.Errorf("GetTrusted is %v, want false", gotTrusted) } + if gotActive != false { t.Errorf("GetActive is %v, want false", gotActive) } + if gotAllowPull != false { t.Errorf("GetAllowPull is %v, want false", gotAllowPull) } + if gotAllowPush != false { t.Errorf("GetAllowPush is %v, want false", gotAllowPush) } + if gotAllowDeploy != false { t.Errorf("GetAllowDeploy is %v, want false", gotAllowDeploy) } + if gotAllowTag != false { t.Errorf("GetAllowTag is %v, want false", gotAllowTag) } @@ -211,6 +255,7 @@ func TestLibrary_Repo_Setters(t *testing.T) { wantID := num64 wantUserID := num64 + wantHash := str wantOrg := str wantName := str wantFullName := str @@ -230,6 +275,7 @@ func TestLibrary_Repo_Setters(t *testing.T) { // run test r.SetID(wantID) r.SetUserID(wantUserID) + r.SetHash(wantHash) r.SetOrg(wantOrg) r.SetName(wantName) r.SetFullName(wantFullName) @@ -249,51 +295,71 @@ func TestLibrary_Repo_Setters(t *testing.T) { if r.GetID() != wantID { t.Errorf("GetID is %v, want %v", r.GetID(), wantID) } + if r.GetUserID() != wantUserID { t.Errorf("GetUserID is %v, want %v", r.GetUserID(), wantUserID) } + + if r.GetHash() != wantHash { + t.Errorf("GetHash is %v, want %v", r.GetHash(), wantHash) + } + if r.GetOrg() != wantOrg { t.Errorf("GetOrg is %v, want %v", r.GetOrg(), wantOrg) } + if r.GetName() != wantName { t.Errorf("GetName is %v, want %v", r.GetName(), wantName) } + if r.GetFullName() != wantFullName { t.Errorf("GetFullName is %v, want %v", r.GetFullName(), wantFullName) } + if r.GetLink() != wantLink { t.Errorf("GetLink is %v, want %v", r.GetLink(), wantLink) } + if r.GetClone() != wantClone { t.Errorf("GetClone is %v, want %v", r.GetClone(), wantClone) } + if r.GetBranch() != wantBranch { t.Errorf("GetBranch is %v, want %v", r.GetBranch(), wantBranch) } + if r.GetTimeout() != wantTimeout { t.Errorf("GetTimeout is %v, want %v", r.GetTimeout(), wantTimeout) } + if r.GetVisibility() != wantVisibility { t.Errorf("GetVisibility is %v, want %v", r.GetVisibility(), wantVisibility) } + if r.GetPrivate() != wantPrivate { t.Errorf("GetPrivate is %v, want %v", r.GetPrivate(), wantPrivate) } + if r.GetTrusted() != wantTrusted { t.Errorf("GetTrusted is %v, want %v", r.GetTrusted(), wantTrusted) } + if r.GetActive() != wantActive { t.Errorf("GetActive is %v, want %v", r.GetActive(), wantActive) } + if r.GetAllowPull() != wantAllowPull { t.Errorf("GetAllowPull is %v, want %v", r.GetAllowPull(), wantAllowPull) } + if r.GetAllowPush() != wantAllowPush { t.Errorf("GetAllowPush is %v, want %v", r.GetAllowPush(), wantAllowPush) } + if r.GetAllowDeploy() != wantAllowDeploy { t.Errorf("GetAllowDeploy is %v, want %v", r.GetAllowDeploy(), wantAllowDeploy) } + if r.GetAllowTag() != wantAllowTag { t.Errorf("GetAllowTag is %v, want %v", r.GetAllowTag(), wantAllowTag) } @@ -306,6 +372,7 @@ func TestLibrary_Repo_Setters_Empty(t *testing.T) { // run test r.SetID(0) r.SetUserID(0) + r.SetHash("") r.SetOrg("") r.SetName("") r.SetFullName("") @@ -325,51 +392,71 @@ func TestLibrary_Repo_Setters_Empty(t *testing.T) { if r.GetID() != 0 { t.Errorf("GetID is %v, want 0", r.GetID()) } + if r.GetUserID() != 0 { t.Errorf("GetUserID is %v, want 0", r.GetUserID()) } + + if r.GetHash() != "" { + t.Errorf("GetHash is %v, want \"\"", r.GetHash()) + } + if r.GetOrg() != "" { t.Errorf("GetOrg is %v, want \"\"", r.GetOrg()) } + if r.GetName() != "" { t.Errorf("GetName is %v, want \"\"", r.GetName()) } + if r.GetFullName() != "" { t.Errorf("GetFullName is %v, want \"\"", r.GetFullName()) } + if r.GetLink() != "" { t.Errorf("GetLink is %v, want \"\"", r.GetLink()) } + if r.GetClone() != "" { t.Errorf("GetClone is %v, want \"\"", r.GetClone()) } + if r.GetBranch() != "" { t.Errorf("GetBranch is %v, want \"\"", r.GetBranch()) } + if r.GetTimeout() != 0 { t.Errorf("GetTimeout is %v, want 0", r.GetTimeout()) } + if r.GetVisibility() != "" { t.Errorf("GetVisibility is %v, want \"\"", r.GetVisibility()) } + if r.GetPrivate() != false { t.Errorf("GetPrivate is %v, want false", r.GetPrivate()) } + if r.GetTrusted() != false { t.Errorf("GetTrusted is %v, want false", r.GetTrusted()) } + if r.GetActive() != false { t.Errorf("GetActive is %v, want false", r.GetActive()) } + if r.GetAllowPull() != false { t.Errorf("GetAllowPull is %v, want false", r.GetAllowPull()) } + if r.GetAllowPush() != false { t.Errorf("GetAllowPush is %v, want false", r.GetAllowPush()) } + if r.GetAllowDeploy() != false { t.Errorf("GetAllowDeploy is %v, want false", r.GetAllowDeploy()) } + if r.GetAllowTag() != false { t.Errorf("GetAllowTag is %v, want false", r.GetAllowTag()) } @@ -384,6 +471,7 @@ func TestLibrary_Repo_String(t *testing.T) { r := &Repo{ ID: &num64, UserID: &num64, + Hash: &str, Org: &str, Name: &str, FullName: &str,