Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
KenWilliamson committed Sep 6, 2023
1 parent 6f01064 commit bc19b15
Show file tree
Hide file tree
Showing 19 changed files with 1,998 additions and 1 deletion.
40 changes: 40 additions & 0 deletions db/coverage.out
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ github.com/Ulbora/go-micro-blog/db/config.go:85.17,87.18 2 1
github.com/Ulbora/go-micro-blog/db/config.go:87.18,89.19 2 1
github.com/Ulbora/go-micro-blog/db/config.go:89.19,93.6 3 1
github.com/Ulbora/go-micro-blog/db/config.go:97.2,97.13 1 1
github.com/Ulbora/go-micro-blog/db/home.go:28.52,31.25 3 1
github.com/Ulbora/go-micro-blog/db/home.go:31.25,33.3 1 1
github.com/Ulbora/go-micro-blog/db/home.go:34.2,34.15 1 1
github.com/Ulbora/go-micro-blog/db/home.go:34.15,40.3 5 1
github.com/Ulbora/go-micro-blog/db/home.go:41.2,41.16 1 1
github.com/Ulbora/go-micro-blog/db/home.go:45.46,47.25 2 1
github.com/Ulbora/go-micro-blog/db/home.go:47.25,49.3 1 1
github.com/Ulbora/go-micro-blog/db/home.go:50.2,50.15 1 1
github.com/Ulbora/go-micro-blog/db/home.go:50.15,55.3 4 1
github.com/Ulbora/go-micro-blog/db/home.go:56.2,56.12 1 1
github.com/Ulbora/go-micro-blog/db/home.go:60.38,61.25 1 1
github.com/Ulbora/go-micro-blog/db/home.go:61.25,63.3 1 1
github.com/Ulbora/go-micro-blog/db/home.go:64.2,68.40 5 1
github.com/Ulbora/go-micro-blog/db/home.go:68.40,70.28 2 1
github.com/Ulbora/go-micro-blog/db/home.go:70.28,74.4 3 1
github.com/Ulbora/go-micro-blog/db/home.go:76.2,76.13 1 1
github.com/Ulbora/go-micro-blog/db/home.go:79.59,82.24 3 1
github.com/Ulbora/go-micro-blog/db/home.go:82.24,85.17 3 1
github.com/Ulbora/go-micro-blog/db/home.go:85.17,88.4 2 1
github.com/Ulbora/go-micro-blog/db/home.go:90.2,90.13 1 1
github.com/Ulbora/go-micro-blog/db/like.go:26.42,29.25 3 1
github.com/Ulbora/go-micro-blog/db/like.go:29.25,31.3 1 1
github.com/Ulbora/go-micro-blog/db/like.go:32.2,32.14 1 1
Expand All @@ -144,6 +164,26 @@ github.com/Ulbora/go-micro-blog/db/myBlogDB.go:55.17,57.4 1 1
github.com/Ulbora/go-micro-blog/db/myBlogDB.go:58.3,58.20 1 1
github.com/Ulbora/go-micro-blog/db/myBlogDB.go:58.20,60.4 1 1
github.com/Ulbora/go-micro-blog/db/myBlogDB.go:62.2,62.12 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:28.70,31.25 3 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:31.25,33.3 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:34.2,34.15 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:34.15,40.3 5 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:41.2,41.16 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:45.64,47.25 2 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:47.25,49.3 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:50.2,50.15 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:50.15,55.3 4 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:56.2,56.12 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:60.56,61.25 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:61.25,63.3 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:64.2,68.40 5 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:68.40,70.28 2 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:70.28,74.4 3 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:76.2,76.13 1 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:79.77,82.24 3 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:82.24,85.17 3 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:85.17,88.4 2 1
github.com/Ulbora/go-micro-blog/db/privacyPolicy.go:90.2,90.13 1 1
github.com/Ulbora/go-micro-blog/db/role.go:26.55,29.25 3 1
github.com/Ulbora/go-micro-blog/db/role.go:29.25,31.3 1 1
github.com/Ulbora/go-micro-blog/db/role.go:32.2,32.16 1 1
Expand Down
10 changes: 10 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ type BlogDB interface {
AddAbout(ab *About) (bool, int64)
UpdateAbout(ab *About) bool
GetAbout() *[]About

//Home
AddHome(ab *Home) (bool, int64)
UpdateHome(ab *Home) bool
GetHome() *[]Home

//PrivacyPolicy
AddPrivacyPolicy(ab *PrivacyPolicy) (bool, int64)
UpdatePrivacyPolicy(ab *PrivacyPolicy) bool
GetPrivacyPolicy() *[]PrivacyPolicy
}
91 changes: 91 additions & 0 deletions db/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package db

import (
"strconv"
)

