litestar-org / litestar-org/polyfactory

Bug: different number of minimum characters for a string generators, if field meta in SQLAlchemyFactory have constraints

Open Beginner friendly
#880 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Description

Different number of minimum characters for a string, if field meta in SQLAlchemyFactory have constraints

### URL to code causing the issue

https://github.com/litestar-org/polyfactory/blob/main/polyfactory/value_generators/primitives.py

### MCVE

```python

```

### Steps to reproduce

```bash
If we get `field_meta` for a `String` column with non-empty constraints and without `min_lengths`, then in `process_kwargs` in `get_field_value`, we will call the `get_constrained_field_value` function, which will eventually call the `create_random_bytes` function with `min_length=None`, and this function will set `min_length=0`. Although `faker.pystr()`, which we would use, generates strings of at least 4 characters.

Because of this, it often happened to me that my unique fields with a limit on the maximum number of characters generated empty lines for me and caused an error limiting the integrity of unique elements for a column.

It was hard to detect this behavior. I think it would be better to have `min_length` default to 4 when `None` is passed to `create_random_bytes`, or to catch `min_length=None` in `create_random_string` and set it to 4.
```

### Screenshots

"In the format of: `![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)`"

### Logs

```bash

```

### Release Version

3.3.0

### Platform

- [x] Linux
- [ ] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)

Contributor guide

Open the contributing guide

Research direction

Start in polyfactory/value_generators/primitives.py and trace create_random_bytes, create_random_string, and get_constrained_field_value when min_length is None. Reproduce the SQLAlchemyFactory case with a constrained String field and inspect the generated lengths. Done means the minimum length is consistent with the intended behavior and regression coverage verifies it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.