Skip to content

Commit

Permalink
<fix> naming error 'exctract_trees_from_sample'
Browse files Browse the repository at this point in the history
  • Loading branch information
kirianguiller committed Nov 6, 2020
1 parent b64f615 commit 3b837db
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 94 deletions.
2 changes: 1 addition & 1 deletion app/trees/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get(self, projectName: str, sampleName: str):
if exercise_level_obj:
exercise_level = exercise_level_obj.exercise_level.code

sample_trees = exctract_trees_from_sample(samples, sampleName)
sample_trees = extract_trees_from_sample(samples, sampleName)
sample_trees = add_base_tree(sample_trees)

username = "anonymous"
Expand Down
115 changes: 41 additions & 74 deletions app/user/controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@


def make_user(
# id: str = "123",
# name: str = "Test user",
# purpose: str = "Test purpose",
id: str = "123",
auth_provider: str = "google",
username: str = "JohnDoe",
Expand All @@ -38,74 +35,44 @@ def make_user(
)


# class TestUserResource:
# @patch.object(
# UserService,
# "get_all",
# lambda: [
# make_user(id="123", username="username 1"),
# make_user(id="456", username="username 2"),
# ],
# )
# def test_get(self, client: FlaskClient): # noqa
# with client:
# results = client.get(f"/api/{BASE_ROUTE}", follow_redirects=True).get_json()
# expected = UserSchema(many=True).dump(
# [
# make_user(id="123", username="username 1"),
# make_user(id="456", username="username 2"),
# ]
# )
# for r in results:
# assert r in expected

# @patch.object(UserService, "create", lambda create_request: User(**create_request))
# def test_post(self, client: FlaskClient): # noqa
# with client:

# payload = dict(id="123", username="username 1")
# result = client.post(f"/api/{BASE_ROUTE}/", json=payload).get_json()
# expected = UserSchema().dump(
# User(name=payload["name"], purpose=payload["purpose"])
# )
# assert result == expected


# def fake_update(user: User, changes: UserInterface) -> User:
# # To fake an update, just return a new object
# updated_User = User(id=user.id, username=changes["username"])
# return updated_User






# class TestUserIdResource:
# @patch.object(UserService, "get_by_id", lambda id: make_user(id=id))
# def test_get(self, client: FlaskClient): # noqa
# with client:
# result = client.get(f"/api/{BASE_ROUTE}/123").get_json()
# expected = make_user(id=123)
# print(f"result = ", result)
# assert result["userId"] == expected.id

# @patch.object(UserService, "delete_by_id", lambda id: id)
# def test_delete(self, client: FlaskClient): # noqa
# with client:
# result = client.delete(f"/api/{BASE_ROUTE}/123").get_json()
# expected = dict(status="Success", id=123)
# assert result == expected

# @patch.object(UserService, "get_by_id", lambda id: make_user(id=id))
# @patch.object(UserService, "update", fake_update)
# def test_put(self, client: FlaskClient): # noqa
# with client:
# result = client.put(
# f"/api/{BASE_ROUTE}/123",
# json={"name": "New User", "purpose": "New purpose"},
# ).get_json()
# expected = UserSchema().dump(
# User(id=123, name="New User", purpose="New purpose")
# )
# assert result == expected
class TestUserResource:
@patch.object(
UserService,
"get_all",
lambda: [
make_user("123", username="user1"),
make_user("456", username="user2"),
],
)
def test_get(self, client: FlaskClient): # noqa
with client:
results = client.get(f"/api/{BASE_ROUTE}", follow_redirects=True).get_json()
expected = (
UserSchema(many=True)
.dump(
[
make_user("123", username="user1"),
make_user("456", username="user2"),
]
)

)
for r in results:
assert r in expected

# @patch.object(
# UserService, "create", lambda create_request: User(**create_request)
# )
# def test_post(self, client: FlaskClient): # noqa
# with client:

# payload = dict(username="Test user", first_name="test first name")
# result = client.post(f"/api/{BASE_ROUTE}/", json=payload).get_json()
# expected = (
# UserSchema()
# .dump(User(username=payload["username"], first_name=payload["first_name"]))

# )
# print("result", result)
# print("expected", expected)
# assert result == expected
112 changes: 93 additions & 19 deletions app/user/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def test_get_all(db: SQLAlchemy): # noqa
yin: User = User(
user1: User = User(
id="1",
auth_provider="google",
username="JohnDoe",
Expand All @@ -21,7 +21,7 @@ def test_get_all(db: SQLAlchemy): # noqa
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
yang: User = User(
user2: User = User(
id="2",
auth_provider="github",
username="JamesCarl",
Expand All @@ -32,18 +32,55 @@ def test_get_all(db: SQLAlchemy): # noqa
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
db.session.add(yin)
db.session.add(yang)
db.session.add(user1)
db.session.add(user2)
db.session.commit()

results: List[User] = UserService.get_all()

assert len(results) == 2
assert yin in results and yang in results
assert user1 in results and user2 in results


def test_get_by_username(db: SQLAlchemy): # noqa
user1: User = User(
id="1",
auth_provider="google",
username="user1",
first_name="John",
family_name="Doe",
picture_url="www.google.com",
super_admin=True,
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
user2: User = User(
id="2",
auth_provider="github",
username="user2",
first_name="James",
family_name="Carl",
picture_url="www.google.com",
super_admin=True,
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
db.session.add(user1)
db.session.add(user2)
db.session.commit()

retrieved_user1 = UserService.get_by_username("user1")
retrieved_user2 = UserService.get_by_username("user2")

assert retrieved_user1.username == "user1"
assert retrieved_user1.id == "1"

assert retrieved_user2.username == "user2"
assert retrieved_user2.id == "2"


def test_update(db: SQLAlchemy): # noqa
yin: User = User(
user1: User = User(
id="1",
auth_provider="google",
username="JohnDoe",
Expand All @@ -55,18 +92,18 @@ def test_update(db: SQLAlchemy): # noqa
last_seen=datetime.utcnow(),
)

db.session.add(yin)
db.session.add(user1)
db.session.commit()
updates: UserInterface = dict(first_name="New first_name")

UserService.update(yin, updates)
UserService.update(user1, updates)

result: User = User.query.get(yin.id)
result: User = User.query.get(user1.id)
assert result.first_name == "New first_name"


def test_delete_by_id(db: SQLAlchemy): # noqa
yin: User = User(
user1: User = User(
id="1",
auth_provider="google",
username="JohnDoe",
Expand All @@ -77,7 +114,7 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
yang: User = User(
user2: User = User(
id="2",
auth_provider="github",
username="JamesCarl",
Expand All @@ -88,8 +125,8 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
db.session.add(yin)
db.session.add(yang)
db.session.add(user1)
db.session.add(user2)
db.session.commit()

UserService.delete_by_id("1")
Expand All @@ -98,12 +135,11 @@ def test_delete_by_id(db: SQLAlchemy): # noqa
results: List[User] = User.query.all()

assert len(results) == 1
assert yin not in results and yang in results
assert user1 not in results and user2 in results


def test_create(db: SQLAlchemy): # noqa

yin: UserInterface = dict(
user1: UserInterface = dict(
id="1",
auth_provider="google",
username="JohnDoe",
Expand All @@ -114,10 +150,48 @@ def test_create(db: SQLAlchemy): # noqa
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
UserService.create(yin)
UserService.create(user1)
results: List[User] = User.query.all()

assert len(results) == 1

for k in yin.keys():
assert getattr(results[0], k) == yin[k]
for k in user1.keys():
assert getattr(results[0], k) == user1[k]


def test_change_super_admin(db: SQLAlchemy):
user1: User = User(
id="1",
auth_provider="google",
username="JohnDoe",
first_name="John",
family_name="Doe",
picture_url="www.google.com",
super_admin=True,
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)
user2: User = User(
id="2",
auth_provider="github",
username="JamesCarl",
first_name="James",
family_name="Carl",
picture_url="www.google.com",
super_admin=False,
created_date=datetime.utcnow(),
last_seen=datetime.utcnow(),
)

db.session.add(user1)
db.session.add(user2)
db.session.commit()

UserService.change_super_admin(user1, False)
UserService.change_super_admin(user2, True)

changed_user1 = UserService.get_by_id("1")
changed_user2 = UserService.get_by_id("2")

assert changed_user1.super_admin == False
assert changed_user2.super_admin == True

0 comments on commit 3b837db

Please sign in to comment.