# 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

        # Make sure lookups of a related field are allowed even if the hashid looks like an integer
        # With the id 161051 on Artist.id, we get the hashid "6966666" which is all numerics
        artist = Artist.objects.create(id=161051, name="Test Artist 161051")
        self.assertEqual(artist.id.hashid, "6966666")
        record_id = Record._meta.get_field('id').get_hashid(768)
        data = {
            'id': record_id.hashid,
            'name': "Test Record 768",
            'artist': "6966666",
            'reference_id': reference_id.hashid,
        }
        s = RecordSerializer(data=data)
        s.is_valid()
        self.assertTrue(s.is_valid())
        r768 = s.save()
        self.assertEqual(r768.id.hashid, record_id.hashid)
        self.assertEqual(r768.name, "Test Record 768")
        self.assertEqual(r768.artist, artist)

    def test_int_lookups_on_int_field(self):
        class RecordSerializer(serializers.ModelSerializer):
            id = HashidSerializerIntegerField(source_field='tests.Record.id')
            artist = serializers.PrimaryKeyRelatedField(
                pk_field=HashidSerializerIntegerField(source_field='tests.Artist.id'),
                queryset=Artist.objects.all(),
                required=False)
            reference_id = HashidSerializerIntegerField(source_field='tests.Record.reference_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


        # Make sure lookups of a related field are allowed even if the hashid looks like an integer
        # With the id 161051 on Artist.id, we get the hashid "6966666" which is all numerics
        artist = Artist.objects.create(id=161051, name="Test Artist 161051")
        self.assertEqual(artist.id.hashid, "6966666")
        record_id = Record._meta.get_field('id').get_hashid(768)
        data = {
            'id': record_id.hashid,
            'name': "Test Record 768",
            'artist': "6966666",
            'reference_id': reference_id.hashid,
        }
        s = RecordSerializer(data=data)
        s.is_valid()
        self.assertTrue(s.is_valid())
        r768 = s.save()
        self.assertEqual(r768.id.hashid, record_id.hashid)
        self.assertEqual(r768.name, "Test Record 768")
        self.assertEqual(r768.artist, artist)

    def test_int_lookups_on_int_field(self):
        class RecordSerializer(serializers.ModelSerializer):
            id = HashidSerializerIntegerField(source_field='tests.Record.id')
            artist = serializers.PrimaryKeyRelatedField(
                pk_field=HashidSerializerIntegerField(source_field='tests.Artist.id'),
                queryset=Artist.objects.all(),
                required=False)
            reference_id = HashidSerializerIntegerField(source_field='tests.Record.reference_id')

            class Meta:

                model = Record