Skip to content

Commit

Permalink
feat: Validate records against stream schema in standard tap tests (#…
Browse files Browse the repository at this point in the history
…1711)

* feat: Validate records against stream schema in standard tap tests

* Fix `join`

* Fix types

* Fix `weight` type

* Fix time estimate types
  • Loading branch information
edgarrmondragon authored May 18, 2023
1 parent 9ebe372 commit 973f245
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
22 changes: 12 additions & 10 deletions samples/sample_tap_gitlab/schemas/epic_issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@
"type": "object"
},
"closed_at": {
"type": "null"
"type": ["string", "null"],
"format": "date-time"
},
"confidential": {
"type": "boolean"
Expand All @@ -120,7 +121,8 @@
"type": "integer"
},
"due_date": {
"type": "null"
"type": ["string", "null"],
"format": "date-time"
},
"epic_issue_id": {
"type": "integer"
Expand All @@ -133,8 +135,7 @@
},
"labels": {
"items": {
"properties": {},
"type": "object"
"type": "string"
},
"type": "array"
},
Expand All @@ -148,7 +149,8 @@
"type": "string"
},
"due_date": {
"type": "null"
"type": ["string", "null"],
"format": "date-time"
},
"id": {
"type": "integer"
Expand All @@ -160,7 +162,8 @@
"type": "integer"
},
"start_date": {
"type": "null"
"type": ["string", "null"],
"format": "date-time"
},
"state": {
"type": "string"
Expand All @@ -179,7 +182,6 @@
"due_date",
"id",
"iid",
"project_id",
"start_date",
"state",
"title",
Expand All @@ -196,10 +198,10 @@
"time_stats": {
"properties": {
"human_time_estimate": {
"type": "null"
"type": ["string", "null"]
},
"human_total_time_spent": {
"type": "null"
"type": ["string", "null"]
},
"time_estimate": {
"type": "integer"
Expand Down Expand Up @@ -233,7 +235,7 @@
"type": "string"
},
"weight": {
"type": "null"
"type": ["integer", "null"]
}
},
"required": [
Expand Down
28 changes: 27 additions & 1 deletion samples/sample_tap_gitlab/schemas/issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,33 @@
"assignees": {
"type": "array",
"items": {
"type": "integer"
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"username": {
"type": "string"
},
"state": {
"type": "string"
},
"avatar_url": {
"type": [
"null",
"string"
]
},
"web_url": {
"type": [
"null",
"string"
]
}
}
}
},
"closed_by_id": {
Expand Down
2 changes: 2 additions & 0 deletions singer_sdk/testing/suites.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AttributeNotNullTest,
StreamCatalogSchemaMatchesRecordTest,
StreamPrimaryKeysTest,
StreamRecordMatchesStreamSchema,
StreamRecordSchemaMatchesCatalogTest,
StreamReturnsRecordTest,
TapCLIPrintsTest,
Expand Down Expand Up @@ -61,6 +62,7 @@ class TestSuite:
kind="tap_stream",
tests=[
StreamCatalogSchemaMatchesRecordTest,
StreamRecordMatchesStreamSchema,
StreamRecordSchemaMatchesCatalogTest,
StreamReturnsRecordTest,
StreamPrimaryKeysTest,
Expand Down
25 changes: 25 additions & 0 deletions singer_sdk/testing/tap_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings

from dateutil import parser
from jsonschema import Draft7Validator

import singer_sdk.helpers._typing as th
from singer_sdk import Tap
Expand Down Expand Up @@ -107,6 +108,30 @@ def test(self) -> None:
assert not diff, f"Fields in records but not in catalog: ({diff})"


class StreamRecordMatchesStreamSchema(StreamTestTemplate):
"""Test all attributes in the record schema are present in the catalog schema."""

name = "record_matches_stream_schema"

def test(self) -> None:
"""Run test."""
schema = self.stream.schema
validator = Draft7Validator(
schema,
format_checker=Draft7Validator.FORMAT_CHECKER,
)
for record in self.stream_records:
errors = list(validator.iter_errors(record))
error_messages = "\n".join(
[
f"{e.message} (path: {'.'.join(str(p) for p in e.path)})"
for e in errors
if e.path
],
)
assert not errors, f"Record does not match stream schema: {error_messages}"


class StreamPrimaryKeysTest(StreamTestTemplate):
"""Test all records for a stream's primary key are unique and non-null."""

Expand Down

0 comments on commit 973f245

Please sign in to comment.