Esempio n. 1
0
def get_foreign_key_ref(
    foreign_value: QueryExpression,
    reference_collection_name: QueryExpression,
) -> QueryExpression:
    """Get the Ref to a document associated with a foreign key value.

    Params:
    -------
    foreign_value: The value to look up, usually an ID.
    references: Field metadata dict that defines the collection (key) and field name (value)
        that the foreign key refers to.

    Returns:
    --------
    Fauna query expression that returns an array of Refs for the associated document(s).
    """
    return q.let(
        {
            "is_blank_reference":
            q.or_(
                q.is_null(foreign_value),
                q.equals(foreign_value, NULL),
                q.equals(reference_collection_name, NULL),
            ),
        },
        q.if_(
            q.var("is_blank_reference"),
            None,
            q.ref(q.collection(reference_collection_name), foreign_value),
        ),
    )
 def test_or_expr(self):
     self.assertJson(query.or_(False), '{"or":false}')
     self.assertJson(query.or_(False, True, True),
                     '{"or":[false,true,true]}')
     self.assertJson(query.or_([False, True, True]),
                     '{"or":[false,true,true]}')
Esempio n. 3
0
 def test_or(self):
     self.assertTrue(self._q(query.or_(False, False, True)))
     self.assertFalse(self._q(query.or_(False, False, False)))
     self.assertTrue(self._q(query.or_(True)))
     self.assertFalse(self._q(query.or_(False)))
     self._assert_bad_query(query.or_())