Skip to content

Commit

Permalink
Merge pull request #3077 from SEED-platform/Remove-warnings-from-tests
Browse files Browse the repository at this point in the history
Remove warnings from tests
  • Loading branch information
macintoshpie committed Jan 12, 2022
2 parents 6b89d84 + 76078b2 commit 9d08a30
Show file tree
Hide file tree
Showing 38 changed files with 307 additions and 172 deletions.
3 changes: 2 additions & 1 deletion config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
'BACKENDS': {
'default': 'smtp.EmailBackend',
'post_office_backend': 'django.core.mail.backends.console.EmailBackend',
}
},
'CELERY_ENABLED': True,
}


Expand Down
3 changes: 2 additions & 1 deletion config/settings/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
'BACKENDS': {
'default': EMAIL_BACKEND,
'post_office_backend': EMAIL_BACKEND,
}
},
'CELERY_ENABLED': True,
}

# PostgreSQL DB config
Expand Down
4 changes: 2 additions & 2 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
# built documents.
#
# Grab the version from the package.json file
f = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../package.json"))
data = json.load(f)
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../package.json")) as f:
data = json.load(f)

version = data['version']
# The full version, including alpha/beta/rc tags.
Expand Down
6 changes: 3 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ django-pint==0.6
djangorestframework==3.12.2
# Django-post-office dependency needs to be installed via github b/c
# with pypi version Celery fails to auto discover post_office tasks
-e git+https://github.com/ui/django-post_office.git@v3.5.2#egg=django-post_office
-e git+https://github.com/ui/django-post_office.git@v3.6.0#egg=django-post_office
drf-yasg==1.20.0
django-filter==2.4.0
drf-nested-routers==0.91
Expand All @@ -36,7 +36,7 @@ drf-nested-routers==0.91
raven==6.10.0

# Various packages
jellyfish==0.7.2
jellyfish==0.8.2
Markdown==3.1.1
pyyaml==5.4.1
polling==0.3.2
Expand Down Expand Up @@ -66,7 +66,7 @@ django-braces==1.14.0
# Use forked jwt-oauth2 b/c the published version broke when updating to Django 3.x
# See: https://docs.djangoproject.com/en/dev/releases/3.0/#removed-private-python-2-compatibility-apis
# jwt-oauth2>=0.1.1
-e git+https://github.com/SEED-platform/jwt-oauth2.git@0490b817b9b74b7363c4b39fc0a175f58a14c967#egg=jwt-oauth2
-e git+https://github.com/SEED-platform/jwt-oauth2.git@9726367db36b332c84fb1288a7df32196509c06d#egg=jwt-oauth2
django-oauth-toolkit==1.2.0

future==0.18.2
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Faker==0.9.3
mock==2.0.0
nose-exclude==0.5.0
coveralls-merge==0.0.3
vcrpy==2.0.1
vcrpy==4.0.0

# static code analysis
flake8==3.8.1
Expand Down
20 changes: 10 additions & 10 deletions seed/building_sync/tests/test_buildingsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def test_file_type_lookup(self):

def test_buildingsync_constructor(self):
filename = path.join(path.dirname(__file__), 'data', 'ex_1.xml')
file = open(filename, 'rb')
simple_uploaded_file = SimpleUploadedFile(file.name, file.read())
with open(filename, 'rb') as f:
simple_uploaded_file = SimpleUploadedFile(f.name, f.read())

