def create_database(self, name: str, database: Type['FaunaDatabase'], key_type: str) -> Secret: self.client.query(q.create_database({'name': name})) key = self.client.query( q.create_key({ 'database': q.database(name), 'role': key_type })) secret = Secret(key['secret']) database_instance = database(secret, self.client_factory) for class_ in database.classes(): log.info(f'creating class {class_}') database_instance._create_class(class_) for name, index in database.indices().items(): log.info(f'creating index {name}') database_instance.client.query( q.create_index({ 'name': name, 'source': q.class_(index.source.name()), 'terms': [{ 'field': ['data', field] } for field in index.fields], 'values': [{ 'field': ["data", value] } for value in index.values] + [{ 'field': ['ref'] }], 'unique': index.unique })) return Secret(secret)
def test_create_key(self): self.assertJson( query.create_key({ "database": query.database("db-name"), "role": "client" }), '{"create_key":{"object":{"database":{"database":"db-name"},"role":"client"}}}' )
def create_new_database(self, client, name): client.query(query.create_database({"name": name})) key = client.query( query.create_key({ "database": query.database(name), "role": "admin" })) return client.new_session_client(secret=key["secret"])
def test_key_from_secret(self): db_ref = self.admin_client.query( query.create_database({"name": "database_for_key_from_secret_test"}))["ref"] key = self.admin_client.query(query.create_key({ "database": db_ref, "role": "server"})) self.assertEqual( self.admin_client.query(query.key_from_secret(key["secret"]))["ref"], key["ref"] )
def test_permission_denied(self): # Create client with client key client = self.root_client.new_session_client( secret=self.root_client.query( create_key({"database": self.db_ref, "role": "client"}))["secret"] ) exception = self.assert_raises( PermissionDenied, lambda: client.query(query.paginate(Native.DATABASES))) self._assert_error(exception, "permission denied", ["paginate"])
def test_create_key(self): self.admin_client.query(query.create_database({"name": "database_for_key_test"})) resource = self.admin_client.query(query.create_key({ "database": query.database("database_for_key_test"), "role": "server"})) new_client = self.admin_client.new_session_client(secret=resource["secret"]) new_client.query(query.create_collection({"name": "collection_for_test"})) self.assertTrue(new_client.query(query.exists(query.collection("collection_for_test"))))
def test_nested_keys(self): client = self.create_new_database(self.admin_client, "db-for-keys") client.query(query.create_database({"name": "db-test"})) server_key = client.query(query.create_key({ "database": query.database("db-test"), "role": "server" })) admin_key = client.query(query.create_key({ "database": query.database("db-test"), "role": "admin" })) server_key_ref = Ref(server_key["ref"].id(), cls=Ref("keys", db=Ref("db-for-keys", cls=Native.DATABASES))) admin_key_ref = Ref(admin_key["ref"].id(), cls=Ref( "keys", db=Ref("db-for-keys", cls=Native.DATABASES))) self.assertEqual( self.admin_client.query(query.paginate(query.keys(query.database("db-for-keys"))))["data"], [server_key_ref, admin_key_ref] )
def test_queries_are_made(): # The client mock is used by both the # database and annotation database # so here we assert calls for both # could maybe be improved by changing the way databases are created? create_annotation_database_queries = [ q.create_database({'name': '1234-annotations'}), q.create_key( {'database': q.database('1234-annotations'), 'role': 'server'} ) ] mock_dependencies.database.client.query.assert_has_calls( [call(query) for query in create_annotation_database_queries] ) setup_annotation_database_queries = [ q.create_class({'name': 'SpanLabel'}), q.create_class({'name': 'DocumentLabel'}), q.create_class({'name': 'RelationLabel'}), q.create_class({'name': 'Relation'}), q.create_class({'name': 'Span'}), q.create_class({'name': 'Document'}), q.create_index( {'name': 'documents', 'source': q.class_('Document')}), q.create_index( {'name': 'span_labels', 'source': q.class_('SpanLabel')} ), q.create_index( {'name': 'relation_labels', 'source': q.class_('RelationLabel')} ), q.create_index( {'name': 'document_labels', 'source': q.class_('DocumentLabel')} ) ] mock_dependencies.database.client.query.assert_has_calls( [call(query) for query in setup_annotation_database_queries] ) event = event_reader('post_confirmation_event.json') handle(event, {}, dependencies=mock_dependencies) assert event == event test_buckets_are_created() test_queries_are_made() test_user_attributes_are_updated()
def __init__(self, database="Balthus", collection="domains"): self.database = database self.collection = collection FAUNA_SECRET = os.getenv("FAUNA_SECRET") self.client = FaunaClient(secret=FAUNA_SECRET) try: self.client.query(q.create_database({"name": database})) except: print("Database: {} already exist".format(database)) ref = self.client.query( q.create_key({ "database": q.database(database), "role": "server" })) self.client = FaunaClient(secret=ref['secret']) try: self.client.query(q.create_collection({"name": collection})) except: print("Collection: {} already exist".format(collection))
def create_database(scheme, domain, port, secret, db_name): # # Create an admin client. This is the client we will use to create the database. # # If you are using the the FaunaDB-Cloud you will need to replace the value of the # 'secret' in the command below with your "secret". # adminClient = FaunaClient(secret=secret, domain=domain, scheme=scheme, port=port) print("Connected to FaunaDB as admin!") # # The code below creates the Database that will be used for this example. Please note that # the existence of the database is evaluated, deleted if it exists and recreated with a single # call to the Fauna DB. # res = adminClient.query( q.if_(q.exists(q.database(db_name)), [ q.delete(q.database(db_name)), q.create_database({"name": db_name}) ], q.create_database({"name": db_name}))) print('DB {0} created: {1}'.format(db_name, res)) # # Create a key specific to the database we just created. We will use this to # create a new client we will use in the remainder of the examples. # res = adminClient.query( q.select(["secret"], q.create_key({ "database": q.database(db_name), "role": "server" }))) print('DB {0} secret: {1}'.format(db_name, res)) return res
### Rename a database client.query(q.update(q.database("annuvin"), {"name": "llyr"})) ### Annotate a database client.query(q.update(q.database("llyr"), {"data": {"env": "test"}})) ### Delete a database client.query(q.delete(q.database("llyr"))) ## Keys client.query(q.create_database({"name": "caledonia"})) ### Create a key ref = client.query( q.create_key({ "database": q.database("caledonia"), "role": "server" })) ### Paginate all keys client.query(q.paginate(q.keys())) ### Get a key #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")))
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)
from faunadb.client import FaunaClient client = FaunaClient(secret="secret", domain="localhost", scheme="http", port="8443") # create db client.query(q.create_database({"name": "my_app"})) # Accessing the database # Create an initial server key by using an admin key. The server key has unrestricted access to a single database; # in this case, the server key will only access the blog post database we just created. client.query(q.create_key({ "database": q.database("my_app"), "role": "server" })) # Set up a collection # Create a collection using the CreateCollection function with a param_object containing the name of the collection. # We shall name our collection "posts": client.query(q.create_collection({"name": "posts"})) # Create an index # The customary way to access documents within a collection is by specifying a criteria for one of the fields. # To enable criteria-based searches, we need to first create an index using the path of the field within the document. client.query( q.create_index({ "name": "posts_by_title", "source": q.collection("posts"),