import pytest
from girder.models.file import File
from girder.models.model_base import Model
from girder.utility import assetstore_utilities
from girder.utility.model_importer import ModelImporter
from girder.utility.abstract_assetstore_adapter import AbstractAssetstoreAdapter


class Fake(Model):
    def initialize(self):
        self.name = 'fake_collection'

    def validate(self, doc):
        return doc


class FakeAdapter(AbstractAssetstoreAdapter):
    def __init__(self, assetstore):
        self.the_assetstore = assetstore


@pytest.fixture
def fakeModel(db):
    ModelImporter.registerModel('fake', Fake(), plugin='fake_plugin')

    yield Fake().save({
        'foo': 'bar',
        'type': 'fake'
    })

    ModelImporter.unregisterModel('fake', plugin='fake_plugin')


@pytest.fixture
def fakeAdapter(db):
    assetstore_utilities.setAssetstoreAdapter('fake', FakeAdapter)

    yield

    assetstore_utilities.removeAssetstoreAdapter('fake')


def testAssetstoreModelOverride(fakeModel, fakeAdapter, admin):
    file = File().createFile(
        creator=admin, item=None, name='a.out', size=0, assetstore=fakeModel,
        assetstoreModel='fake', assetstoreModelPlugin='fake_plugin')

    adapter = File().getAssetstoreAdapter(file)
    assert adapter.the_assetstore == fakeModel

@@ -656,69 +656,8 @@
 Fake
-().save(%7B%0A        'foo': 'bar',%0A        'type': 'fake'%0A    %7D)
 %0A%0A  
@@ -959,16 +959,108 @@
 admin):%0A
+    fakeAssetstore = fakeModel().save(%7B%0A        'foo': 'bar',%0A        'type': 'fake'%0A    %7D)%0A
     file
@@ -1152,21 +1152,26 @@
 ore=fake
-Model
+Assetstore
 ,%0A      
@@ -1282,16 +1282,60 @@
 r(file)%0A
+    assert isinstance(adapter, FakeAdapter)%0A
     asse
@@ -1367,14 +1367,19 @@
  == fake
-Model
+Assetstore
 %0A

Improve clarity of fake assetstore model fixture