diff options
| author | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-14 10:59:07 -0700 | 
|---|---|---|
| committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-04-14 10:59:17 -0700 | 
| commit | 8aeaee2a0a8eead6a1aad4c0c05ec67a636f202f (patch) | |
| tree | 24c528a6451e22665dc3f88df495d2e3ad2bc381 | |
| parent | a1d236466a471aa8d43740e4bcd6aa686faaa17d (diff) | |
| download | fatcat-8aeaee2a0a8eead6a1aad4c0c05ec67a636f202f.tar.gz fatcat-8aeaee2a0a8eead6a1aad4c0c05ec67a636f202f.zip | |
refactor dummy generation
| -rw-r--r-- | fatcat/__init__.py | 25 | ||||
| -rw-r--r-- | fatcat/api.py | 2 | ||||
| -rw-r--r-- | fatcat/dummy.py | 135 | ||||
| -rw-r--r-- | fatcat/models.py | 6 | ||||
| -rw-r--r-- | fatcat/routes.py | 4 | ||||
| -rw-r--r-- | fatcat/sql.py | 129 | ||||
| -rw-r--r-- | tests/test_backend.py | 14 | 
7 files changed, 152 insertions, 163 deletions
| diff --git a/fatcat/__init__.py b/fatcat/__init__.py index d8494a19..b3a82a0f 100644 --- a/fatcat/__init__.py +++ b/fatcat/__init__.py @@ -7,27 +7,4 @@ app = Flask(__name__)  app.config.from_object(Config)  db = SQLAlchemy(app) -examples = { -    "work": { -        "id": "rzga5b9cd7efgh04iljk", -        "rev": "12345", -        "redirect_id": None, -        "edit_id": None, -        "extra_json": None, -        "title": "Structure and Interpretation", -        "work_type": "journal-article", -        "contributors": [ -            {"name": "Alyssa P. Hacker"}, -        ], -        "primary": { -            "title": "Structure and Interpretation", -            "release_type": "online", -            "date": "2000-01-01", -            "doi": "10.491/599.sdo14", -        }, -        "releases": [ -        ], -    }, -} - -from fatcat import routes, models, api, sql +from fatcat import routes, models, api, sql, dummy diff --git a/fatcat/api.py b/fatcat/api.py index c62d14a2..175d4d82 100644 --- a/fatcat/api.py +++ b/fatcat/api.py @@ -1,7 +1,7 @@  from flask import Flask, render_template, send_from_directory, request, \      url_for, abort, g, redirect, jsonify -from fatcat import app, db, examples +from fatcat import app, db  from fatcat.models import *  from fatcat.sql import * diff --git a/fatcat/dummy.py b/fatcat/dummy.py new file mode 100644 index 00000000..b547cf16 --- /dev/null +++ b/fatcat/dummy.py @@ -0,0 +1,135 @@ + +import random +import hashlib +from fatcat import db +from fatcat.models import * + +def insert_example_works(): +    """ +    TODO: doesn't create an edit trail (yet) +    """ + +    n_elkies = CreatorRev( +        name="Noam D. Elkies", +        sortname="Elkies, N", +        orcid=None) +    n_elkies_id = CreatorIdent(revision=n_elkies) +    pi_work = WorkRev( +        title="Why is π^2 so close to 10?", +        work_type="journal-article") +    pi_work_id = WorkIdent(revision=pi_work) +    pi_release = ReleaseRev( +        title=pi_work.title, +        work_ident_id=pi_work.id, +        release_type="journal-article") +    pi_contrib = ReleaseContrib(creator=n_elkies_id) +    pi_release.creators.append(pi_contrib) +    pi_release_id = ReleaseIdent(revision=pi_release) +    pi_work.primary_release = pi_release + +    # TODO: +    #pi_file = File( +    #    sha1="efee52e46c86691e2b892dbeb212f3b92e92e9d3", +    #    url="http://www.math.harvard.edu/~elkies/Misc/pi10.pdf") +    db.session.add_all([n_elkies, n_elkies_id, pi_work, pi_work_id, pi_release, +        pi_release_id]) + +    # TODO: +    #ligo_collab = CreatorRev(name="LIGO Scientific Collaboration") +    #ligo_paper = ReleaseRev( +    #    title="Full Band All-sky Search for Periodic Gravitational Waves in the O1 LIGO Data") +    db.session.commit() + + +def insert_random_works(count=100): +    """ +    TODO: doesn't create an edit trail (yet) +    """ + +    first_names = ("Sarah", "Robin", "Halko", "Jefferson", "Max", "桃井", +        "Koizumi", "Rex", "Billie", "Tenzin") +    last_names = ("Headroom", "はるこ", "Jun'ichirō", "Wong", "Smith") + +    author_revs = [] +    author_ids = [] +    for _ in range(count): +        first = random.choice(first_names) +        last = random.choice(last_names) +        ar = CreatorRev( +            name="{} {}".format(first, last), +            sortname="{}, {}".format(last, first[0]), +            orcid=None) +        author_revs.append(ar) +        author_ids.append(CreatorIdent(revision=ar)) + +    container_revs = [] +    container_ids = [] +    for _ in range(5): +        cr = ContainerRev( +            name="The Fake Journal of Stuff", +            #container_id=None, +            publisher="Big Paper", +            sortname="Fake Journal of Stuff", +            issn="1234-5678") +        container_revs.append(cr) +        container_ids.append(ContainerIdent(revision=cr)) + +    title_start = ("All about ", "When I grow up I want to be", +        "The final word on", "Infinity: ", "The end of") +    title_ends = ("Humankind", "Bees", "Democracy", "Avocados", "«küßî»", "“ЌύБЇ”") +    work_revs = [] +    work_ids = [] +    release_revs = [] +    release_ids = [] +    file_revs = [] +    file_ids = [] +    for _ in range(count): +        title = "{} {}".format(random.choice(title_start), random.choice(title_ends)) +        work = WorkRev(title=title) +        work_id = WorkIdent(revision=work) +        authors = set(random.sample(author_ids, 5)) +        release = ReleaseRev( +            title=work.title, +            creators=[ReleaseContrib(creator=a) for a in list(authors)], +            #work=work, +            container=random.choice(container_ids)) +        release_id = ReleaseIdent(revision=release) +        work.primary_release = release +        authors.add(random.choice(author_ids)) +        release2 = ReleaseRev( +            title=work.title + " (again)", +            creators=[ReleaseContrib(creator=a) for a in list(authors)], +            #work=work, +            container=random.choice(container_ids)) +        release_id2 = ReleaseIdent(revision=release2) +        work_revs.append(work) +        work_ids.append(work_id) +        release_revs.append(release) +        release_revs.append(release2) +        release_ids.append(release_id) +        release_ids.append(release_id2) + +        file_content = str(random.random()) * random.randint(3,100) +        file_sha = hashlib.sha1(file_content.encode('utf-8')).hexdigest() +        file_rev = FileRev( +            sha1=file_sha, +            size=len(file_content), +            url="http://archive.invalid/{}".format(file_sha), +            releases=[FileRelease(release=release_id), FileRelease(release=release_id2)], +        ) +        file_id = FileIdent(revision=file_rev) +        file_revs.append(file_rev) +        file_ids.append(file_id) + +    db.session.add_all(author_revs) +    db.session.add_all(author_ids) +    db.session.add_all(work_revs) +    db.session.add_all(work_ids) +    db.session.add_all(release_revs) +    db.session.add_all(release_ids) +    db.session.add_all(container_revs) +    db.session.add_all(container_ids) +    db.session.add_all(file_revs) +    db.session.add_all(file_ids) + +    db.session.commit() diff --git a/fatcat/models.py b/fatcat/models.py index 43e0bfea..ce7e2e13 100644 --- a/fatcat/models.py +++ b/fatcat/models.py @@ -225,15 +225,15 @@ class Editor(db.Model):      username = db.Column(db.String)      group = db.Column(db.String) - -### Other ################################################################### -  class ChangelogEntry(db.Model):      __tablename__= 'changelog'      id = db.Column(db.Integer, primary_key=True, autoincrement=True)      edit_group_id = db.Column(db.ForeignKey('edit_group.id'))      timestamp = db.Column(db.Integer) + +### Other ################################################################### +  class ExtraJson(db.Model):      __tablename__ = 'extra_json'      sha1 = db.Column(db.String, primary_key=True) diff --git a/fatcat/routes.py b/fatcat/routes.py index c59922b3..3c1822ce 100644 --- a/fatcat/routes.py +++ b/fatcat/routes.py @@ -2,7 +2,7 @@  import os  from flask import Flask, render_template, send_from_directory, request, \      url_for, abort, g, redirect, jsonify -from fatcat import app, db, examples +from fatcat import app, db  ### Views ################################################################### @@ -13,7 +13,7 @@ def work_create():  @app.route('/work/random', methods=['GET'])  def work_random(): -    work = examples['work'] +    work = {} # XXX      return render_template('work_view.html', work=work, primary=work['primary'])  @app.route('/work/random', methods=['GET']) diff --git a/fatcat/sql.py b/fatcat/sql.py index 45980925..c38e6f0e 100644 --- a/fatcat/sql.py +++ b/fatcat/sql.py @@ -6,134 +6,7 @@ from fatcat import db  from fatcat.models import *  def populate_db(): -    """ -    TODO: doesn't create an edit trail (yet) -    """ - -    n_elkies = CreatorRev( -        name="Noam D. Elkies", -        sortname="Elkies, N", -        orcid=None) -    n_elkies_id = CreatorIdent(revision=n_elkies) -    pi_work = WorkRev( -        title="Why is π^2 so close to 10?", -        work_type="journal-article") -    pi_work_id = WorkIdent(revision=pi_work) -    pi_release = ReleaseRev( -        title=pi_work.title, -        work_ident_id=pi_work.id, -        release_type="journal-article") -    pi_contrib = ReleaseContrib(creator=n_elkies_id) -    pi_release.creators.append(pi_contrib) -    pi_release_id = ReleaseIdent(revision=pi_release) -    pi_work.primary_release = pi_release - -    # TODO: -    #pi_file = File( -    #    sha1="efee52e46c86691e2b892dbeb212f3b92e92e9d3", -    #    url="http://www.math.harvard.edu/~elkies/Misc/pi10.pdf") -    db.session.add_all([n_elkies, n_elkies_id, pi_work, pi_work_id, pi_release, -        pi_release_id]) - -    # TODO: -    #ligo_collab = CreatorRev(name="LIGO Scientific Collaboration") -    #ligo_paper = ReleaseRev( -    #    title="Full Band All-sky Search for Periodic Gravitational Waves in the O1 LIGO Data") -    db.session.commit() - - -def populate_complex_db(count=100): -    """ -    TODO: doesn't create an edit trail (yet) -    """ - -    first_names = ("Sarah", "Robin", "Halko", "Jefferson", "Max", "桃井", -        "Koizumi", "Rex", "Billie", "Tenzin") -    last_names = ("Headroom", "はるこ", "Jun'ichirō", "Wong", "Smith") - -    author_revs = [] -    author_ids = [] -    for _ in range(count): -        first = random.choice(first_names) -        last = random.choice(last_names) -        ar = CreatorRev( -            name="{} {}".format(first, last), -            sortname="{}, {}".format(last, first[0]), -            orcid=None) -        author_revs.append(ar) -        author_ids.append(CreatorIdent(revision=ar)) - -    container_revs = [] -    container_ids = [] -    for _ in range(5): -        cr = ContainerRev( -            name="The Fake Journal of Stuff", -            #container_id=None, -            publisher="Big Paper", -            sortname="Fake Journal of Stuff", -            issn="1234-5678") -        container_revs.append(cr) -        container_ids.append(ContainerIdent(revision=cr)) - -    title_start = ("All about ", "When I grow up I want to be", -        "The final word on", "Infinity: ", "The end of") -    title_ends = ("Humankind", "Bees", "Democracy", "Avocados", "«küßî»", "“ЌύБЇ”") -    work_revs = [] -    work_ids = [] -    release_revs = [] -    release_ids = [] -    file_revs = [] -    file_ids = [] -    for _ in range(count): -        title = "{} {}".format(random.choice(title_start), random.choice(title_ends)) -        work = WorkRev(title=title) -        work_id = WorkIdent(revision=work) -        authors = set(random.sample(author_ids, 5)) -        release = ReleaseRev( -            title=work.title, -            creators=[ReleaseContrib(creator=a) for a in list(authors)], -            #work=work, -            container=random.choice(container_ids)) -        release_id = ReleaseIdent(revision=release) -        work.primary_release = release -        authors.add(random.choice(author_ids)) -        release2 = ReleaseRev( -            title=work.title + " (again)", -            creators=[ReleaseContrib(creator=a) for a in list(authors)], -            #work=work, -            container=random.choice(container_ids)) -        release_id2 = ReleaseIdent(revision=release2) -        work_revs.append(work) -        work_ids.append(work_id) -        release_revs.append(release) -        release_revs.append(release2) -        release_ids.append(release_id) -        release_ids.append(release_id2) - -        file_content = str(random.random()) * random.randint(3,100) -        file_sha = hashlib.sha1(file_content.encode('utf-8')).hexdigest() -        file_rev = FileRev( -            sha1=file_sha, -            size=len(file_content), -            url="http://archive.invalid/{}".format(file_sha), -            releases=[FileRelease(release=release_id), FileRelease(release=release_id2)], -        ) -        file_id = FileIdent(revision=file_rev) -        file_revs.append(file_rev) -        file_ids.append(file_id) - -    db.session.add_all(author_revs) -    db.session.add_all(author_ids) -    db.session.add_all(work_revs) -    db.session.add_all(work_ids) -    db.session.add_all(release_revs) -    db.session.add_all(release_ids) -    db.session.add_all(container_revs) -    db.session.add_all(container_ids) -    db.session.add_all(file_revs) -    db.session.add_all(file_ids) - -    db.session.commit() +    pass  def add_crossref(meta): diff --git a/tests/test_backend.py b/tests/test_backend.py index 93e3f11a..7a139ef7 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -28,6 +28,7 @@ class FatcatTestCase(unittest.TestCase):          fatcat.db.session.remove()          fatcat.db.drop_all()          fatcat.db.create_all() +        fatcat.sql.populate_db()      def test_health(self):          rv = self.app.get('/health') @@ -35,7 +36,7 @@ class FatcatTestCase(unittest.TestCase):          assert obj['ok']      def test_works(self): -        fatcat.sql.populate_db() +        fatcat.dummy.insert_example_works()          # Invalid Id          rv = self.app.get('/v0/work/_') @@ -61,8 +62,11 @@ class FatcatTestCase(unittest.TestCase):      def test_populate(self):          fatcat.sql.populate_db() -    def test_populate_complex(self): -        fatcat.sql.populate_complex_db() +    def test_example_works(self): +        fatcat.dummy.insert_example_works() + +    def test_random_works(self): +        fatcat.dummy.insert_random_works()      def test_load_crossref(self):          with open('./tests/files/crossref-works.2018-01-21.badsample.json', 'r') as f: @@ -71,9 +75,9 @@ class FatcatTestCase(unittest.TestCase):              fatcat.sql.add_crossref(obj)      def test_hydrate_work(self): -        fatcat.sql.populate_complex_db() +        fatcat.dummy.insert_random_works()          fatcat.sql.hydrate_work(1)      def test_hydrate_release(self): -        fatcat.sql.populate_complex_db() +        fatcat.dummy.insert_random_works()          fatcat.sql.hydrate_release(1) | 
