def _delete_data(): resources = [q.functions(), q.indexes(), q.collections()] delete = lambda res: q.foreach( q.lambda_("res", q.delete(q.var("res"))), q.paginate(res) ) delete_queries = [delete(res) for res in resources] _execute_with_retries(q.do(*delete_queries))
def test_create_role(self): self.admin_client.query(query.create_role({ "name": "a_role", "privileges": { "resource": query.collections(), "actions": {"read": True} } })) self.assertTrue(self.admin_client.query(query.exists(query.role("a_role"))))
def test_nested_references(self): client1 = self.create_new_database(self.admin_client, "parent-database") client2 = self.create_new_database(client1, "child-database") client2.query(query.create_collection({"name": "a_collection"})) client2.query( query.create_role({ "name": "a_role", "privileges": { "resource": query.collections(), "actions": { "read": True } } })) nested_database_ref = query.database("child-database", query.database("parent-database")) nested_collection_ref = query.collection("a_collection", nested_database_ref) nested_role_ref = query.role("a_role", nested_database_ref) self.assertEqual( self.admin_client.query(query.exists(nested_collection_ref)), True) self.assertEqual( self.admin_client.query(query.exists(nested_role_ref)), True) parent_db_ref = Ref("parent-database", Native.DATABASES) child_db_ref = Ref("child-database", Native.DATABASES, parent_db_ref) self.assertEqual( self.admin_client.query( query.paginate( query.collections(nested_database_ref)))["data"], [Ref("a_collection", Native.COLLECTIONS, child_db_ref)]) self.assertEqual( self.admin_client.query( query.paginate(query.roles(nested_database_ref)))["data"], [Ref("a_role", Native.ROLES, child_db_ref)])
def test_create_role(self): self.assertJson( query.create_role({ "name": "a_role", "privileges": { "resource": query.collections(), "actions": { "read": True } } }), '{"create_role":{"object":{"name":"a_role","privileges":{"object":{"actions":{"object":{"read":true}},"resource":{"collections":null}}}}}}' )
from faunadb import query as q from faunadb.objects import Ref from faunadb.client import FaunaClient from dotenv import dotenv_values from munch import AutoMunch from pprint import pprint cfg = AutoMunch(dotenv_values()) # pprint(cfg) # exit() fdb = FaunaClient(secret=cfg.FAUNASECRET) if __name__ == "__main__": indexes = fdb.query(q.paginate(q.collections())) pprint(indexes)
#client.query(q.get(q.ref(q.keys(), "243725029947212288"))) client.query(q.get(q.ref(q.keys(), ref["ref"].id()))) ### Delete a key #client.query(q.delete(q.ref(q.keys(), "243725029947212288"))) client.query(q.delete(q.ref(q.keys(), ref["ref"].id()))) client.query(q.delete(q.database("caledonia"))) ## Collections ### Create a collection client.query(q.create_collection({"name": "spells"})) ### Paginate all collections client.query(q.paginate(q.collections())) ### Get a collection client.query(q.get(q.collection("spells"))) ### Rename a collection client.query(q.update(q.collection("spells"), {"name": "dilapidated_huts"})) ### Delete a collection client.query(q.delete(q.collection("dilapidated_huts"))) ### Create a document in a collection client.query(q.create_collection({"name": "spells"})) client.query( q.create(q.collection("spells"), {"data": {
def test_typecheckfns(self): coll = query.collection("typecheck_coll") db = query.database("typecheck_db") fn = query.function("typecheck_fn") index = query.index("typecheck_index") self.admin_client.query(query.create_collection({"name": "typecheck_coll"})) self.admin_client.query(query.create_index( {"name": "typecheck_index", "source": coll, "active": True})) doc = self.admin_client.query(query.create( coll, {"data": {}, "credentials": {"password": "******"}})) self.admin_client.query(query.create_database({"name": "typecheck_db"})) function = self._q(query.create_function( {"name": "typecheck_fn", "body": query.query(query.lambda_("x", query.now()))})) key = self.admin_client.query( query.create_key({"database": db, "role": "admin"})) token = self._q(query.login(doc["ref"], {"password": "******"})) credentials = self._q(query.select(['data', 0], query.paginate(query.credentials()))) role = self.admin_client.query(query.create_role( {"name": "typecheck_role", "membership": [], "privileges": []})) values = [ None, bytearray([12,3,4,5]), credentials, 90, 3.14, True, query.to_date(query.now()), query.date("1970-01-01"), query.now(), query.epoch(1, "second"), query.time("1970-01-01T00:00:00Z"), {"x": 10}, query.get(doc["ref"]), query.paginate(query.collections()), [1, 2, 3], "a string", coll, query.collections(), query.match(index), query.union(query.match(index)), doc["ref"], query.get(doc["ref"]), index, db, coll, token["ref"], role["ref"], key["ref"], function["ref"], query.get(function["ref"]), query.query(query.lambda_("x", query.var("x"))), ] pairs = [ ["array", query.is_array], ["object", query.is_object], ["string", query.is_string], ["null", query.is_null], ["number", query.is_number], ["bytes", query.is_bytes], ["date", query.is_date], ["timestamp", query.is_timestamp], ["set", query.is_set], ["ref", query.is_ref], ["boolean", query.is_boolean], ["double", query.is_double], ["integer", query.is_integer], ["database", query.is_database], ["index", query.is_index], ["collection", query.is_collection], ["token", query.is_token], ["function", query.is_function], ["collection", query.is_collection], ["role", query.is_role], ["credentials", query.is_credentials], ["key", query.is_key], ] expected = { "array": 1, "boolean": 1, "bytes": 1, "collection": 3, "credentials": 1, "database": 1, "date": 2, "double": 1, "function": 2, "integer": 1, "index": 1, "key": 1, "null": 1, "number": 2, "object": 5, "ref": 11, "role": 1, "set": 3, "string": 1, "timestamp": 3, "token": 1, } q = [] for p in pairs: d = dict() d[p[0]] = query.count(query.filter_(query.lambda_("v", p[1](query.var("v"))), query.var("vals"))) q.append(d) actual = self._q(query.let({"vals": values}, query.merge({}, q))) self.assertEqual(actual, expected)