Пример #1
0
def test_flush_buffer_with_dependencies(mocker):
    class DummyBackend(Backend):
        def write(self, item, objs):
            return range(len(objs))

    blueprint = Blueprint(backend=mocker.Mock(wraps=DummyBackend()))
    blueprint.add_item({'name': 'foo', 'table': 'test'})
    blueprint.add_item({
        'name': 'bar',
        'table': 'test',
        'count': {
            'number': 1,
            'by': 'foo'
        }
    })

    blueprint.add_item({
        'name': 'lol',
        'table': 'test',
        'count': {
            'number': 1,
            'by': 'bar'
        }
    })

    buffer = Buffer(blueprint)
    blueprint.items['foo'].generate(buffer, 5)
    assert len(buffer.buffers['foo']) == 5
    assert len(buffer.buffers['bar']) == 0
    assert len(buffer.buffers['lol']) == 0

    buffer.flush()
    assert len(buffer.buffers['foo']) == 0
    assert len(buffer.buffers['bar']) == 0
    assert len(buffer.buffers['lol']) == 0

    assert blueprint.backend.write.call_count == 3
    assert (blueprint.backend.write.call_args_list[0] == mocker.call(
        blueprint.items['foo'], ((), (), (), (), ())))
    assert (blueprint.backend.write.call_args_list[1] == mocker.call(
        blueprint.items['bar'], ((), (), (), (), ())))
    assert (blueprint.backend.write.call_args_list[2] == mocker.call(
        blueprint.items['lol'], ((), (), (), (), ())))
Пример #2
0
def test_write_buffer(mocker):
    class DummyBackend(Backend):
        def write(self, item, objs):
            return range(len(objs))

    blueprint = Blueprint(backend=DummyBackend())
    blueprint.add_item({'name': 'foo', 'table': 'test', 'fields': {'a': 42}})
    item = blueprint.items['foo']

    mocker.patch.object(item, 'store_final_values')
    mocker.patch.object(item, 'generate_dependencies')

    buffer = Buffer(blueprint, maxlen=10)
    item.generate(buffer, 10)

    objs = tuple(item.namedtuple(id=x, a=42) for x in range(10))
    assert len(buffer.buffers['foo']) == 0
    assert item.store_final_values.call_args == mocker.call(objs)
    assert item.generate_dependencies.call_args == mocker.call(buffer, objs)
Пример #3
0
def test_flush_buffer(mocker):
    blueprint = Blueprint(backend=mocker.MagicMock())
    blueprint.add_item({'name': 'foo', 'table': 'test'})
    blueprint.add_item({'name': 'bar', 'table': 'test'})

    buffer = Buffer(blueprint)
    blueprint.items['foo'].generate(buffer, 5)
    blueprint.items['bar'].generate(buffer, 4)
    assert len(buffer.buffers['foo']) == 5
    assert len(buffer.buffers['bar']) == 4

    buffer.flush()
    assert len(buffer.buffers['foo']) == 0
    assert len(buffer.buffers['bar']) == 0

    assert blueprint.backend.write.call_count == 2
    assert (blueprint.backend.write.call_args_list[0] == mocker.call(
        blueprint.items['foo'], ((), (), (), (), ())))
    assert (blueprint.backend.write.call_args_list[1] == mocker.call(
        blueprint.items['bar'], ((), (), (), ())))
Пример #4
0
    def generate(self):
        logger.info("Getting existing unique values...")

        self.preprocess()

        buffer = Buffer(self)

        logger.info("Starting generation...")

        for item in self.items.values():
            if item.count.by:
                # we only create the items with no dependency,
                # the others will be created on the fly
                continue

            item.generate(buffer, item.count())

        # write everything left in the buffer
        buffer.flush()

        logger.info("Generation done.")
