Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Check field types in database emulation #4862

Merged
merged 33 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7cabb71
[FEAT] Check field types in emulation layer
rix0rrr Dec 6, 2023
de29d52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2023
73576a6
Fix some TODOs
rix0rrr Dec 6, 2023
a305114
Merge branch 'dynamodb-types' of github.com:Felienne/hedy into dynamo…
rix0rrr Dec 6, 2023
74c1bf6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 6, 2023
a9706ee
Remove this file
rix0rrr Dec 7, 2023
6c14204
Precommit
rix0rrr Dec 8, 2023
e6f1de1
Merge remote-tracking branch 'origin/main' into dynamodb-types
rix0rrr Apr 24, 2024
0e6a3f4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 24, 2024
4e7fc67
Fix tests
rix0rrr Apr 25, 2024
a47872a
Merge branch 'dynamodb-types' of github.com:Felienne/hedy into dynamo…
rix0rrr Apr 25, 2024
27d0a01
Fix linter problems
rix0rrr Apr 27, 2024
23c3d04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 27, 2024
44a2ee7
adding types to users and programs
jpelay Jun 5, 2024
29bc7aa
adding types for adventures table
jpelay Jun 5, 2024
de01cd0
update invitations and tags types
jpelay Jun 5, 2024
1c82014
Merge branch 'main' into dynamodb-types
jpelay Jun 5, 2024
a7bfa0d
some more types
jpelay Jun 6, 2024
e0278f8
some more types and set debug mode directly from the env variable
jpelay Jun 6, 2024
3f217a3
adding a more generic type checking for records
jpelay Jun 6, 2024
7330e76
flake8 made this look weird
jpelay Jun 6, 2024
93043ca
more types
jpelay Jun 6, 2024
d840587
Merge branch 'main' into dynamodb-types
jpelay Jun 6, 2024
c182318
fix is_teacher field
jpelay Jun 6, 2024
dd20d55
Fix set validation
rix0rrr Jun 7, 2024
e28982b
change recordof to dictof
jpelay Jun 10, 2024
76a872c
Merge branch 'main' into dynamodb-types
jpelay Jun 10, 2024
5b77fdf
fix e2e tests
jpelay Jun 10, 2024
6e93937
some fields are actually optional
jpelay Jun 10, 2024
8771d66
add or validator
jpelay Jun 11, 2024
8e86827
fix tags definition
jpelay Jun 11, 2024
a28e451
Merge branch 'main' into dynamodb-types
jpelay Jun 11, 2024
5e90276
change name from of to eitherOf
jpelay Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions testddb.py

This file was deleted.

135 changes: 87 additions & 48 deletions tests/test_dynamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock

from website import dynamo
from website.dynamo import Optional


class Helpers:
Expand Down Expand Up @@ -41,7 +42,9 @@ def get_pages(self, key, **kwargs):

class TestDynamoAbstraction(unittest.TestCase, Helpers):
def setUp(self):
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', 'id')
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', 'id', types={
'id': str,
})

def test_set_manipulation(self):
"""Test that adding to a set and removing from a set works."""
Expand Down Expand Up @@ -84,16 +87,38 @@ def test_cannot_set_manipulate_lists(self):
def test_sets_are_serialized_properly(self):
"""Test that adding to a set and removing from a set works."""
with with_clean_file('test.json'):
table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id')
table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id',
types={'id': str})
table.create(dict(
id='key',
values=set(['a', 'b', 'c']),
))

table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id')
table = dynamo.Table(dynamo.MemoryStorage('test.json'), 'table', 'id',
types={'id': str})
final = table.get(dict(id='key'))
self.assertEqual(final['values'], set(['a', 'b', 'c']))

def test_set_validation(self):
"""Test that adding to a set and removing from a set validates the elements."""
self.table = dynamo.Table(dynamo.MemoryStorage(), 'table', 'id', types={
'id': str,
'values': dynamo.SetOf(str),
})
self.table.create(dict(
id='key',
values=set(['a', 'b', 'c']),
))

self.table.update(dict(id='key'), dict(
values=dynamo.DynamoAddToStringSet('x', 'y'),
))
# This should not have thrown an exception
with self.assertRaises(ValueError):
self.table.update(dict(id='key'), dict(
values=dynamo.DynamoAddToStringSet(3),
))

def test_batch_get(self):
self.insert(
dict(id='k1', bla=1),
Expand Down Expand Up @@ -189,7 +214,10 @@ def setUp(self):
dynamo.MemoryStorage(),
'table',
partition_key='id',
sort_key='sort')
sort_key='sort', types={
'id': str,
'sort': str,
})

