Skip to content

Commit

Permalink
Add unit test for new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MissterHao committed Feb 3, 2023
1 parent 9cf2a29 commit 5a71d09
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 deletions.
3 changes: 1 addition & 2 deletions dysession/aws/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def insert_session_item(
insert_item = {pk: data.session_key}
for key in data:
insert_item[key] = data[key]
print(key, data[key])

response = table.put_item(
TableName=table_name,
Expand Down Expand Up @@ -238,4 +237,4 @@ def delete(self, data: SessionDataModel, table_name: Optional[str] = None) -> bo
try:
delete_session_item(data=data, table_name=table_name)
except AssertionError:
pass
raise
6 changes: 2 additions & 4 deletions dysession/backends/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,11 @@ def delete(self, session_key=None):
if session_key is None:
session_key = self._session_key

print(f"On Delete: session_key: {session_key}")
self.db.delete(self._get_session())

try:
pass
self.db.delete(self._get_session())
except DeleteSessionError:
print(f"On Delete Error: session_key: {session_key}")
pass

def load(self) -> SessionDataModel:
"""
Expand Down
54 changes: 48 additions & 6 deletions tests/test_aws_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dysession.aws.dynamodb import (
check_dynamodb_table_exists,
create_dynamodb_table,
delete_session_item,
destory_dynamodb_table,
get_item,
insert_session_item,
Expand Down Expand Up @@ -362,9 +363,7 @@ def test_get_item_using_not_exist_key(self):
)

with self.assertRaises(DynamodbItemNotFound):
resp = get_item(
session_key="not_exist_key", table_name=options["table"]
)
resp = get_item(session_key="not_exist_key", table_name=options["table"])

# Insert Item
@parameterized.expand(
Expand Down Expand Up @@ -407,9 +406,7 @@ def test_insert_item_with_tablename(self, session_key: str):
resp = insert_session_item(data=model)
self.assertEqual(resp["ResponseMetadata"]["HTTPStatusCode"], 200)

resp = get_item(
session_key=session_key, table_name=options["table"]
)
resp = get_item(session_key=session_key, table_name=options["table"])
self.assertIsInstance(resp, SessionDataModel)

@parameterized.expand(
Expand Down Expand Up @@ -454,3 +451,48 @@ def test_insert_item_without_tablename(self, session_key: str):

resp = get_item(session_key=session_key)
self.assertIsInstance(resp, SessionDataModel)

@parameterized.expand(
[
["aaaaaaaaa"],
["bbbbbbbbb"],
["ccccccccc"],
]
)
@mock_dynamodb
def test_delete_item_without_tablename(self, session_key: str):

options = {
"pk": get_config()["PARTITION_KEY_NAME"],
"sk": get_config()["SORT_KEY_NAME"],
"table": "sessions",
"region": "ap-northeast-1",
}

client = boto3.client("dynamodb", region_name=options["region"])
try:
check_dynamodb_table_exists(table_name=options["table"], client=client)
except DynamodbTableNotFound:
create_dynamodb_table(
options={
"pk": options["pk"],
"sk": options["sk"],
"table": options["table"],
},
client=client,
)

model = SessionDataModel(session_key)
model["a"] = 1
model["b"] = 2
model["c"] = 3
model["d"] = 4
model["e"] = "qwerty"

resp = insert_session_item(data=model)
self.assertEqual(resp["ResponseMetadata"]["HTTPStatusCode"], 200)

resp = delete_session_item(data=model)

with self.assertRaises(DynamodbItemNotFound):
resp = get_item(session_key=session_key)
31 changes: 31 additions & 0 deletions tests/test_backend_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,34 @@ def test_exist_check_input_type_error_via_dynamodb_controller(

with self.assertRaises(TypeError):
db.exists(error_input)

@mock_dynamodb
def test_delete_item_via_dynamodb_controller(self):

session_key = "test_set_duplicated_datamodel_via_dynamodb_controller"
self.create_dynamodb_table()

model = SessionDataModel(session_key)
model["a"] = 1
model[get_config()["TTL_ATTRIBUTE_NAME"]] = int(datetime.now().timestamp()) + 50

db = DynamoDB(self.client)
db.set(model, get_config()["DYNAMODB_TABLENAME"])
query_model = db.get(session_key=session_key)
self.assertEqual(model.a, query_model.a)

db.delete(model)

@mock_dynamodb
def test_delete_item_via_dynamodb_controller_raise_error(self):

session_key = None
self.create_dynamodb_table()

model = SessionDataModel(session_key)
model["a"] = 1
model[get_config()["TTL_ATTRIBUTE_NAME"]] = int(datetime.now().timestamp()) + 50

db = DynamoDB(self.client)
with self.assertRaises(AssertionError):
db.delete(model)
18 changes: 12 additions & 6 deletions tests/test_backend_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def test_get_function_default_value(self):

def test_is_empty(self):
model = SessionDataModel()
self.assertTrue(model.is_empty)
self.assertFalse(model.is_empty)

model["good_key"] = 0
self.assertFalse(model.is_empty)

del model["good_key"]
self.assertTrue(model.is_empty)
self.assertFalse(model.is_empty)

def test_iter(self):
model = SessionDataModel()
Expand All @@ -105,7 +105,7 @@ def test_iter(self):
model["c"] = 1
model["d"] = 1

self.assertEqual(set(model), set(["a", "b", "c", "d", "session_key"]))
self.assertEqual(set(model), set(["a", "b", "c", "d", "PK"]))

def test_items(self):
model = SessionDataModel("session_key")
Expand All @@ -123,7 +123,7 @@ def test_items(self):
values.append(v)


self.assertEqual(set(keys), set(["a", "b", "c", "d", "session_key"]))
self.assertEqual(set(keys), set(["a", "b", "c", "d", "PK"]))
self.assertEqual(set(values), set([1, 2, 3, 4, "session_key"]))

def test_str_magic_method(self):
Expand All @@ -137,7 +137,7 @@ def test_str_magic_method(self):

data = json.loads(str(model))

for k in ["session_key", "a", "b", "c", "d"]:
for k in ["PK", "a", "b", "c", "d"]:
self.assertIn(k, data.keys())

def test_get__session_expiry(self):
Expand All @@ -152,4 +152,10 @@ def test_not_found_allow_list(self):
model[k]

with self.assertRaises(KeyError):
self.assertIsNone(model.get(k))
self.assertIsNone(model.get(k))


def test_get_and_set_session_key(self):
model = SessionDataModel()
model.session_key = "key"
self.assertEqual(model.session_key, "key")

0 comments on commit 5a71d09

Please sign in to comment.