Exemplo n.º 1
0
 def test_add_single_synonym(self):
     synonym_key = "synonym_key_1"
     TaxonomyManager.register_synonym_key("synonym_key_1")
     data = [
         {
             'id': 0,
             'full_name': 'Family One',
             'rank_name': 'One',
             'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA,
             'parent_id': None,
             'synonyms': {},
             'mptt_left': 0,
             'mptt_right': 0,
             'mptt_tree_id': 0,
             'mptt_depth': 0,
         },
     ]
     ins = niamoto_db_meta.taxon.insert().values(data)
     with Connector.get_connection() as connection:
         connection.execute(ins)
     TaxonomyManager.add_synonym_for_single_taxon(0, synonym_key, 1)
     df1 = TaxonomyManager.get_raw_taxon_dataframe()
     self.assertEqual(df1.loc[0]['synonyms'], {synonym_key: 1})
     TaxonomyManager.add_synonym_for_single_taxon(0, synonym_key, 2)
     df2 = TaxonomyManager.get_raw_taxon_dataframe()
     self.assertEqual(df2.loc[0]['synonyms'], {
         synonym_key: 1,
         synonym_key: 2,
     })
Exemplo n.º 2
0
 def test_get_synonym_key(self):
     self.assertRaises(NoRecordFoundError, TaxonomyManager.get_synonym_key,
                       'Not existing')
     TaxonomyManager.register_synonym_key("test")
     TaxonomyManager.get_synonym_key("test")
     with Connector.get_connection() as connection:
         TaxonomyManager.get_synonym_key("test", bind=connection)
 def test_base_data_provider(self):
     TestDataProvider.register_data_provider('test_data_provider_1')
     test_data_provider = TestDataProvider('test_data_provider_1')
     self.assertIsNotNone(test_data_provider)
     self.assertIsNotNone(test_data_provider.db_id)
     # Test with synonym key
     TaxonomyManager.register_synonym_key("synonym")
     TestDataProvider.register_data_provider(
         'test_data_provider_2',
         synonym_key="synonym"
     )
 def test_update_data_provider(self):
     add_data_provider(
         "pl@ntnote_provider_1",
         "PLANTNOTE",
     )
     TaxonomyManager.register_synonym_key("YO")
     update_data_provider(
         "pl@ntnote_provider_1",
         new_name="pl@ntnote",
         synonym_key="YO"
     )
     l2 = get_data_provider_list()
     self.assertIn("pl@ntnote", list(l2['name']))
     self.assertIn("YO", list(l2['synonym_key']))
Exemplo n.º 5
0
 def test_register_unregister_synonym_key(self):
     TaxonomyManager.register_synonym_key("synonym_key_1")
     self.assertRaises(RecordAlreadyExistsError,
                       TaxonomyManager.register_synonym_key,
                       "synonym_key_1")
     df = TaxonomyManager.get_synonym_keys()
     self.assertEqual(len(df), 1)
     TaxonomyManager.unregister_synonym_key("synonym_key_1")
     self.assertRaises(NoRecordFoundError,
                       TaxonomyManager.unregister_synonym_key,
                       "synonym_key")
     df = TaxonomyManager.get_synonym_keys()
     self.assertEqual(len(df), 0)
     # Test with bind
     with Connector.get_connection() as connection:
         TaxonomyManager.register_synonym_key("test", bind=connection)
         TaxonomyManager.assert_synonym_key_exists("test", bind=connection)
         TaxonomyManager.unregister_synonym_key("test", bind=connection)
         TaxonomyManager.assert_synonym_key_does_not_exists("test",
                                                            bind=connection)
Exemplo n.º 6
0
 def test_duplicate_synonym(self):
     synonym_key = "synonym_key_1"
     TaxonomyManager.register_synonym_key("synonym_key_1")
     data = [
         {
             'id': 0,
             'full_name': 'Family One',
             'rank_name': 'One',
             'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA,
             'parent_id': None,
             'synonyms': {},
             'mptt_left': 0,
             'mptt_right': 0,
             'mptt_tree_id': 0,
             'mptt_depth': 0,
         },
         {
             'id': 1,
             'full_name': 'Family Two',
             'rank_name': 'Two',
             'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA,
             'parent_id': None,
             'synonyms': {},
             'mptt_left': 0,
             'mptt_right': 0,
             'mptt_tree_id': 0,
             'mptt_depth': 0,
         },
     ]
     ins = niamoto_db_meta.taxon.insert().values(data)
     with Connector.get_connection() as connection:
         connection.execute(ins)
     TaxonomyManager.add_synonym_for_single_taxon(0, synonym_key, 1)
     self.assertRaises(
         IntegrityError,
         TaxonomyManager.add_synonym_for_single_taxon,
         1,
         synonym_key,
         1,
     )
