Commit 4f7419b7 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[libraries][tests] Cleanup

parent f491bc0e
...@@ -36,13 +36,20 @@ from django.conf import settings ...@@ -36,13 +36,20 @@ from django.conf import settings
from ...dataformats.models import DataFormat from ...dataformats.models import DataFormat
from ...common.testutils import BaseTestCase, tearDownModule from ...common.testutils import BaseTestCase
from ...common.testutils import ( # noqa test runner will call it, tearDownModule
tearDownModule,
)
from ...team.models import Team from ...team.models import Team
from ..models import Library from ..models import Library
from beat.core.library import Storage from beat.core.library import Storage
TEST_PWD = "1234"
class LibrariesBaseTestCase(BaseTestCase): class LibrariesBaseTestCase(BaseTestCase):
class Meta: class Meta:
model = Library model = Library
...@@ -51,29 +58,24 @@ class LibrariesBaseTestCase(BaseTestCase): ...@@ -51,29 +58,24 @@ class LibrariesBaseTestCase(BaseTestCase):
if os.path.exists(settings.LIBRARIES_ROOT): if os.path.exists(settings.LIBRARIES_ROOT):
shutil.rmtree(settings.LIBRARIES_ROOT) shutil.rmtree(settings.LIBRARIES_ROOT)
def tearDown(self): def tearDown(self):
if os.path.exists(settings.LIBRARIES_ROOT): if os.path.exists(settings.LIBRARIES_ROOT):
shutil.rmtree(settings.LIBRARIES_ROOT) shutil.rmtree(settings.LIBRARIES_ROOT)
class LibrariesCreationFunction(LibrariesBaseTestCase): class LibrariesCreationFunction(LibrariesBaseTestCase):
def test_without_code(self): def test_without_code(self):
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234') user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user, author=user, name="library1", short_description="some description"
name='library1',
short_description='some description',
) )
assert library, errors self.assertIsNotNone(library, errors)
storage = Storage(settings.PREFIX, storage = Storage(settings.PREFIX, library.fullname())
library.fullname()) storage.language = "python"
storage.language = 'python'
assert storage.exists() self.assertTrue(storage.exists())
self.assertEqual(library.author, user) self.assertEqual(library.author, user)
...@@ -81,12 +83,11 @@ class LibrariesCreationFunction(LibrariesBaseTestCase): ...@@ -81,12 +83,11 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
self.assertNotEqual(len(declaration), 0) self.assertNotEqual(len(declaration), 0)
declaration = json.loads(declaration) declaration = json.loads(declaration)
storage.language = declaration['language'] storage.language = declaration["language"]
code = storage.code.try_load() code = storage.code.try_load()
self.assertNotEqual(len(code), 0) self.assertNotEqual(len(code), 0)
def test_with_code(self): def test_with_code(self):
declaration = """{ declaration = """{
"description": "some description", "description": "some description",
...@@ -100,22 +101,18 @@ class LibrariesCreationFunction(LibrariesBaseTestCase): ...@@ -100,22 +101,18 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
""" """
user = User.objects.create_user('johndoe', 'johndoe@test.org', '1234') user = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user, author=user, name="library1", declaration=declaration, code=code
name='library1',
declaration=declaration,
code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
storage = Storage(settings.PREFIX, storage = Storage(settings.PREFIX, library.fullname())
library.fullname()) storage.language = "python"
storage.language = 'python'
assert storage.exists() self.assertTrue(storage.exists())
self.assertEqual(library.author, user) self.assertEqual(library.author, user)
self.assertTrue(library.parameters) self.assertTrue(library.parameters)
...@@ -123,29 +120,36 @@ class LibrariesCreationFunction(LibrariesBaseTestCase): ...@@ -123,29 +120,36 @@ class LibrariesCreationFunction(LibrariesBaseTestCase):
parameters = json.loads(library.parameters) parameters = json.loads(library.parameters)
self.assertEqual(len(parameters), 1) self.assertEqual(len(parameters), 1)
self.assertEqual(parameters[0]['name'], 'dummy') self.assertEqual(parameters[0]["name"], "dummy")
in_storage = json.loads(storage.json.load()) in_storage = json.loads(storage.json.load())
expected = json.loads(declaration) expected = json.loads(declaration)
assert in_storage == expected, "There are differences on the " \ self.assertEqual(
"declaration (beyond white spaces): %r != %r" % \ in_storage,
(in_storage, expected) expected,
"There are differences on the "
"declaration (beyond white spaces): %r != %r" % (in_storage, expected),
)
# set storage language so it can find the code # set storage language so it can find the code
storage.language = in_storage['language'] storage.language = in_storage["language"]
in_storage = storage.code.try_load() in_storage = storage.code.try_load()
expected = code expected = code
assert in_storage == expected, "There are differences on the " \ self.assertEqual(
"code code: %r != %r" % (in_storage, expected) in_storage,
expected,
"There are differences on the "
"code code: %r != %r" % (in_storage, expected),
)
class LibrariesTestCase(LibrariesBaseTestCase): class LibrariesTestCase(LibrariesBaseTestCase):
def setup_libraries(self, declaration, code): def setup_libraries(self, declaration, code):
user1 = User.objects.get(username='jackdoe') user1 = User.objects.get(username="jackdoe")
user2 = User.objects.get(username='johndoe') user2 = User.objects.get(username="johndoe")
team1 = Team.objects.get(name="teamdoe", owner=user1) team1 = Team.objects.get(name="teamdoe", owner=user1)
team2 = Team.objects.get(name="teamdoe2", owner=user1) team2 = Team.objects.get(name="teamdoe2", owner=user1)
...@@ -153,70 +157,70 @@ class LibrariesTestCase(LibrariesBaseTestCase): ...@@ -153,70 +157,70 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Personal # Personal
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='personal', name="personal",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
# Usable by one user # Usable by one user
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='usable_by_one_user', name="usable_by_one_user",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=False, users=[user2]) library.share(public=False, users=[user2])
# Usable by one team # Usable by one team
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='usable_by_one_team', name="usable_by_one_team",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=False, teams=[team1]) library.share(public=False, teams=[team1])
# Usable by teams # Usable by teams
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='usable_by_teams', name="usable_by_teams",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=False, teams=[team1, team2]) library.share(public=False, teams=[team1, team2])
# Usable by all # Usable by all
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='usable_by_all', name="usable_by_all",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=False) library.share(public=False)
# Accessible to one user # Accessible to one user
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='public_for_one_user', name="public_for_one_user",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
# library.shared_with.add(user2) # library.shared_with.add(user2)
library.share(public=True, users=[user2]) library.share(public=True, users=[user2])
...@@ -224,12 +228,12 @@ class LibrariesTestCase(LibrariesBaseTestCase): ...@@ -224,12 +228,12 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Accessible to one team # Accessible to one team
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='public_for_one_team', name="public_for_one_team",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
# library.shared_with_team.add(team1) # library.shared_with_team.add(team1)
library.share(public=True, teams=[team1]) library.share(public=True, teams=[team1])
...@@ -237,30 +241,29 @@ class LibrariesTestCase(LibrariesBaseTestCase): ...@@ -237,30 +241,29 @@ class LibrariesTestCase(LibrariesBaseTestCase):
# Accessible to teams # Accessible to teams
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='public_for_teams', name="public_for_teams",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=True, teams=[team1, team2]) library.share(public=True, teams=[team1, team2])
# Accessible to all # Accessible to all
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user1, author=user1,
name='public_for_all', name="public_for_all",
short_description='', short_description="",
declaration=declaration, declaration=declaration,
code=code, code=code,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.share(public=True) library.share(public=True)
class LibrariesAccessibilityFunctionsBase(LibrariesTestCase): class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
def setUp(self): def setUp(self):
super(LibrariesAccessibilityFunctionsBase, self).setUp() super(LibrariesAccessibilityFunctionsBase, self).setUp()
...@@ -274,10 +277,10 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase): ...@@ -274,10 +277,10 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
""" """
# Users # Users
user1 = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234') user1 = User.objects.create_user("jackdoe", "jackdoe@test.org", TEST_PWD)
user2 = User.objects.create_user('johndoe', 'johndoe@test.org', '1234') user2 = User.objects.create_user("johndoe", "johndoe@test.org", TEST_PWD)
user3 = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') user3 = User.objects.create_user("janedoe", "janedoe@test.org", TEST_PWD)
User.objects.create_user('jacquelinedoe', 'jacquelinedoe@test.org', '1234') User.objects.create_user("jacquelinedoe", "jacquelinedoe@test.org", TEST_PWD)
team1 = Team.objects.create(name="teamdoe", owner=user1) team1 = Team.objects.create(name="teamdoe", owner=user1)
team2 = Team.objects.create(name="teamdoe2", owner=user1) team2 = Team.objects.create(name="teamdoe2", owner=user1)
...@@ -289,10 +292,9 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase): ...@@ -289,10 +292,9 @@ class LibrariesAccessibilityFunctionsBase(LibrariesTestCase):
# Create a public dataformat # Create a public dataformat
(dataformat, errors) = DataFormat.objects.create_dataformat( (dataformat, errors) = DataFormat.objects.create_dataformat(
author=user1, author=user1, name="public_for_all"
name='public_for_all',
) )
assert dataformat, errors self.assertIsNotNone(dataformat, errors)
dataformat.share() dataformat.share()
self.setup_libraries(declaration, code) self.setup_libraries(declaration, code)
...@@ -317,15 +319,14 @@ class LibrariesAPIBase(LibrariesTestCase): ...@@ -317,15 +319,14 @@ class LibrariesAPIBase(LibrariesTestCase):
"language": "python" "language": "python"
}""" }"""
def setUp(self): def setUp(self):
super(LibrariesAPIBase, self).setUp() super(LibrariesAPIBase, self).setUp()
# Users # Users
user1 = User.objects.create_user('jackdoe', 'jackdoe@test.org', '1234') user1 = User.objects.create_user("jackdoe", "jackdoe@test.org", "1234")
user2 = User.objects.create_user('johndoe', 'johndoe@test.org', '1234') user2 = User.objects.create_user("johndoe", "johndoe@test.org", "1234")
user3 = User.objects.create_user('janedoe', 'janedoe@test.org', '1234') user3 = User.objects.create_user("janedoe", "janedoe@test.org", "1234")
User.objects.create_user('jacquelinedoe', 'jacquelinedoe@test.org', '1234') User.objects.create_user("jacquelinedoe", "jacquelinedoe@test.org", "1234")
team1 = Team.objects.create(name="teamdoe", owner=user1) team1 = Team.objects.create(name="teamdoe", owner=user1)
team2 = Team.objects.create(name="teamdoe2", owner=user1) team2 = Team.objects.create(name="teamdoe2", owner=user1)
...@@ -337,10 +338,9 @@ class LibrariesAPIBase(LibrariesTestCase): ...@@ -337,10 +338,9 @@ class LibrariesAPIBase(LibrariesTestCase):
# Create a public dataformat # Create a public dataformat
(dataformat, errors) = DataFormat.objects.create_dataformat( (dataformat, errors) = DataFormat.objects.create_dataformat(
author=user2, author=user2, name="single_integer"
name='single_integer',
) )
assert dataformat, errors self.assertIsNotNone(dataformat, errors)
dataformat.share() dataformat.share()
self.setup_libraries(self.DECLARATION, LibrariesAPIBase.CODE) self.setup_libraries(self.DECLARATION, LibrariesAPIBase.CODE)
...@@ -348,13 +348,22 @@ class LibrariesAPIBase(LibrariesTestCase): ...@@ -348,13 +348,22 @@ class LibrariesAPIBase(LibrariesTestCase):
# Private fork # Private fork
(library, errors) = Library.objects.create_library( (library, errors) = Library.objects.create_library(
author=user2, author=user2,
name='forked_algo', name="forked_algo",
short_description='', short_description="",
declaration=LibrariesAPIBase.DECLARATION, declaration=LibrariesAPIBase.DECLARATION,
code=LibrariesAPIBase.CODE, code=LibrariesAPIBase.CODE,
) )
assert library, errors self.assertIsNotNone(library, errors)
library.previous_version = None library.previous_version = None
library.fork_of = Library.objects.get(name='public_for_all') library.fork_of = Library.objects.get(name="public_for_all")
library.save() library.save()
def login(self, username):
self.client.login(username=username, password=TEST_PWD)
def login_johndoe(self):
self.login("johndoe")
def login_jackdoe(self):
self.login("jackdoe")
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment