Skip to content

Commit

Permalink
minor fixes in JSON generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa-mahmoud authored and Mostafa M. Mohamed committed Sep 26, 2022
1 parent 5b366ce commit 92e60a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
18 changes: 13 additions & 5 deletions djenerator/core/fields_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module has a function that matches django fields to the corresponding
random value generator.
"""
import json
import os
import random
import warnings
Expand Down Expand Up @@ -324,18 +325,25 @@ def generate_random_value(field):
extensions = kwargs["extensions"][:]
del kwargs["extensions"]
else:
extensions = [".txt", ".json"]
if ".txt" in extensions and ".json" in extensions:
extensions = [".txt", ".json"]
elif ".txt" in extensions:
extensions = [".txt"]
if ".txt" in extensions:
extensions = [".txt"]
elif ".json" in extensions:
extensions = [".json"]
else:
warn = True
name = generate_file_name(12, extensions=extensions)
if warn:
warnings.warn(
"Native text (not following the file extension) is written in "
+ str(os.path.join(field.upload_to, name))
"Native text (not following the file extension) is "
"written in " + str(os.path.join(field.upload_to, name))
)
txt = generate_text(**kwargs)
if name.endswith(".json"):
txt = json.dumps(generate_json(**kwargs))
else:
txt = generate_text(**kwargs)
content = ContentFile(txt)
val = FieldFile(content, field, name)
val.save(name, content, False)
Expand Down
17 changes: 10 additions & 7 deletions djenerator/core/values_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def generate_dictionary():
WORDS_DICTIONARY = generate_dictionary()


def generate_text(max_length, min_length=0, sep=" "):
def generate_text(max_length=None, min_length=0, sep=" "):
max_length = max_length or 1000
if max_length <= 8:
return generate_sentence(max_length)

Expand Down Expand Up @@ -468,24 +469,26 @@ def generate_integer_list(
return res


def generate_json(depth=0):
def generate_json(depth=0, max_length=None):
if random.random() < 1 - 1 / (1 + depth):
return random.choice([
generate_small_integer(),
generate_small_integer(),
generate_small_integer(),
True,
False,
# None,
None,
] + (
choices(WORDS_DICTIONARY[3], 3) +
choices(WORDS_DICTIONARY[5], 3) +
choices(WORDS_DICTIONARY[7], 3)
list(map(lambda x: x.lower(), choices(WORDS_DICTIONARY[3], 3))) +
list(map(lambda x: x.lower(), choices(WORDS_DICTIONARY[4], 3))) +
list(map(lambda x: x.lower(), choices(WORDS_DICTIONARY[5], 3))) +
list(map(lambda x: x.lower(), choices(WORDS_DICTIONARY[7], 3)))
))

values = [generate_json(depth + 1) for _ in range(0, 4)]
words = [
random.choice(WORDS_DICTIONARY[random.randint(3, 7)]) for _ in values
random.choice(WORDS_DICTIONARY[random.randint(3, 7)]).lower()
for _ in values
]
if random.random() < 0.5:
return values
Expand Down
36 changes: 19 additions & 17 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,26 @@ class TestModelFieldsTwo(models.Model):
fieldH = models.BooleanField()
fieldZ = models.ManyToManyField(TestModelE)
fieldQ = models.FileField(
upload_to=os.path.join("media", "trash"),
validators=(
[validators.FileExtensionValidator([".rst", ".json", ".txt"])]
if hasattr(validators, "FileExtensionValidator") else []
)
)
fieldR = models.FileField(
upload_to=os.path.join("media", "trash"),
validators=(
[validators.FileExtensionValidator([".rst"])]
if hasattr(validators, "FileExtensionValidator") else []
)
)
fieldI = models.ImageField(
upload_to=os.path.join('media', 'trash'),
validators=(
[validators.FileExtensionValidator([".png", ".jpg"])]
if hasattr(validators, "FileExtensionValidator") else []
)
)


class TestModelFields(models.Model):
Expand Down Expand Up @@ -226,7 +240,7 @@ class CycleF(models.Model):
class AllFieldsModel(models.Model):
bigint_field = models.BigIntegerField()
int_field = models.IntegerField()
email_field = models.EmailField(max_length=40)
email_field = models.EmailField()
bool_field = models.BooleanField()
char_field = models.CharField(max_length=100)
decimal_field = models.DecimalField(max_digits=15, decimal_places=8)
Expand All @@ -243,7 +257,7 @@ class AllFieldsModel(models.Model):
if hasattr(models, "PositiveBigIntegerField") else
models.BigIntegerField()
)
text_field = models.TextField(max_length=500)
text_field = models.TextField()
time_field = models.TimeField()
gen_ip_field = models.GenericIPAddressField()
url_field = models.URLField()
Expand All @@ -252,21 +266,9 @@ class AllFieldsModel(models.Model):
file_path_field = models.FilePathField()
# binary_field = models.BinaryField(max_length=200)
binary_field = models.BinaryField()
file_field = models.FileField(
upload_to=os.path.join('media', 'files'),
validators=(
[validators.FileExtensionValidator([".txt", ".rst"])]
if hasattr(validators, "FileExtensionValidator") else []
)
)
image_field = models.ImageField(
upload_to=os.path.join('media', 'images'),
validators=(
[validators.FileExtensionValidator([".png", ".jpg"])]
if hasattr(validators, "FileExtensionValidator") else []
)
)
file_field = models.FileField(upload_to=os.path.join('media', 'files'))
image_field = models.ImageField(upload_to=os.path.join('media', 'images'))
js_field = (
models.JSONField() if hasattr(models, "JSONField")
else models.TextField(max_length=100)
else models.TextField()
)

0 comments on commit 92e60a5

Please sign in to comment.