Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
orangecat
ollama
Commits
0174665d
"examples/vscode:/vscode.git/clone" did not exist on "7436e30c720547c97b602155b8f5690976efdbc4"
Unverified
Commit
0174665d
authored
Dec 14, 2023
by
Patrick Devine
Committed by
GitHub
Dec 14, 2023
Browse files
add API tests for list handler (#1535)
parent
630518f0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
server/routes_test.go
server/routes_test.go
+61
-0
No files found.
server/routes_test.go
View file @
0174665d
...
@@ -2,12 +2,19 @@ package server
...
@@ -2,12 +2,19 @@ package server
import
(
import
(
"context"
"context"
"encoding/json"
"fmt"
"io"
"io"
"net/http"
"net/http"
"net/http/httptest"
"net/http/httptest"
"os"
"strings"
"testing"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/parser"
)
)
func
setupServer
(
t
*
testing
.
T
)
(
*
Server
,
error
)
{
func
setupServer
(
t
*
testing
.
T
)
(
*
Server
,
error
)
{
...
@@ -40,6 +47,55 @@ func Test_Routes(t *testing.T) {
...
@@ -40,6 +47,55 @@ func Test_Routes(t *testing.T) {
assert
.
Equal
(
t
,
`{"version":"0.0.0"}`
,
string
(
body
))
assert
.
Equal
(
t
,
`{"version":"0.0.0"}`
,
string
(
body
))
},
},
},
},
{
Name
:
"Tags Handler (no tags)"
,
Method
:
http
.
MethodGet
,
Path
:
"/api/tags"
,
Expected
:
func
(
t
*
testing
.
T
,
resp
*
http
.
Response
)
{
contentType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
assert
.
Equal
(
t
,
contentType
,
"application/json; charset=utf-8"
)
body
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
assert
.
Nil
(
t
,
err
)
var
modelList
api
.
ListResponse
err
=
json
.
Unmarshal
(
body
,
&
modelList
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
0
,
len
(
modelList
.
Models
))
},
},
{
Name
:
"Tags Handler (yes tags)"
,
Method
:
http
.
MethodGet
,
Path
:
"/api/tags"
,
Setup
:
func
(
t
*
testing
.
T
,
req
*
http
.
Request
)
{
f
,
err
:=
os
.
CreateTemp
(
""
,
"ollama-modelfile"
)
assert
.
Nil
(
t
,
err
)
defer
os
.
RemoveAll
(
f
.
Name
())
modelfile
:=
strings
.
NewReader
(
fmt
.
Sprintf
(
"FROM %s"
,
f
.
Name
()))
commands
,
err
:=
parser
.
Parse
(
modelfile
)
assert
.
Nil
(
t
,
err
)
fn
:=
func
(
resp
api
.
ProgressResponse
)
{}
err
=
CreateModel
(
context
.
TODO
(),
"test"
,
""
,
commands
,
fn
)
assert
.
Nil
(
t
,
err
)
},
Expected
:
func
(
t
*
testing
.
T
,
resp
*
http
.
Response
)
{
contentType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
assert
.
Equal
(
t
,
contentType
,
"application/json; charset=utf-8"
)
body
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
assert
.
Nil
(
t
,
err
)
var
modelList
api
.
ListResponse
err
=
json
.
Unmarshal
(
body
,
&
modelList
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
1
,
len
(
modelList
.
Models
))
assert
.
Equal
(
t
,
modelList
.
Models
[
0
]
.
Name
,
"test:latest"
)
},
},
}
}
s
,
err
:=
setupServer
(
t
)
s
,
err
:=
setupServer
(
t
)
...
@@ -50,6 +106,11 @@ func Test_Routes(t *testing.T) {
...
@@ -50,6 +106,11 @@ func Test_Routes(t *testing.T) {
httpSrv
:=
httptest
.
NewServer
(
router
)
httpSrv
:=
httptest
.
NewServer
(
router
)
t
.
Cleanup
(
httpSrv
.
Close
)
t
.
Cleanup
(
httpSrv
.
Close
)
workDir
,
err
:=
os
.
MkdirTemp
(
""
,
"ollama-test"
)
assert
.
Nil
(
t
,
err
)
defer
os
.
RemoveAll
(
workDir
)
os
.
Setenv
(
"OLLAMA_MODELS"
,
workDir
)
for
_
,
tc
:=
range
testCases
{
for
_
,
tc
:=
range
testCases
{
u
:=
httpSrv
.
URL
+
tc
.
Path
u
:=
httpSrv
.
URL
+
tc
.
Path
req
,
err
:=
http
.
NewRequestWithContext
(
context
.
TODO
(),
tc
.
Method
,
u
,
nil
)
req
,
err
:=
http
.
NewRequestWithContext
(
context
.
TODO
(),
tc
.
Method
,
u
,
nil
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment