Skip to content

Commit

Permalink
add bulk import / export examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vroldanbet committed Oct 16, 2023
1 parent 853aab3 commit d53bd9d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
74 changes: 74 additions & 0 deletions examples/v1/bulk_import_export_relationships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from authzed.api.v1 import (
BulkExportRelationshipsRequest,
BulkImportRelationshipsRequest,
Client,
Consistency,
ObjectReference,
Relationship,
SubjectReference,
WriteSchemaRequest,
)
from grpcutil import insecure_bearer_token_credentials

emilia = SubjectReference(
object=ObjectReference(
object_type="user",
object_id="emilia",
)
)
beatrice = SubjectReference(
object=ObjectReference(
object_type="user",
object_id="beatrice",
)
)

post_one = ObjectReference(object_type="post", object_id="1")

client = Client(
"localhost:50051",
insecure_bearer_token_credentials("t_your_token_here_1234567deadbeef"),
)

schema = """
definition post {
relation writer: user
relation reader: user
permission write = writer
permission view = reader + writer
}
definition user {}
"""
client.WriteSchema(WriteSchemaRequest(schema=schema))


reqs = [
BulkImportRelationshipsRequest(
relationships=[
Relationship(
resource=post_one,
relation="reader",
subject=emilia,
),
Relationship(
resource=post_one,
relation="writer",
subject=beatrice,
),
]
)
]

import_reps = client.BulkImportRelationships(((req for req in reqs)))
assert import_reps.num_loaded == 2

export_resp = client.BulkExportRelationships(
BulkExportRelationshipsRequest(consistency=Consistency(fully_consistent=True))
)

rels = []
for response in export_resp:
for rel in response.relationships:
rels.append(rel)
assert len(rels) == 2
48 changes: 48 additions & 0 deletions tests/v1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from authzed.api.v1 import (
BulkCheckPermissionRequest,
BulkCheckPermissionRequestItem,
BulkExportRelationshipsRequest,
BulkImportRelationshipsRequest,
CheckPermissionRequest,
CheckPermissionResponse,
Client,
Expand Down Expand Up @@ -219,6 +221,52 @@ def test_bulk_check(client):
)


def test_bulk_export_import(client):
write_test_schema(client)
write_test_tuples(client)

# validate bulk export returns all relationships written
resp = client.BulkExportRelationships(
BulkExportRelationshipsRequest(consistency=Consistency(fully_consistent=True))
)

rels = rels_from_bulk_export_response(resp)
assert len(rels) == 4

# create a new empty client
empty_client = Client("localhost:50051", insecure_bearer_token_credentials(str(uuid.uuid4())))
write_test_schema(empty_client)

# validate indeed empty client is empty
resp = empty_client.BulkExportRelationships(
BulkExportRelationshipsRequest(consistency=Consistency(fully_consistent=True))
)

no_rels = rels_from_bulk_export_response(resp)
assert len(no_rels) == 0

# do bulk import
reqs = [BulkImportRelationshipsRequest(relationships=rels)]
import_rels = empty_client.BulkImportRelationships(((req for req in reqs)))
assert import_rels.num_loaded == 4

# validate all relationships were imported
resp = empty_client.BulkExportRelationships(
BulkExportRelationshipsRequest(consistency=Consistency(fully_consistent=True))
)

rels = rels_from_bulk_export_response(resp)
assert len(rels) == 4


def rels_from_bulk_export_response(resp):
rels = []
for response in resp:
for rel in response.relationships:
rels.append(rel)
return rels


def write_test_tuples(client):
emilia = SubjectReference(
object=ObjectReference(
Expand Down

0 comments on commit d53bd9d

Please sign in to comment.