Coverage for audoma_api/models.py: 3%

1 import random

2 from itertools import cycle

3

4 from django.utils.functional import lazy

5

6 from audoma.choices import make_choices

7 from audoma.django.db import models

8

9

10# Create your models here.

11 class ExampleModel(models.Model):

12

13 EXAMPLE_CHOICES = make_choices(

14 "CHOICES",

15 (

16 (1, "EX_1", "example 1"),

17 (2, "EX_2", "example 2"),

18 (3, "EX_3", "example 3"),

19 ),

20 )

21 char_field = models.CharField(max_length=255)

22 phone_number = models.PhoneNumberField()

23 phone_number_example = models.PhoneNumberField(example="+123456789")

24 email = models.EmailField()

25 text_field = models.TextField()

26 url = models.URLField()

27 boolean = models.BooleanField()

28 nullboolean = models.BooleanField(null=True)

29 mac_adress = models.MACAddressField()

30 slug = models.SlugField()

31 uuid = models.UUIDField()

32 ip_address = models.GenericIPAddressField()

33 integer = models.IntegerField()

34 _float = models.FloatField()

35 decimal = models.DecimalField(decimal_places=2, max_digits=10)

36 datetime = models.DateTimeField()

37 date = models.DateField()

38 time = models.TimeField()

39 duration = models.DurationField()

40 choices = models.IntegerField(choices=EXAMPLE_CHOICES.get_choices())

41 json = models.JSONField()

42 money = models.MoneyField(decimal_places=2, max_digits=10)

43

44

45 example_countries = cycle(["United States", "Canada", "France", "Poland", "Italy"])

46 example_cities = ["New York", "Athens", "Toronto", "Rome", "Tokyo", "Oslo"]

47

48

49 def get_countries():

50 return next(example_countries)

51

52

53 def get_random_age():

54 return random.randint(18, 80)

55

56

57 class Country(models.Model):

58 name = models.CharField(max_length=225, example=get_countries)

59

60

61 class ExamplePerson(models.Model):

62 first_name = models.CharField(max_length=225, example="Adam")

63 last_name = models.CharField(max_length=255, example="Smith")

64 age = models.IntegerField(example=get_random_age)

65 email = models.EmailField(example="example_person@example.com")

66 birth_country = models.ForeignKey(Country, on_delete=models.CASCADE)

67 residence_city = models.CharField(

68 max_length=255, example=lazy(lambda: random.choice(example_cities), str)

69 )

70 has_valid_account = models.BooleanField()

71 ip_address = models.GenericIPAddressField()

72 savings = models.MoneyField(max_digits=14, decimal_places=2, default_currency="PLN")

73 phone_number = models.PhoneNumberField(region="IT")

74

75

76 class ExampleFileModel(models.Model):

77 file_field = models.FileField()

78 name = models.CharField(max_length=255)

79

80

81 class Manufacturer(models.Model):

82 name = models.CharField(max_length=255)

83 slug_name = models.SlugField()

84

85

86 class Car(models.Model):

87

88 CAR_BODY_TYPES = make_choices(

89 "BODY_TYPES",

90 (

91 (1, "SEDAN", "Sedan"),

92 (2, "COUPE", "Coupe"),

93 (3, "HATCHBACK", "Hatchback"),

94 (4, "PICKUP", "Pickup Truck"),

95 ),

96 )

97

98 ENGINE_TYPES = make_choices(

99 "ENGINE_TYPES",

100 (

101 (1, "PETROL", "Petrol"),

102 (2, "DIESEL", "Diesel"),

103 (3, "ELECTRIC", "Electric"),

104 (4, "HYBRID", "Hybrid"),

105 ),

106 )

107

108 name = models.CharField(max_length=255)

109 body_type = models.IntegerField(choices=CAR_BODY_TYPES.get_choices())

110

111 manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)

112 engine_size = models.FloatField()

113 engine_type = models.IntegerField(choices=ENGINE_TYPES.get_choices())