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_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(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_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_invalid_json(monkeypatch, caplog): with caplog.at_level(logging.ERROR): stdin = read('release-package_minimal.json', 'rb') + b'\n{\n' assert_streaming_error(monkeypatch, main, ['echo'], stdin, expected=read('release-package_minimal.json')) assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'CRITICAL' assert caplog.records[0].message.startswith('JSON error: ')
def test_merge_with_schema(): builder = ProfileBuilder('1__1__4', {'additionalContactPoint': 'master'}) schema = builder.patched_release_schema() data = json.loads( read('release-package_additional-contact-points.json'))['releases'] compiled_release = list(merge(data, schema=schema))[0] assert compiled_release == json.loads(read('compile_extensions.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_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_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_package_default_arguments(): data = [ item for filename in ('realdata/record-package-1.json', 'realdata/record-package-2.json') for item in json.loads(read(filename))['records'] ] actual = package_records(data) assert actual == json.loads( read('realdata/record-package_record-package.json'))
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_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_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_post_extension(self, mock_get): mock_get.return_value = ("1__1__3", "1__1__4") response = self.client.post( self.url, { 'type': 'extension', 'version': '1__1__4', 'extension_url_0': ('https://raw.githubusercontent.com/open-contracting-extensions/ocds_bid_extension/v1.1.4/' + 'extension.json'), 'extension_url_1': ('https://raw.githubusercontent.com/open-contracting-extensions/ocds_location_extension/v1.1.4' + '/extension.json') }) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/csv') self.assertEqual(response['Content-Disposition'], 'attachment; filename="mapping-sheet.csv"') self.assertEqual( response.content.decode('utf-8').replace('\r\n', '\n'), read('results/bids-location-mapping-sheet.csv'))
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_release_tenderers_amendment(monkeypatch, caplog): stdin = read('release_1.0.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) == 1 assert caplog.records[0].levelname == 'WARNING' assert caplog.records[0].message == ( 'party differs in "supplier" role than in "tenderer" roles:\n' '{"name": "Acme Inc.", "identifier": {"id": 1}, "additionalIdentifiers": [{"id": "a"}], "id": "3c9756cf8983b14066a034079aa7aae4"}\n' # noqa '{"id": "3c9756cf8983b14066a034079aa7aae4", "name": "Acme Inc.", "identifier": {"id": 1}}' )
def _remove_package_metadata(filenames): outputs = [] for filename in filenames: data = json.loads(read(filename)) data['publisher'] = {} data['packages'] = [] outputs.append(data) return ''.join(json_dumps(data) + '\n')
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_post_file(self): input_file = StringIO( read('schemas/ocds-ppp-1_0_0-release-schema.json')) input_file.name = 'test.json' response = self.client.post(self.url, { 'type': 'file', 'custom_file': input_file }) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/csv') self.assertEqual(response['Content-Disposition'], 'attachment; filename="mapping-sheet.csv"') self.assertEqual( response.content.decode('utf-8').replace('\r\n', '\n'), read('results/ocds-ppp-1_0_0-mapping-sheet.csv'))
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 assertSuccess(self, method, expected, data): response = getattr(self.client, method)(self.url, data) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/csv') self.assertEqual(response['Content-Disposition'], 'attachment; filename="mapping-sheet.csv"') self.assertEqual( response.content.decode('utf-8').replace('\r\n', '\n'), read(expected))
def test_output_package_no_streaming(): data = [ json.loads(read(filename)) for filename in ('realdata/release-package-1.json', 'realdata/release-package-2.json') ] with Packager() as packager: packager.package['version'] = '1.1' packager.add(data) prefix = packager.version.replace('.', '__') + '__' tag = next(tag for tag in reversed(get_tags()) if tag.startswith(prefix)) schema = get_release_schema_url(tag) actual = next(packager.output_package(Merger(schema))) assert actual == json.loads(read('realdata/record-package_package.json'))
def test_command_release_party_id_missing(monkeypatch): data = json.loads(read('release-package_minimal.json')) data['releases'][0]['parties'] = [{'name': 'Acme Inc.'}] del data['version'] stdin = json.dumps(data).encode('utf-8') # Should not raise an error. run_streaming(monkeypatch, main, ['upgrade', '1.0:1.1'], stdin)
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_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_release_party_roles_str(monkeypatch): data = json.loads(read('release-package_minimal.json')) data['releases'][0]['parties'] = [{'id': '1', 'roles': 'buyer'}] data['releases'][0]['tender'] = {'procuringEntity': {'id': '1'}} del data['version'] stdin = json.dumps(data).encode('utf-8') # Should not raise an error. run_streaming(monkeypatch, main, ['upgrade', '1.0:1.1'], stdin)
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_get_url(self): response = self.client.get( self.url, { 'source': 'https://standard.open-contracting.org/schema/1__1__4/release-schema.json' }) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/csv') self.assertEqual( response.content.decode('utf-8').replace('\r\n', '\n'), read('results/mapping-sheet.csv'))
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() == ''