From 992137441fc100f8698de1ca1bc366b9aa7d7de9 Mon Sep 17 00:00:00 2001 From: Tpt Date: Thu, 6 Oct 2022 11:41:35 +0200 Subject: [PATCH] Uses black for code formatting --- .github/workflows/tests.yml | 2 ++ python/pyproject.toml | 3 +++ python/requirements.dev.txt | 1 + python/tests/test_io.py | 39 ++++++++++++++++++++++---------- python/tests/test_model.py | 45 +++++++++++++++++++++---------------- python/tests/test_store.py | 24 ++++++++++++-------- 6 files changed, 74 insertions(+), 40 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ab49582b..cdedf6fa 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -94,6 +94,8 @@ jobs: with: python-version: "3.10" - run: pip install --upgrade -r python/requirements.dev.txt + - run: python -m black --check --diff --color . + working-directory: ./python - run: maturin sdist -m python/Cargo.toml - run: pip install target/wheels/*.tar.gz - run: python -m unittest diff --git a/python/pyproject.toml b/python/pyproject.toml index 548ed9c1..a9789454 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -28,3 +28,6 @@ Documentation = "https://pyoxigraph.readthedocs.io/" Homepage = "https://pyoxigraph.readthedocs.io/" Source = "https://github.com/oxigraph/oxigraph/tree/main/python" Tracker = "https://github.com/oxigraph/oxigraph/issues" + +[tool.black] +target-version = ['py37'] diff --git a/python/requirements.dev.txt b/python/requirements.dev.txt index a22a21c0..b52a219a 100644 --- a/python/requirements.dev.txt +++ b/python/requirements.dev.txt @@ -1,3 +1,4 @@ +black furo maturin sphinx diff --git a/python/tests/test_io.py b/python/tests/test_io.py index 4e29bad9..62894b31 100644 --- a/python/tests/test_io.py +++ b/python/tests/test_io.py @@ -6,9 +6,7 @@ from pyoxigraph import * EXAMPLE_TRIPLE = Triple( - NamedNode("http://example.com/foo"), - NamedNode("http://example.com/p"), - Literal("1") + NamedNode("http://example.com/foo"), NamedNode("http://example.com/p"), Literal("1") ) @@ -19,25 +17,37 @@ class TestParse(unittest.TestCase): fp.flush() self.assertEqual( list(parse(fp.name, "text/turtle", base_iri="http://example.com/")), - [EXAMPLE_TRIPLE] + [EXAMPLE_TRIPLE], ) def test_parse_not_existing_file(self): with self.assertRaises(IOError) as _: parse("/tmp/not-existing-oxigraph-file.ttl", "text/turtle") - + def test_parse_str_io(self): self.assertEqual( - list(parse(StringIO('

"1" .'), "text/turtle", base_iri="http://example.com/")), - [EXAMPLE_TRIPLE] + list( + parse( + StringIO('

"1" .'), + "text/turtle", + base_iri="http://example.com/", + ) + ), + [EXAMPLE_TRIPLE], ) def test_parse_bytes_io(self): self.assertEqual( - list(parse(BytesIO(b'

"1" .'), "text/turtle", base_iri="http://example.com/")), - [EXAMPLE_TRIPLE] + list( + parse( + BytesIO(b'

"1" .'), + "text/turtle", + base_iri="http://example.com/", + ) + ), + [EXAMPLE_TRIPLE], ) - + def test_parse_io_error(self): class BadIO(RawIOBase): pass @@ -50,9 +60,14 @@ class TestSerialize(unittest.TestCase): def test_serialize_to_bytes_io(self): output = BytesIO() serialize([EXAMPLE_TRIPLE], output, "text/turtle") - self.assertEqual(output.getvalue(), b' "1" .\n') + self.assertEqual( + output.getvalue(), + b' "1" .\n', + ) def test_serialize_to_file(self): with NamedTemporaryFile() as fp: serialize([EXAMPLE_TRIPLE], fp.name, "text/turtle") - self.assertEqual(fp.read(), b' "1" .\n') + self.assertEqual( + fp.read(), b' "1" .\n' + ) diff --git a/python/tests/test_model.py b/python/tests/test_model.py index d07e0303..d663d7a1 100644 --- a/python/tests/test_model.py +++ b/python/tests/test_model.py @@ -29,8 +29,8 @@ class TestBlankNode(unittest.TestCase): def test_equal(self): self.assertEqual(BlankNode("foo"), BlankNode("foo")) self.assertNotEqual(BlankNode("foo"), BlankNode("bar")) - self.assertNotEqual(BlankNode('foo'), NamedNode('http://foo')) - self.assertNotEqual(NamedNode('http://foo'), BlankNode('foo')) + self.assertNotEqual(BlankNode("foo"), NamedNode("http://foo")) + self.assertNotEqual(NamedNode("http://foo"), BlankNode("foo")) class TestLiteral(unittest.TestCase): @@ -59,10 +59,10 @@ class TestLiteral(unittest.TestCase): Literal("foo", language="en", datatype=RDF_LANG_STRING), Literal("foo", language="en"), ) - self.assertNotEqual(NamedNode('http://foo'), Literal('foo')) - self.assertNotEqual(Literal('foo'), NamedNode('http://foo')) - self.assertNotEqual(BlankNode('foo'), Literal('foo')) - self.assertNotEqual(Literal('foo'), BlankNode('foo')) + self.assertNotEqual(NamedNode("http://foo"), Literal("foo")) + self.assertNotEqual(Literal("foo"), NamedNode("http://foo")) + self.assertNotEqual(BlankNode("foo"), Literal("foo")) + self.assertNotEqual(Literal("foo"), BlankNode("foo")) class TestTriple(unittest.TestCase): @@ -81,25 +81,32 @@ class TestTriple(unittest.TestCase): Triple( NamedNode("http://example.com/ss"), NamedNode("http://example.com/sp"), - NamedNode("http://example.com/so") + NamedNode("http://example.com/so"), ), NamedNode("http://example.com/p"), Triple( NamedNode("http://example.com/os"), NamedNode("http://example.com/op"), - NamedNode("http://example.com/oo") - ), ) - self.assertEqual(t.subject, Triple( - NamedNode("http://example.com/ss"), - NamedNode("http://example.com/sp"), - NamedNode("http://example.com/so") - )) + NamedNode("http://example.com/oo"), + ), + ) + self.assertEqual( + t.subject, + Triple( + NamedNode("http://example.com/ss"), + NamedNode("http://example.com/sp"), + NamedNode("http://example.com/so"), + ), + ) self.assertEqual(t.predicate, NamedNode("http://example.com/p")) - self.assertEqual(t.object, Triple( - NamedNode("http://example.com/os"), - NamedNode("http://example.com/op"), - NamedNode("http://example.com/oo") - )) + self.assertEqual( + t.object, + Triple( + NamedNode("http://example.com/os"), + NamedNode("http://example.com/op"), + NamedNode("http://example.com/oo"), + ), + ) def test_mapping(self): t = Triple( diff --git a/python/tests/test_store.py b/python/tests/test_store.py index 8e27178c..88152f7d 100644 --- a/python/tests/test_store.py +++ b/python/tests/test_store.py @@ -88,7 +88,8 @@ class TestStore(unittest.TestCase): results = store.query("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }") self.assertIsInstance(results, QueryTriples) self.assertEqual( - set(results), {Triple(foo, bar, baz)}, + set(results), + {Triple(foo, bar, baz)}, ) def test_select_query(self): @@ -154,30 +155,34 @@ class TestStore(unittest.TestCase): def test_update_insert_data(self): store = Store() - store.update('INSERT DATA { }') + store.update("INSERT DATA { }") self.assertEqual(len(store), 1) def test_update_delete_data(self): store = Store() store.add(Quad(foo, foo, foo)) - store.update('DELETE DATA { }') + store.update("DELETE DATA { }") self.assertEqual(len(store), 0) def test_update_delete_where(self): store = Store() store.add(Quad(foo, foo, foo)) - store.update('DELETE WHERE { ?v ?v ?v }') + store.update("DELETE WHERE { ?v ?v ?v }") self.assertEqual(len(store), 0) def test_update_load(self): store = Store() - store.update('LOAD ') + store.update("LOAD ") self.assertGreater(len(store), 100) def test_update_star(self): store = Store() - store.update('PREFIX : INSERT DATA { :alice :claims << :bob :age 23 >> }') - results = store.query('PREFIX : SELECT ?p ?a WHERE { ?p :claims << :bob :age ?a >> }') + store.update( + "PREFIX : INSERT DATA { :alice :claims << :bob :age 23 >> }" + ) + results = store.query( + "PREFIX : SELECT ?p ?a WHERE { ?p :claims << :bob :age ?a >> }" + ) self.assertEqual(len(list(results)), 1) def test_load_ntriples_to_default_graph(self): @@ -245,7 +250,8 @@ class TestStore(unittest.TestCase): output = BytesIO() store.dump(output, "application/n-triples", from_graph=graph) self.assertEqual( - output.getvalue(), b" .\n", + output.getvalue(), + b" .\n", ) def test_dump_nquads(self): @@ -264,7 +270,7 @@ class TestStore(unittest.TestCase): store = Store() store.add(Quad(foo, bar, baz, graph)) store.dump(file_name, "application/n-quads") - with open(file_name, 'rt') as fp: + with open(file_name, "rt") as fp: file_content = fp.read() self.assertEqual( file_content,