-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean up tests and minor hygiene
- remove spuriously additional build_aip from archive creation - start to refactor various Factory test mock classes - add a flag defer_fs currently only used by tests but in the future could support creating the published archive asynchronously as a scheduled task (huey / temporal / etc) - hypothesis tests were generating inconsistent results due to issues with our state generation for Codebases, CodebaseRelease, Users, etc. switching to get_or_create for now, this may have downstream effects but probably shouldn't - minor logger tuning to remove unnecessary messaging
- Loading branch information
Showing
9 changed files
with
118 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from os import path | ||
|
||
from .defaults import * | ||
|
||
from os import path | ||
|
||
DEPLOY_ENVIRONMENT = Environment.TEST | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ | |
import shlex | ||
import shutil | ||
import subprocess | ||
from datetime import date, timedelta | ||
|
||
from datetime import date, timedelta | ||
from django.conf import settings | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
|
@@ -17,6 +17,8 @@ | |
|
||
logger = logging.getLogger(__name__) | ||
|
||
User = get_user_model() | ||
|
||
|
||
class ContentModelFactory(ABC): | ||
def __init__(self, submitter): | ||
|
@@ -57,6 +59,9 @@ def create(self, **overrides): | |
content.save() | ||
return content | ||
|
||
def get_or_create(self, **kwargs): | ||
return self.model.objects.get_or_create(**kwargs) | ||
|
||
def create_unsaved(self, **overrides): | ||
kwargs = self.get_default_data() | ||
kwargs.update(overrides) | ||
|
@@ -95,21 +100,17 @@ def get_default_data(self): | |
} | ||
|
||
|
||
def make_user( | ||
username="test_user", | ||
password="default.testing.password", | ||
email="[email protected]", | ||
def create_test_user( | ||
username="test_user", email="[email protected]", **kwargs | ||
): | ||
factory = UserFactory() | ||
return factory.create(username=username, password=password, email=email), factory | ||
factory = UserFactory(username=username, email=email, **kwargs) | ||
return factory.create(), factory | ||
|
||
|
||
class UserFactory: | ||
def __init__(self, **defaults): | ||
if not defaults.get("password"): | ||
defaults["password"] = "test" | ||
self.id = 0 | ||
self.password = defaults.get("password") | ||
self.password = defaults.get("password", "testing-password") | ||
self.defaults = {} | ||
username = defaults.get("username") | ||
if username: | ||
|
@@ -119,41 +120,41 @@ def __init__(self, **defaults): | |
self.defaults.update({"email": email}) | ||
|
||
def extract_password(self, overrides): | ||
if overrides.get("password"): | ||
if "password" in overrides: | ||
return overrides.pop("password") | ||
else: | ||
return self.password | ||
|
||
def get_default_data(self): | ||
defaults = self.defaults.copy() | ||
defaults["username"] = defaults.get("username", "submitter{}".format(self.id)) | ||
defaults["username"] = defaults.get("username", f"submitter{self.id}") | ||
self.id += 1 | ||
return defaults | ||
|
||
def create(self, **overrides): | ||
user = self.create_unsaved(**overrides) | ||
def create(self, **kwargs): | ||
return self.get_or_create(**kwargs) | ||
|
||
def get_or_create(self, **kwargs): | ||
password = self.extract_password(kwargs) | ||
default_data = self.get_default_data() | ||
if "email" not in kwargs: | ||
default_email = f"{default_data['username']}@mailinator.com" | ||
kwargs.update(email=default_email) | ||
if "username" not in kwargs: | ||
kwargs.update(username=default_data["username"]) | ||
user, created = User.objects.get_or_create(**kwargs) | ||
user.set_password(password) | ||
user.save() | ||
return user | ||
|
||
def create_unsaved(self, **overrides): | ||
password = self.extract_password(overrides) | ||
kwargs = self.get_default_data() | ||
kwargs.update(overrides) | ||
if not kwargs.get("email"): | ||
kwargs["email"] = "{}@gmail.com".format(kwargs["username"]) | ||
user = User(**kwargs) | ||
if password: | ||
user.set_password(password) | ||
return user | ||
|
||
|
||
class BaseModelTestCase(TestCase): | ||
def setUp(self): | ||
self.user = self.create_user() | ||
|
||
def create_user(self, username="test_user", password="test", **kwargs): | ||
kwargs.setdefault("email", "[email protected]") | ||
return User.objects.create_user(username=username, password=password, **kwargs) | ||
def create_user(self, **kwargs): | ||
user, factory = create_test_user(**kwargs) | ||
return user | ||
|
||
|
||
def initialize_test_shared_folders(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.