Skip to content
Snippets Groups Projects
Commit 01847ccc authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-repr-identifiable' into 'dev'

ENH: add representation to  identifiables

See merge request !73
parents ca052812 3529b4d7
Branches
Tags
2 merge requests!91Release 0.3,!73ENH: add representation to identifiables
Pipeline #31183 passed
...@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -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. - Identifiable class to represent the information used to identify Records.
- Added some StructureElements: BooleanElement, FloatElement, IntegerElement, - Added some StructureElements: BooleanElement, FloatElement, IntegerElement,
ListElement, DictElement ListElement, DictElement
- String representation for Identifiables
### Changed ### ### Changed ###
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
from __future__ import annotations from __future__ import annotations
import caosdb as db import caosdb as db
from datetime import datetime from datetime import datetime
import json
from hashlib import sha256 from hashlib import sha256
from typing import Union from typing import Union
...@@ -127,3 +128,10 @@ class Identifiable(): ...@@ -127,3 +128,10 @@ class Identifiable():
return True return True
else: else:
return False 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}")
...@@ -42,7 +42,8 @@ def test_create_hashable_string(): ...@@ -42,7 +42,8 @@ def test_create_hashable_string():
assert a == b assert a == b
assert ( assert (
Identifiable._create_hashable_string( 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") ) == "P<B>N<A>a:12")
a = Identifiable._create_hashable_string( a = 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)]}))
...@@ -51,7 +52,8 @@ def test_create_hashable_string(): ...@@ -51,7 +52,8 @@ def test_create_hashable_string():
Identifiable(name="A", record_type="B", properties={'a': [12]})) == "P<B>N<A>a:[12]") Identifiable(name="A", record_type="B", properties={'a': [12]})) == "P<B>N<A>a:[12]")
assert ( assert (
Identifiable._create_hashable_string( 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]") ) == "P<B>N<A>a:[12, 11]")
assert ( assert (
Identifiable._create_hashable_string( Identifiable._create_hashable_string(
...@@ -65,6 +67,17 @@ def test_name(): ...@@ -65,6 +67,17 @@ def test_name():
Identifiable(properties={"Name": 'li'}) 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(): def test_equality():
assert Identifiable( assert Identifiable(
record_id=12, properties={"a": 0}) == Identifiable(record_id=12, properties={"a": 1}) record_id=12, properties={"a": 0}) == Identifiable(record_id=12, properties={"a": 1})
...@@ -78,5 +91,7 @@ def test_equality(): ...@@ -78,5 +91,7 @@ def test_equality():
path="a", properties={"a": 0}) == Identifiable(path="a", properties={"a": 1}) path="a", properties={"a": 0}) == Identifiable(path="a", properties={"a": 1})
assert Identifiable( assert Identifiable(
path="a", properties={"a": 0}) == Identifiable(properties={"a": 0}) path="a", properties={"a": 0}) == Identifiable(properties={"a": 0})
assert Identifiable(properties={"a": 0}) == Identifiable(properties={"a": 0}) assert Identifiable(properties={"a": 0}) == Identifiable(
assert Identifiable(properties={"a": 0}) != Identifiable(properties={"a": 1}) properties={"a": 0})
assert Identifiable(properties={"a": 0}) != Identifiable(
properties={"a": 1})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment