-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea6c8b3
commit dabc918
Showing
14 changed files
with
1,094 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.