Ejemplo n.º 1
0
 def test_contextReset(self, tmp_path):
     '''
     Test that when a context is re-entered, the previous context data is
     reset.
     '''
     dba = DatabaseAccess(tmp_path / "db.json")
     d = DummyContextOut()
     schemaRepo = SchemaRepository()
     with ModelAccess(dba, schemaRepo, d) as mdb:
         id = mdb.insert("dummy.output", {"name": "dummy model"})
         assert id is not None
     ctxs = dba.table('fashion.prime.context').all()
     assert len(ctxs) == 1
     models = dba.table('dummy.output').all()
     assert len(models) == 1
     # Now re-enter the same context
     with ModelAccess(dba, schemaRepo, d) as mdb:
         ctxs = dba.table('fashion.prime.context').all()
         assert len(ctxs) == 0
         models = dba.table('dummy.output').all()
         assert len(models) == 0
         id = mdb.insert("dummy.output", {"name": "dummy model the second"})
         assert id is not None
     ctxs = dba.table('fashion.prime.context').all()
     assert len(ctxs) == 1
     models = dba.table('dummy.output').all()
     assert len(models) == 1
     assert models[0]["name"] == "dummy model the second"
     dba.close()
Ejemplo n.º 2
0
 def create(self):
     '''Create a new portfolio.'''
     if not self.exists():
         self.segments = {}
         self.__setDefaultProperties()
         self.fashionPath.mkdir(parents=True, exist_ok=True)
         (self.fashionPath / "warehouse").mkdir(parents=True, exist_ok=True)
         self.db = DatabaseAccess(self.fashionDbPath)
         self.loadWarehouses()
         self.warehouse.newSegment("local", self.db)
         self.save()
Ejemplo n.º 3
0
 def test_loadSchemas(self, tmp_path):
     dba = DatabaseAccess(tmp_path / "db.json")
     fw = Warehouse(FASHION_WAREHOUSE_PATH)
     wh = Warehouse(tmp_path, fw)
     wh.loadSegments(dba)
     r = Runway(dba, wh)
     r.loadSchemas()
Ejemplo n.º 4
0
 def test_create(self, tmp_path):
     dba = DatabaseAccess(tmp_path / "db.json")
     fw = Warehouse(FASHION_WAREHOUSE_PATH)
     wh = Warehouse(tmp_path, fw)
     wh.loadSegments(dba)
     r = Runway(dba, wh)
     assert r is not None
Ejemplo n.º 5
0
    def __init__(self, projDir):
        '''
        Initialize a portfolio mapped to the given directory, whether or not 
        the directory actually exists.

        :param str projDir: where the project is located. 
        '''
        self.projectPath = projDir.absolute()
        self.fashionPath = self.projectPath / 'fashion'
        self.mirrorPath = self.fashionPath / 'mirror'
        self.portfolioPath = self.fashionPath / 'portfolio.json'
        self.fashionDbPath = self.fashionPath / 'database.json'
        self.mirror = Mirror(self.projectPath, self.mirrorPath)
        if self.fashionDbPath.exists():
            self.load()
            self.db = DatabaseAccess(self.fashionDbPath)
Ejemplo n.º 6
0
 def test_warehouseFallback(self, tmp_path):
     '''Test the local warehouse refers properly to another warehouse.'''
     dba = DatabaseAccess(tmp_path / "db.json")
     fw = Warehouse(FASHION_WAREHOUSE_PATH)
     lw = Warehouse(tmp_path, fw)
     coreSeg = lw.loadSegment("fashion.core", dba)
     assert coreSeg is not None
Ejemplo n.º 7
0
 def test_loadSegments(self, tmp_path):
     '''Test loading all segments in a warehouse.'''
     dba = DatabaseAccess(tmp_path / "db.json")
     wh = Warehouse(tmp_path)
     newSegName = "custom"
     wh.newSegment(newSegName, dba)
     segs = wh.loadSegments(dba)
     assert len(segs) == 1
     assert segs[0].properties.name == newSegName
Ejemplo n.º 8
0
 def test_newSegment(self, tmp_path):
     '''Test creating a new segment in a warehouse.'''
     dba = DatabaseAccess(tmp_path / "db.json")
     newSegName = "custom"
     wh = Warehouse(tmp_path)
     wh.newSegment(newSegName, dba)
     segNames = wh.listSegments()
     assert len(segNames) == 1
     assert segNames[0] == newSegName
     seg = wh.loadSegment(segNames[0], dba)
     assert seg is not None
Ejemplo n.º 9
0
 def test_reading(self, tmp_path):
     '''Test searching for models.'''
     dba = DatabaseAccess(tmp_path / "db.json")
     dOut = DummyContextOut()
     schemaRepo = SchemaRepository()
     with ModelAccess(dba, schemaRepo, dOut) as mdb:
         id = mdb.insert("dummy.output", {"name": "dummy model"})
     dIn = DummyContextIn()
     with ModelAccess(dba, schemaRepo, dIn) as mdb:
         m = mdb.getById("dummy.output", id)
         assert m is not None
         assert m["name"] == "dummy model"
         m = mdb.getByKind("dummy.output")
         assert m is not None
         assert len(m) == 1
         assert m[0]["name"] == "dummy model"
         dummyQ = Query()
         m = mdb.search("dummy.output", dummyQ.name == "dummy model")
         assert m is not None
         assert len(m) == 1
         assert m[0]["name"] == "dummy model"
     dba.close()
