all repos — litestore @ e76a8b14b77cbb30034cf3d4a54f969a6c3d32d1

A minimalist nosql document store.

test/http_api.nim

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
import unittest, json, httpclient, strutils, os

suite "LiteStore HTTP API":

  var contents = newSeq[JsonNode](0)
  for i in 1..8:
    contents.add parseFile("data/$1.json" % i.intToStr)
  var rpost: Response;
  var ids = newSeq[string](0)

  const srv = "http://localhost:9500/"
  let cli = newHttpClient()
  cli.headers = newHttpHeaders({ "Content-Type": "application/json" })

  proc jget(url: string): Response =
    return cli.request(srv & url, HttpGet)

  proc jhead(url: string): Response =
    return cli.request(srv & url, HttpHead)

  proc jpost(url: string, body: JsonNode): Response {.discardable.} =
    return cli.request(srv & url, HttpPost, $body)

  proc jput(url: string, body: JsonNode): Response {.discardable.} =
    return cli.request(srv & url, HttpPut, $body)

  proc jpatch(url: string, body: JsonNode): Response =
    return cli.request(srv & url, HttpPatch, $body)

  proc jdelete(url: string): Response {.discardable.} =
    return cli.request(srv & url, HttpDelete)

  proc info(prop: string): JsonNode =
    return jget("info").body.parseJson[prop]

  proc total(resp: Response): BiggestInt =
    return resp.body.parseJson["total"].getNum

  setup:
    var count = 0
    for c in contents:
      rpost = jpost("docs/test/", c)
      var id = rpost.body.parseJson["id"].getStr
      ids.add(id)
      var ops = """
      [
        {"op": "add", "path": "/tags/3", "value": "tag1$1"},
        {"op": "add", "path": "/tags/4", "value": "tag$2"},
      ]
      """ % [$count, $(count mod 2)]
      discard jpatch("docs/" & ids[count], ops.parseJson)
      count += 1

  teardown:
    for i in ids:
      jdelete("docs/$1" % i)
    ids = newSeq[string](0)


  test "GET info":
    check(info("datastore_version") == %1)

  test "POST document":
    var rpost = jpost("docs", contents[0])
    var id = rpost.body.parseJson["id"].getStr
    check(rpost.body.parseJson["data"]["_id"] == %"5a6c566020d0d4ba242d6501")
    var rget = jget("docs/$1" % id)
    check(rget.body.parseJson["_id"] == %"5a6c566020d0d4ba242d6501")
    var rdel = jdelete("docs/$1" % id)
    check(rdel.status == "204 No Content")
    check(info("total_documents") == %8)
    rpost = jpost("docs/f1/f2/", contents[0])
    id = rpost.body.parseJson["id"].getStr
    check(id.startsWith("f1/f2/"))
    check(rpost.body.parseJson["data"]["_id"] == %"5a6c566020d0d4ba242d6501")
    rget = jget("docs/$1" % id)
    check(rget.body.parseJson["_id"] == %"5a6c566020d0d4ba242d6501")
    check(info("total_documents") == %9)
    rdel = jdelete("docs/$1" % id)
    check(rdel.status == "204 No Content")
    check(info("total_documents") == %8)

  test "DELETE document":
    for i in ids:
      jdelete("docs/$1" % i)
    ids = newSeq[string](0)

  test "PUT document":
    var rput = jput("docs/" & ids[5], contents[0])
    var id = rput.body.parseJson["id"].getStr
    check(id == ids[5])
    var rget = jget("docs/" & ids[5])
    check(rget.body.parseJson["_id"] == %"5a6c566020d0d4ba242d6501")

  test "PATCH document tags":
    var rget = jget("docs?tags=t1")
    check(rget.status == "404 Not Found")
    var ops = """
    [
      {"op": "add", "path": "/tags/3", "value": "t1"},
      {"op": "add", "path": "/tags/4", "value": "t2"},
      {"op": "add", "path": "/tags/5", "value": "t3"}
    ]
    """
    var rpatch = jpatch("docs/" & ids[0], ops.parseJson)
    check(rpatch.status == "200 OK")
    rget = jget("docs/?tags=t1")
    check(rget.body.parseJson["total"] == %1)
    ops = """
    [
      {"op": "add", "path": "/tags/3", "value": "t1"},
      {"op": "add", "path": "/tags/4", "value": "t3"}
    ]
    """
    rpatch = jpatch("docs/" & ids[1], ops.parseJson)
    check(rpatch.status == "200 OK")
    ops = """
    [
      {"op": "add", "path": "/tags/3", "value": "t2"},
      {"op": "add", "path": "/tags/4", "value": "t3"}
    ]
    """
    rpatch = jpatch("docs/" & ids[2], ops.parseJson)
    check(rpatch.status == "200 OK")
    ops = """
    [
      {"op": "replace", "path": "/tags/3", "value": "t4"},
      {"op": "remove", "path": "/tags/4"}
    ]
    """
    rpatch = jpatch("docs/" & ids[0], ops.parseJson)
    check(rpatch.status == "200 OK")
    rget = jget("docs/?tags=t2,t3")
    check(rget.body.parseJson["total"] == %1)
    check(info("total_documents") == %8)

  test "HEAD documents":
    var rhead = jhead("docs/invalid/")
    check(rhead.status == "404 Not Found")
    rhead = jhead("docs/test/")
    check(rhead.status == "200 OK")

  test "GET documents by tags":
    var rget = jget("docs/?tags=tag1")
    check(total(rget) == 4)
    rget = jget("docs/?tags=tag10,tag0")
    check(total(rget) == 1)
    rget = jget("docs/?tags=$type:application,$subtype:json")
    check(total(rget) == 8)

  test "GET documents by search":
    var rget = jget("docs/?search=Lorem&contents=false")
    check(rget.body.parseJson["total"] == %5)

  test "GET documents by filter":
    var rget = jget("docs/?filter=$.age%20gte%2034%20and%20$.age%20lte%2036%20and%20$.tags%20contains%20\"labore\"")
    check(rget.body.parseJson["total"] == %1)
    rget = jget("docs/?filter=$.age%20eq%2034%20or%20$.age%20eq%2036%20or%20$.eyeColor%20eq%20\"brown\"")
    check(rget.body.parseJson["total"] == %5)
    rget = jget("docs/?filter=$.name.first%20eq%20\"Jensen\"")
    check(rget.body.parseJson["total"] == %1)

  test "GET documents selecting fields":
    var rget = jget("docs/?select=$.age%20as%20age,$.email%20as%20email")
    var json = rget.body.parseJson
    var testdata = %*{
      "age": 36,
      "email": "lawson.logan@trasola.co.uk"
    }
    check(json["total"] == %8)
    check(json["results"][3]["data"] == testdata)
    rget = jget("docs/" & ids[2] & "?select=$.age%20as%20age&raw=true")
    json = rget.body.parseJson
    testdata = %*{
      "age": 31
    }
    check(json["data"] == testdata)