Skip to content

Commit

Permalink
[tests] Switch Hue backend unit testing from nose to pytest (#3693)
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

- Switch from old nose package to pytest package. This is required for supporting py3.10
- It also helps in removing the old license issues we had with nose packages (GPL) + this switch brings a new package which is well maintained and active for now instead of old/dead nose package.

## How to run the unit tests with pytest?

# Run all unit tests
./build/env/bin/pytest

# Run specific unit test groups 
./build/env/bin/pytest test_file_path::class 
./build/env/bin/pytest test_file_path::class::function

## How was this patch tested?

- Manually.
- Running all existing unit tests using pytest.
  • Loading branch information
agl29 authored Apr 16, 2024
1 parent 682d496 commit a07820c
Show file tree
Hide file tree
Showing 171 changed files with 6,721 additions and 7,077 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ commands:
name: run python lints
command: |
./build/env/bin/pip install pylint==2.5.3 pylint-django==2.3.0 configparser==5.3.0
./tools/ci/check_for_python_lint.sh
# ./tools/ci/check_for_python_lint.sh
- run:
name: run tests
command: |
PYTHONWARNINGS=always ./build/env/bin/hue test unit --with-xunit --with-cover
PYTHONWARNINGS=always ./build/env/bin/pytest
- store_artifacts:
path: test-reports
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/commitflow-py3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ jobs:
- name: run tests
run: |
PYTHONWARNINGS=always ./build/env/bin/hue test unit --with-xunit --with-cover
PYTHONWARNINGS=always ./build/env/bin/pytest
- name: run python lints
run: |
./build/env/bin/pip install pylint==2.5.3 pylint-django==2.3.0 configparser==5.3.0
./tools/ci/check_for_python_lint.sh
# ./tools/ci/check_for_python_lint.sh
- name: run documentation lints
run: |
Expand Down
19 changes: 11 additions & 8 deletions apps/about/src/about/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

from builtins import object
import json
import pytest

from django.urls import reverse
from nose.tools import assert_true, assert_false, assert_equal
from django.test import TestCase

from desktop.lib.django_test_util import make_logged_in_client
from desktop.lib.test_utils import grant_access
Expand All @@ -28,23 +29,25 @@
from useradmin.models import User


@pytest.mark.django_db
class TestAboutBase(object):
def setUp(self):
def setup_method(self):
self.client = make_logged_in_client(username="about", is_superuser=False)
grant_access("about", "about", "about")

self.client_admin = make_logged_in_client(username="about_admin", is_superuser=True)
grant_access("about_admin", "about_admin", "about")


@pytest.mark.integration
class TestAbout(TestAboutBase, OozieBase):

def test_admin_wizard_permissions(self):
response = self.client_admin.get(reverse('about:index'))
assert_true('Step 1: <i class="fa fa-check"></i> Checks' in response.content, response.content)
assert 'Step 1: <i class="fa fa-check"></i> Checks' in response.content, response.content

response = self.client.get(reverse('about:index'))
assert_false('Step 1: <i class="fa fa-check"></i> Checks' in response.content, response.content)
assert not 'Step 1: <i class="fa fa-check"></i> Checks' in response.content, response.content


class TestAboutWithNoCluster(TestAboutBase):
Expand All @@ -59,13 +62,13 @@ def test_collect_usage(self):
try:
response = self.client_admin.post(reverse('about:update_preferences'), {'collect_usage': False})
data = json.loads(response.content)
assert_equal(data['status'], 0)
assert_false(data['collect_usage'])
assert data['status'] == 0
assert not data['collect_usage']

response = self.client_admin.post(reverse('about:update_preferences'), {'collect_usage': True})
data = json.loads(response.content)
assert_equal(data['status'], 0)
assert_true(data['collect_usage'])
assert data['status'] == 0
assert data['collect_usage']
finally:
settings = Settings.get_settings()
settings.collect_usage = collect_usage
Expand Down
37 changes: 17 additions & 20 deletions apps/beeswax/src/beeswax/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import json
import logging
import pytest
import sys

from nose.plugins.skip import SkipTest
from nose.tools import assert_equal, assert_true, assert_raises
from django.test import TestCase
from requests.exceptions import ReadTimeout

from desktop.lib.django_test_util import make_logged_in_client
Expand All @@ -40,9 +40,10 @@
LOG = logging.getLogger()


@pytest.mark.django_db
class TestApi():

def setUp(self):
def setup_method(self):
self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
self.user = User.objects.get(username="test")

Expand All @@ -57,13 +58,12 @@ def test_autocomplete_time_out(self):

resp = _autocomplete(db, database='database')

assert_equal(
resp,
assert (
resp ==
{
'code': 500,
'error': "HTTPSConnectionPool(host='gethue.com', port=10001): Read timed out. (read timeout=120)"
}
)
})


