def test_command_order_by(monkeypatch): with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'mapping-sheet', '--order-by', 'path', '--infer-required', path('release-schema.json')]) main() assert actual.getvalue() == read('mapping-sheet_order-by.csv', newline='')
def test_conflicting_codelists(monkeypatch, caplog): with TemporaryDirectory() as d: with TemporaryDirectory() as e: for directory in (d, e): os.mkdir(os.path.join(directory, 'codelists')) with open(os.path.join(directory, 'release-schema.json'), 'w') as f: f.write(schema) for basename in ('a', 'b', 'c', 'd'): with open(os.path.join(d, 'codelists', '{}.csv'.format(basename)), 'w') as f: f.write(codelist) with open(os.path.join(e, 'codelists', 'a.csv'), 'w') as f: f.write('Code\nbaz\n') with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'set-closed-codelist-enums', d, e]) main() assert actual.getvalue() == '' with open(os.path.join(e, 'release-schema.json')) as f: assert f.read() == schema_with_enum assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'ERROR' assert caplog.records[0].message == 'conflicting codelists: a.csv'
def test_modified_codelists(monkeypatch): with TemporaryDirectory() as d: with TemporaryDirectory() as e: for directory in (d, e): os.mkdir(os.path.join(directory, 'codelists')) with open(os.path.join(directory, 'release-schema.json'), 'w') as f: f.write(schema) for basename in ('a', 'b', 'c', 'd'): with open(os.path.join(d, 'codelists', '{}.csv'.format(basename)), 'w') as f: f.write(codelist) with open(os.path.join(e, 'codelists', '+a.csv'), 'w') as f: f.write('Code\nbaz\n') with open(os.path.join(e, 'codelists', '-b.csv'), 'w') as f: f.write('Code,Description\nbar,bzz\n') with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'set-closed-codelist-enums', d, e]) main() assert actual.getvalue() == '' with open(os.path.join(e, 'release-schema.json')) as f: assert f.read() == schema_with_modification
def test_command_bods(monkeypatch): with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'mapping-sheet', '--order-by', 'path', path('bods/person-statement.json')]) main() assert actual.getvalue() == read('mapping-sheet_bods.csv', newline='')
def test_command(monkeypatch): with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'schema-report']) main() assert actual.getvalue() == '''openCodelist: False
def test_command(monkeypatch): stdin = read('realdata/record-package_package.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'split-record-packages', '1']) main() assert actual.getvalue() == read('realdata/record-package_split.json')
def test_command_pretty(monkeypatch): stdin = read('release-package_minimal.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', '--pretty', 'compile']) main() assert actual.getvalue() == read('compile_pretty_minimal.json')
def test_command_encoding(monkeypatch): stdin = read('realdata/release-package_encoding-iso-8859-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', '--encoding', 'iso-8859-1', 'compile']) main() assert actual.getvalue() == read('realdata/compile_encoding_encoding.json')
def test_command(monkeypatch): stdin = read('realdata/release-package-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'tabulate', 'sqlite://']) main() assert actual.getvalue() == ''
def test_command_record_package(monkeypatch): stdin = read('record-package_minimal.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'tabulate', 'sqlite://', '--drop']) main() assert actual.getvalue() == ''
def test_command_no_check_urls(monkeypatch): stdin = read('release-package_urls.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'validate']) main() assert actual.getvalue() == ''
def test_command_release_package_transactions(monkeypatch): stdin = read('realdata/release-package_1.0-2.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'upgrade', '1.0:1.1']) main() assert actual.getvalue() == read('realdata/release-package_1.1-2.json')
def test_command(monkeypatch): stdin = read('release_minimal-1.json', 'rb') + read('release_minimal-2.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'package-releases', '--uri', 'http://example.com', '--published-date', '9999-01-01T00:00:00Z', '--publisher-name', '']) main() assert actual.getvalue() == read('release-package_minimal-1-2.json')
def test_command_identity(monkeypatch): stdin = b'{}' for versions in ('1.0:1.0', '1.1:1.1'): with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'upgrade', versions]) main() assert actual.getvalue() == '{}\n'
def test_command_no_extensions(monkeypatch): stdin = read('record-package_minimal.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'combine-record-packages']) main() assert actual.getvalue() == read('combine-record-packages_minimal.json')
def test_command(monkeypatch): stdin = read('realdata/release-package-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'validate']) main() assert actual.getvalue( ) == "item 0: 'version' is a required property (required)\n"
def test_command_package_versioned(monkeypatch): stdin = read('realdata/release-package-1.json', 'rb') + read( 'realdata/release-package-2.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'compile', '--package', '--versioned']) main() assert actual.getvalue() == read('realdata/record-package_versioned.json')
def test_command_nonexistent(monkeypatch, caplog): with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'indent', 'nonexistent']) main() assert actual.getvalue() == '' assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'ERROR' assert caplog.records[ 0].message == 'nonexistent: No such file or directory'
def test_command_passthrough_release(monkeypatch, caplog): stdin = read('release_1.1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'upgrade', '1.0:1.1']) main() assert actual.getvalue() == read('release_1.1.json') assert len(caplog.records) == 0
def test_command_check_urls(monkeypatch): stdin = read('release-package_urls.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr( sys, 'argv', ['ocdskit', 'validate', '--check-urls', '--timeout', '3']) main() assert actual.getvalue() == """HTTP 500 on GET http://httpbin.org/status/500
def test_command_directory(monkeypatch, caplog): with TemporaryDirectory() as d: with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'indent', d]) main() assert actual.getvalue() == '' assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'WARNING' assert caplog.records[0].message.endswith(' is a directory. Set --recursive to recurse into directories.')
def test_command_no_definitions(monkeypatch): with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', [ 'ocdskit', 'schema-report', '--min-occurrences', '2', '--no-definitions' ]) main() assert 'codelist,openCodelist' in actual.getvalue() assert ':' not in actual.getvalue()
def test_help(stdout, monkeypatch, caplog): stdin = read('release-package_minimal.json', 'rb') with pytest.raises(SystemExit) as excinfo: with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))): monkeypatch.setattr(sys, 'argv', ['ocdskit', '--help']) main() assert stdout.getvalue().startswith('usage: ocdskit [-h] ') assert len(caplog.records) == 0 assert excinfo.value.code == 0
def test_command_order_by_nonexistent(monkeypatch, caplog): with pytest.raises(SystemExit) as excinfo: with patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'mapping-sheet', '--order-by', 'nonexistent', path('release-schema.json')]) main() assert actual.getvalue() == '' assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'CRITICAL' assert caplog.records[0].message == "the column 'nonexistent' doesn't exist – did you make a typo?" assert excinfo.value.code == 1
def test_command_package_publisher(monkeypatch): stdin = read('release-package_minimal.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', [ 'ocdskit', 'compile', '--package', '--publisher-name', 'Acme Inc.' ]) main() package = json.loads(actual.getvalue()) assert package['publisher']['name'] == 'Acme Inc.'
def test_command_missing_field(stdout, stderr, monkeypatch): stdin = read('release-package_minimal.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))): monkeypatch.setattr(sys, 'argv', ['ocdskit', 'combine-record-packages']) main() assert stdout.getvalue( ) == '{"uri":"","publisher":{"name":"Acme"},"publishedDate":"","version":"1.1","records":[]}\n' # noqa: E501 assert stderr.getvalue( ) == 'item 0 has no "records" field (check that it is a record package)\n'
def test_command_valid_release_package_url(monkeypatch): url = 'https://standard.open-contracting.org/schema/1__0__3/release-package-schema.json' stdin = read('realdata/release-package-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'validate', '--schema', url]) main() assert actual.getvalue() == ''
def test_command_invalid_record_package(monkeypatch): url = 'https://standard.open-contracting.org/latest/en/record-package-schema.json' stdin = read('realdata/record-package-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'validate', '--schema', url]) main() assert "item 0: None is not of type 'string' (properties/records/items/properties/compiledRelease/properties/tender/properties/submissionMethod/items/type)" in actual.getvalue( ) # noqa
def test_command_valid_release_package_file_verbose(monkeypatch): url = 'file://{}'.format( os.path.realpath( os.path.join('tests', 'fixtures', 'release-package-schema.json'))) stdin = read('realdata/release-package-1.json', 'rb') with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch( 'sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr( sys, 'argv', ['ocdskit', 'validate', '--schema', url, '--verbose']) main() assert actual.getvalue() == 'item 0: no errors\n'
def test_command_drop(monkeypatch, tmpdir): stdin = read('release-package_minimal.json', 'rb') database_url = 'sqlite:///{}'.format(tmpdir.join('tmp.db')) with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'tabulate', database_url]) main() with patch('sys.stdin', TextIOWrapper(BytesIO(stdin))), patch('sys.stdout', new_callable=StringIO) as actual: monkeypatch.setattr(sys, 'argv', ['ocdskit', 'tabulate', database_url, '--drop']) main() assert actual.getvalue() == ''