Пример #5
0
def test_generate__count_with_var():
    class DummyBackend(Backend):
        def write(self, item, objs):
            return range(len(objs))

    blueprint = Blueprint(backend=DummyBackend())
    blueprint.add_item({
        'name': 'foo',
        'table': 'test',
        'fields': {
            'nb_bars': {
                'generator': 'Integer',
                'min': 1,
                'max': 10
            }
        },
        'store_in': {
            'foos': '$this'
        }
    })
    blueprint.add_item({
        'name': 'bar',
        'table': 'test',
        'count': {
            'number': '$foo.nb_bars',
            'by': 'foo'
        },
        'store_in': {
            'this.foo.bars': '$this'
        }
    })

    buffer = Buffer(blueprint)
    blueprint.items['foo'].generate(buffer, 10)
    buffer.flush()

    assert len(blueprint.vars['foos']) == 10
    for foo in blueprint.vars['foos']:
        assert 1 <= foo.nb_bars <= 10
        assert len(foo.bars) == foo.nb_bars
Пример #6
0
def test_generate_dependencies_tree():
    class DummyBackend(Backend):
        def write(self, item, objs):
            return range(len(objs))

    blueprint = Blueprint(backend=DummyBackend())

    # definition without count
    blueprint.add_item({'name': 'foo', 'table': 'test'})
    # root
    blueprint.add_item({
        'name': 'foo1',
        'parent': 'foo',
        'store_in': {
            'foo1s': '$this'
        },
        'count': {
            'number': 2
        }
    })
    # level 2
    blueprint.add_item({
        'name': 'foo2',
        'parent': 'foo1',
        'store_in': {
            'foo2s': '$this'
        },
        'count': {
            'number': 2,
            'by': 'foo1'
        }
    })
    # level 3
    blueprint.add_item({
        'name': 'foo3',
        'parent': 'foo2',
        'store_in': {
            'foo3s': '$this'
        },
        'count': {
            'number': 2,
            'by': 'foo2'
        }
    })
    # item created for each foo (root or level)
    blueprint.add_item({
        'name': 'bar',
        'table': 'test',
        'store_in': {
            'bars': '$this'
        },
        'count': {
            'number': 1,
            'by': 'foo'
        }
    })

    buffer = Buffer(blueprint)
    foo1 = blueprint.items['foo1']
    foo1.generate(buffer, foo1.count())

    # only the first level have been generated
    assert len(blueprint.vars['foo1s']) == 2
    assert len(blueprint.vars['foo2s']) == 0
    assert len(blueprint.vars['foo3s']) == 0
    assert len(blueprint.vars['bars']) == 0

    buffer.write(foo1)

    assert len(blueprint.vars['foo1s']) == 2
    assert len(blueprint.vars['foo2s']) == 4
    assert len(blueprint.vars['foo3s']) == 8
    assert len(blueprint.vars['bars']) == 14
Пример #7
0
def test_generate_dependencies():
    class DummyBackend(Backend):
        def write(self, item, objs):
            return range(len(objs))

    blueprint = Blueprint(backend=DummyBackend())
    blueprint.add_item({'name': 'foo', 'table': 'test'})
    blueprint.add_item({
        'name': 'bar',
        'table': 'test',
        'count': {
            'number': 2,
            'by': 'foo'
        },
        'store_in': {
            'bars': '$this'
        },
        'fields': {
            'parent_id': '$this.foo.id'
        }
    })
    blueprint.add_item({
        'name': 'lol',
        'table': 'test',
        'count': {
            'min': 1,
            'max': 2,
            'by': 'foo'
        },
        'store_in': {
            'lols': '$this'
        },
        'fields': {
            'parent_id': '$this.foo.id'
        }
    })
    blueprint.add_item({'name': 'abc', 'table': 'test'})
    blueprint.add_item({
        'name': 'baz',
        'table': 'test',
        'count': {
            'number': 20,
            'by': 'abc'
        }
    })

    buffer = Buffer(blueprint)
    foo = blueprint.items['foo']
    foo.generate(buffer, 10)
    assert len(buffer.buffers['foo']) == 10
    assert len(buffer.buffers['bar']) == 0
    assert len(buffer.buffers['lol']) == 0

    buffer.write(foo)
    assert len(buffer.buffers['foo']) == 0
    assert len(buffer.buffers['bar']) == 0
    assert len(buffer.buffers['lol']) == 0

    assert len(blueprint.vars['bars']) == 20
    assert 10 <= len(blueprint.vars['lols']) <= 20

    def ids():
        for x in range(10):
            yield x
            yield x

    for x, bar in zip(ids(), blueprint.vars['bars']):
        assert bar.parent_id == x
        assert bar.foo.id == x