Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.4.x] Used feature #88

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
cache: true
- name: Install dependencies
run: go get .
- name: Test with Go
run: go test -json ./... 2>&1 | tee -a TestResults-${{ matrix.go-version }}.json
- name: Upload Go test results
Expand Down
16 changes: 15 additions & 1 deletion api/openapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.2
servers:
- url: /api
info:
version: 2.1.0
version: 2.4.0
title: GripMock API Schema
tags:
- name: stubs
Expand Down Expand Up @@ -41,6 +41,20 @@ paths:
$ref: '#/components/schemas/MessageOK'

# stubs
/stubs/used:
get:
tags:
- stubs
summary: Getting a list of used stubs
description: The list is needed to quickly find used stubs
operationId: listUsedStubs
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/StubList'
/stubs/unused:
get:
tags:
Expand Down
10 changes: 10 additions & 0 deletions internal/app/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ func (h *StubsServer) BatchStubsDelete(w http.ResponseWriter, r *http.Request) {
}
}

func (h *StubsServer) ListUsedStubs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(h.stubs.Used())
if err != nil {
h.responseError(err, w)

return
}
}

func (h *StubsServer) ListUnusedStubs(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(h.stubs.Unused())
Expand Down
20 changes: 20 additions & 0 deletions internal/domain/rest/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/pkg/patcher/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestWriterWrapper_OptionUpdate(t *testing.T) {

var result bytes.Buffer

tmp := patcher.NewWriterWrapper(&result, "patcher/v1")
tmp := patcher.NewWriterWrapper(&result, "protogen/patcher/v1")

_, err = io.Copy(tmp, pile)
require.NoError(t, err)
Expand Down Expand Up @@ -50,7 +50,7 @@ message Reply1 {

var result bytes.Buffer

tmp := patcher.NewWriterWrapper(&result, "patcher/v2")
tmp := patcher.NewWriterWrapper(&result, "protogen/patcher/v2")

_, err := io.Copy(tmp, pile)
require.NoError(t, err)
Expand All @@ -77,7 +77,7 @@ message Reply1 {

var result bytes.Buffer

tmp := patcher.NewWriterWrapper(&result, "patcher/v3")
tmp := patcher.NewWriterWrapper(&result, "protogen/patcher/v3")

_, err := io.Copy(tmp, pile)
require.ErrorIs(t, patcher.ErrSyntaxNotFound, err)
Expand Down
102 changes: 102 additions & 0 deletions pkg/sdk/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions pkg/storage/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,37 @@ func (r *StubStorage) ItemsBy(service, method string, ID *uuid.UUID) ([]storage,
return result, nil
}

func (r *StubStorage) Used() []Stub {
txn := r.db.Txn(false)
defer txn.Abort()

iter, err := txn.Get(TableName, IDField)
if err != nil {
return nil
}

it := memdb.NewFilterIterator(iter, func(raw interface{}) bool {
obj, ok := raw.(*Stub)
if !ok {
return true
}

_, ok = r.used[obj.GetID()]

return !ok
})

result := make([]Stub, 0, atomic.LoadInt64(&r.total))

for obj := it.Next(); obj != nil; obj = it.Next() {
stub := obj.(*Stub)

result = append(result, *stub)
}

return result
}

func (r *StubStorage) Unused() []Stub {
txn := r.db.Txn(false)
defer txn.Abort()
Expand Down
16 changes: 16 additions & 0 deletions stub/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func TestStub(t *testing.T) {
handler: api.ListUnusedStubs,
expect: "[{\"id\":\"43739ed8-2810-4f57-889b-4d3ff5795bce\",\"service\":\"Testing\",\"method\":\"TestMethod\",\"headers\":{\"equals\":null,\"contains\":null,\"matches\":null},\"input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\",\"headers\":null}}]",
},
{
name: "used stubs (zero)",
mock: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/api/stubs/used", nil)
},
handler: api.ListUsedStubs,
expect: "[]",
},
{
name: "find stub equals",
mock: func() *http.Request {
Expand All @@ -83,6 +91,14 @@ func TestStub(t *testing.T) {
handler: api.ListUnusedStubs,
expect: "[]",
},
{
name: "used stubs (all stubs)",
mock: func() *http.Request {
return httptest.NewRequest(http.MethodGet, "/api/stubs/used", nil)
},
handler: api.ListUsedStubs,
expect: "[{\"id\":\"43739ed8-2810-4f57-889b-4d3ff5795bce\",\"service\":\"Testing\",\"method\":\"TestMethod\",\"headers\":{\"equals\":null,\"contains\":null,\"matches\":null},\"input\":{\"equals\":{\"Hola\":\"Mundo\"},\"contains\":null,\"matches\":null},\"output\":{\"data\":{\"Hello\":\"World\"},\"error\":\"\",\"headers\":null}}]",
},
{
name: "find stub by ID",
mock: func() *http.Request {
Expand Down