Exemplo n.º 7
0
 def test_get_synonyms_map(self):
     synonym_key = "synonym_key_1"
     TaxonomyManager.register_synonym_key("synonym_key_1")
     data = [
         {
             'id': 0,
             'full_name': 'Family One',
             'rank_name': 'One',
             'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA,
             'parent_id': None,
             'synonyms': {
                 synonym_key: 10,
             },
             'mptt_left': 0,
             'mptt_right': 0,
             'mptt_tree_id': 0,
             'mptt_depth': 0,
         },
         {
             'id': 1,
             'full_name': 'Family Two',
             'rank_name': 'Two',
             'rank': niamoto_db_meta.TaxonRankEnum.FAMILIA,
             'parent_id': None,
             'synonyms': {
                 synonym_key: 20,
             },
             'mptt_left': 0,
             'mptt_right': 0,
             'mptt_tree_id': 0,
             'mptt_depth': 0,
         },
     ]
     ins = niamoto_db_meta.taxon.insert().values(data)
     with Connector.get_connection() as connection:
         connection.execute(ins)
     synonyms = TaxonomyManager.get_synonyms_for_key(synonym_key)
     self.assertEqual(synonyms.loc[10], 0)
     self.assertEqual(synonyms.loc[20], 1)
def upgrade():
    op.create_table('data_provider_type',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint(
                        'name', name=op.f('uq_data_provider_type_name')),
                    schema='niamoto')
    op.create_table('taxon',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('full_name', sa.Text(), nullable=False),
                    sa.Column('rank_name', sa.Text(), nullable=False),
                    sa.Column('rank', taxon_rank_enum, nullable=False),
                    sa.Column('parent_id',
                              sa.Integer(),
                              nullable=True,
                              index=True),
                    sa.Column('synonyms', postgresql.JSONB(), nullable=False),
                    sa.Column('mptt_left', sa.Integer(), nullable=False),
                    sa.Column('mptt_right', sa.Integer(), nullable=False),
                    sa.Column('mptt_tree_id', sa.Integer(), nullable=False),
                    sa.Column('mptt_depth', sa.Integer(), nullable=False),
                    sa.CheckConstraint('mptt_depth >= 0',
                                       name=op.f('ck_taxon_mptt_depth_gt_0')),
                    sa.CheckConstraint('mptt_left >= 0',
                                       name=op.f('ck_taxon_mptt_left_gt_0')),
                    sa.CheckConstraint('mptt_right >= 0',
                                       name=op.f('ck_taxon_mptt_right_gt_0')),
                    sa.CheckConstraint(
                        'mptt_tree_id >= 0',
                        name=op.f('ck_taxon_mptt_tree_id_gt_0')),
                    sa.ForeignKeyConstraint(
                        ['parent_id'],
                        ['niamoto.taxon.id'],
                        deferrable=True,
                    ),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('full_name',
                                        name=op.f('uq_taxon_full_name')),
                    schema='niamoto')
    op.create_table('synonym_key_registry',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('date_create', sa.DateTime(), nullable=False),
                    sa.Column('date_update', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint(
                        'name', name=op.f('uq_synonym_key_registry_name')),
                    schema='niamoto')
    op.create_table('raster_registry',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('date_create', sa.DateTime(), nullable=False),
                    sa.Column('date_update', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('name',
                                        name=op.f('uq_raster_registry_name')),
                    schema='niamoto')
    op.create_table('vector_registry',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('date_create', sa.DateTime(), nullable=False),
                    sa.Column('date_update', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('name',
                                        name=op.f('uq_vector_registry_name')),
                    schema='niamoto')
    op.create_table('dimension_registry',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('dimension_key',
                              sa.String(length=100),
                              nullable=False),
                    sa.Column('date_create', sa.DateTime(), nullable=False),
                    sa.Column('date_update', sa.DateTime(), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint(
                        'name', name=op.f('uq_dimension_registry_name')),
                    schema='niamoto')
    op.create_table('data_provider',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=100), nullable=False),
                    sa.Column('provider_type_id',
                              sa.Integer(),
                              nullable=False,
                              index=True),
                    sa.Column('synonym_key_id',
                              sa.Integer(),
                              nullable=True,
                              index=True),
                    sa.Column('properties', postgresql.JSONB(),
                              nullable=False),
                    sa.Column('date_create', sa.DateTime(), nullable=False),
                    sa.Column('date_update', sa.DateTime(), nullable=True),
                    sa.Column('last_sync', sa.DateTime(), nullable=True),
                    sa.ForeignKeyConstraint(
                        ['provider_type_id'],
                        ['niamoto.data_provider_type.id'],
                    ),
                    sa.ForeignKeyConstraint(
                        ['synonym_key_id'],
                        ['niamoto.synonym_key_registry.id'],
                        ondelete='SET NULL',
                    ),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('name',
                                        name=op.f('uq_data_provider_name')),
                    schema='niamoto')
    op.create_table(
        'occurrence',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('provider_id', sa.Integer(), nullable=False, index=True),
        sa.Column('provider_pk', sa.Integer(), nullable=False, index=True),
        sa.Column(
            'location',
            geoalchemy2.types.Geometry(geometry_type='POINT', srid=4326),
        ),
        sa.Column('taxon_id', sa.Integer(), nullable=True, index=True),
        sa.Column('provider_taxon_id', sa.Integer(), nullable=True),
        sa.Column('properties', postgresql.JSONB(), nullable=False),
        sa.ForeignKeyConstraint(
            ['provider_id'],
            ['niamoto.data_provider.id'],
            onupdate='CASCADE',
            ondelete='CASCADE',
        ),
        sa.ForeignKeyConstraint(
            ['taxon_id'],
            ['niamoto.taxon.id'],
            onupdate="CASCADE",
            ondelete="SET NULL",
            deferrable=True,
        ),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint(
            'id',
            'provider_id',
            'provider_pk',
            name=op.f('uq_occurrence_id__provider_id__provider_pk')),
        schema='niamoto')
    op.create_table(
        'plot',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('provider_id', sa.Integer(), nullable=False, index=True),
        sa.Column('provider_pk', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('location',
                  geoalchemy2.types.Geometry(geometry_type='POINT', srid=4326),
                  nullable=False),
        sa.Column('properties', postgresql.JSONB(), nullable=False),
        sa.ForeignKeyConstraint(
            ['provider_id'],
            ['niamoto.data_provider.id'],
            onupdate='CASCADE',
            ondelete='CASCADE',
        ),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('id',
                            'provider_id',
                            'provider_pk',
                            name=op.f('uq_plot_id__provider_id__provider_pk')),
        sa.UniqueConstraint('name', name=op.f('uq_plot_name')),
        schema='niamoto')
    op.create_table(
        'plot_occurrence',
        sa.Column('plot_id', sa.Integer(), nullable=False, index=True),
        sa.Column('occurrence_id', sa.Integer(), nullable=False, index=True),
        sa.Column('provider_id', sa.Integer(), nullable=True, index=True),
        sa.Column('provider_plot_pk', sa.Integer(), nullable=True),
        sa.Column('provider_occurrence_pk', sa.Integer(), nullable=True),
        sa.Column('occurrence_identifier', sa.String(length=50),
                  nullable=True),
        sa.ForeignKeyConstraint(
            ['occurrence_id', 'provider_id', 'provider_occurrence_pk'],
            [
                'niamoto.occurrence.id', 'niamoto.occurrence.provider_id',
                'niamoto.occurrence.provider_pk'
            ],
            onupdate='CASCADE',
            ondelete='CASCADE',
        ),
        sa.ForeignKeyConstraint(
            ['plot_id', 'provider_id', 'provider_plot_pk'],
            [
                'niamoto.plot.id', 'niamoto.plot.provider_id',
                'niamoto.plot.provider_pk'
            ],
            onupdate='CASCADE',
            ondelete='CASCADE',
        ),
        sa.PrimaryKeyConstraint('plot_id', 'occurrence_id'),
        sa.UniqueConstraint(
            'plot_id',
            'occurrence_identifier',
            name=op.f('uq_plot_occurrence_plot_id__occurrence_identifier'),
            deferrable=True,
        ),
        schema='niamoto')
    connection = op.get_bind()
    PlantnoteDataProvider.register_data_provider_type(bind=connection, )
    CsvDataProvider.register_data_provider_type(bind=connection, )
    TaxonomyManager.register_synonym_key(
        'niamoto',
        bind=connection,
    )