Ejemplo n.º 10
0
 def test_create(self, tmp_path):
     dba = DatabaseAccess(tmp_path / "db.json")
     d = DummyContextOut()
     schemaRepo = SchemaRepository()
     with ModelAccess(dba, schemaRepo, d) as mdb:
         id = mdb.insert("dummy.output", {"name": "dummy model"})
         assert id is not None
     ctxs = dba.table('fashion.prime.context').all()
     assert len(ctxs) == 1
     assert len(ctxs[0]["insert"]) == 1
     assert 'dummy.output' in ctxs[0]["insert"]
     assert id in ctxs[0]["insert"]['dummy.output']
     models = dba.table('dummy.output').all()
     assert len(models) == 1
     dba.close()
Ejemplo n.º 11
0
 def test_loadModules(self, tmp_path):
     dba = DatabaseAccess(tmp_path / "db.json")
     fw = Warehouse(FASHION_WAREHOUSE_PATH)
     wh = Warehouse(tmp_path, fw)
     wh.loadSegments(dba)
     r = Runway(dba, wh)
     dba.setSingleton('fashion.prime.args',
         {
             "force":False,
             "verbose":True
         })
     mirrorDir = tmp_path / "mirror"
     mirrorDir.mkdir()
     dba.setSingleton('fashion.prime.portfolio',
         {
             "projectPath":".",
             "mirrorPath":mirrorDir.as_posix()
         })
     r.loadModules()
     r.initModules()
     r.plan()
     r.execute()
Ejemplo n.º 12
0
class Portfolio(object):
    '''Represents a fashion user project.'''
    def __init__(self, projDir):
        '''
        Initialize a portfolio mapped to the given directory, whether or not 
        the directory actually exists.

        :param str projDir: where the project is located. 
        '''
        self.projectPath = projDir.absolute()
        self.fashionPath = self.projectPath / 'fashion'
        self.mirrorPath = self.fashionPath / 'mirror'
        self.portfolioPath = self.fashionPath / 'portfolio.json'
        self.fashionDbPath = self.fashionPath / 'database.json'
        self.mirror = Mirror(self.projectPath, self.mirrorPath)
        if self.fashionDbPath.exists():
            self.load()
            self.db = DatabaseAccess(self.fashionDbPath)

    def __setDefaultProperties(self):
        self.properties = munchify({
            "name":
            "fashion",
            "defaultSegment":
            "local",
            "warehouses": [(self.fashionPath / 'warehouse').as_posix()]
        })

    def loadWarehouses(self):
        self.warehouse = None
        wl = copy.copy(self.properties.warehouses)
        wl.append(FASHION_WAREHOUSE_PATH.as_posix())
        wl.reverse()
        for wp in wl:
            self.warehouse = Warehouse(Path(wp), self.warehouse)

    def exists(self):
        '''Check if this project exists.'''
        return self.fashionPath.exists()

    def create(self):
        '''Create a new portfolio.'''
        if not self.exists():
            self.segments = {}
            self.__setDefaultProperties()
            self.fashionPath.mkdir(parents=True, exist_ok=True)
            (self.fashionPath / "warehouse").mkdir(parents=True, exist_ok=True)
            self.db = DatabaseAccess(self.fashionDbPath)
            self.loadWarehouses()
            self.warehouse.newSegment("local", self.db)
            self.save()

    def delete(self):
        '''Delete an existing project.'''
        if self.exists():
            self.db.close()
            shutil.rmtree(str(self.fashionPath))

    def save(self):
        '''Save the portfolio.'''
        with self.portfolioPath.open(mode="w") as pf:
            pf.write(self.properties.toJSON(sort_keys=True, indent=4))

    def load(self):
        '''Load a portfolio from a file.'''
        with self.portfolioPath.open(mode='r') as fd:
            dict = json.loads(fd.read())
            self.properties = munchify(dict)
        self.loadWarehouses()

    def defaultSegment(self):
        return self.warehouse.loadSegment(self.defaultSegmentName(), self.db)

    def defaultSegmentName(self):
        return self.properties.defaultSegment

    def setDefaultSegment(self, segname):
        self.properties.defaultSegment = segname

    def getRunway(self, tags=None):
        '''
        Get a Runway for this Portfolio.

        :param boolean verbose: True if the runway should be verbose.
        :param list tags: a list of tags for the Runway to include.
        :returns: a Runway object.
        :rtype: fashion.Runway
        '''
        self.warehouse.loadSegments(self.db)
        r = Runway(self.db, self.warehouse)
        r.loadModules()
        r.initModules()
        return r

    def normalizeFilename(self, filename):
        '''Convert filename to realtive to project directory.'''
        fn = Path(filename).absolute()
        return fn.relative_to(self.projectPath)
Ejemplo n.º 13
0
 def test_init(self, tmp_path):
     fashionDbPath = tmp_path / 'fashion_database.json'
     db = DatabaseAccess(fashionDbPath)
     assert db is not None