Skip to content

Commit 2f5f487

Browse files
cofiemdi
andauthored
Use factory.Sequence with faker.unique to remove the chance of duplicate values (#17740)
* use a unique faker class to remove the chance of duplicate values Replace faker providers known to occasionally fail with the unique faker (safe_email, user_name, ipv4_private Based on: FactoryBoy/factory_boy#305 See also: FactoryBoy/factory_boy#820 closes #17734 relates to #15398 * use existing approach for getting unique faker sequences in tests --------- Co-authored-by: Dustin Ingram <[email protected]>
1 parent 9d8342c commit 2f5f487

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

tests/common/db/accounts.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ class Meta:
112112
model = Email
113113

114114
user = factory.SubFactory(UserFactory)
115-
email = factory.Faker("safe_email")
115+
116+
# TODO: Replace when factory_boy supports `unique`.
117+
# See https://github.com/FactoryBoy/factory_boy/pull/997
118+
email = factory.Sequence(lambda _: fake.unique.safe_email())
119+
116120
verified = True
117121
primary = True
118122
public = False
@@ -133,4 +137,6 @@ class ProhibitedUsernameFactory(WarehouseFactory):
133137
class Meta:
134138
model = ProhibitedUserName
135139

136-
name = factory.Faker("user_name")
140+
# TODO: Replace when factory_boy supports `unique`.
141+
# See https://github.com/FactoryBoy/factory_boy/pull/997
142+
name = factory.Sequence(lambda _: fake.unique.user_name())

tests/common/db/ip_addresses.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@
1313
import hashlib
1414

1515
import factory
16+
import faker
1617

1718
from warehouse.ip_addresses.models import IpAddress
1819

1920
from .base import WarehouseFactory
2021

22+
fake = faker.Faker()
23+
2124

2225
class IpAddressFactory(WarehouseFactory):
2326
class Meta:
2427
model = IpAddress
2528

26-
ip_address = factory.Faker("ipv4_private")
29+
# TODO: Replace when factory_boy supports `unique`.
30+
# See https://github.com/FactoryBoy/factory_boy/pull/997
31+
ip_address = factory.Sequence(lambda _: fake.unique.ipv4_private())
32+
2733
hashed_ip_address = factory.LazyAttribute(
2834
lambda o: hashlib.sha256(o.ip_address.encode("utf8")).hexdigest()
2935
)

tests/common/db/oidc.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# limitations under the License.
1212

1313
import factory
14+
import faker
1415

1516
from warehouse.oidc.models import (
1617
ActiveStatePublisher,
@@ -26,6 +27,8 @@
2627
from .accounts import UserFactory
2728
from .base import WarehouseFactory
2829

30+
fake = faker.Faker()
31+
2932

3033
class GitHubPublisherFactory(WarehouseFactory):
3134
class Meta:
@@ -82,7 +85,11 @@ class Meta:
8285
model = GooglePublisher
8386

8487
id = factory.Faker("uuid4", cast_to=None)
85-
email = factory.Faker("safe_email")
88+
89+
# TODO: Replace when factory_boy supports `unique`.
90+
# See https://github.com/FactoryBoy/factory_boy/pull/997
91+
email = factory.Sequence(lambda _: fake.unique.safe_email())
92+
8693
sub = factory.Faker("pystr", max_chars=12)
8794

8895

@@ -92,7 +99,11 @@ class Meta:
9299

93100
id = factory.Faker("uuid4", cast_to=None)
94101
project_name = "fake-nonexistent-project"
95-
email = factory.Faker("safe_email")
102+
103+
# TODO: Replace when factory_boy supports `unique`.
104+
# See https://github.com/FactoryBoy/factory_boy/pull/997
105+
email = factory.Sequence(lambda _: fake.unique.safe_email())
106+
96107
sub = factory.Faker("pystr", max_chars=12)
97108
added_by = factory.SubFactory(UserFactory)
98109

tests/common/db/packaging.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ class Meta:
107107

108108
release = factory.SubFactory(ReleaseFactory)
109109
python_version = "source"
110-
# TODO: Replace when factory_boy supports `unique`. See https://git.io/JM6kx
110+
111+
# TODO: Replace when factory_boy supports `unique`.
112+
# See https://github.com/FactoryBoy/factory_boy/pull/997
111113
filename = factory.Sequence(lambda _: fake.unique.file_name())
114+
112115
md5_digest = factory.LazyAttribute(
113116
lambda o: hashlib.md5(o.filename.encode("utf8")).hexdigest()
114117
)

tests/common/db/ses.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import datetime
1414

1515
import factory
16+
import faker
1617

1718
from warehouse.email.ses.models import EmailMessage, Event, EventTypes
1819

1920
from .base import WarehouseFactory
2021

22+
fake = faker.Faker()
23+
2124

2225
class EmailMessageFactory(WarehouseFactory):
2326
class Meta:
@@ -29,8 +32,12 @@ class Meta:
2932
- datetime.timedelta(days=14),
3033
)
3134
message_id = factory.Faker("pystr", max_chars=12)
32-
from_ = factory.Faker("safe_email")
33-
to = factory.Faker("safe_email")
35+
36+
# TODO: Replace when factory_boy supports `unique`.
37+
# See https://github.com/FactoryBoy/factory_boy/pull/997
38+
from_ = factory.Sequence(lambda _: fake.unique.safe_email())
39+
to = factory.Sequence(lambda _: fake.unique.safe_email())
40+
3441
subject = factory.Faker("sentence")
3542

3643

tests/common/db/subscriptions.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# limitations under the License.
1212

1313
import factory
14+
import faker
1415

1516
from warehouse.subscriptions.models import (
1617
StripeCustomer,
@@ -23,14 +24,19 @@
2324

2425
from .base import WarehouseFactory
2526

27+
fake = faker.Faker()
28+
2629

2730
class StripeCustomerFactory(WarehouseFactory):
2831
class Meta:
2932
model = StripeCustomer
3033

3134
id = factory.Faker("uuid4", cast_to=None)
3235
customer_id = factory.Faker("uuid4")
33-
billing_email = factory.Faker("safe_email")
36+
37+
# TODO: Replace when factory_boy supports `unique`.
38+
# See https://github.com/FactoryBoy/factory_boy/pull/997
39+
billing_email = factory.Sequence(lambda _: fake.unique.safe_email())
3440

3541

3642
class StripeSubscriptionProductFactory(WarehouseFactory):

0 commit comments

Comments
 (0)