Procuret / procuret-swift

Procuret API Swift library
0 stars 1 forks source link

create a `TestEmail` struct #14

Closed hwjeremy closed 3 years ago

hwjeremy commented 3 years ago

Testing Procuret Swift requires email addresses at various points. For example, it is painful to manually specify a new email during every Human.create test.

Ideally, you could create emails as needed for testing.

Here is some Python code used by Procuret API's test suite. Use it as inspiration for the creation of a TestEmail struct that you can use during tests that require an email address.

"""
Procuret API
Test Email Module
author: hugh@blinkybeach.com
"""
from procuret_api.ancillary.immutable import Immutable
from procuret_api.security.random_number import RandomNumber

class TestEmail:
    """A randomised testing email"""
    _BIT_LENGTH = 48
    DOMAIN = 'procuret-test-domain.org'

    def __init__(self) -> None:

        self._seed = RandomNumber(self._BIT_LENGTH)
        return

    string = Immutable(lambda s: 'T' + s._seed.urlsafe_base64 + '@' + s.DOMAIN)

    def __str__(self) -> str:
        return self.string

Don't get too fixated on replicating the TestEmail Python class in Swift. Instead, try to consider what TestEmail does, and find a appropriate way to achieve the same objective in Swift. Put another way: Be inspired by the Python class, rather than trying to translate it precisely line by line.

Once you have created a TestEmail struct in Swift, use it in the Human.create test.

kaythoyet commented 3 years ago

@hwjeremy an update here. I thought about using a random number generator to generate the front portion of the email here, but in swift, it looks like in order to do that, I have to include a stop point and an end point. That method is simple, and would theoretically do the trick (I could set the numbers to any amount I wanted, between 1 and 10000 for example).

With a method like that, I feel like it's highly improbable we would end up with a duplication scenario, but it's still possible, right? I'm going to be working on a solution to see if I can try to avoid that today.

kaythoyet commented 3 years ago

@hwjeremy This is a simple workaround, but it does work. I'd like to figure out a way where we will never run into a potential for duplications, BUT for now I've given the random number sequence a wide enough birth that I think we should be fine. Here's the code:


//
//  TestEmail.swift
//  
//
//  Created by Kayla Hoyet on 10/22/21.
//

import Foundation

public struct TestEmail: Codable {

    static func generateEmail() -> String {

        let domain: String = "@procuret-test-domain.org"
        let randomNumber: Int = Int.random(in: 1000...99999)

        return String(randomNumber) + domain

    }
}

It turns out random number generations have gone through several rebirths since Swift came about, so when you initially assigned this task I found some conflicting information about the easiest way in which to tackle that part of what I wanted to do. I eventually settled on this for a few reasons:

  1. For now, it's less code, which is less confusing to both of us.
  2. It should be fairly easy to update or add on to later, should the need arise.
  3. It seems to serve the purpose we need it for.

Additional code inside the HumanTests.Swift file. Weirdly, this is what threw me for a bit of a loop. It turns out, I had initially declared things incorrectly inside the TestEmail.Swift file and it was throwing things off, so I updated the function. I also didn't include a catch here. It should be unnecessary.


func provideTestEmail() ->  String {

        do {
            return TestEmail.generateEmail()
        }
    }

I then replaced the generic email I'd used when initially testing Human.create with provideTestEmail().

Test successful.

Results below.


Test Suite 'Selected tests' started at 2021-10-25 16:32:32.784
Test Suite 'ProcuretAPITests.xctest' started at 2021-10-25 16:32:32.784
Test Suite 'HumanTests' started at 2021-10-25 16:32:32.784
Test Case '-[ProcuretAPITests.HumanTests testCreateHuman]' started.
Making request to Procuret API: /human|POST
Raw response data (if any) will be printed below.
--- Begin raw data returned by Procuret API ---
{
    "active_entity_id": null,
    "code": "399519",
    "created": "2021-10-25 20:32:33.715473",
    "disposition": {
        "count": 1,
        "limit": 1,
        "offset": 0,
        "sequence": 1
    },
    "documents": null,
    "email_address": {
        "confirmation_received": null,
        "confirmation_request_sent": null,
        "confirmation_required": false,
        "created": "2021-10-25 20:32:33.692710",
        "email_address": "78534@procuret-test-domain.org",
        "indexid": 203
    },
    "first_name": "TestKayla",
    "identity": null,
    "indexid": 27144684924846194,
    "last_name": "Test",
    "modified": "2021-10-25 20:32:33.715473",
    "other_name": null,
    "phone_number": {
        "confirmation_received": null,
        "confirmation_request_sent": null,
        "confirmation_required": false,
        "created": "2021-10-25 20:32:33.710086",
        "digits": "+17654615534",
        "indexid": 249,
        "latest_challenge_code": null,
        "permission_record": {
            "administered_by": [
                47472003359637183,
                103,
                102,
                100,
                100
            ],
            "managed_by": [],
            "owned_by": 27144684924846194,
            "readable_by": [
                5001,
                5002,
                5326185758106047,
                9474525592051103,
                46067396665687150,
                47472003359637183,
                47472003359637183
            ],
            "writable_by": []
        }
    },
    "public_id": 27144684924846194,
    "reset_key": null
}
--- End raw data returned by Procuret API ---
Test Case '-[ProcuretAPITests.HumanTests testCreateHuman]' passed (1.072 seconds).
Test Suite 'HumanTests' passed at 2021-10-25 16:32:33.857.
     Executed 1 test, with 0 failures (0 unexpected) in 1.072 (1.073) seconds
Test Suite 'ProcuretAPITests.xctest' passed at 2021-10-25 16:32:33.857.
     Executed 1 test, with 0 failures (0 unexpected) in 1.072 (1.073) seconds
Test Suite 'Selected tests' passed at 2021-10-25 16:32:33.858.
     Executed 1 test, with 0 failures (0 unexpected) in 1.072 (1.074) seconds
Program ended with exit code: 0