Factory Boy Base64 Encoded String Faker

419 Views Asked by At

Is it possible to fake a base64 encoded string within factory-boy? For example, in a given factory, I'm able to fake (generate) a random name by doing name = factory.Faker("name"). In addition, I can fake an email by doing email = factory.Faker("email").

However, I'm not sure how to fake a base64 encoded string. In my model, I have a field which is a simple CharField as such: encoded_string = models.CharField(max_length=512, blank=True, null=True). How can I go by faking this within my factory?

I have tried several options, but they all result in errors.

base64.b64encode("sample_string".encode("ascii")) results in:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 3: invalid start byte

Doing base64.b64encode(bytes("[{\"key\":\"Key Example:\",\"value\":\"Value Example\\\"\"}]", 'utf-8')) results in:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 5: invalid start byte

Doing base64.b64encode(b'data to be encoded') results in:

binascii.Error: Invalid base64-encoded string: number of data characters (25) cannot be 1 more than a multiple of 4

0

There are 0 best solutions below