# Path: tests/models.py
# class Artist(models.Model):
#     id = HashidAutoField(primary_key=True, alphabet="1234567890abcdef")
#     name = models.CharField(max_length=40)
# 
#     def __str__(self):
#         return "{} ({})".format(self.name, self.id)
# 
# class Record(models.Model):
#     id = BigHashidAutoField(primary_key=True)
#     name = models.CharField(max_length=40)
#     artist = models.ForeignKey(Artist, on_delete=models.CASCADE, null=True, blank=True, related_name="records")
#     reference_id = HashidField()
#     prefixed_id = HashidField(null=True, blank=True, prefix="prefix_")
#     string_id = HashidField(null=True, blank=True, enable_hashid_object=False)
#     plain_hashid = HashidField(null=True, blank=True, enable_descriptor=False)
#     plain_id = HashidField(null=True, blank=True, enable_descriptor=False, enable_hashid_object=False)
#     alternate_id = HashidField(salt="a different salt", null=True, blank=True)
#     key = BigHashidField(min_length=10, alphabet="abcdlmnotuvwxyz123789", null=True, blank=True)
# 
#     def __str__(self):
#         return "{} ({})".format(self.name, self.reference_id)
# 
# class Track(models.Model):
#     id = BigHashidAutoField(primary_key=True, allow_int_lookup=True, min_length=10, prefix='albumtrack:', salt="abcd", alphabet="abcdefghijklmnop")


from unittest import skipUnless
from django.core import exceptions
from django.test import TestCase
from rest_framework.exceptions import ErrorDetail
from tests.models import Artist, Record, Track
    from rest_framework import serializers
    from rest_framework.renderers import JSONRenderer
    from hashid_field.rest import UnconfiguredHashidSerialField, HashidSerializerCharField, HashidSerializerIntegerField
import hashids
                fields = ('id', 'name')

        with self.assertRaises(exceptions.ImproperlyConfigured):
            ArtistSerializer().fields()  # Fields aren't built until first accessed

    def test_modelserializer_charfield(self):
        class ArtistSerializer(serializers.ModelSerializer):
            id = HashidSerializerCharField(source_field='tests.Artist.id')

            class Meta:
                model = Artist
                fields = ('id', 'name')

        artist = Artist.objects.create(id=128, name="Test Artist")
        orig_id = artist.id
        s = ArtistSerializer(artist)
        self.assertEqual(Artist._meta.get_field('id').salt, s.fields['id'].hashid_salt)
        self.assertTrue(isinstance(s.data['id'], str))
        self.assertEqual(artist.id.hashid, s.data['id'])
        s2 = ArtistSerializer(artist, data={'id': artist.id.hashid, 'name': "Test Artist Changed"})
        self.assertTrue(s2.is_valid())
        artist = s2.save()
        self.assertEqual(artist.id, orig_id)
        self.assertEqual(artist.name, "Test Artist Changed")

    def test_modelserializer_integerfield(self):
        class ArtistSerializer(serializers.ModelSerializer):
            id = HashidSerializerIntegerField(source_field=Artist._meta.get_field('id'))

            class Meta:

# Path: tests/models.py
# class Artist(models.Model):
#     id = HashidAutoField(primary_key=True, alphabet="1234567890abcdef")
#     name = models.CharField(max_length=40)
# 
#     def __str__(self):
#         return "{} ({})".format(self.name, self.id)
# 
# class Record(models.Model):
#     id = BigHashidAutoField(primary_key=True)
#     name = models.CharField(max_length=40)
#     artist = models.ForeignKey(Artist, on_delete=models.CASCADE, null=True, blank=True, related_name="records")
#     reference_id = HashidField()
#     prefixed_id = HashidField(null=True, blank=True, prefix="prefix_")
#     string_id = HashidField(null=True, blank=True, enable_hashid_object=False)
#     plain_hashid = HashidField(null=True, blank=True, enable_descriptor=False)
#     plain_id = HashidField(null=True, blank=True, enable_descriptor=False, enable_hashid_object=False)
#     alternate_id = HashidField(salt="a different salt", null=True, blank=True)
#     key = BigHashidField(min_length=10, alphabet="abcdlmnotuvwxyz123789", null=True, blank=True)
# 
#     def __str__(self):
#         return "{} ({})".format(self.name, self.reference_id)
# 
# class Track(models.Model):
#     id = BigHashidAutoField(primary_key=True, allow_int_lookup=True, min_length=10, prefix='albumtrack:', salt="abcd", alphabet="abcdefghijklmnop")


# Path: tests/test_rest_framework.py
from unittest import skipUnless
from django.core import exceptions
from django.test import TestCase
from rest_framework.exceptions import ErrorDetail
from tests.models import Artist, Record, Track
    from rest_framework import serializers
    from rest_framework.renderers import JSONRenderer
    from hashid_field.rest import UnconfiguredHashidSerialField, HashidSerializerCharField, HashidSerializerIntegerField
import hashids

                fields = ('id', 'name')

        with self.assertRaises(exceptions.ImproperlyConfigured):
            ArtistSerializer().fields()  # Fields aren't built until first accessed

    def test_modelserializer_charfield(self):
        class ArtistSerializer(serializers.ModelSerializer):
            id = HashidSerializerCharField(source_field='tests.Artist.id')

            class Meta:
                model = Artist
                fields = ('id', 'name')

        artist = Artist.objects.create(id=128, name="Test Artist")
        orig_id = artist.id
        s = ArtistSerializer(artist)
        self.assertEqual(Artist._meta.get_field('id').salt, s.fields['id'].hashid_salt)
        self.assertTrue(isinstance(s.data['id'], str))
        self.assertEqual(artist.id.hashid, s.data['id'])
        s2 = ArtistSerializer(artist, data={'id': artist.id.hashid, 'name': "Test Artist Changed"})
        self.assertTrue(s2.is_valid())
        artist = s2.save()
        self.assertEqual(artist.id, orig_id)
        self.assertEqual(artist.name, "Test Artist Changed")

    def test_modelserializer_integerfield(self):
        class ArtistSerializer(serializers.ModelSerializer):
            id = HashidSerializerIntegerField(source_field=Artist._meta.get_field('id'))

            class Meta:

                model = Artist