diff --git a/CHANGELOG.md b/CHANGELOG.md index a3a80183c7b6a6980db3abfeb0abc9187fb9ea3e..02dcb340a1055881a12c31d90e079c2a5859a041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Identifiable class to represent the information used to identify Records. - Added some StructureElements: BooleanElement, FloatElement, IntegerElement, ListElement, DictElement +- String representation for Identifiables ### Changed ### diff --git a/src/caoscrawler/identifiable.py b/src/caoscrawler/identifiable.py index 5b2b215263b0edc7817c18ad9eecf85ad648f10f..0db566bf34e62d09553a4890ceb7bab4375079f2 100644 --- a/src/caoscrawler/identifiable.py +++ b/src/caoscrawler/identifiable.py @@ -22,6 +22,7 @@ from __future__ import annotations import caosdb as db from datetime import datetime +import json from hashlib import sha256 from typing import Union @@ -127,3 +128,10 @@ class Identifiable(): return True else: return False + + def __repr__(self): + pstring = json.dumps(self.properties) + return (f"{self.__class__.__name__} for RT {self.record_type}: id={self.record_id}; " + f"name={self.name}\n\tpath={self.path}\n" + f"\tproperties:\n{pstring}\n" + f"\tbackrefs:\n{self.backrefs}") diff --git a/unittests/test_identifiable.py b/unittests/test_identifiable.py index c06ef0ec4b0ab7a91e0397884d95a9e496240df2..847438cac752ea91a94adf6fb5dc81e54b87882d 100644 --- a/unittests/test_identifiable.py +++ b/unittests/test_identifiable.py @@ -42,7 +42,8 @@ def test_create_hashable_string(): assert a == b assert ( Identifiable._create_hashable_string( - Identifiable(name="A", record_type="B", properties={'a': db.Record(id=12)}) + Identifiable(name="A", record_type="B", + properties={'a': db.Record(id=12)}) ) == "P<B>N<A>a:12") a = Identifiable._create_hashable_string( Identifiable(name="A", record_type="B", properties={'a': [db.Record(id=12)]})) @@ -51,7 +52,8 @@ def test_create_hashable_string(): Identifiable(name="A", record_type="B", properties={'a': [12]})) == "P<B>N<A>a:[12]") assert ( Identifiable._create_hashable_string( - Identifiable(name="A", record_type="B", properties={'a': [db.Record(id=12), 11]}) + Identifiable(name="A", record_type="B", properties={ + 'a': [db.Record(id=12), 11]}) ) == "P<B>N<A>a:[12, 11]") assert ( Identifiable._create_hashable_string( @@ -65,6 +67,17 @@ def test_name(): Identifiable(properties={"Name": 'li'}) +def test_repr(): + # only test that something meaningful is returned + assert 'properties' in str(Identifiable(name="A", record_type="B")) + assert str(Identifiable(name="A", record_type="B", properties={'a': 0})).split( + "properties:\n")[1].split('\n')[0] == '{"a": 0}' + assert str(Identifiable(name="A", record_type="B", properties={'a': 0, 'b': "test"})).split( + "properties:\n")[1].split('\n')[0] == '{"a": 0, "b": "test"}' + + # TODO(henrik): Add a test using backrefs once that's implemented. + + def test_equality(): assert Identifiable( record_id=12, properties={"a": 0}) == Identifiable(record_id=12, properties={"a": 1}) @@ -78,5 +91,7 @@ def test_equality(): path="a", properties={"a": 0}) == Identifiable(path="a", properties={"a": 1}) assert Identifiable( path="a", properties={"a": 0}) == Identifiable(properties={"a": 0}) - assert Identifiable(properties={"a": 0}) == Identifiable(properties={"a": 0}) - assert Identifiable(properties={"a": 0}) != Identifiable(properties={"a": 1}) + assert Identifiable(properties={"a": 0}) == Identifiable( + properties={"a": 0}) + assert Identifiable(properties={"a": 0}) != Identifiable( + properties={"a": 1})