/*
Copyright (C) 2023 Ulbora Labs LLC. (www.ulboralabs.com)
All rights reserved.
Copyright (C) 2023 Ken Williamson
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// AddHome AddHome
func (d *MyBlogDB) AddHome(ab *Home) (bool, int64) {
var suc bool
var id int64
if !d.testConnection() {
d.DB.Connect()
}
if ab != nil {
var a []any
a = append(a, ab.Content)
suc, id = d.DB.Insert(insertHome, a...)
d.Log.Debug("suc in add Home", suc)
d.Log.Debug("id in add Home", id)
}
return suc, id
}

// UpdateHome UpdateHome
func (d *MyBlogDB) UpdateHome(ab *Home) bool {
var suc bool
if !d.testConnection() {
d.DB.Connect()
}
if ab != nil {
var a []any
a = append(a, ab.Content, ab.ID)
suc = d.DB.Update(updateHome, a...)
d.Log.Debug("suc in update Home", suc)
}
return suc
}

// GetHome GetHome
func (d *MyBlogDB) GetHome() *[]Home {
if !d.testConnection() {
d.DB.Connect()
}
var rtn = []Home{}
var a []any
a = append(a)
rows := d.DB.GetList(selectHome, a...)
if rows != nil && len(rows.Rows) != 0 {
foundRows := rows.Rows
for r := range foundRows {
foundRow := foundRows[r]
rowContent := d.parseHomeRow(&foundRow)
rtn = append(rtn, *rowContent)
}
}
return &rtn
}

func (d *MyBlogDB) parseHomeRow(foundRow *[]string) *Home {
var rtn Home
d.Log.Debug("foundRow in Home", *foundRow)
if len(*foundRow) > 0 {
id, err := strconv.ParseInt((*foundRow)[0], 10, 64)
d.Log.Debug("id err in get Home", err)
if err == nil {
rtn.ID = id
rtn.Content = (*foundRow)[1]
}
}
return &rtn
}
219 changes: 219 additions & 0 deletions db/home_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package db

import (
"reflect"
"testing"

lg "github.com/GolangToolKits/go-level-logger"
gdb "github.com/GolangToolKits/go-mysql"
)

func TestMyBlogDB_AddHome(t *testing.T) {

// db := gdb.MyDB{
// Host: "localhost:3306",
// User: "admin",
// Password: "admin",
// Database: "go_micro_blog",
// }

db := gdb.MyDBMock{
Host: "localhost:3306",
User: "admin",
Password: "admin",
Database: "go_micro_blog",
}
db.MockTestRow = &gdb.DbRow{
//Row: []string{"0"},
Row: []string{},
}
db.MockConnectSuccess = true
db.MockInsertID1 = 1
db.MockInsertSuccess1 = true

var l lg.Logger
log := l.New()
log.SetLogLevel(lg.AllLevel)

type fields struct {
DB gdb.Database
Log lg.Log
}
type args struct {
r *Home
}
tests := []struct {
name string
fields fields
args args
want bool
want1 int64
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
DB: db.New(),
Log: log,
},
args: args{
r: &Home{
Content: "test Home content",
},
},
want: true,
want1: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &MyBlogDB{
DB: tt.fields.DB,
Log: tt.fields.Log,
}
d.DB.Connect()
got, got1 := d.AddHome(tt.args.r)
if got != tt.want {
t.Errorf("MyBlogDB.AddHome() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("MyBlogDB.AddHome() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestMyBlogDB_UpdateHome(t *testing.T) {

// db := gdb.MyDB{
// Host: "localhost:3306",
// User: "admin",
// Password: "admin",
// Database: "go_micro_blog",
// }

db := gdb.MyDBMock{
Host: "localhost:3306",
User: "admin",
Password: "admin",
Database: "go_micro_blog",
}
db.MockTestRow = &gdb.DbRow{
//Row: []string{"0"},
Row: []string{},
}
db.MockConnectSuccess = true
db.MockUpdateSuccess1 = true

var l lg.Logger
log := l.New()
log.SetLogLevel(lg.AllLevel)

type fields struct {
DB gdb.Database
Log lg.Log
}
type args struct {
r *Home
}
tests := []struct {
name string
fields fields
args args
want bool
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
DB: db.New(),
Log: log,
},
args: args{
r: &Home{
ID: 1,
Content: "test home content 1",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &MyBlogDB{
DB: tt.fields.DB,
Log: tt.fields.Log,
}
d.DB.Connect()
if got := d.UpdateHome(tt.args.r); got != tt.want {
t.Errorf("MyBlogDB.UpdateHome() = %v, want %v", got, tt.want)
}
})
}
}

func TestMyBlogDB_GetHome(t *testing.T) {
// db := gdb.MyDB{
// Host: "localhost:3306",
// User: "admin",
// Password: "admin",
// Database: "go_micro_blog",
// }

db := gdb.MyDBMock{
Host: "localhost:3306",
User: "admin",
Password: "admin",
Database: "go_micro_blog",
}
db.MockTestRow = &gdb.DbRow{
//Row: []string{"0"},
Row: []string{},
}

db.MockRows1 = &gdb.DbRows{
Rows: [][]string{
{"1", "test home content 1"},
//{"2", "test content 3"},
},
}

var l lg.Logger
log := l.New()
log.SetLogLevel(lg.AllLevel)

type fields struct {
DB gdb.Database
Log lg.Log
}
tests := []struct {
name string
fields fields
want *[]Home
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
DB: db.New(),
Log: log,
},
want: &[]Home{
{1, "test home content 1"},
// {3, "test Content 3"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &MyBlogDB{
DB: tt.fields.DB,
Log: tt.fields.Log,
}
d.DB.Connect()
if got := d.GetHome(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MyBlogDB.GetHome() = %v, want %v", got, tt.want)
}
})
}
}
12 changes: 12 additions & 0 deletions db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,15 @@ type About struct {
ID int64 `json:"id"`
Content string `json:"content"`
}

// Home Home
type Home struct {
ID int64 `json:"id"`
Content string `json:"content"`
}

// PrivacyPolicy Privacy
type PrivacyPolicy struct {
ID int64 `json:"id"`
Content string `json:"content"`
}
Loading

0 comments on commit bc19b15

Please sign in to comment.