'Tests the creation of document links.'

def test_create_document_with_links(self):

    f = SimpleUploadedFile('test_img_file.gif', self.test_file.read(), 'image/gif')
    superuser = get_user_model().objects.get(pk=2)
    d = Document.objects.create(doc_file=f, owner=superuser, title='theimg')
    self.assertEquals(Document.objects.get(pk=d.id).title, 'theimg')
    maps = list(Map.objects.all())
    layers = list(Layer.objects.all())
    resources = (maps + layers)
    mixin1 = DocumentFormMixin()
    mixin1.instance = d
    mixin1.cleaned_data = dict(links=mixin1.generate_link_values(resources=resources))
    mixin1.save_many2many()
    for resource in resources:
        ct = ContentType.objects.get_for_model(resource)
        l = DocumentResourceLink.objects.get(document_id=d.id, content_type=ct.id, object_id=resource.id)
        self.assertEquals(l.object_id, resource.id)
    mixin2 = DocumentFormMixin()
    mixin2.instance = d
    mixin2.cleaned_data = dict(links=mixin2.generate_link_values(resources=layers))
    mixin2.save_many2many()
    for resource in layers:
        ct = ContentType.objects.get_for_model(resource)
        l = DocumentResourceLink.objects.get(document_id=d.id, content_type=ct.id, object_id=resource.id)
        self.assertEquals(l.object_id, resource.id)
    for resource in maps:
        ct = ContentType.objects.get_for_model(resource)
        with self.assertRaises(DocumentResourceLink.DoesNotExist):
            DocumentResourceLink.objects.get(document_id=d.id, content_type=ct.id, object_id=resource.id)
