Ejemplo n.º 1
0
    def test_ORMWrapper_post_save_fixups_add(self):
        """ Apply a post_save_fixup that adds a reverse foreign key """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
        testModelTwo.save()

        # Make sure the reverse_relation is unpopulated. This should be the case, as fake_stub.py() doesn't populate
        # the reverse relation. But let's be sure, in case someone fixes that.
        testModel._wrapped_class.testmodeltwos_ids = []

        post_save_fixups = [{
            "src_fieldName": "testmodel",
            "dest_id": None,  # this field appears to not be used...
            "dest_model": testModel,
            "remove": False,
            "reverse_fieldName": "testmodeltwos",
        }]

        testModelTwo.post_save_fixups = post_save_fixups
        testModelTwo.do_post_save_fixups()

        self.assertEqual(testModel._wrapped_class.testmodeltwos_ids,
                         [testModelTwo.id])
Ejemplo n.º 2
0
    def test_ORMWrapper_get_generic_foreignkeys(self):
        """ Currently this is a placeholder that returns an empty list """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        self.assertEqual(testModel.get_generic_foreignkeys(), [])
Ejemplo n.º 3
0
    def test_deleted_objects_filter(self):
        orm = self.make_coreapi()
        with patch.object(orm.grpc_stub,
                          "FilterTestModel",
                          wraps=orm.grpc_stub.FilterTestModel) as filter:
            foo = orm.TestModel(name="foo")
            foo.save()
            foo.delete()

            # There should be no live objects
            objs = orm.TestModel.objects.filter(name="foo")
            self.assertEqual(len(objs), 0)

            # There should be one deleted object
            deleted_objs = orm.TestModel.deleted_objects.filter(name="foo")
            self.assertEqual(len(deleted_objs), 1)

            # Two calls, one for when we checked live objects, the other for when we checked deleted objects
            self.assertEqual(filter.call_count, 2)
            q = filter.call_args[0][0]

            # Now spy on the query that was generated, to make sure it looks like we expect
            self.assertEqual(q.kind, q.SYNCHRONIZER_DELETED_OBJECTS)
            self.assertEqual(len(q.elements), 1)
            self.assertEqual(q.elements[0].operator, q.elements[0].EQUAL)
            self.assertEqual(q.elements[0].sValue, "foo")
Ejemplo n.º 4
0
    def test_field_null_new(self):
        """ For models that haven't been saved yet, reading the field should return the gRPC default """

        orm = self.make_coreapi()
        tm = orm.TestModel()

        self.assertEqual(tm.intfield, 0)
Ejemplo n.º 5
0
    def test_ORMWrapper_post_save_fixups_remove(self):
        """ Apply a post_save_fixup that removes a reverse foreign key """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)

        # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done...
        testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id]

        post_save_fixups = [{
            "src_fieldName": "testmodel",
            "dest_id": None,  # this field appears to not be used...
            "dest_model": testModel,
            "remove": True,
            "reverse_fieldName": "testmodeltwos",
        }]

        testModelTwo.post_save_fixups = post_save_fixups
        testModelTwo.do_post_save_fixups()

        self.assertEqual(testModel._wrapped_class.testmodeltwos_ids, [])
Ejemplo n.º 6
0
    def test_ORMWrapper_ansible_tag(self):
        """ Ansible_tag is used by old-style synchronizers. Deprecated. """

        orm = self.make_coreapi()

        testModel = orm.TestModel(id=7)

        self.assertEqual(testModel.ansible_tag, "TestModel_7")
Ejemplo n.º 7
0
    def test_ORMWrapper_dict(self):
        orm = self.make_coreapi()

        testModel = orm.TestModel(intfield=7, stringfield="foo")

        self.assertDictEqual(testModel._dict, {
            "intfield": 7,
            "stringfield": "foo"
        })
Ejemplo n.º 8
0
    def test_field_non_null(self):
        """ In a saved object, if a nullable field is set to a value, then make sure the ORM returns the value """

        orm = self.make_coreapi()
        tm = orm.TestModel(intfield=3)
        tm.save()

        tm = orm.TestModel.objects.all()[0]
        self.assertEqual(tm.intfield, 3)
Ejemplo n.º 9
0
    def test_ORMLocalObjectManager_remove(self):
        orm = self.make_coreapi()

        t = orm.TestModel()
        t.save()

        lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], True)
        lobjs.remove(t)
        self.assertEqual(lobjs.count(), 0)
Ejemplo n.º 10
0
    def test_ORMLocalObjectManager_add(self):
        orm = self.make_coreapi()

        t = orm.TestModel()
        t.save()

        lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], True)
        lobjs.add(t)
        self.assertEqual(lobjs.count(), 1)
        self.assertEqual(lobjs.first().id, t.id)
Ejemplo n.º 11
0
    def test_field_null(self):
        """ In a saved object, if a nullable field is left set to None, make sure the ORM returns None """

        orm = self.make_coreapi()
        tm = orm.TestModel()
        tm.save()

        tm = orm.TestModel.objects.all()[0]
        self.assertFalse(tm._wrapped_class.HasField("intfield"))
        self.assertEqual(tm.intfield, None)
Ejemplo n.º 12
0
    def test_ORMWrapper_tologdict(self):
        """ Tologdict contains the model name and id, used for structured logging """
        orm = self.make_coreapi()

        testModel = orm.TestModel(intfield=7, stringfile="foo")

        self.assertDictEqual(testModel.tologdict(), {
            "model_name": "TestModel",
            "pk": 0
        })
