Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated smart defaults #30

Merged
merged 5 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions codegen/endpoint.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

:return Response
"""
% if params['smart_db_branch_name'] :
db_branch_name = self.client.get_db_branch_name(db_name, branch_name)
% endif
% if params['has_path_params'] :
url_path = f"${path}"
% else :
Expand Down
41 changes: 38 additions & 3 deletions codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"string": "str",
}
RESERVED_WORDS = ["from"]
REF_DB_BRANCH_NAME_PARAM = "#/components/parameters/DBBranchNameParam"


def fetch_openapi_specs(spec_url: str) -> dict:
Expand Down Expand Up @@ -174,19 +175,53 @@ def get_endpoint_params(
"has_query_params": 0,
"has_payload": False,
"has_optional_params": 0,
"smart_db_branch_name": False,
}
if len(parameters) > 0:
# Check for convience param swaps
curatedParamList = []
for r in parameters:
# if not in ref -> endpoint specific params
# else if name not in r -> method specific params
# else fail with code: 11
if "$ref" in r and r["$ref"] == REF_DB_BRANCH_NAME_PARAM:
logging.info(
">> adding smart value for %s"
% "#/components/parameters/DBBranchNameParam"
)
# push two new params to cover for string creation
curatedParamList.append(
{
"name": "db_name",
"in": "path",
"schema": {"type": "string"},
"type": "str",
"description": "The name of the database to query. Default: database name from the client.",
"required": False,
}
)
curatedParamList.append(
{
"name": "branch_name",
"in": "path",
"schema": {"type": "string"},
"type": "str",
"description": "The name of the branch to query. Default: branch name from the client.",
"required": False,
}
)
skel["smart_db_branch_name"] = True
else:
curatedParamList.append(r)

for r in curatedParamList:
p = None
# if not in ref -> endpoint specific params
if "$ref" in r and r["$ref"] in references:
p = references[r["$ref"]]
p["type"] = type_replacement(references[p["schema"]["$ref"]]["type"])
# else if name not in r -> method specific params
elif "name" in r:
p = r
p["type"] = type_replacement(r["schema"]["type"])
# else fail with code: 11
else:
logging.error("could resolve reference %s in the lookup." % r["$ref"])
exit(11)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xata"
version = "0.3.0"
version = "0.4.0"
description = "Python client for Xata.io"
authors = ["Xata <[email protected]>"]
license = "Apache-2.0"
Expand Down
49 changes: 27 additions & 22 deletions tests/integration-tests/branch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#

import utils
import pytest

from xata.client import XataClient


class TestClass(object):
@classmethod

def setup_class(self):
self.db_name = utils.get_db_name()
self.branch_name = "main"
Expand All @@ -41,12 +42,11 @@ def setup_class(self):
assert r.status_code == 201

# create table posts
r = self.client.table().createTable(self.client.get_db_branch_name(), "Posts")
r = self.client.table().createTable("Posts")
assert r.status_code == 201

# create schema
r = self.client.table().setTableSchema(
self.client.get_db_branch_name(),
"Posts",
{
"columns": [
Expand All @@ -59,7 +59,6 @@ def setup_class(self):
)
assert r.status_code == 200

@classmethod
def teardown_class(self):
r = self.client.databases().deleteDatabase(
self.client.get_config()["workspaceId"], self.db_name
Expand All @@ -81,7 +80,7 @@ def test_get_branch_list(self):
assert r.status_code == 404

def test_get_branch_details(self):
r = self.client.branch().getBranchDetails(self.client.get_db_branch_name())
r = self.client.branch().getBranchDetails()
assert r.status_code == 200
assert "databaseName" in r.json()
assert "branchName" in r.json()
Expand All @@ -91,7 +90,7 @@ def test_get_branch_details(self):
# TODO be exhastive testing the ^ dict keys

r = self.client.branch().getBranchDetails("NonExistingDatabase")
assert r.status_code == 400
assert r.status_code == 404

def test_create_database_branch(self):
payload = {
Expand All @@ -102,46 +101,52 @@ def test_create_database_branch(self):
"stage": "testing",
},
}
"""
r = self.client.branch().createBranch(self.client.get_db_branch_name(), payload)
assert r.json() == ""
r = self.client.branch().createBranch(
payload, branch_name="new-super-duper-feature"
)
assert r.status_code == 201
assert "databaseName" in r.json()
assert "branchName" in r.json()
assert "status" in r.json()
assert r.json()["databaseName"] == self.client.get_config()["dbName"]
assert r.json()["branchName"] == payload["metadata"]["branch"]
assert r.json()["branchName"] == "new-super-duper-feature"
assert r.json()["status"] == "completed"

pytest.branch["branch"] = payload

r = self.client.branch().createBranch(self.client.get_db_branch_name(), payload)
assert r.status_code == 422
"""
r = self.client.branch().createBranch("NonExistingDbBranchName", payload)
r = self.client.branch().createBranch(payload, branch_name="the-incredible-hulk", _from="avengers")
assert r.status_code == 400

r = self.client.branch().createBranch(self.client.get_db_branch_name(), {})
r = self.client.branch().createBranch(
payload, db_name="NOPE", branch_name=self.branch_name
)
assert r.status_code == 404

r = self.client.branch().createBranch({})
assert r.status_code == 422

def test_get_branch_metadata(self):
r = self.client.branch().getBranchMetadata(self.client.get_db_branch_name())
r = self.client.branch().getBranchMetadata()
assert r.status_code == 200

# TODO test from a previously created branch
# assert "repository" in r.json()
# assert "branch" in r.json()
# assert "stage" in r.json()

r = self.client.branch().getBranchMetadata("NonExistingDbBranchName")
assert r.status_code == 400
r = self.client.branch().getBranchMetadata(branch_name=self.branch_name)
assert r.status_code == 200
r = self.client.branch().getBranchMetadata(db_name=self.db_name)
assert r.status_code == 200

r = self.client.branch().getBranchMetadata(db_name="NOPE")
assert r.status_code == 404
r = self.client.branch().getBranchMetadata(branch_name="shrug")
assert r.status_code == 404

def test_get_branch_stats(self):
r = self.client.branch().getBranchStats(self.client.get_db_branch_name())
r = self.client.branch().getBranchStats()
assert r.status_code == 200
assert "timestamp" in r.json()
assert "interval" in r.json()
# TODO test more ^ dict keys

r = self.client.branch().getBranchStats("NonExistingDbBranchName")
assert r.status_code == 400
4 changes: 4 additions & 0 deletions tests/integration-tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def pytest_configure():
pytest.branch = {
"branch": None,
}
pytest.table = {
"client": None,
"db_name": None,
}


def create_demo_db(client: XataClient, db_name: string):
Expand Down
Loading