Skip to content

Commit

Permalink
add used method
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Dec 13, 2023
1 parent b1c3343 commit bd597c4
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 5 deletions.
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
14 changes: 14 additions & 0 deletions api/openapi/api.yaml
Original file line number Diff line number Diff line change
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

0 comments on commit bd597c4

Please sign in to comment.