Example #1
0
class MapperTestCase(TestCase):

    def setUp(self):
        self.engine = create_fixtures()
        self.config = Config({})
        self.config._engine = self.engine
        self.config.setup()
        spec = load_config(os.path.join(FIXTURE_PATH, 'spec.yaml'))
        self.spec = Spec(self.config, spec)
        self.spec._engine = self.engine
        self.mapper = Mapper(self.config, self.spec)
        self.gen = self.mapper.generator

    def test_create_source(self):
        source = {'slug': 'foo', 'title': 'Foo source', 'url': 'http://foo'}
        Source.ensure(source)
        session.commit()
        cnt = session.query(Source).count()
        assert cnt == 1, cnt
        Source.ensure(source)
        session.commit()
        cnt = session.query(Source).count()
        assert cnt == 1, cnt

    def test_create_object(self):
        schema = self.spec.get('mappings').get('companies').get('schema')
        self.config.add_schema(schema)
        entity = {'id': 'foo', 'name': 'Foo entity'}
        assert len(self.config.types) == 0, len(self.config.types)
        self.config.entities.save(schema['id'], entity, 'foo_source')
        assert len(self.config.types) == 1, len(self.config.types)
Example #2
0
class MapperTestCase(TestCase):

    def setUp(self):
        self.engine = create_fixtures()
        self.config = Config({})
        self.config._engine = self.engine
        self.config.setup()
        spec = load_config(os.path.join(FIXTURE_PATH, 'spec.yaml'))
        self.spec = Spec(self.config, spec)
        self.spec._engine = self.engine
        self.mapper = Mapper(self.config, self.spec)
        self.gen = self.mapper.generator

    def tearDown(self):
        pass

    @raises(SpecException)
    def test_invalid_table(self):
        self.gen.get_column('financials_xxx.price')

    @raises(SpecException)
    def test_no_table(self):
        self.gen.get_column('price')

    @raises(SpecException)
    def test_invalid_column(self):
        self.gen.get_column('fin.value')

    @raises(SpecException)
    def test_unaliased_table(self):
        self.gen.get_column('financials.value')

    @raises(ConfigException)
    def test_invalid_output(self):
        for x in self.gen.generate('knuffels'):
            pass

    def test_generate_records(self):
        comps = [e for e in self.gen.generate('companies')]
        assert 'companies.symbol' in comps[0], comps[0]
        assert 'fin' not in comps[0], comps[0]
        assert len(comps) == 496, len(comps)

    def test_mapping_records(self):
        for stmt in self.mapper.records('companies'):
            assert 'companies.symbol' not in stmt
            (s, p, o, t) = stmt
            assert s is not None

    def test_map_objects(self):
        self.mapper.map()
        subject = 'sp500:MMM:MMM'
        data = self.config.entities.get(subject)
        assert '$schema' in data, data
        assert data['name'] == '3M Co', data
        assert len(self.config.types) > 900, len(self.config.types)
Example #3
0
class ConfigTestCase(TestCase):

    def setUp(self):
        self.engine = create_fixtures()
        self.config = Config({})
        self.config._engine = self.engine
        self.config.setup()
        spec = load_config(os.path.join(FIXTURE_PATH, 'spec.yaml'))
        self.spec = Spec(self.config, spec)
        self.spec._engine = self.engine
        self.gen = Generator(self.spec)

    def tearDown(self):
        pass

    def test_load_spec(self):
        assert len(self.gen.tables) == 2
        col = self.gen.get_column('fin.price')
        assert col is not None, col

    def test_make_alias(self):
        alias = self.config.get_alias('http://occrp.org/foo/bar.json#xxx')
        assert alias == 'bar', alias
        again = self.config.get_alias('http://occrp.org/foo/bar.json#xxx')
        assert again == alias, again

    def test_expand_envvars(self):
        config = Config({'foo': '$PATH'})
        assert '$PATH' not in config.get('foo'), config.get('foo')
        assert len(config.get('foo')), config.get('foo')

    @raises(ConfigException)
    def test_invalid_alias(self):
        self.config.get_alias('http://occrp.org/foo/bar.json#xxx')
        self.config.get_alias('http://foo.org/xxx/bar.json')
Example #4
0
def create_app(config={}):
    app = Flask('spindle')
    app.config.from_object(default_settings)
    app.config.from_envvar('SPINDLE_SETTINGS', silent=True)
    app.config.update(config)

    config = {
        'schemas': app.config.get('SCHEMAS'),
        'database': app.config.get('DATABASE_URI'),
        'elastic_host': app.config.get('ELASTICSEARCH_HOST'),
        'elastic_index': app.config.get('ELASTICSEARCH_INDEX')
    }
    app.loom_config = Config(config)
    load_local_schema(app.loom_config.resolver)
    app.loom_config.setup()

    assets.init_app(app)
    oauth.init_app(app)

    if not app.debug and app.config.get('MAIL_ADMINS'):
        app_name = app.config.get('APP_NAME')
        credentials = app.config.get('MAIL_CREDENTIALS', ())
        mail_handler = SMTPHandler(app.config.get('MAIL_HOST'),
                                   app.config.get('MAIL_FROM'),
                                   app.config.get('MAIL_ADMINS'),
                                   '%s crash report' % app_name,
                                   credentials=credentials,
                                   secure=())
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
    return app
Example #5
0
 def setUp(self):
     self.engine = create_fixtures()
     self.config = Config({})
     self.config._engine = self.engine
     self.config.setup()
     spec = load_config(os.path.join(FIXTURE_PATH, 'spec.yaml'))
     self.spec = Spec(self.config, spec)
     self.spec._engine = self.engine
     self.gen = Generator(self.spec)
Example #6
0
 def test_expand_envvars(self):
     config = Config({'foo': '$PATH'})
     assert '$PATH' not in config.get('foo'), config.get('foo')
     assert len(config.get('foo')), config.get('foo')