aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-04-14 14:15:26 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-04-14 14:15:26 -0700
commit3c9e30a5b029cb607fc78a3451536d0dc65dcecd (patch)
treedf59be59e4d829963d0f7ff8c8047dfac6effcb4
parent8fdd0b859047cf7e1dabc9f0f88ec6a5201e1f74 (diff)
downloadfatcat-3c9e30a5b029cb607fc78a3451536d0dc65dcecd.tar.gz
fatcat-3c9e30a5b029cb607fc78a3451536d0dc65dcecd.zip
more mashmallow fixes
-rw-r--r--fatcat/models.py74
-rw-r--r--tests/test_backend.py75
2 files changed, 137 insertions, 12 deletions
diff --git a/fatcat/models.py b/fatcat/models.py
index c145b6a7..49bdef22 100644
--- a/fatcat/models.py
+++ b/fatcat/models.py
@@ -189,7 +189,7 @@ class FileRev(db.Model):
extra_json = db.Column(db.ForeignKey('extra_json.sha1'), nullable=True)
size = db.Column(db.Integer)
- sha1 = db.Column(db.Integer) # TODO: hash table... only or in addition?
+ sha1 = db.Column(db.String) # TODO: hash table... only or in addition?
url = db.Column(db.Integer) # TODO: URL table
releases = db.relationship('FileRelease', lazy='subquery')
@@ -236,17 +236,36 @@ class ChangelogEntry(db.Model):
class ExtraJson(db.Model):
__tablename__ = 'extra_json'
- sha1 = db.Column(db.String, primary_key=True)
- json = db.Column(db.String)
+ sha1 = db.Column(db.String, primary_key=True, nullable=False)
+ json = db.Column(db.String, nullable=False)
### Marshmallow Wrappers ####################################################
+class ExtraJsonSchema(ma.ModelSchema):
+
+ class Meta:
+ model = ExtraJson
+
+ @post_dump(pass_many=False)
+ def unflatten(self, data):
+ assert(hashlib.sha1sum(data['json']).hexdigest() == data['sha1'])
+ data.pop('sha1')
+ raw = data.pop('json')
+ data.update(json.loads(raw))
+
+ @post_dump(pass_many=False)
+ def flatten(self, data):
+ raw = json.dumps(data, indent=None)
+ for k in list(data.keys()):
+ data.pop(k)
+ data['sha1'] = hashlib.sha1sum(raw).hexdigest()
+ data['json'] = raw
+
class EntitySchema(ma.ModelSchema):
@post_dump(pass_many=False)
def merge_rev(self, data):
- print(data)
if data.get('rev', None) != None:
rev_id = data['rev'].pop('id')
data.update(data['rev'])
@@ -256,8 +275,26 @@ class EntitySchema(ma.ModelSchema):
# TODO: should be able to set as an allow_none field somewhere
if not data.get('redirect_id', None):
data['redirect_id'] = None
- print(data)
+ extra_json = ma.Nested(ExtraJsonSchema)
+
+class ReleaseContribSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseContrib
+ #creator = db.relationship("CreatorIdent")
+ #release = db.relationship("ReleaseRev")
+
+class ReleaseRefSchema(ma.ModelSchema):
+ class Meta:
+ model = ReleaseRef
+ #release = db.relationship("ReleaseRev")
+ #target = db.relationship("ReleaseIdent")
+
+class FileReleaseSchema(ma.ModelSchema):
+ class Meta:
+ model = FileRelease
+ #release = db.relationship("ReleaseIdent")
+ #file = db.relationship("FileRev")
class WorkRevSchema(ma.ModelSchema):
class Meta:
@@ -279,16 +316,21 @@ work_edit_schema = WorkEditSchema()
class ReleaseRevSchema(ma.ModelSchema):
class Meta:
model = ReleaseRev
+ container = ma.Nested('ContainerSchema')
+ creators = ma.Nested(ReleaseContribSchema, many=True)
+ refs = ma.Nested(ReleaseRefSchema, many=True)
class ReleaseSchema(EntitySchema):
class Meta:
model = ReleaseIdent
rev = ma.Nested(ReleaseRevSchema)
+ # XXX: files = ma.Nested('FileSchema', many=True)
class ReleaseEditSchema(ma.ModelSchema):
class Meta:
model = ReleaseEdit
+release_rev_schema = ReleaseRevSchema()
release_schema = ReleaseSchema()
release_edit_schema = ReleaseEditSchema()
@@ -306,6 +348,7 @@ class CreatorEditSchema(ma.ModelSchema):
class Meta:
model = CreatorEdit
+creator_rev_schema = CreatorRevSchema()
creator_schema = CreatorSchema()
creator_edit_schema = CreatorEditSchema()
@@ -323,11 +366,30 @@ class ContainerEditSchema(ma.ModelSchema):
class Meta:
model = ContainerEdit
+container_rev_schema = ContainerRevSchema()
container_schema = ContainerSchema()
container_edit_schema = ContainerEditSchema()
-class Editor(ma.ModelSchema):
+class FileRevSchema(ma.ModelSchema):
+ class Meta:
+ model = FileRev
+
+class FileSchema(EntitySchema):
+ class Meta:
+ model = FileIdent
+ rev = ma.Nested(FileRevSchema)
+
+class FileEditSchema(ma.ModelSchema):
+ class Meta:
+ model = FileEdit
+
+file_rev_schema = FileRevSchema()
+file_schema = FileSchema()
+file_edit_schema = FileEditSchema()
+
+
+class EditorSchema(ma.ModelSchema):
class Meta:
model = Editor
diff --git a/tests/test_backend.py b/tests/test_backend.py
index 4cd474cc..1b470429 100644
--- a/tests/test_backend.py
+++ b/tests/test_backend.py
@@ -4,6 +4,7 @@ import json
import pytest
import fatcat
import fatcat.sql
+from fatcat.models import *
import unittest
import tempfile
@@ -18,24 +19,22 @@ def check_entity_fields(e):
assert e[key] is not None
def check_release(e):
- check_entity_fields(e)
for key in ('work', 'release_type'):
assert key in e
for key in ('title'):
assert e[key] is not None
+ for key in ('refs', 'creators'):
+ assert type(e[key]) == list
def check_creator(e):
- check_entity_fields(e)
- for key in ('name'):
+ for key in ('name',):
assert e[key] is not None
def check_container(e):
- check_entity_fields(e)
- for key in ('name'):
+ for key in ('name',):
assert e[key] is not None
def check_file(e):
- check_entity_fields(e)
for key in ('size', 'sha1'):
assert e[key] is not None
@@ -78,6 +77,70 @@ class ModelTestCase(FatcatTestCase):
fatcat.dummy.insert_random_works()
fatcat.sql.hydrate_release(1)
+ def test_schema_release_rev(self):
+ assert ReleaseRev.query.count() == 0
+ e = {
+ "title": "Bogus title",
+ "release_type": "book",
+ "creators": [],
+ "refs": [],
+ }
+ model = release_rev_schema.load(e)
+ fatcat.db.session.add(model.data)
+ fatcat.db.session.commit()
+ assert ReleaseRev.query.count() == 1
+ model_after = ReleaseRev.query.first()
+ serial = release_rev_schema.dump(model_after).data
+ #check_release(serial)
+ for k in e.keys():
+ assert e[k] == serial[k]
+
+ def test_schema_creator_rev(self):
+ assert ReleaseRev.query.count() == 0
+ e = {
+ "name": "Robin (Batman)",
+ }
+ model = creator_rev_schema.load(e)
+ fatcat.db.session.add(model.data)
+ fatcat.db.session.commit()
+ assert CreatorRev.query.count() == 1
+ model_after = CreatorRev.query.first()
+ serial = creator_rev_schema.dump(model_after).data
+ check_creator(serial)
+ for k in e.keys():
+ assert e[k] == serial[k]
+
+ def test_schema_container_rev(self):
+ assert ReleaseRev.query.count() == 0
+ e = {
+ "name": "Papers Monthly",
+ }
+ model = container_rev_schema.load(e)
+ fatcat.db.session.add(model.data)
+ fatcat.db.session.commit()
+ assert ContainerRev.query.count() == 1
+ model_after = ContainerRev.query.first()
+ serial = container_rev_schema.dump(model_after).data
+ check_container(serial)
+ for k in e.keys():
+ assert e[k] == serial[k]
+
+ def test_schema_file_rev(self):
+ assert ReleaseRev.query.count() == 0
+ e = {
+ "sha1": "asdf",
+ "size": 6,
+ }
+ model = file_rev_schema.load(e)
+ print(model)
+ fatcat.db.session.add(model.data)
+ fatcat.db.session.commit()
+ assert FileRev.query.count() == 1
+ model_after = FileRev.query.first()
+ serial = file_rev_schema.dump(model_after).data
+ check_file(serial)
+ for k in e.keys():
+ assert e[k] == serial[k]
## API Tests #################################################################