Skip to content

Instantly share code, notes, and snippets.

t.Run("returns an error on failure to get customer booking count", func(t *testing.T) {
var customerID uint = 1
var want = errors.New("oh no!")
ctrl := gomock.NewController(t)
mockCustomerGetter := NewMockCustomerGetter(ctrl)
mockCustomerBookingCounter := NewMockCustomerGetter(ctrl)
mockCustomerGetter.EXPECT().GetCustomer(customerID).Return(entities.Customer{}, nil)
@CharlesWinter
CharlesWinter / get_customer.go
Created January 22, 2021 16:05
Usecase after adding the new customer booking counter
package usecases
import "github.com/example/go-abstractions/entities"
type CustomerGetter interface {
GetCustomer(customerID uint) (entities.Customer, error)
}
type CustomerBookingsCounter interface {
@CharlesWinter
CharlesWinter / count_customer_bookings.go
Created January 22, 2021 15:59
Our repository method for counting customer bookings
ackage bookings
type Repository struct {
// in reality, this client will likely be something we generate from a
// schema, or implement ourselves if needs be. Its not discussed here for
// conciseness
client bookingsServiceClient
}
type booking struct {
@CharlesWinter
CharlesWinter / customer.go
Created January 22, 2021 15:28
The entities package customer struct
package entities
type Customer struct {
ID uint
FirstName string
Postcode string
}
@CharlesWinter
CharlesWinter / get_customer.go
Last active January 22, 2021 15:23
The get_customer Business Logic
package usecases
import "github.com/example/go-abstractions/entities"
type CustomerGetter interface {
GetCustomer(customerID uint) (entities.Customer, error)
}
@CharlesWinter
CharlesWinter / get_customer.go
Created January 22, 2021 15:16
The get_customer.go File
package customers
import "github.com/example/go-abstractions/entities"
// representation of a fictional database customer.
type customerData struct {
ID uint `db:"id"`
FirstName string `db:"name"`
Postcode string `db:"postcode"`
}
@CharlesWinter
CharlesWinter / repository.go
Created January 22, 2021 15:13
The repositories/customers/repository.go File
package customers
type Repository struct {
// This is where whatever the repository will use to access the DB will live
// as an unexported field. Nothing groundbreaking here.
somethingToAccessTheDB interface{}
}
// NewRepository will return a new instance of the customers.Repository. I've
// not outfitted it with actual DB connection logic so as to keep this article
@CharlesWinter
CharlesWinter / qxrecs.js
Created March 5, 2018 10:01
Golang API
const qxrecs = require('qxrecs');
/*
Configure the module with your company name, unique token
and communication protocol (currently either https or wss)
*/
qxrecs.Configure("my_company", "my_token", "https");
//Insert some products using callbacks. Can be used with multiple items
qxrecs.PostProducts([{itemid: "A" }], (err, body) => {