From 2fe7ca65e9b8af40578023bab12c90e78411b001 Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Fri, 24 Apr 2020 17:06:07 +0200 Subject: [PATCH] TST: fix tests and imports in apiutils --- src/caosdb/apiutils.py | 2 +- unittests/test_apiutils.py | 18 ++++++++++++++++++ unittests/test_entity.py | 19 ++++--------------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/caosdb/apiutils.py b/src/caosdb/apiutils.py index c4eb3c28..cda2c09b 100644 --- a/src/caosdb/apiutils.py +++ b/src/caosdb/apiutils.py @@ -35,7 +35,7 @@ from caosdb.common.datatype import (BOOLEAN, DATETIME, DOUBLE, FILE, INTEGER, REFERENCE, TEXT) from caosdb.common.models import (Container, Entity, File, Property, Query, Record, RecordType, get_config, - execute_query) + execute_query, is_reference) def new_record(record_type, name=None, description=None, diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py index 54760593..6705cdeb 100644 --- a/unittests/test_apiutils.py +++ b/unittests/test_apiutils.py @@ -25,6 +25,7 @@ # A. Schlemmer, 02/2018 import caosdb as db +from caosdb.apiutils import apply_to_ids from .record import testrecord import pickle import tempfile @@ -42,3 +43,20 @@ def test_pickle_object(): f.seek(0) rn2 = pickle.load(f) assert r2.date == rn2.date + + +def test_apply_to_ids(): + parent = db.RecordType(id=3456) + rec = db.Record(id=23) + p = db.Property(id=23345, datatype = db.INTEGER) + rec.add_parent(parent) + rec.add_property(p) + + def invert(id_): + return id_ * -1 + apply_to_ids([rec], invert) + + assert invert(3456) == -3456 + assert rec.parents[0].id == -3456 + assert rec.properties[0].id == -23345 + assert rec.id == -23 diff --git a/unittests/test_entity.py b/unittests/test_entity.py index 258f6dfd..64f18039 100644 --- a/unittests/test_entity.py +++ b/unittests/test_entity.py @@ -31,12 +31,15 @@ from caosdb.connection.mockup import MockUpServerConnection class TestEntity(unittest.TestCase): + + def setUp(self): self.assertIsNotNone(Entity) configure_connection(url="unittests", username="testuser", password="testpassword", timeout=200, implementation=MockUpServerConnection) + def test_instance_variables(self): entity = Entity() self.assertTrue(hasattr(entity, "role")) @@ -46,27 +49,13 @@ class TestEntity(unittest.TestCase): self.assertTrue(hasattr(entity, "parents")) self.assertTrue(hasattr(entity, "properties")) + def test_role(self): entity = Entity(role="TestRole") self.assertEqual(entity.role, "TestRole") entity.role = "TestRole2" self.assertEqual(entity.role, "TestRole2") - def test_apply_to_ids(self): - parent = RecordType(id=3456) - rec = Record(id=23) - p = Property(id=23345, datatype=INTEGER) - rec.add_parent(parent) - rec.add_property(p) - - def invert(id_): - return id_ * -1 - rec.apply_to_ids(invert) - - self.assertEqual(invert(3456), -3456) - self.assertEqual(rec.parents[0].id, -3456) - self.assertEqual(rec.properties[0].id, -23345) - self.assertEqual(rec.id, -23) def test_instanciation(self): self.assertRaises(Exception, Entity()) -- GitLab