Skip to content

Commit 9cbdfa2

Browse files
author
Nick McCoy
committed
validating schema in discovery mode
Added write_catalog function
1 parent 4ac31ab commit 9cbdfa2

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

singer/catalog.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
from singer.schema import Schema
7+
from jsonschema import ValidationError, Draft4Validator, FormatChecker
78

89
# pylint: disable=too-many-instance-attributes
910
class CatalogEntry(object):
@@ -109,3 +110,29 @@ def get_stream(self, tap_stream_id):
109110
if stream.tap_stream_id == tap_stream_id:
110111
return stream
111112
return None
113+
114+
115+
CATALOG_SCHEMA = {'type': 'object',
116+
'required': ['streams'],
117+
'properties': {
118+
'streams' : {
119+
'type': 'array',
120+
'items': {
121+
'type': 'object',
122+
'required': ['stream', 'tap_stream_id', 'schema'],
123+
'properties': {
124+
'stream': {'type': 'string'},
125+
'tap_stream_id': {'type': 'string'},
126+
'schema': {'type': 'object'}
127+
}
128+
}
129+
}
130+
}
131+
}
132+
133+
CATALOG_VALIDATOR = Draft4Validator(CATALOG_SCHEMA,
134+
format_checker=FormatChecker())
135+
136+
def write_catalog(streams):
137+
CATALOG_VALIDATOR.validate(streams)
138+
json.dump(streams, sys.stdout, indent=2)

tests/test_catalog.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import unittest
2+
import singer.catalog
23

34
from singer.schema import Schema
45
from singer.catalog import Catalog, CatalogEntry
56

6-
class TestToDictAndFromDict(unittest.TestCase):
7-
8-
dict_form = {
7+
dict_form = {
98
'streams': [
109
{
1110
'stream': 'users',
@@ -38,7 +37,7 @@ class TestToDictAndFromDict(unittest.TestCase):
3837
]
3938
}
4039

41-
obj_form = Catalog(streams=[
40+
obj_form = Catalog(streams=[
4241
CatalogEntry(
4342
stream='users',
4443
tap_stream_id='prod_users',
@@ -62,11 +61,15 @@ class TestToDictAndFromDict(unittest.TestCase):
6261
'id': Schema(type='integer', selected=True),
6362
'amount': Schema(type='number', selected=True)}))])
6463

64+
65+
66+
67+
class TestToDictAndFromDict(unittest.TestCase):
6568
def test_from_dict(self):
66-
self.assertEqual(self.obj_form, Catalog.from_dict(self.dict_form))
69+
self.assertEqual(obj_form, Catalog.from_dict(dict_form))
6770

6871
def test_to_dict(self):
69-
self.assertEqual(self.dict_form, self.obj_form.to_dict())
72+
self.assertEqual(dict_form, obj_form.to_dict())
7073

7174

7275
class TestGetStream(unittest.TestCase):
@@ -77,3 +80,7 @@ def test(self):
7780
CatalogEntry(tap_stream_id='c')])
7881
entry = catalog.get_stream('b')
7982
self.assertEquals('b', entry.tap_stream_id)
83+
84+
class TestWriteCatalog(unittest.TestCase):
85+
def test(self):
86+
singer.catalog.write_catalog(dict_form)

0 commit comments

Comments
 (0)