Exemple #1
0
def test_delete(db):
    """Test delete."""
    fl = Template.create('FL', '{year}: File {counter}')

    fl15 = Sequence(fl, year=2015)
    assert list(islice(fl15, 20))
    assert fl15.next() == '2015: File 20'

    fl16 = Sequence(fl, year=2016)
    assert list(islice(fl16, 30))
    assert fl16.next() == '2016: File 30'

    fl17 = Sequence(fl, year=2017)
    assert fl17.next() == '2017: File 0'

    assert db.session.query(Counter).count() == 3

    db.session.delete(fl17.counter)
    assert db.session.query(Counter).count() == 2

    db.session.delete(fl.model)
    db.session.commit()

    with pytest.raises(SequenceNotFound):
        Sequence('FL', year=2016)
    assert db.session.query(Counter).count() == 0
Exemple #2
0
def test_iter(db):
    """Test simple files use-case."""
    fl = Sequence(Template.create('FL', 'File {counter}'))
    assert list(islice(fl, 10)) == ['File {}'.format(c) for c in range(0, 10)]

    y = Sequence(Template.create('Y', '{year}: {counter}'), year=2016)
    assert list(islice(y, 10)) == ['2016: {}'.format(c) for c in range(0, 10)]
Exemple #3
0
def test_inspire_authors(db):
    """Test INSPIRE authors use-case."""
    auth = Template.create('AUTH', '{author}.{counter}')
    john = Sequence(auth, author='John.Tester')
    tom = Sequence(auth, author='Tom.Phake')

    assert john.next() == 'John.Tester.0'
    assert john.next() == 'John.Tester.1'
    assert john.next() == 'John.Tester.2'
    assert tom.next() == 'Tom.Phake.0'
    assert tom.next() == 'Tom.Phake.1'
    assert john.next() == 'John.Tester.3'
    assert tom.next() == 'Tom.Phake.2'
    assert tom.next() == 'Tom.Phake.3'
Exemple #4
0
def test_formatting(db):
    """Test counter formatting options."""
    seq1 = Sequence(Template.create('P', 'P-{counter:03d}'))
    assert seq1.next() == 'P-000'
    assert seq1.next() == 'P-001'
    assert seq1.next() == 'P-002'

    seq2 = Template.create('C', '{P}-{counter:04d}')
    p1 = Sequence(seq2, P='P-000')
    assert p1.next() == 'P-000-0000'
    assert p1.next() == 'P-000-0001'
    assert p1.next() == 'P-000-0002'
    p2 = Sequence(seq2, P='P-001')
    assert p2.next() == 'P-001-0000'
    assert p2.next() == 'P-001-0001'
def sequences():
    """Load test data fixture."""
    pl = Template.create('PL', '{year}: Playlist {counter}', start=1)
    fl = Template.create('FL', '{PL} > Audio File {counter:02d}', start=1)
    pl15 = Sequence(pl, year=2015)
    pl15.next()
    pl15.next()
    pl16 = Sequence(pl, year=2016)
    pl16.next()
    fl15 = Sequence(fl, PL='2015: Playlist 2')
    fl15.next()
    fl15.next()
    fl16 = Sequence(fl, PL='2016: Playlist 1')
    fl16.next()
    db.session.commit()
Exemple #6
0
def test_cern_articles(db):
    """Test CERN articles use-case."""
    with pytest.raises(SequenceNotFound):
        Sequence('ART')

    articles = Template.create('ART', 'CERN-ARTICLE-{category}-{counter}')
    journals = Sequence(articles, category='JOURNALS')
    theses = Sequence(articles, category='THESES')

    assert journals.next() == 'CERN-ARTICLE-JOURNALS-0'
    assert journals.next() == 'CERN-ARTICLE-JOURNALS-1'
    assert journals.next() == 'CERN-ARTICLE-JOURNALS-2'
    assert theses.next() == 'CERN-ARTICLE-THESES-0'
    assert theses.next() == 'CERN-ARTICLE-THESES-1'
    assert journals.next() == 'CERN-ARTICLE-JOURNALS-3'
    assert theses.next() == 'CERN-ARTICLE-THESES-2'
def get_next(template_name, keywords):
    """Get the next counter for a specific sequence."""
    next_counter = Sequence(
        template_name,
        **{k: v
           for k, v in map(lambda arg: arg.split('='), keywords)}).next()
    db.session.commit()
    click.echo(next_counter)
Exemple #8
0
def test_reset(db):
    """Test reset sequence."""
    y = Template.create('FL', '{year}: File {counter}')
    y15 = Sequence(y, year=2015)
    y16 = Sequence(y, year=2016)

    assert y15.next() == '2015: File 0'
    assert y15.next() == '2015: File 1'

    assert y16.next() == '2016: File 0'
    assert y16.next() == '2016: File 1'

    y15.counter.reset()
    assert y15.next() == '2015: File 0'
    assert y15.next() == '2015: File 1'
    y16.counter.reset(start=156)
    assert y16.next() == '2016: File 156'
    assert y16.next() == '2016: File 157'
