We will create our own sql.DB class for the purposes of being able to mock out our tests.
[x] First we make an interface for mocking, called IAccountsTableConnection, in the ifaces package
[x] Add all the function signatures from accountstable to this interface, excluding the parameter sql.DB from all of them. Please read about resources in the link below if you're unfamiliar with them.
[x] The last two functions are Lock and Unlock, which have no parameters or returns.
[x] Add a struct to accountstable package called AccountsTableConnection that contains a sync.RWMutex lock, which is a read/write lock. Also add a sql.DB field.
[x] Implement the functions. Using the struct from the previous step as a receiver, first implement Lock and Unlock. These should simply call Lock and Unlock on the RWMutex variable in the struct.
[x] All the other implemented functions should simply call their corresponding accountstable functions. However, there is a caveat. **With the exception of the Get... type functions, call theLockmethod before executed theaccountstablefunction and callUnlock` after it.
We will create our own
sql.DB
class for the purposes of being able to mock out our tests.IAccountsTableConnection
, in theifaces
packageaccountstable
to this interface, excluding the parametersql.DB
from all of them. Please read about resources in the link below if you're unfamiliar with them.Lock
andUnlock
, which have no parameters or returns.accountstable
package calledAccountsTableConnection
that contains async.RWMutex
lock, which is a read/write lock. Also add asql.DB
field.Lock
andUnlock
. These should simply callLock
andUnlock
on theRWMutex
variable in the struct.accountstable
functions. However, there is a caveat. **With the exception of theGet...
type functions, call the
Lockmethod before executed the
accountstablefunction and call
Unlock` after it.Some resources to read up on: Interfaces: https://gobyexample.com/interfaces DB: https://golang.org/pkg/database/sql/#DB RWLock: https://golang.org/pkg/sync/#RWMutex