class TestLocationParentIdExpression(SimpleTestCase):
    def setUp(self):
        # we have to set the fake database before any other calls
        self.domain = "test-loc-parent-id"
        self.evaluation_context = EvaluationContext({"domain": self.domain})
        self.orig_db = Location.get_db()
        self.database = FakeCouchDb()
        Location.set_db(self.database)
        self.parent = self._make_location(_id=uuid.uuid4().hex)
        self.child = self._make_location(_id=uuid.uuid4().hex, lineage=[self.parent._id])
        self.grandchild = self._make_location(_id=uuid.uuid4().hex, lineage=[self.child._id, self.parent._id])
        self.expression_spec = {
            "type": "location_parent_id",
            "location_id_expression": {"type": "property_name", "property_name": "location_id"},
        }
        self.expression = ExpressionFactory.from_spec(self.expression_spec)

    def tearDown(self):
        Location.set_db(self.orig_db)

    def test_location_parent_id(self):
        self.assertEqual(self.parent._id, self.expression({"location_id": self.child._id}, self.evaluation_context))
        self.assertEqual(self.child._id, self.expression({"location_id": self.grandchild._id}, self.evaluation_context))

    def test_location_parent_missing(self):
        self.assertEqual(None, self.expression({"location_id": "bad-id"}, self.evaluation_context))

    def test_location_parent_bad_domain(self):
        self.assertEqual(
            None, self.expression({"location_id": self.child._id}, EvaluationContext({"domain": "bad-domain"}))
        )

    def test_location_parents_chained(self):
        expression = ExpressionFactory.from_spec(
            {
                "type": "location_parent_id",
                "location_id_expression": {
                    "type": "location_parent_id",
                    "location_id_expression": {"type": "property_name", "property_name": "location_id"},
                },
            }
        )
        self.assertEqual(self.parent._id, expression({"location_id": self.grandchild._id}, self.evaluation_context))

    def _make_location(self, **kwargs):
        kwargs["domain"] = self.domain
        loc = Location(**kwargs)
        self.database.save_doc(loc.to_json())
        return loc
Beispiel #2
0
class TestLocationParentIdExpression(SimpleTestCase):
    def setUp(self):
        # we have to set the fake database before any other calls
        self.domain = 'test-loc-parent-id'
        self.evaluation_context = EvaluationContext({"domain": self.domain})
        self.orig_db = Location.get_db()
        self.database = FakeCouchDb()
        Location.set_db(self.database)
        self.parent = self._make_location(_id=uuid.uuid4().hex)
        self.child = self._make_location(_id=uuid.uuid4().hex,
                                         lineage=[self.parent._id])
        self.grandchild = self._make_location(
            _id=uuid.uuid4().hex, lineage=[self.child._id, self.parent._id])
        self.expression_spec = {
            "type": "location_parent_id",
            "location_id_expression": {
                "type": "property_name",
                "property_name": "location_id",
            }
        }
        self.expression = ExpressionFactory.from_spec(self.expression_spec)

    def tearDown(self):
        Location.set_db(self.orig_db)

    def test_location_parent_id(self):
        self.assertEqual(
            self.parent._id,
            self.expression({'location_id': self.child._id},
                            self.evaluation_context))
        self.assertEqual(
            self.child._id,
            self.expression({'location_id': self.grandchild._id},
                            self.evaluation_context))

    def test_location_parent_missing(self):
        self.assertEqual(
            None,
            self.expression({'location_id': 'bad-id'},
                            self.evaluation_context))

    def test_location_parent_bad_domain(self):
        self.assertEqual(
            None,
            self.expression({'location_id': self.child._id},
                            EvaluationContext({"domain": 'bad-domain'})))

    def test_location_parents_chained(self):
        expression = ExpressionFactory.from_spec({
            "type": "location_parent_id",
            "location_id_expression": {
                "type": "location_parent_id",
                "location_id_expression": {
                    "type": "property_name",
                    "property_name": "location_id",
                }
            }
        })
        self.assertEqual(
            self.parent._id,
            expression({'location_id': self.grandchild._id},
                       self.evaluation_context))

    def _make_location(self, **kwargs):
        kwargs['domain'] = self.domain
        loc = Location(**kwargs)
        self.database.save_doc(loc.to_json())
        return loc