Skip to content

Commit

Permalink
Splitting the integration tests files (#25)
Browse files Browse the repository at this point in the history
* first part of splitting the integration tests

* isolated tests for search and filter namespace

* parts moved of to namespaced endpoints

* bump to next version

* add new db branch name method with tests

* teardown and setup for search_and_filter

* stub more namespaces

* more tests for table namespace

* complete integration tests for table namespace

* completed databases namespace

* add authentication namespace

* users namespace

* add workspaces namespace

* add partially branch tests

* linter improvements

* add faker dependency and update deps

* added one more tests for records

* add records tests

* codegen bugfix for duplicate params

* add tests for records namespace
  • Loading branch information
philkra authored Feb 15, 2023
1 parent 11ff788 commit 378d52c
Show file tree
Hide file tree
Showing 19 changed files with 1,654 additions and 282 deletions.
25 changes: 14 additions & 11 deletions codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def generate_endpoints(path: str, endpoints: dict, references: dict):
"""
Generate the endpoints of a namespace
"""
params = endpoints["parameters"] if "parameters" in endpoints else []
for method in HTTP_METHODS:
if method in endpoints:
params = endpoints["parameters"] if "parameters" in endpoints else []
out = generate_endpoint(path, method, endpoints[method], params, references)
file_name = "%s/%s.py" % (
WS_DIR,
Expand Down Expand Up @@ -138,23 +138,28 @@ def generate_endpoint(
Generate a single endpoint
"""
if "parameters" in endpoint:
parameters += endpoint["parameters"]
endpointParams = get_endpoint_params(
path, endpoint, parameters + endpoint["parameters"], references
)
else:
endpointParams = get_endpoint_params(path, endpoint, parameters, references)
if "description" in endpoint:
desc = endpoint["description"].strip()
else:
desc = endpoint["summary"].strip()

vars = {
"operation_id": endpoint["operationId"].strip(),
"description": endpoint["description"].strip()
if "description" in endpoint
else endpoint["summary"].strip(),
"description": desc,
"http_method": method.upper(),
"path": path.lower(),
"params": get_endpoint_params(path, endpoint, parameters, references),
"params": endpointParams,
"request_body": get_endpoint_request_body(endpoint),
# "responses": list(endpoint["responses"].keys()),
# "responses": list(endpoint["responses"].keys()),
}
out = Template(filename="codegen/endpoint.tpl", output_encoding="utf-8").render(
return Template(filename="codegen/endpoint.tpl", output_encoding="utf-8").render(
**vars
)
return out


def get_endpoint_params(
Expand All @@ -168,8 +173,6 @@ def get_endpoint_params(
"has_optional_params": 0,
}
if len(parameters) > 0:
counter_path = 0
counter_query = 0
for r in parameters:
# if not in ref -> endpoint specific params
# else if name not in r -> method specific params
Expand Down
56 changes: 49 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xata"
version = "0.2.1"
version = "0.3.0"
description = "Python client for Xata.io"
authors = ["Xata <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -24,6 +24,7 @@ flake8-bugbear = "^22.10.27"
flake8-annotations = "^2.9.1"
Mako = "1.2.4"
pdoc3 = "^0.10.0"
Faker = "^17.0.0"

[build-system]
requires = ["poetry-core"]
Expand Down
67 changes: 67 additions & 0 deletions tests/integration-tests/authentication_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# Licensed to Xatabase, Inc under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Xatabase, Inc licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

import utils

from xata.client import XataClient


class TestClass(object):
@classmethod
def setup_class(self):
self.db_name = utils.get_db_name()
self.branch_name = "main"
self.new_api_key = "one-key-to-rule-them-all-%s" % utils.get_random_string(6)
self.client = XataClient(db_name=self.db_name, branch_name=self.branch_name)

def test_get_user_api_keys(self):
r = self.client.authentication().getUserAPIKeys()
assert r.status_code == 200
assert "keys" in r.json()
assert len(r.json()["keys"]) > 0
assert "name" in r.json()["keys"][0]
assert "createdAt" in r.json()["keys"][0]

def test_create_user_api_keys(self):
r = self.client.authentication().getUserAPIKeys()
assert r.status_code == 200
count = len(r.json()["keys"])

r = self.client.authentication().createUserAPIKey(self.new_api_key)
assert r.status_code == 201
assert "name" in r.json()
assert "key" in r.json()
assert "createdAt" in r.json()
assert self.new_api_key == r.json()["name"]

r = self.client.authentication().getUserAPIKeys()
assert len(r.json()["keys"]) == (count + 1)

r = self.client.authentication().createUserAPIKey(self.new_api_key)
assert r.status_code == 409

r = self.client.authentication().createUserAPIKey("")
assert r.status_code == 404

def test_delete_user_api_key(self):
r = self.client.authentication().deleteUserAPIKey(self.new_api_key)
assert r.status_code == 204

r = self.client.authentication().deleteUserAPIKey("NonExistingApiKey")
assert r.status_code == 404
147 changes: 147 additions & 0 deletions tests/integration-tests/branch_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# Licensed to Xatabase, Inc under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Xatabase, Inc licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

import utils

from xata.client import XataClient


class TestClass(object):
@classmethod
def setup_class(self):
self.db_name = utils.get_db_name()
self.branch_name = "main"
self.client = XataClient(db_name=self.db_name, branch_name=self.branch_name)

# create database
r = self.client.databases().createDatabase(
self.client.get_config()["workspaceId"],
self.db_name,
{
"region": self.client.get_config()["region"],
"branchName": self.client.get_config()["branchName"],
},
)
assert r.status_code == 201

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

# create schema
r = self.client.table().setTableSchema(
self.client.get_db_branch_name(),
"Posts",
{
"columns": [
{"name": "title", "type": "string"},
{"name": "labels", "type": "multiple"},
{"name": "slug", "type": "string"},
{"name": "text", "type": "text"},
]
},
)
assert r.status_code == 200

@classmethod
def teardown_class(self):
r = self.client.databases().deleteDatabase(
self.client.get_config()["workspaceId"], self.db_name
)
assert r.status_code == 200

def test_get_branch_list(self):
r = self.client.branch().getBranchList(self.db_name)
assert r.status_code == 200
assert "databaseName" in r.json()
assert "branches" in r.json()
assert r.json()["databaseName"] == self.db_name
assert len(r.json()["branches"]) == 1
assert "name" in r.json()["branches"][0]
assert "createdAt" in r.json()["branches"][0]
assert r.json()["branches"][0]["name"] == "main"

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

def test_get_branch_details(self):
r = self.client.branch().getBranchDetails(self.client.get_db_branch_name())
assert r.status_code == 200
assert "databaseName" in r.json()
assert "branchName" in r.json()
assert "metadata" in r.json()
assert "schema" in r.json()
assert r.json()["databaseName"] == self.client.get_config()["dbName"]
# TODO be exhastive testing the ^ dict keys

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

def test_create_database_branch(self):
payload = {
"from": "main",
"metadata": {
"repository": "github.com/xataio/xata-py",
"branch": "integration-testing-%s" % utils.get_random_string(6),
"stage": "testing",
},
}
"""
r = self.client.branch().createBranch(self.client.get_db_branch_name(), payload)
assert r.json() == ""
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()["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)
assert r.status_code == 400

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

def test_get_branch_metadata(self):
r = self.client.branch().getBranchMetadata(self.client.get_db_branch_name())
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

def test_get_branch_stats(self):
r = self.client.branch().getBranchStats(self.client.get_db_branch_name())
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
Loading

0 comments on commit 378d52c

Please sign in to comment.