def test_graphql_delete_synthesis(graphql_registry, graphql_request,
                                  fulltext_synthesis_post):
    fulltext_synthesis_post_id = to_global_id('Post',
                                              fulltext_synthesis_post.id)
    res = schema.execute(graphql_registry['deleteSynthesis'],
                         variable_values={"id": fulltext_synthesis_post_id},
                         context_value=graphql_request)
    assert res.errors is None
    assert res.data['deleteSynthesis']['success'] is True
    assert SynthesisPost.get(fulltext_synthesis_post.id) is None
def test_graphql_update_synthesis(graphql_registry, graphql_request,
                                  fulltext_synthesis_post_with_image):
    synthesis_post = fulltext_synthesis_post_with_image
    synthesis_post_graphql_id = to_global_id('Post', synthesis_post.id)
    graphql_request.POST['variables.image'] = FakeUploadedFile(
        u'path/to/new-img.png', 'image/png')
    subject_entries = [{
        "value": u"My synthesis v2",
        "localeCode": u"en"
    }, {
        "value": u"Ma synthèse v2",
        "localeCode": u"fr"
    }]
    body_entries = [{
        "value": u"Text in english v2",
        "localeCode": u"en"
    }, {
        "value": u"Texte en français v2",
        "localeCode": u"fr"
    }]
    res = schema.execute(graphql_registry['updateSynthesis'],
                         context_value=graphql_request,
                         variable_values={
                             "id": synthesis_post_graphql_id,
                             "image": u"variables.image",
                             "embedCode": u"nothing",
                             "lang": u"fr",
                             "subjectEntries": subject_entries,
                             "bodyEntries": body_entries,
                             "publicationState": u"PUBLISHED"
                         })
    assert res.errors is None
    assert res.data is not None
    assert res.data['updateSynthesis'] is not None
    assert res.data['updateSynthesis']['synthesisPost'][
        'publicationState'] == u"PUBLISHED"
    assert res.data['updateSynthesis']['synthesisPost'][
        'publishesSynthesis'] is not None
    synthesis = res.data['updateSynthesis']['synthesisPost'][
        'publishesSynthesis']
    assert sorted(synthesis['subjectEntries'],
                  key=lambda e: e['localeCode']) == subject_entries
    assert sorted(synthesis['bodyEntries'],
                  key=lambda e: e['localeCode']) == body_entries

    assert '/documents/' in synthesis['img']['externalUrl']

    new_synthesis_post = SynthesisPost.get(synthesis_post.id)
    assert new_synthesis_post.attachments[0].document.title == 'new-img.png'
    new_synthesis = new_synthesis_post.publishes_synthesis
    assert new_synthesis.body.closest_entry(
        'en').value == u"Text in english v2"
    assert new_synthesis.body.closest_entry(
        'fr').value == u"Texte en français v2"