Skip to content

Commit 818b0b5

Browse files
authored
chore: Add JSONB support (#46)
* Add JSONB to tableschema type mapping. * Add documentation for troubleshooting. * Add test for JSONB.
1 parent 1f4c9bb commit 818b0b5

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,11 @@ pipenv install --dev
324324
2. Run the tests with the following command,
325325
```bash
326326
pipenv run pytest -c pytest.ini
327-
```
327+
```
328+
329+
## Troubleshooting
330+
If you have trouble starting the application with `docker-compose up` the problem may lie in your postgres container.
331+
332+
Try running `docker rm postgres-container-id` to remove any prevoiusly saved data in the postgres container. This will allow the application to rebuild the database from scratch and should start successfully.
333+
334+
Or try running `docker system prune`.

data_resource_api/factories/table_schema_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from sqlalchemy import Integer, String, Float, Boolean, Date, DateTime
6+
from sqlalchemy.dialects.postgresql import JSONB
67

78
# Mapping of a Frictionless Table Schema to SQLAlchemy Data Type.
89
#
@@ -15,7 +16,7 @@
1516
'number': Float,
1617
'integer': Integer,
1718
'boolean': Boolean,
18-
'object': String,
19+
'object': JSONB,
1920
'array': String,
2021
'date': Date,
2122
'time': DateTime,

tests/schemas.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
custom_descriptor = [{
2-
"api": {
3-
"resource": "tests",
4-
"methods": [
5-
{
6-
"get": {
7-
"enabled": True,
8-
"secured": False,
9-
"grants": []
10-
}
11-
}
12-
]
13-
},
14-
"datastore": {
15-
"tablename": "tests",
16-
"restricted_fields": [],
17-
"schema": {
18-
"fields": [
19-
{
20-
"name": "id",
21-
"title": "Test ID",
22-
"description": "Test Desc",
23-
"type": "integer",
24-
"required": True
25-
},
26-
{
27-
"name": "name",
28-
"title": "namename",
29-
"description": "test name",
30-
"type": "string",
31-
"required": True
32-
}
33-
],
34-
"primaryKey": "id"
35-
}
36-
}
37-
}
38-
]
39-
401
frameworks_descriptor = {
412
"api": {
423
"resource": "frameworks",
@@ -102,6 +63,12 @@
10263
"description": "framework name",
10364
"type": "string",
10465
"required": True
66+
},
67+
{
68+
"name": "jsonb",
69+
"title": "jsonb",
70+
"type": "object",
71+
"required": False
10572
}
10673
],
10774
"primaryKey": "id"

tests/test_custom_descriptor.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ def post_a_framework(c, skills: list):
3030

3131
return body['id']
3232

33+
@staticmethod
34+
def get_a_framework_by_id(c, id: int):
35+
route = '/frameworks'
36+
response = c.get(f'{route}/{id}')
37+
body = json.loads(response.data)
38+
39+
expect(response.status_code).to(equal(200))
40+
41+
return body
42+
3343
@staticmethod
3444
def post_a_framework_with_no_skills(c):
3545
route = '/frameworks'
@@ -43,6 +53,20 @@ def post_a_framework_with_no_skills(c):
4353

4454
return body['id']
4555

56+
@staticmethod
57+
def post_json(c, data: object):
58+
route = '/frameworks'
59+
post_body = {
60+
"name": "test",
61+
"jsonb": data
62+
}
63+
response = c.post(route, json=post_body)
64+
body = json.loads(response.data)
65+
66+
expect(response.status_code).to(equal(201))
67+
68+
return body['id']
69+
4670
@staticmethod
4771
def put_a_framework_skill(c, framework_id: int, skills: list):
4872
route = f'/frameworks/{framework_id}/skills'
@@ -205,3 +229,12 @@ def test_mn_reverse_relationship(self, frameworks_skills_client):
205229
resp = ApiHelper.get_frameworks_on_skill(c, skill_1)
206230

207231
expect(resp['frameworks']).to(equal([framework_id]))
232+
233+
def test_json(self, frameworks_skills_client):
234+
c = frameworks_skills_client
235+
236+
json_framework_id = ApiHelper.post_json(c, {"testkey": "testvalue"})
237+
238+
resp = ApiHelper.get_a_framework_by_id(c, json_framework_id)
239+
240+
expect(resp['jsonb']).to(equal("{'testkey': 'testvalue'}"))

tests/test_junc_holder.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
import pytest
44

55

6-
class TestTable:
7-
def __init__(self, table_name):
8-
self.__tablename__ = table_name
9-
10-
116
class TestJuncHolder(object):
127
def test_use(self):
138
JuncHolder.reset()

0 commit comments

Comments
 (0)