Ejemplo n.º 13
0
    def test_field_set_null(self):
        """ Setting a field to None is not allowed """

        orm = self.make_coreapi()
        tm = orm.TestModel()
        with self.assertRaises(Exception) as e:
            tm.intfile = None
        self.assertEqual(
            e.exception.message,
            "Setting a non-foreignkey field to None is not supported",
        )
Ejemplo n.º 14
0
    def test_invalidate_cache(self):
        orm = self.make_coreapi()
        testModel = orm.TestModel()

        # populate the caches with some placeholders we can test for
        testModel.cache = {"a": 1}
        testModel.reverse_cache = {"b": 2}

        testModel.invalidate_cache()

        self.assertEqual(testModel.cache, {})
        self.assertEqual(testModel.reverse_cache, {})
Ejemplo n.º 15
0
    def test_ORMWrapper_fk_set(self):
        """ fk_set will set the testmodel field on TesTModelTwo to point to the TestModel. """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModelTwo = orm.TestModelTwo()

        testModelTwo.fk_set("testmodel", testModel)

        self.assertEqual(testModelTwo.testmodel_id, testModel.id)
Ejemplo n.º 16
0
    def test_ORMLocalObjectManager_empty(self):
        """ Test all(), first(), exists(), and count() together since they're all closely related. Use an empty
            list.
        """
        orm = self.make_coreapi()

        t = orm.TestModel()
        t.save()

        lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [], False)
        self.assertEqual(len(lobjs.all()), 0)
        self.assertEqual(lobjs.exists(), False)
        self.assertEqual(lobjs.count(), 0)
        self.assertEqual(lobjs.first(), None)
Ejemplo n.º 17
0
    def test_ORMWrapper_recompute_initial(self):
        """ For saved models, Recompute_initial should take recompute the set of initial values, removing all items
            from the diff set.
        """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModel.intfield = 9
        self.assertEqual(testModel.changed_fields, ["intfield"])

        testModel.recompute_initial()
        self.assertEqual(testModel.changed_fields, [])
Ejemplo n.º 18
0
    def test_ORMWrapper_fk_resolve(self):
        """ If we create a TestModelTwo that has a foreign key reference to a TestModel, then calling fk_resolve should
            return that model.
        """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)

        testModel_resolved = testModelTwo.fk_resolve("testmodel")
        self.assertEqual(testModel_resolved.id, testModel.id)

        # the cache should have been populated
        self.assertIn(("testmodel", testModel_resolved),
                      testModelTwo.cache.items())
Ejemplo n.º 19
0
    def test_ORMLocalObjectManager_not_writeable(self):
        """ An ORMLocalObjectManager that is not writeable should throw exceptions on add() and remove() """
        orm = self.make_coreapi()

        t = orm.TestModel()
        t.save()

        lobjs = self.ORMLocalObjectManager(t.stub, "TestModel", [t.id], False)

        with self.assertRaises(Exception) as e:
            lobjs.add(123)
        self.assertEqual(e.exception.message,
                         "Only ManyToMany lists are writeable")

        with self.assertRaises(Exception) as e:
            lobjs.remove(123)
        self.assertEqual(e.exception.message,
                         "Only ManyToMany lists are writeable")
Ejemplo n.º 20
0
    def test_ORMWrapper_gen_reverse_fkmap(self):
        """ TestModel includes a reverse relation back to TestModelTwo, and the reverse_fkmap should contain that
            relation.
        """

        orm = self.make_coreapi()

        testModel = orm.TestModel()

        self.assertDictEqual(
            testModel.gen_reverse_fkmap(),
            {
                "testmodeltwos": {
                    "modelName": "TestModelTwo",
                    "src_fieldName": "testmodeltwos_ids",
                    "writeable": False,
                }
            },
        )
Ejemplo n.º 21
0
    def test_ORMWrapper_save_changed_fields(self):
        orm = self.make_coreapi()

        testModel = orm.TestModel(intfield=7, stringfield="foo")
        testModel.save()

        testModel.intfield = 9

        with patch.object(orm.grpc_stub,
                          "UpdateTestModel",
                          wraps=orm.grpc_stub.UpdateTestModel) as update:
            testModel.save_changed_fields()

            self.assertEqual(update.call_count, 1)
            self.assertIn("metadata", update.call_args[1])
            update_fields_arg = [
                x[1] for x in update.call_args[1]["metadata"]
                if x[0] == "update_fields"
            ]
            self.assertEqual(update_fields_arg, ["intfield"])
Ejemplo n.º 22
0
    def test_ORMWrapper_reverse_fk_resolve(self):
        """ If a TestModelTwo has a relation to TestModel, then TestModel's reverse_fk should be resolvable to a list
            of TestModelTwo objects.
        """

        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.save()

        testModelTwo = orm.TestModelTwo(testmodel_id=testModel.id)
        testModelTwo.save()

        # fake_stub.py doesn't populate the reverse relations for us, so force what the server would have done...
        testModel._wrapped_class.testmodeltwos_ids = [testModelTwo.id]

        testModelTwos_resolved = testModel.reverse_fk_resolve("testmodeltwos")
        self.assertEqual(testModelTwos_resolved.count(), 1)

        # the reverse_cache should have been populated
        self.assertIn(("testmodeltwos", testModelTwos_resolved),
                      testModel.reverse_cache.items())
Ejemplo n.º 23
0
    def test_ORMWrapper_create_attr(self):
        orm = self.make_coreapi()

        testModel = orm.TestModel()
        testModel.create_attr("some_new_attribute", "foo")
        self.assertEqual(testModel.some_new_attribute, "foo")