Skip to content

Commit

Permalink
added addition functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
KenWilliamson committed Aug 27, 2023
1 parent ea6c8b3 commit dabc918
Show file tree
Hide file tree
Showing 14 changed files with 1,094 additions and 2 deletions.
91 changes: 91 additions & 0 deletions db/about.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/>.
*/

// AddAbout AddAbout
func (d *MyBlogDB) AddAbout(ab *About) (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(insertAbout, a...)
d.Log.Debug("suc in add config", suc)
d.Log.Debug("id in add config", id)
}
return suc, id
}

// UpdateAbout UpdateAbout
func (d *MyBlogDB) UpdateAbout(ab *About) 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(updateAbout, a...)
d.Log.Debug("suc in update About", suc)
}
return suc
}

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

func (d *MyBlogDB) parseAboutRow(foundRow *[]string) *About {
var rtn About
d.Log.Debug("foundRow in Tos", *foundRow)
if len(*foundRow) > 0 {
id, err := strconv.ParseInt((*foundRow)[0], 10, 64)
d.Log.Debug("id err in get About", err)
if err == nil {
rtn.ID = id
rtn.Content = (*foundRow)[1]
}
}
return &rtn
}
219 changes: 219 additions & 0 deletions db/about_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_AddAbout(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 *About
}
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: &About{
Content: "test About 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.AddAbout(tt.args.r)
if got != tt.want {
t.Errorf("MyBlogDB.AddAbout() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("MyBlogDB.AddAbout() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestMyBlogDB_UpdateAbout(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 *About
}
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: &About{
ID: 1,
Content: "test about 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.UpdateAbout(tt.args.r); got != tt.want {
t.Errorf("MyBlogDB.UpdateAbout() = %v, want %v", got, tt.want)
}
})
}
}

func TestMyBlogDB_GetAbout(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 about 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 *[]About
}{
// TODO: Add test cases.
{
name: "test 1",
fields: fields{
DB: db.New(),
Log: log,
},
want: &[]About{
{1, "test about 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.GetAbout(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MyBlogDB.GetAbout() = %v, want %v", got, tt.want)
}
})
}
}
60 changes: 60 additions & 0 deletions db/coverage.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
mode: set
github.com/Ulbora/go-micro-blog/db/about.go:28.54,31.25 3 1
github.com/Ulbora/go-micro-blog/db/about.go:31.25,33.3 1 1
github.com/Ulbora/go-micro-blog/db/about.go:34.2,34.15 1 1
github.com/Ulbora/go-micro-blog/db/about.go:34.15,40.3 5 1
github.com/Ulbora/go-micro-blog/db/about.go:41.2,41.16 1 1
github.com/Ulbora/go-micro-blog/db/about.go:45.48,47.25 2 1
github.com/Ulbora/go-micro-blog/db/about.go:47.25,49.3 1 1
github.com/Ulbora/go-micro-blog/db/about.go:50.2,50.15 1 1
github.com/Ulbora/go-micro-blog/db/about.go:50.15,55.3 4 1
github.com/Ulbora/go-micro-blog/db/about.go:56.2,56.12 1 1
github.com/Ulbora/go-micro-blog/db/about.go:60.40,61.25 1 1
github.com/Ulbora/go-micro-blog/db/about.go:61.25,63.3 1 1
github.com/Ulbora/go-micro-blog/db/about.go:64.2,68.40 5 1
github.com/Ulbora/go-micro-blog/db/about.go:68.40,70.28 2 1
github.com/Ulbora/go-micro-blog/db/about.go:70.28,74.4 3 1
github.com/Ulbora/go-micro-blog/db/about.go:76.2,76.13 1 1
github.com/Ulbora/go-micro-blog/db/about.go:79.61,82.24 3 1
github.com/Ulbora/go-micro-blog/db/about.go:82.24,85.17 3 1
github.com/Ulbora/go-micro-blog/db/about.go:85.17,88.4 2 1
github.com/Ulbora/go-micro-blog/db/about.go:90.2,90.13 1 1
github.com/Ulbora/go-micro-blog/db/blog.go:29.51,32.25 3 1
github.com/Ulbora/go-micro-blog/db/blog.go:32.25,34.3 1 1
github.com/Ulbora/go-micro-blog/db/blog.go:35.2,35.14 1 1
Expand Down Expand Up @@ -145,6 +165,46 @@ github.com/Ulbora/go-micro-blog/db/role.go:86.59,89.24 3 1
github.com/Ulbora/go-micro-blog/db/role.go:89.24,92.17 3 1
github.com/Ulbora/go-micro-blog/db/role.go:92.17,95.4 2 1
github.com/Ulbora/go-micro-blog/db/role.go:97.2,97.13 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:28.51,31.25 3 1
github.com/Ulbora/go-micro-blog/db/rules.go:31.25,33.3 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:34.2,34.14 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:34.14,40.3 5 1
github.com/Ulbora/go-micro-blog/db/rules.go:41.2,41.16 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:45.45,47.25 2 1
github.com/Ulbora/go-micro-blog/db/rules.go:47.25,49.3 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:50.2,50.14 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:50.14,55.3 4 1
github.com/Ulbora/go-micro-blog/db/rules.go:56.2,56.12 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:60.38,61.25 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:61.25,63.3 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:64.2,68.40 5 1
github.com/Ulbora/go-micro-blog/db/rules.go:68.40,70.28 2 1
github.com/Ulbora/go-micro-blog/db/rules.go:70.28,74.4 3 1
github.com/Ulbora/go-micro-blog/db/rules.go:76.2,76.13 1 1
github.com/Ulbora/go-micro-blog/db/rules.go:79.59,82.24 3 1
github.com/Ulbora/go-micro-blog/db/rules.go:82.24,85.17 3 1
github.com/Ulbora/go-micro-blog/db/rules.go:85.17,88.4 2 1
github.com/Ulbora/go-micro-blog/db/rules.go:90.2,90.13 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:28.49,31.25 3 1
github.com/Ulbora/go-micro-blog/db/tos.go:31.25,33.3 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:34.2,34.14 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:34.14,40.3 5 1
github.com/Ulbora/go-micro-blog/db/tos.go:41.2,41.16 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:45.43,47.25 2 1
github.com/Ulbora/go-micro-blog/db/tos.go:47.25,49.3 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:50.2,50.14 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:50.14,55.3 4 1
github.com/Ulbora/go-micro-blog/db/tos.go:56.2,56.12 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:60.36,61.25 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:61.25,63.3 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:64.2,68.40 5 1
github.com/Ulbora/go-micro-blog/db/tos.go:68.40,70.28 2 1
github.com/Ulbora/go-micro-blog/db/tos.go:70.28,74.4 3 1
github.com/Ulbora/go-micro-blog/db/tos.go:76.2,76.13 1 1
github.com/Ulbora/go-micro-blog/db/tos.go:79.57,82.24 3 1
github.com/Ulbora/go-micro-blog/db/tos.go:82.24,85.17 3 1
github.com/Ulbora/go-micro-blog/db/tos.go:85.17,88.4 2 1
github.com/Ulbora/go-micro-blog/db/tos.go:90.2,90.13 1 1
github.com/Ulbora/go-micro-blog/db/user.go:26.51,29.25 3 1
github.com/Ulbora/go-micro-blog/db/user.go:29.25,31.3 1 1
github.com/Ulbora/go-micro-blog/db/user.go:32.2,32.14 1 1
Expand Down
Loading

0 comments on commit dabc918

Please sign in to comment.