Exemple #9
0
def test_cern_playlists(db):
    """Test CERN playlists use-case."""

    pl = Template.create('PL', 'C-{year}-{counter}')
    fl = Template.create('FL', '{PL}:{counter}')
    pl15 = Sequence(pl, year=2015)
    pl16 = Sequence(pl, year=2016)

    assert pl15.next() == 'C-2015-0'
    assert pl15.next() == 'C-2015-1'
    assert pl15.next() == 'C-2015-2'
    assert pl16.next() == 'C-2016-0'

    fl_15_2 = Sequence(fl, PL='C-2015-1')
    fl_16_1 = Sequence(fl, PL='C-2016-0')
    assert fl_15_2.next() == 'C-2015-1:0'
    assert fl_15_2.next() == 'C-2015-1:1'
    assert fl_15_2.next() == 'C-2015-1:2'
    assert fl_16_1.next() == 'C-2016-0:0'
Exemple #10
0
    def get_report_number_sequence(self, **kwargs):
        """Get the sequence generator for Projects."""
        try:
            year = arrow.get(self['date']).year
        except KeyError:
            year = datetime.date.today().year

        return Sequence(self.sequence_name,
                        year=year,
                        category=self['category'],
                        type=self['type']), kwargs
Exemple #11
0
def test_repr(db):
    """Test __repr__."""
    template = Template.create('A', '{counter}')
    assert repr(
        template.model) == ("TemplateDefinition(name='A', "
                            "meta_template='{counter}', start=0, step=1)")
    sequence = Sequence(template)
    assert repr(sequence.counter) == (
        "Counter(template_instance='{counter}', "
        "definition_name='A', counter=0, template_definition="
        "TemplateDefinition(name='A', meta_template='{counter}', "
        "start=0, step=1))")
Exemple #12
0
def test_exceptions(db):
    """Test exceptions."""

    p1 = Sequence(Template.create('P1', 'P-{counter}'))
    assert p1.next() == 'P-0'
    Template.create('P2', 'P:{counter}')

    with pytest.raises(InvalidTemplate) as exc:
        Template.create('invalid', '{P1}-{P2}-{counter}')
    assert 'More than 1 parents in template' in str(exc)

    with pytest.raises(InvalidTemplate) as exc:
        Template.create('invalid', 'INVALID')
    assert 'No counter placeholder' in str(exc)

    assert Sequence('P1')
    with pytest.raises(SequenceNotFound):
        Sequence('invalid')

    seq = Template.create('C1', '{P1}:{counter}')
    seq1 = Sequence(seq, P1='P-0')
    assert seq1.next() == 'P-0:0'
    with pytest.raises(KeyError) as exc:
        Sequence(seq).next()
    assert 'P1' in str(exc)

    with pytest.raises(InvalidResetCall) as exc:
        Sequence('P1').counter.reset()
    assert 'Cannot reset sequence: children exist' in str(exc)
Exemple #13
0
def test_step(db):
    """Test larger incremental steps."""
    fl = Template.create('FL', 'File {counter}', start=0, step=100)
    seq = Sequence(fl)

    assert seq.next() == 'File 0'
    assert seq.next() == 'File 100'
    assert seq.next() == 'File 200'
    assert seq.next() == 'File 300'
    seq.counter.reset(start=15)
    assert seq.next() == 'File 15'
    assert seq.next() == 'File 115'
    assert seq.next() == 'File 215'
    assert seq.next() == 'File 315'
Exemple #14
0
def test_get(db):
    """Test get sequence."""
    Template.create('A', 'A-{counter}')
    Template.create('B', 'B-{counter}')

    assert Sequence('A')
    assert Sequence('A').next() == 'A-0'
    assert Sequence('A').next() == 'A-1'

    assert Sequence('B')
    assert Sequence('B').next() == 'B-0'
    assert Sequence('B').next() == 'B-1'
Exemple #15
0
def test_admin(app, db):
    """Test admin interface."""
    assert isinstance(ca, dict)
    assert isinstance(ta, dict)

    assert 'model' in ca
    assert 'modelview' in ca
    assert 'model' in ta
    assert 'modelview' in ta

    # Create admin
    admin = Admin(app, name='Example: Sequence Generator')

    # Add views
    admin.add_view(ta['modelview'](ta['model'], db.session))
    admin.add_view(ca['modelview'](ca['model'], db.session))

    with app.app_context():
        # Create test data
        seq = Sequence(Template.create('ID', 'File {counter}'))
        assert seq.next() == 'File 0'
        assert seq.next() == 'File 1'
        assert seq.next() == 'File 2'
        db.session.commit()

        with app.test_request_context():
            request_url = url_for('counter.reset_view')
        with app.test_client() as client:
            # Reset counter
            client.post(request_url,
                        data={
                            'start': 0,
                            'rowid': 'File {counter}'
                        },
                        follow_redirects=False)

        # Assert that reset was successful
        assert seq.next() == 'File 0'
Exemple #16
0
 def get_report_number_sequence(self, **kwargs):
     """Get the sequence generator for Videos."""
     assert 'parent_report_number' in kwargs
     parent_rn = kwargs.pop('parent_report_number')
     parent_name = self.project.sequence_name
     return Sequence(self.sequence_name, **{parent_name: parent_rn}), kwargs
Exemple #17
0
def test_py3_next(db):
    """Test PY3 __next__."""
    assert Sequence(Template.create('PY3', '{counter}')).__next__() == '0'
Exemple #18
0
 def update_counter(next_counter, **sequence_kwargs):
     """Update sequence counter."""
     counter = Sequence(**sequence_kwargs).counter
     counter.counter = next_counter
     db.session.add(counter)
     return counter