Ejemplo n.º 1
0
    def test_paginate(self):
        n_value = 200

        refs = [
            self._create(n=n_value)["ref"],
            self._create(n=n_value)["ref"],
            self._create(n=n_value)["ref"]
        ]

        test_set = query.match(self.n_index_ref, n_value)
        self.assertEqual(self._q(query.paginate(test_set)), {"data": refs})

        data = []
        page1 = self._q(query.paginate(test_set, size=1))
        data.extend(page1["data"])
        page2 = self._q(query.paginate(test_set, size=1, after=page1["after"]))
        data.extend(page2["data"])
        self.assertEqual(data, [refs[0], refs[1]])

        self.assertEqual(
            self._q(query.paginate(test_set, sources=True)), {
                "data": [{
                    "sources": [SetRef(test_set)],
                    "value": refs[0]
                }, {
                    "sources": [SetRef(test_set)],
                    "value": refs[1]
                }, {
                    "sources": [SetRef(test_set)],
                    "value": refs[2]
                }]
            })
Ejemplo n.º 2
0
    def test_set(self):
        index = Ref("frogs_by_size", Native.INDEXES)
        json_index = '{"@ref":{"collection":{"@ref":{"id":"indexes"}},"id":"frogs_by_size"}}'
        match = SetRef(query.match(index, self.ref))
        json_match = '{"@set":{"match":%s,"terms":%s}}' % (json_index,
                                                           self.json_ref)
        self.assertJson(match, json_match)

        self.assertNotEqual(
            match,
            SetRef(
                query.match(index, query.ref(query.collection("frogs"),
                                             "456"))))
Ejemplo n.º 3
0
def _parse_json_hook(dct):
    #pylint: disable=too-many-return-statements
    """
  Looks for FaunaDB types in a JSON object and converts to them if possible.
  """
    if "@ref" in dct:
        ref = dct["@ref"]

        if (not "collection" in ref) and (not "database" in ref):
            return Native.from_name(ref["id"])

        return Ref(ref["id"], ref.get("collection"), ref.get("database"))
    if "@obj" in dct:
        return dct["@obj"]
    if "@set" in dct:
        return SetRef(dct["@set"])
    if "@query" in dct:
        return Query(dct["@query"])
    if "@ts" in dct:
        return FaunaTime(dct["@ts"])
    if "@date" in dct:
        return parse_date(dct["@date"]).date()
    if "@bytes" in dct:
        return bytearray(urlsafe_b64decode(dct["@bytes"].encode()))
    return dct
Ejemplo n.º 4
0
    def test_complex_object(self):
        self.maxDiff = None
        json = """{
      "ref": {"@ref":{"collection":{"@ref":{"id":"collections"}},"id":"widget"}},
      "set_ref": {"@set":{"match":{"@ref":{"collection":{"@ref":{"id":"collections"}},"id":"widget"}},"terms":"Laptop"}},
      "date": {"@date":"1970-01-01"},
      "time": {"@ts":"1970-01-01T00:00:00.123456789Z"},
      "object": {"@obj":{"key":"value"}},
      "array": [1, 2],
      "string": "a string",
      "number": 1
    }"""

        self.assertJson(
            json, {
                "ref":
                Ref("widget", Native.COLLECTIONS),
                "set_ref":
                SetRef({
                    "match": Ref("widget", Native.COLLECTIONS),
                    "terms": "Laptop"
                }),
                "date":
                parse_date("1970-01-01").date(),
                "time":
                FaunaTime("1970-01-01T00:00:00.123456789Z"),
                "object": {
                    "key": "value"
                },
                "array": [1, 2],
                "string":
                "a string",
                "number":
                1
            })
Ejemplo n.º 5
0
 def test_repr(self):
     self.assertRegexCompat(repr(query.var("x")),
                            r"Expr\({u?'var': u?'x'}\)")
     self.assertRegexCompat(repr(Ref("collections")),
                            r"Ref\(id=collections\)")
     self.assertRegexCompat(
         repr(SetRef(query.match(query.index("widgets")))),
         r"SetRef\({u?'match': Expr\({u?'index': u?'widgets'}\)}\)")
 def test_set_ref(self):
   self.assertJson(SetRef({"match": Ref("widgets", Native.INDEXES), "terms": "Laptop"}),
                   ('{'
                    '"@set":{'
                    '"match":{"@ref":{"collection":{"@ref":{"id":"indexes"}},"id":"widgets"}},'
                    '"terms":"Laptop"'
                    '}'
                    '}'))
Ejemplo n.º 7
0
 def test_set_ref(self):
     self.assertJson(
         """{
                   "@set":{
                     "match":{"@ref":{"id":"widgets","collection":{"@ref":{"id":"collections"}}}},
                     "terms":"Laptop"
                   }
                 }""",
         SetRef({
             "match": Ref("widgets", Native.COLLECTIONS),
             "terms": "Laptop"
         }))
 def test_distinct(self):
     self.assertJson(
         query.distinct(SetRef({"match": query.index("widget")})),
         '{"distinct":{"@set":{"match":{"index":"widget"}}}}')