Added initial ideas and code

master
Andrew Sellers 4 years ago
commit 582ccf92c8
  1. 7
      .vscode/settings.json
  2. 6
      config/config.go
  3. 112
      db/db.go
  4. 241
      db/db_test.go
  5. BIN
      main
  6. 23
      main.go

@ -0,0 +1,7 @@
{
"go.gopath": "/home/andrew",
"editor.formatOnSave": true,
"go.lintOnSave": "package",
"go.lintTool": "gometalinter",
"go.lintFlags": ["--line-length=100"]
}

@ -0,0 +1,6 @@
package config
var (
Dev = true
Production = false
)

@ -0,0 +1,112 @@
package db
import (
"errors"
"log"
"andrewcsellers.com/shiptdemo/config"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
var TODO = errors.New("Unimplemented")
var dynamo *dynamodb.DynamoDB
func Init(p *session.Session, c *aws.Config) error {
dynamo = dynamodb.New(p, c)
lto, err := dynamo.ListTables(&dynamodb.ListTablesInput{})
if err != nil {
return err
}
for _, tn := range lto.TableNames {
if *tn == "Products" {
return nil
}
if config.Dev {
log.Println("Table Name: ", *tn)
}
}
return errors.New("No Products Table Found")
}
// Product represents a single product. Generally
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Vendor string `json:"vendor"`
Price float64 `json:"price"`
}
// Insert will create a new Product in the database.
func (p *Product) Insert() error {
return TODO
}
// Upsert will create a new Product in the database only
// if another product doesn't already have the same name
// and vendor. If there is another product with the same
// name and vendor, that other product will be updated
// with the price and attributes of the Product Upsert
// was called on.
func (p *Product) Upsert() error {
return TODO
}
// Update will update the Product record attributes.
func (p *Product) Update() error {
return TODO
}
// Delete will remove the Product record from the DB.
func (p *Product) Delete() error {
return TODO
}
func ProductMassInsert(p []Product) error {
return TODO
}
func ProductMassDelete(p []Product) error {
return TODO
}
type ProductQuery struct {
q []query
Limit int
Skip int
}
// One will return a single product based on the query, or
// nothing if there isn't a product that match the query. If
// there isn't a product to return, an error will be present.
func (p ProductQuery) One() (*Product, error) {
return nil, TODO
}
// All will return all products matching the query, if Limit
// isn't set (or set to 0), 1,000 will be used as the limit.
// For more than 1,000 products, set the Limit value to -1
// (or less than zero)
func (p ProductQuery) All() ([]*Product, error) {
return nil, TODO
}
type query struct {
Field string
Op Operation
Values []interface{}
}
type Operation int
const (
OpEqual Operation = iota
OpNotEqual
OpIn
OpNotIn
OpLess
OpMore
OpLessEqual
OpMoreEqual
Op
)

@ -0,0 +1,241 @@
package db
import (
"reflect"
"testing"
)
func TestInit(t *testing.T) {
tests := []struct {
name string
}{
// TODO: Add test cases.
}
for range tests {
t.Run(tt.name, func(t *testing.T) {
Init()
})
}
}
func TestProduct_Insert(t *testing.T) {
type fields struct {
ID int
Name string
Vendor string
Price float64
}
tests := []struct {
name string
fields fields
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Product{
ID: tt.fields.ID,
Name: tt.fields.Name,
Vendor: tt.fields.Vendor,
Price: tt.fields.Price,
}
if err := p.Insert(); (err != nil) != tt.wantErr {
t.Errorf("Product.Insert() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProduct_Upsert(t *testing.T) {
type fields struct {
ID int
Name string
Vendor string
Price float64
}
tests := []struct {
name string
fields fields
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Product{
ID: tt.fields.ID,
Name: tt.fields.Name,
Vendor: tt.fields.Vendor,
Price: tt.fields.Price,
}
if err := p.Upsert(); (err != nil) != tt.wantErr {
t.Errorf("Product.Upsert() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProduct_Update(t *testing.T) {
type fields struct {
ID int
Name string
Vendor string
Price float64
}
tests := []struct {
name string
fields fields
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Product{
ID: tt.fields.ID,
Name: tt.fields.Name,
Vendor: tt.fields.Vendor,
Price: tt.fields.Price,
}
if err := p.Update(); (err != nil) != tt.wantErr {
t.Errorf("Product.Update() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProduct_Delete(t *testing.T) {
type fields struct {
ID int
Name string
Vendor string
Price float64
}
tests := []struct {
name string
fields fields
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Product{
ID: tt.fields.ID,
Name: tt.fields.Name,
Vendor: tt.fields.Vendor,
Price: tt.fields.Price,
}
if err := p.Delete(); (err != nil) != tt.wantErr {
t.Errorf("Product.Delete() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProductMassInsert(t *testing.T) {
type args struct {
p []Product
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ProductMassInsert(tt.args.p); (err != nil) != tt.wantErr {
t.Errorf("ProductMassInsert() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProductMassDelete(t *testing.T) {
type args struct {
p []Product
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ProductMassDelete(tt.args.p); (err != nil) != tt.wantErr {
t.Errorf("ProductMassDelete() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestProductQuery_One(t *testing.T) {
type fields struct {
q []query
Limit int
Skip int
}
tests := []struct {
name string
fields fields
want *Product
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := ProductQuery{
q: tt.fields.q,
Limit: tt.fields.Limit,
Skip: tt.fields.Skip,
}
got, err := p.One()
if (err != nil) != tt.wantErr {
t.Errorf("ProductQuery.One() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ProductQuery.One() = %v, want %v", got, tt.want)
}
})
}
}
func TestProductQuery_All(t *testing.T) {
type fields struct {
q []query
Limit int
Skip int
}
tests := []struct {
name string
fields fields
want []*Product
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := ProductQuery{
q: tt.fields.q,
Limit: tt.fields.Limit,
Skip: tt.fields.Skip,
}
got, err := p.All()
if (err != nil) != tt.wantErr {
t.Errorf("ProductQuery.All() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ProductQuery.All() = %v, want %v", got, tt.want)
}
})
}
}

BIN
main

Binary file not shown.

@ -0,0 +1,23 @@
package main
import (
"fmt"
"log"
"andrewcsellers.com/shiptdemo/db"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
)
func init() {
sess, err := session.NewSession()
if err != nil {
log.Fatal("Cannot start AWS Session: ", err)
}
db.Init(sess, &aws.Config{Region: aws.String("us-east-1")})
}
func main() {
fmt.Println("Implement more stuff")
}
Loading…
Cancel
Save