def test_put_and_get(self):
self.table.create(dict(
Expand Down Expand Up @@ -231,6 +259,14 @@ def setUp(self):
'table',
partition_key='id',
sort_key='sort',
types={
'id': str,
'sort': int,
'x': Optional(int),
'y': Optional(int),
'm': Optional(int),
'n': Optional(int),
},
indexes=[
dynamo.Index(
'x',
Expand All @@ -240,14 +276,14 @@ def setUp(self):
])

def test_query(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another'})
self.table.create({'id': 'key', 'sort': 1, 'm': 999})
self.table.create({'id': 'key', 'sort': 2, 'm': 888})

ret = list(self.table.get_many({'id': 'key'}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'm': 'val'},
{'id': 'key', 'sort': 2, 'm': 'another'}
{'id': 'key', 'sort': 1, 'm': 999},
{'id': 'key', 'sort': 2, 'm': 888}
])

def test_cant_use_array_for_indexed_field(self):
Expand All @@ -259,93 +295,93 @@ def test_cant_use_array_for_partition(self):
self.table.create({'id': [1, 2]})

def test_query_with_filter(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another'})
self.table.create({'id': 'key', 'sort': 1, 'm': 999})
self.table.create({'id': 'key', 'sort': 2, 'm': 888})

ret = list(self.table.get_many({'id': 'key'}, filter={'m': 'another'}))
ret = list(self.table.get_many({'id': 'key'}, filter={'m': 888}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'm': 'another'}
{'id': 'key', 'sort': 2, 'm': 888}
])

def test_between_query(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'x'})
self.table.create({'id': 'key', 'sort': 2, 'x': 'y'})
self.table.create({'id': 'key', 'sort': 3, 'x': 'z'})
self.table.create({'id': 'key', 'sort': 1, 'x': 1})
self.table.create({'id': 'key', 'sort': 2, 'x': 2})
self.table.create({'id': 'key', 'sort': 3, 'x': 3})

ret = list(self.table.get_many({
'id': 'key',
'sort': dynamo.Between(2, 5),
}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'y'},
{'id': 'key', 'sort': 3, 'x': 'z'},
{'id': 'key', 'sort': 2, 'x': 2},
{'id': 'key', 'sort': 3, 'x': 3},
])

def test_no_superfluous_keys(self):
with self.assertRaises(ValueError):
self.table.get_many({'m': 'some_value', 'Z': 'some_other_value'})

def test_query_index(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another'})
self.table.create({'id': 'key', 'sort': 1, 'm': 999})
self.table.create({'id': 'key', 'sort': 2, 'm': 888})

ret = list(self.table.get_many({'m': 'val'}))
ret = list(self.table.get_many({'m': 999}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'm': 'val'}
{'id': 'key', 'sort': 1, 'm': 999}
])

def test_query_index_with_filter(self):
self.table.create({'id': 'key', 'sort': 1, 'm': 'val', 'p': 'p'})
self.table.create({'id': 'key', 'sort': 3, 'm': 'val', 'p': 'p'})
self.table.create({'id': 'key', 'sort': 2, 'm': 'another', 'q': 'q'})
self.table.create({'id': 'key', 'sort': 1, 'm': 999, 'p': 'p'})
self.table.create({'id': 'key', 'sort': 3, 'm': 999, 'p': 'p'})
self.table.create({'id': 'key', 'sort': 2, 'm': 888, 'q': 'q'})

ret = list(self.table.get_many({'m': 'val'}, filter={'p': 'p'}))
ret = list(self.table.get_many({'m': 999}, filter={'p': 'p'}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'm': 'val', 'p': 'p'},
{'id': 'key', 'sort': 3, 'm': 'val', 'p': 'p'},
{'id': 'key', 'sort': 1, 'm': 999, 'p': 'p'},
{'id': 'key', 'sort': 3, 'm': 999, 'p': 'p'},
])

def test_query_index_with_partition_key(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 'another_val', 'y': 2})
self.table.create({'id': 'key', 'sort': 1, 'x': 1, 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 1, 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 1, 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 2, 'y': 2})

ret = list(self.table.get_many({'x': 'val'}))
ret = list(self.table.get_many({'x': 1}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 1, 'x': 'val', 'y': 0},
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 1},
{'id': 'key', 'sort': 3, 'x': 'val', 'y': 1}
{'id': 'key', 'sort': 1, 'x': 1, 'y': 0},
{'id': 'key', 'sort': 2, 'x': 1, 'y': 1},
{'id': 'key', 'sort': 3, 'x': 1, 'y': 1}
])

def test_query_index_with_partition_sort_key(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 'another_val', 'y': 2})
self.table.create({'id': 'key', 'sort': 1, 'x': 1, 'y': 0})
self.table.create({'id': 'key', 'sort': 2, 'x': 1, 'y': 1})
self.table.create({'id': 'key', 'sort': 3, 'x': 1, 'y': 1})
self.table.create({'id': 'key', 'sort': 4, 'x': 2, 'y': 2})

ret = list(self.table.get_many({'x': 'val', 'y': 1}))
ret = list(self.table.get_many({'x': 1, 'y': 1}))

self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 1},
{'id': 'key', 'sort': 3, 'x': 'val', 'y': 1}
{'id': 'key', 'sort': 2, 'x': 1, 'y': 1},
{'id': 'key', 'sort': 3, 'x': 1, 'y': 1}
])

def test_query_index_sort_key_between(self):
self.table.create({'id': 'key', 'sort': 1, 'x': 'val', 'y': 1})
self.table.create({'id': 'key', 'sort': 2, 'x': 'val', 'y': 3})
self.table.create({'id': 'key', 'sort': 3, 'x': 'val', 'y': 6})
self.table.create({'id': 'key', 'sort': 1, 'x': 1, 'y': 1})
self.table.create({'id': 'key', 'sort': 2, 'x': 1, 'y': 3})
self.table.create({'id': 'key', 'sort': 3, 'x': 1, 'y': 6})

ret = list(self.table.get_many({
'x': 'val',
'x': 1,
'y': dynamo.Between(2, 5),
}))
self.assertEqual(ret, [
{'id': 'key', 'sort': 2, 'x': 'val', 'y': 3}
{'id': 'key', 'sort': 2, 'x': 1, 'y': 3}
])

def test_paginated_query(self):
Expand Down Expand Up @@ -567,7 +603,10 @@ def setUp(self):
''),
'table',
partition_key='id',
sort_key='sort')
sort_key='sort', types={
'id': str,
'sort': int,
})

self.db.query.return_value = {'Items': []}

Expand Down
Loading
Loading