def test_pk_related_lookup_does_not_exist(self): oid = ObjectId() field = ReferenceField(ReferencedModel) with pytest.raises(ValidationError) as excinfo: field.to_internal_value(oid) msg = excinfo.value.detail[0] assert msg == 'Invalid id "%s" - object does not exist.' % oid
def test_output(self): field = ReferenceField(ReferencedDoc) instance = ReferencedDoc.objects.create(name="foo") strid = str(instance.id) ref = instance.to_dbref() assert field.to_representation(instance) == strid assert field.to_representation(ref) == strid
def test_output_other(self): field = ReferenceField(OtherReferencedModel) instance = OtherReferencedModel.objects.create(name="foo") strid = str(instance.id) ref = instance.to_dbref() assert field.to_representation(instance) == strid assert field.to_representation(ref) == strid
class TestReferenceFieldValue(FieldValues, TestCase): field = ReferenceField(ReferencedModel) @classmethod def setUpClass(cls): ReferencedModel.objects.create(id=someobjectid) @classmethod def tearDownClass(cls): ReferencedModel.drop_collection() valid_inputs = { somedbref: somedbref, someobjectid: somedbref, str(someobjectid): somedbref } invalid_inputs = { 123: ['Incorrect type. Expected str|ObjectId|DBRef|Document value, received int.'], 'xxx': ['"xxx" is not a valid ObjectId'] } outputs = { somedbref: str(someobjectid), someobjectid: str(someobjectid), str(someobjectid): str(someobjectid) }
class EventDeserializer(DocumentSerializer): type = ReferenceField(Type) # type = TypeSerializer() class Meta: model = Event fields = '__all__' depth = 2
class TemplateXslRenderingSerializer(DocumentSerializer): """ TemplateXslRendering Serializer """ template = ReferenceField(model=Template) class Meta(object): """ Meta class """ model = TemplateXslRendering fields = "__all__" def update(self, instance, validated_data): """ Update and return an existing `TemplateXslRendering` instance, given the validated data. """ template_id = validated_data.get("template", None) list_xslt = validated_data.get("list_xslt", None) detail_xslt = validated_data.get("detail_xslt", None) return template_xsl_rendering_api.upsert( template_id, list_xslt, detail_xslt, template_xsl_rendering_id=instance.id )
def test_init_with_queryset(self): ReferenceField(queryset=ReferencedDoc.objects.all())
def test_init_with_model(self): ReferenceField(ReferencedDoc)
class ListReferencingModel(Document): refs = fields.ListField(ReferenceField(ReferencedDoc))
def test_input_other(self): field = ReferenceField(OtherReferencedDoc) instance = OtherReferencedDoc.objects.create(name="foo") ref = instance.to_dbref() assert field.to_internal_value(str(instance.id)) == ref assert field.to_internal_value({'_id': str(instance.id)}) == ref
def test_pk_related_lookup_exists(self): field = ReferenceField(ReferencedModel) instance = field.to_internal_value(self.instance.id) assert instance == self.instance.to_dbref()
def test_pk_representation(self): field = ReferenceField(ReferencedModel) representation = field.to_representation(self.instance) assert representation == smart_str(self.instance.id)
def test_input(self): field = ReferenceField(ReferencedModel) instance = ReferencedModel.objects.create(name="foo") ref = instance.to_dbref() assert field.to_internal_value(str(instance.id)) == ref assert field.to_internal_value({'_id': str(instance.id)}) == ref
def test_pk_related_lookup_invalid_type(self): field = ReferenceField(ReferencedModel) with pytest.raises(ValidationError) as excinfo: field.to_internal_value(BadType()) msg = excinfo.value.detail[0] assert msg == 'Incorrect type. Expected str|ObjectId|DBRef|Document value, received BadType.'