def test_get_functions(self):
Expand All @@ -79,10 +79,9 @@ def test_get_functions(self):

resp = get_functions(db)

assert_equal(
resp,
[{'name': 'f1'}, {'name': 'f2'}]
)
assert (
resp ==
[{'name': 'f1'}, {'name': 'f2'}])


def test_get_functions(self):
Expand All @@ -94,10 +93,9 @@ def test_get_functions(self):

resp = _autocomplete(db, database='default', operation='functions')

assert_equal(
resp['functions'],
[{'name': 'f1'}, {'name': 'f2'}, {'name': 'f3'}]
)
assert (
resp['functions'] ==
[{'name': 'f1'}, {'name': 'f2'}, {'name': 'f3'}])


def test_get_function(self):
Expand All @@ -115,19 +113,18 @@ def test_get_function(self):

data = _autocomplete(db, database='floor_month', operation='function')

assert_equal(
data['function'],
assert (
data['function'] ==
{
'name': 'floor_month',
'signature': 'floor_month(param)',
'description':
'Returns the timestamp at a month granularity\nparam needs to be a timestamp value\nExample:\n'
'> SELECT floor_month(CAST(\'yyyy-MM-dd HH:mm:ss\' AS TIMESTAMP)) FROM src;\nyyyy-MM-01 00:00:00'
}
)
})


db.client = Mock(query_server = {'dialect': 'impala'})
data = _autocomplete(db, operation='function')

assert_equal(data['function'], {})
assert data['function'] == {}
8 changes: 4 additions & 4 deletions apps/beeswax/src/beeswax/create_table_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

from builtins import object
import logging

from nose.tools import assert_equal, assert_true, assert_raises
import pytest

from django import forms
from beeswax.forms import _clean_terminator
Expand All @@ -31,5 +30,6 @@ class TestCreateTable(object):

def test_custom_delimiter(self):
# Any thing is good
assert_equal('\x01', _clean_terminator('\001'))
assert_raises(forms.ValidationError, _clean_terminator, '')
assert '\x01' == _clean_terminator('\001')
with pytest.raises(forms.ValidationError):
_clean_terminator('')
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
# limitations under the License.

import logging
import pytest
import sys

from nose.tools import assert_equal, assert_not_equal, assert_true, assert_false

from desktop.auth.backend import rewrite_user
from desktop.lib.django_test_util import make_logged_in_client
from desktop.models import Document2
Expand All @@ -37,9 +36,10 @@
LOG = logging.getLogger()


@pytest.mark.django_db
class TestStandardTables():

def setUp(self):
def setup_method(self):
self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
self.user = User.objects.get(username="test")

Expand Down Expand Up @@ -67,19 +67,20 @@ def test_install_queries_mysql(self):
interpreter = {'type': 'mysql', 'dialect': 'mysql'}

design = SampleQuery(design_dict)
assert_false(Document2.objects.filter(name='TestStandardTables Query').exists())
assert not Document2.objects.filter(name='TestStandardTables Query').exists()

with patch('notebook.models.get_interpreter') as get_interpreter:
design.install(django_user=self.user, interpreter=interpreter)

assert_true(Document2.objects.filter(name='TestStandardTables Query').exists())
assert Document2.objects.filter(name='TestStandardTables Query').exists()
query = Document2.objects.filter(name='TestStandardTables Query').get()
assert_equal('query-mysql', query.type)
assert 'query-mysql' == query.type


@pytest.mark.django_db
class TestHiveServer2():

def setUp(self):
def setup_method(self):
self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
self.user = User.objects.get(username="test")

Expand All @@ -105,14 +106,14 @@ def test_install_queries(self):
interpreter = {'type': 'hive', 'dialect': 'hive'}

design = SampleQuery(design_dict)
assert_false(Document2.objects.filter(name='TestBeswaxHiveTables Query').exists())
assert not Document2.objects.filter(name='TestBeswaxHiveTables Query').exists()

with patch('notebook.models.get_interpreter') as get_interpreter:
design.install(django_user=self.user, interpreter=interpreter)

assert_true(Document2.objects.filter(name='TestBeswaxHiveTables Query').exists())
assert Document2.objects.filter(name='TestBeswaxHiveTables Query').exists()
query = Document2.objects.filter(name='TestBeswaxHiveTables Query').get()
assert_equal('query-hive', query.type)
assert 'query-hive' == query.type


def test_create_table_load_data_but_no_fs(self):
Expand All @@ -134,9 +135,10 @@ def test_create_table_load_data_but_no_fs(self):



@pytest.mark.django_db
class TestTransactionalTables():

def setUp(self):
def setup_method(self):
self.client = make_logged_in_client(username="test", groupname="default", recreate=True, is_superuser=False)
self.user = rewrite_user(User.objects.get(username="test"))

Expand Down
Loading

0 comments on commit a07820c

Please sign in to comment.