Example #1
0
    def initialize(self) -> None:

        if self.app:
            with self.app.app_context():
                try:
                    remove_all_labels()
                # With Neo4j 4.3 remove all labels on empty DB started to fail with:
                # [...]
                #   File "/usr/local/lib/python3.9/dist-packages/neomodel/core.py", ...
                #                                           ... line 62, in drop_indexes
                #     index[7][0], index[8][0]))
                # IndexError: list index out of range
                # Maybe that a future release of neomdel will fix the issue
                # and the try/except will be no longer needed
                # It maily fails on NIG when executing init_hpo.sh
                except IndexError as e:  # pragma: no cover
                    log.warning("Can't remove label, is database empty?")
                    log.error(e)

                # install_all_labels can fail when models are cross-referenced between
                # core and custom. For example:
                # neo4j.exceptions.ClientError:
                #     {code: Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists}
                #     {message: An equivalent constraint already exists,
                #         'Constraint( type='UNIQUENESS', schema=(:XYZ {uuid}), [...]
                # This loop with install_labels prevent errors
                for model in self._models.values():
                    install_labels(model, quiet=False)
def test_install_all():
    install_labels(AbstractNode)
    # run install all labels
    install_all_labels()
    assert True
    # remove constraint for above test
    db.cypher_query("DROP CONSTRAINT on (n:NoConstraintsSetup) ASSERT n.name IS UNIQUE")
def test_install_all():
    install_labels(TestAbstractNode)
    # run install all labels
    install_all_labels()
    assert True
    # remove constraint for above test
    db.cypher_query("DROP CONSTRAINT on (n:NoConstraintsSetup) ASSERT n.name IS UNIQUE")
Example #4
0
def test_install_labels_db_property():
    stdout = StringIO()
    install_labels(SomeNotUniqueNode, quiet=False, stdout=stdout)
    assert 'id' in stdout.getvalue()
    # make sure that the id_ constraint doesn't exist
    with pytest.raises(DatabaseError) as exc_info:
        db.cypher_query(
            'DROP CONSTRAINT on (n:SomeNotUniqueNode) ASSERT n.id_ IS UNIQUE')
    assert 'No such constraint' in exc_info.exconly()
    # make sure the id constraint exists and can be removed
    db.cypher_query('DROP CONSTRAINT on (n:SomeNotUniqueNode) ASSERT n.id IS UNIQUE')
def test_install_labels_db_property():
    class SomeNode(StructuredNode):
        id_ = UniqueIdProperty(db_property='id')
    stdout = StringIO()
    install_labels(SomeNode, quiet=False, stdout=stdout)
    assert 'id' in stdout.getvalue()
    # make sure that the id_ constraint doesn't exist
    with pytest.raises(DatabaseError) as exc_info:
        db.cypher_query(
            'DROP CONSTRAINT on (n:SomeNode) ASSERT n.id_ IS UNIQUE')
    assert 'No such constraint' in exc_info.exconly()
    # make sure the id constraint exists and can be removed
    db.cypher_query('DROP CONSTRAINT on (n:SomeNode) ASSERT n.id IS UNIQUE')
Example #6
0
def post_migrate_handler(sender, **kwargs):
    """
    Create the meta graph after migrations has been installed.
    """
    if settings.ENABLED:
        for model in sender.models.values():
            install_labels(get_node_class_for_model(model),
                           quiet=False,
                           stdout=sys.stdout)
            install_labels(get_meta_node_class_for_model(model),
                           quiet=False,
                           stdout=sys.stdout)

            get_meta_node_for_model(model).sync(
                max_depth=settings.MAX_CONNECTION_DEPTH, update_existing=True)
Example #7
0
    def initialize(self) -> None:

        if self.app:
            with self.app.app_context():
                remove_all_labels()
                # install_all_labels()

                # install_all_labels can fail when models are cross-referenced between
                # core and custom. For example:
                # neo4j.exceptions.ClientError:
                #     {code: Neo.ClientError.Schema.EquivalentSchemaRuleAlreadyExists}
                #     {message: An equivalent constraint already exists,
                #         'Constraint( type='UNIQUENESS', schema=(:XYZ {uuid}), [...]
                # This loop with install_labels prevent errors
                for model in self._models.values():
                    install_labels(model, quiet=False)
Example #8
0
    def traverse(self, default_screen_name='letitbe_or_not'):

        install_labels(schema.User)

        if self.q.size > 0:
            screen_name = self.q.get()
        else:
            screen_name = default_screen_name

        while True:

            try:
                user = self.api.get_user(screen_name)
                user_node = self.save_user(user)

                for page in tweepy.Cursor(self.api.friends,
                                          id=user.id).pages():
                    for friend in page:
                        self.q.put(friend.screen_name)
                        friend_node = self.save_user(friend)
                        friend_node.is_followed_from.connect(user_node)
                    time.sleep(self.sleep_time)

                # for page in tweepy.Cursor(self.api.followers, id=user.id).pages():
                #     for follower in page:
                #         self.q.put(follower.screen_name)
                #         follower_node = self.save_user(follower)
                #         user_node.is_followed_from.connect(follower_node)
                #     time.sleep(self.sleep_time)

                screen_name = self.q.get()

            except tweepy.error.RateLimitError as e:
                logger.warning(e)
                self.sleep_time *= 2
                time.sleep(15 * 60)
            except tweepy.error.TweepError as e:
                if e.api_code >= 400:
                    logger.error(e)
                    self.sleep_time *= 2
Example #9
0
# DB schema updated during run-server
from neomodel import install_labels
from neomodels.NeoModels import DatasetModel, LicenseModel

install_labels(DatasetModel)
install_labels(LicenseModel)
Example #10
0
def test_install_label_twice():
    install_labels(AbstractNode)
    install_labels(AbstractNode)