bf = BuildingFile.objects.create(
file=simple_uploaded_file,
Expand All @@ -55,8 +55,8 @@ def test_buildingsync_constructor(self):

def test_buildingsync_constructor_diff_ns(self):
filename = path.join(path.dirname(__file__), 'data', 'ex_1_different_namespace.xml')
file = open(filename, 'rb')
simple_uploaded_file = SimpleUploadedFile(file.name, file.read())
with open(filename, 'rb') as f:
simple_uploaded_file = SimpleUploadedFile(f.name, f.read())

bf = BuildingFile.objects.create(
file=simple_uploaded_file,
Expand All @@ -72,8 +72,8 @@ def test_buildingsync_constructor_diff_ns(self):
def test_buildingsync_constructor_single_scenario(self):
# test having only 1 measure and 1 scenario
filename = path.join(path.dirname(__file__), 'data', 'test_single_scenario.xml')
file = open(filename, 'rb')
simple_uploaded_file = SimpleUploadedFile(file.name, file.read())
with open(filename, 'rb') as f:
simple_uploaded_file = SimpleUploadedFile(f.name, f.read())

bf = BuildingFile.objects.create(
file=simple_uploaded_file,
Expand All @@ -88,8 +88,8 @@ def test_buildingsync_constructor_single_scenario(self):

def test_buildingsync_bricr_import(self):
filename = path.join(path.dirname(__file__), 'data', 'buildingsync_v2_0_bricr_workflow.xml')
file = open(filename, 'rb')
simple_uploaded_file = SimpleUploadedFile(file.name, file.read())
with open(filename, 'rb') as f:
simple_uploaded_file = SimpleUploadedFile(f.name, f.read())

bf = BuildingFile.objects.create(
file=simple_uploaded_file,
Expand All @@ -113,8 +113,8 @@ def test_buildingsync_bricr_import(self):
def test_buildingsync_audit_template_import(self):
audit_template_filename = 'example_SF_audit_report_BS_v2.3_081321.xml'
filename = path.join(path.dirname(__file__), 'data', audit_template_filename)
file = open(filename, 'rb')
simple_uploaded_file = SimpleUploadedFile(file.name, file.read())
with open(filename, 'rb') as f:
simple_uploaded_file = SimpleUploadedFile(f.name, f.read())

bf = BuildingFile.objects.create(
file=simple_uploaded_file,
Expand Down
52 changes: 27 additions & 25 deletions seed/building_sync/tests/test_buildingsync_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ def test_upload_and_get_building_sync(self):
filename = path.join(path.dirname(__file__), 'data', 'ex_1.xml')

url = reverse('api:v3:building_files-list') + f'?organization_id={self.org.id}&cycle_id={self.cycle.id}'
fsysparams = {
'file': open(filename, 'rb'),
'file_type': 'BuildingSync',
}
with open(filename, 'rb') as f:
response = self.client.post(url, {
'file': f,
'file_type': 'BuildingSync',
})

response = self.client.post(url, fsysparams)
self.assertEqual(response.status_code, 200)
result = json.loads(response.content)
self.assertEqual(result['status'], 'success')
Expand All @@ -102,12 +102,12 @@ def test_upload_batch_building_sync(self):
filename = path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'ex_1_and_buildingsync_ex01_measures.zip')

url = f'/api/v3/building_files/?organization_id={self.org.id}&cycle_id={self.cycle.id}'
fsysparams = {
'file': open(filename, 'rb'),
'file_type': 'BuildingSync',
}
with open(filename, 'rb') as f:
response = self.client.post(url, {
'file': f,
'file_type': 'BuildingSync',
})

response = self.client.post(url, fsysparams)
self.assertEqual(response.status_code, 200)
result = json.loads(response.content)

Expand All @@ -121,11 +121,12 @@ def test_upload_with_measure_duplicates(self):
filename = path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'buildingsync_ex01_measures_bad_names.xml')

url = reverse('api:v3:building_files-list') + f'?organization_id={self.org.id}&cycle_id={self.cycle.id}'
fsysparams = {
'file': open(filename, 'rb'),
'file_type': 'BuildingSync',
}
response = self.client.post(url, fsysparams)
with open(filename, 'rb') as f:
response = self.client.post(url, {
'file': f,
'file_type': 'BuildingSync',
})

self.assertEqual(response.status_code, 200)
result = json.loads(response.content)
self.assertEqual(result['status'], 'success')
Expand All @@ -145,11 +146,12 @@ def test_upload_with_measure_duplicates(self):

# upload the same file again
url = reverse('api:v3:building_files-list') + f'?organization_id={self.org.id}&cycle_id={self.cycle.id}'
fsysparams = {
'file': open(filename, 'rb'),
'file_type': 'BuildingSync',
}
response = self.client.post(url, fsysparams)
with open(filename, 'rb') as f:
response = self.client.post(url, {
'file': f,
'file_type': 'BuildingSync',
})

self.assertEqual(response.status_code, 200)
result = json.loads(response.content)

Expand All @@ -161,12 +163,12 @@ def test_upload_and_get_building_sync_diff_ns(self):

url = reverse('api:v3:building_files-list') + f'?organization_id={self.org.id}&cycle_id={self.cycle.id}'

fsysparams = {
'file': open(filename, 'rb'),
'file_type': 'BuildingSync',
}
with open(filename, 'rb') as f:
response = self.client.post(url, {
'file': f,
'file_type': 'BuildingSync',
})

response = self.client.post(url, fsysparams)
result = json.loads(response.content)
self.assertEqual(response.status_code, 200, f'Expected 200 response. Message body: {result}')
self.assertEqual(result['status'], 'success')
Expand Down
4 changes: 4 additions & 0 deletions seed/building_sync/tests/test_validation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def setUp(self):
self.single_file = open(os.path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'buildingsync_v2_0_bricr_workflow.xml'))
self.zip_file = open(os.path.join(BASE_DIR, 'seed', 'building_sync', 'tests', 'data', 'ex_1_and_buildingsync_ex01_measures.zip'))

def tearDown(self) -> None:
self.single_file.close()
self.zip_file.close()

def test_validation_single_file_ok(self):
good_body = {
'success': True,
Expand Down
3 changes: 2 additions & 1 deletion seed/building_sync/validation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import zipfile

import requests
import pathlib
from seed.building_sync.building_sync import BuildingSync


Expand All @@ -18,7 +19,7 @@ def _validation_api_post(file_, schema_version, use_case_name):
if zipfile.is_zipfile(file_.name):
files = [('file', file_)]
else:
files = {'file': (file_.name, open(file_.name, 'r').read(), 'application/xml')}
files = {'file': (file_.name, pathlib.Path(file_.name).read_text(), 'application/xml')}

return requests.request(
"POST",
Expand Down
4 changes: 4 additions & 0 deletions seed/data_importer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ def save(self, in_validation=False, *args, **kwargs):
pass
# If we're deleting.

def __del__(self):
if hasattr(self, '_local_file'):
self._local_file.close()

@property
def from_portfolio_manager(self):
return self._strcmp(self.source_program, 'PortfolioManager')
Expand Down
8 changes: 4 additions & 4 deletions seed/data_importer/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def map_row_chunk(ids, file_pk, source_type, prog_key, **kwargs):
STR_TO_CLASS[table](organization=map_model_obj.organization),
include_extra_data=False):
# Skip this object as it has no data...
_log.warn(
_log.warning(
"Skipping property or taxlot during mapping because it is identical to another row")
continue

Expand Down Expand Up @@ -1138,7 +1138,7 @@ def geocode_and_match_buildings_task(file_pk):
'Import file is not complete. Retry after mapping is complete', )

if import_file.cycle is None:
_log.warn("Import file cycle is None; This should never happen in production")
_log.warning("Import file cycle is None; This should never happen in production")

post_geocode_tasks = None
if import_file.from_buildingsync:
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def map_additional_models(file_pk):
'Import file is not complete. Retry after mapping is complete', )

if import_file.cycle is None:
_log.warn("This should never happen in production")
_log.warning("This should never happen in production")

source_type_dict = {
'Portfolio Raw': PORTFOLIO_RAW,
Expand Down Expand Up @@ -1346,7 +1346,7 @@ def match_buildings(file_pk):
'Import file is not complete. Retry after mapping is complete', )

if import_file.cycle is None:
_log.warn('This should never happen in production')
_log.warning('This should never happen in production')

# Start, match, pair
progress_data.total = 3
Expand Down
3 changes: 2 additions & 1 deletion seed/data_importer/tests/integration/test_case_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path as osp

from django.core.files.uploadedfile import SimpleUploadedFile
import pathlib

from seed.data_importer import tasks
from seed.data_importer.tests.util import (
Expand Down Expand Up @@ -37,7 +38,7 @@ def setUp(self):
filepath = osp.join(osp.dirname(__file__), '..', 'data', filename)
self.import_file.file = SimpleUploadedFile(
name=filename,
content=open(filepath, 'rb').read()
content=pathlib.Path(filepath).read_bytes()
)
self.import_file.save()

Expand Down
3 changes: 2 additions & 1 deletion seed/data_importer/tests/integration/test_case_b.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path as osp

from django.core.files.uploadedfile import SimpleUploadedFile
import pathlib

from seed.data_importer import tasks
from seed.data_importer.tests.util import (
Expand Down Expand Up @@ -40,7 +41,7 @@ def setUp(self):
filepath = osp.join(osp.dirname(__file__), '..', 'data', filename)
self.import_file.file = SimpleUploadedFile(
name=filename,
content=open(filepath, 'rb').read()
content=pathlib.Path(filepath).read_bytes()
)
self.import_file.save()

Expand Down

0 comments on commit 9d08a30

Please sign in to comment.