async def mutate(self, info, edit_child_field): global document_repo assert (document_repo is not None) try: document = await document_repo.find_by_id( edit_child_field.document_id) except repo.InvalidId as exc: return Errors([Error('id', ['invalid'])]) if not document: return Errors([Error('id', ['not found'])]) try: document.update_child_field( model.ChildField( id=edit_child_field.child_field.id, name=edit_child_field.child_field.name, date=edit_child_field.child_field.date.to_model())) except KeyError as exc: return Errors([Error('contract_id', ['not found'])]) result = await document_repo.save(document) return Document.from_model(result)
def test_to_bson(self, date, name): result = model.ChildField(date, name) bson = result.to_bson() self.assertEqual(result.name, bson['name']) self.assertEqual(result.date.to_bson(), bson['date']) self.assertEqual(ObjectId(result.id), bson['_id'])
def test_remove_child_field(self): document = self.document_repo._save( model.Document(name="Anakin Skywalker", age=99, child_field=[ model.ChildField(name="Luke Skywalker", date=model.Date(month=8, year=2018)) ])) self.document_repo.set_data([document]) REMOVE_CHILD_FIELD_MUTATION = """ mutation { removeChildField(removeChildField:{ documentId: "%s" childFieldId: "%s" }) { __typename ... on Document { id name age childField{ name date{ month year } } } ... on Errors{ errors{ field messages } } } } """ % (document.id, document.child_field[0].id) result = self.execute(REMOVE_CHILD_FIELD_MUTATION) self.assertEqual(result.errors, None) data = to_dict(result.data)['removeChildField'] print(data) child_fields = data['childField'] child_fields_ids = [a['id'] for a in child_fields] self.assertTrue(document.child_field[0].id not in child_fields_ids)
async def mutate(self, info, add_child_field): global document_repo assert (document_repo is not None) try: document = await document_repo.find_by_id( add_child_field.document_id) except repo.InvalidId as exc: return Errors([Error('id', ['invalid'])]) if not document: return Errors([Error('id', ['not found'])]) document.add_child_field( model.ChildField( name=add_child_field.name, date=add_child_field.date.to_model(), )) result = await document_repo.save(document) return Document.from_model(result)
def test_get_all_documents(self): #set up test data document = self.document_repo._save( model.Document(name="Anakin Skywalker", age=99, child_field=[ model.ChildField(name="Luke Skywalker", date=model.Date(month=8, year=2018)) ])) documents = [document] # self.document_repo.set_data(documents) result = self.execute(self.DOCUMENTS_QUERY) self.assertEqual(result.errors, None) self.assertEqual( to_dict(result.data), { 'documents': [{ 'id': d.id, 'name': d.name, 'age': d.age, 'childField': [{ 'name': "Luke Skywalker", 'date': { 'month': 8, 'year': 2018 } }] } for d in documents] })
def test_constructor(self, date, name): result = model.ChildField(date, name) self.assertEqual(result.name, name) self.assertEqual(result.date, date) self.assertTrue(isinstance(result.id, str))
def test_edit_child_field(self): document = self.document_repo._save( model.Document(name="Anakin Skywalker", age=99, child_field=[ model.ChildField(name="Luke Skywalker", date=model.Date(month=8, year=2018)) ])) self.document_repo.set_data([document]) EDIT_CHILD_FIELD_MUTATION = """ mutation { editChildField(editChildField:{ documentId: "%s" childField:{ id: "%s" name: "Yoda" date: { month: 5, year: 2010 } } }) { __typename ... on Document { id name age childField{ name date{ month year } } } ... on Errors{ errors{ field messages } } } } """ % (document.id, document.child_field[0].id) result = self.execute(EDIT_CHILD_FIELD_MUTATION) self.assertEqual(result.errors, None) self.assertEqual( to_dict(result.data), { 'editChildField': { '__typename': 'Document', 'id': document.id, 'name': document.name, 'age': document.age, 'childField': [{ 'name': 'Yoda', 'date': { 'month': 5, 'year': 2010 } }] } })