-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from prest/mock
Mockadapter
- Loading branch information
Showing
3 changed files
with
639 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -11,9 +11,9 @@ os: | |
- linux | ||
|
||
go: | ||
- 1.7 | ||
- 1.8 | ||
- 1.9 | ||
- "1.10" | ||
- tip | ||
|
||
matrix: | ||
|
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,265 @@ | ||
package mock | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/url" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/prest/adapters" | ||
"github.com/prest/adapters/internal/scanner" | ||
) | ||
|
||
// Item mock | ||
type Item struct { | ||
Body []byte | ||
Error error | ||
HasPermission bool | ||
IsCount bool | ||
} | ||
|
||
// Mock adapter | ||
type Mock struct { | ||
mtx *sync.RWMutex | ||
t *testing.T | ||
Items []Item | ||
} | ||
|
||
// New mock | ||
func New(t *testing.T) (m *Mock) { | ||
m = &Mock{ | ||
mtx: &sync.RWMutex{}, | ||
t: t, | ||
} | ||
return | ||
} | ||
|
||
func (m *Mock) validate() { | ||
if len(m.Items) == 0 { | ||
m.t.Fatal("do not have any operations to perform") | ||
} | ||
} | ||
|
||
func (m *Mock) perform() (sc adapters.Scanner) { | ||
m.validate() | ||
m.mtx.Lock() | ||
item := m.Items[0] | ||
sc = &scanner.PrestScanner{ | ||
Error: item.Error, | ||
Buff: bytes.NewBuffer(item.Body), | ||
} | ||
m.Items = m.Items[1:] | ||
m.mtx.Unlock() | ||
return | ||
} | ||
|
||
// TablePermissions mock | ||
func (m *Mock) TablePermissions(table string, op string) (ok bool) { | ||
m.validate() | ||
m.mtx.Lock() | ||
ok = m.Items[0].HasPermission | ||
m.mtx.Unlock() | ||
return | ||
} | ||
|
||
// GetScript mock | ||
func (m *Mock) GetScript(verb string, folder string, scriptName string) (script string, err error) { | ||
return | ||
} | ||
|
||
// ParseScript mock | ||
func (m *Mock) ParseScript(scriptPath string, queryURL url.Values) (sqlQuery string, values []interface{}, err error) { | ||
return | ||
} | ||
|
||
// ExecuteScripts mock | ||
func (m *Mock) ExecuteScripts(method string, sql string, values []interface{}) (sc adapters.Scanner) { | ||
return | ||
} | ||
|
||
// WhereByRequest mock | ||
func (m *Mock) WhereByRequest(r *http.Request, initialPlaceholderID int) (whereSyntax string, values []interface{}, err error) { | ||
return | ||
} | ||
|
||
// DatabaseClause mock | ||
func (m *Mock) DatabaseClause(req *http.Request) (query string, hasCount bool) { | ||
m.validate() | ||
m.mtx.Lock() | ||
hasCount = m.Items[0].IsCount | ||
m.mtx.Unlock() | ||
return | ||
} | ||
|
||
// OrderByRequest mock | ||
func (m *Mock) OrderByRequest(r *http.Request) (values string, err error) { | ||
return | ||
} | ||
|
||
// PaginateIfPossible mock | ||
func (m *Mock) PaginateIfPossible(r *http.Request) (paginatedQuery string, err error) { | ||
return | ||
} | ||
|
||
// Query mock | ||
func (m *Mock) Query(SQL string, params ...interface{}) (sc adapters.Scanner) { | ||
sc = m.perform() | ||
return | ||
} | ||
|
||
// SchemaClause mock | ||
func (m *Mock) SchemaClause(req *http.Request) (query string, hasCount bool) { | ||
m.validate() | ||
m.mtx.Lock() | ||
hasCount = m.Items[0].IsCount | ||
m.mtx.Unlock() | ||
return | ||
} | ||
|
||
// FieldsPermissions mock | ||
func (m *Mock) FieldsPermissions(r *http.Request, table string, op string) (fields []string, err error) { | ||
fields = append(fields, "mock") | ||
return | ||
} | ||
|
||
// SelectFields mock | ||
func (m *Mock) SelectFields(fields []string) (sql string, err error) { | ||
return | ||
} | ||
|
||
// CountByRequest mock | ||
func (m *Mock) CountByRequest(req *http.Request) (countQuery string, err error) { | ||
return | ||
} | ||
|
||
// JoinByRequest mock | ||
func (m *Mock) JoinByRequest(r *http.Request) (values []string, err error) { | ||
return | ||
} | ||
|
||
// GroupByClause mock | ||
func (m *Mock) GroupByClause(r *http.Request) (groupBySQL string) { | ||
return | ||
} | ||
|
||
// QueryCount mock | ||
func (m *Mock) QueryCount(SQL string, params ...interface{}) (sc adapters.Scanner) { | ||
sc = m.perform() | ||
return | ||
} | ||
|
||
// ParseInsertRequest mock | ||
func (m *Mock) ParseInsertRequest(r *http.Request) (colsName string, colsValue string, values []interface{}, err error) { | ||
return | ||
} | ||
|
||
// Insert mock | ||
func (m *Mock) Insert(SQL string, params ...interface{}) (sc adapters.Scanner) { | ||
sc = m.perform() | ||
return | ||
} | ||
|
||
// Delete mock | ||
func (m *Mock) Delete(SQL string, params ...interface{}) (sc adapters.Scanner) { | ||
sc = m.perform() | ||
return | ||
} | ||
|
||
// SetByRequest mock | ||
func (m *Mock) SetByRequest(r *http.Request, initialPlaceholderID int) (setSyntax string, values []interface{}, err error) { | ||
return | ||
} | ||
|
||
// Update mock | ||
func (m *Mock) Update(SQL string, params ...interface{}) (sc adapters.Scanner) { | ||
sc = m.perform() | ||
return | ||
} | ||
|
||
// DistinctClause mock | ||
func (m *Mock) DistinctClause(r *http.Request) (distinctQuery string, err error) { | ||
return | ||
} | ||
|
||
// SetDatabase mock | ||
func (m *Mock) SetDatabase(name string) { | ||
} | ||
|
||
// SelectSQL mock | ||
func (m *Mock) SelectSQL(selectStr string, database string, schema string, table string) (s string) { | ||
return | ||
} | ||
|
||
// InsertSQL mock | ||
func (m *Mock) InsertSQL(database string, schema string, table string, names string, placeholders string) (s string) { | ||
return | ||
} | ||
|
||
// DeleteSQL mock | ||
func (m *Mock) DeleteSQL(database string, schema string, table string) (s string) { | ||
return | ||
} | ||
|
||
// UpdateSQL mock | ||
func (m *Mock) UpdateSQL(database string, schema string, table string, setSyntax string) (s string) { | ||
return | ||
} | ||
|
||
// DatabaseWhere mock | ||
func (m *Mock) DatabaseWhere(requestWhere string) (whereSyntax string) { | ||
return | ||
} | ||
|
||
// DatabaseOrderBy mock | ||
func (m *Mock) DatabaseOrderBy(order string, hasCount bool) (orderBy string) { | ||
return | ||
} | ||
|
||
// SchemaOrderBy mock | ||
func (m *Mock) SchemaOrderBy(order string, hasCount bool) (orderBy string) { | ||
return | ||
} | ||
|
||
// TableClause mock | ||
func (m *Mock) TableClause() (query string) { | ||
return | ||
} | ||
|
||
// TableWhere mock | ||
func (m *Mock) TableWhere(requestWhere string) (whereSyntax string) { | ||
return | ||
} | ||
|
||
// TableOrderBy mock | ||
func (m *Mock) TableOrderBy(order string) (orderBy string) { | ||
return | ||
} | ||
|
||
// SchemaTablesClause mock | ||
func (m *Mock) SchemaTablesClause() (query string) { | ||
return | ||
} | ||
|
||
// SchemaTablesWhere mock | ||
func (m *Mock) SchemaTablesWhere(requestWhere string) (whereSyntax string) { | ||
return | ||
} | ||
|
||
// SchemaTablesOrderBy mock | ||
func (m *Mock) SchemaTablesOrderBy(order string) (orderBy string) { | ||
return | ||
} | ||
|
||
// AddItem on mock object | ||
func (m *Mock) AddItem(body []byte, err error, hasPermission, isCount bool) { | ||
i := Item{ | ||
Body: body, | ||
Error: err, | ||
HasPermission: hasPermission, | ||
IsCount: isCount, | ||
} | ||
m.mtx.Lock() | ||
m.Items = append(m.Items, i) | ||
m.mtx.Unlock() | ||
} |
Oops, something went wrong.