Skip to content

Commit

Permalink
(RUF012) Fixed mutable class Defaults- Task 2 (#10286)
Browse files Browse the repository at this point in the history
  • Loading branch information
ananyakaligal authored Feb 5, 2025
1 parent e4e03a6 commit 8b54d97
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 212 deletions.
323 changes: 174 additions & 149 deletions openlibrary/tests/core/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,76 +155,100 @@ def test_no_allow_delete_on_conflict(self):
assert [dict(row) for row in self.db.select("booknotes")] == rows


class TestUsernameUpdate:
READING_LOG_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 1,
"bookshelf_id": 1,
},
{
"username": "@kilgore_trout",
"work_id": 2,
"edition_id": 2,
"bookshelf_id": 1,
},
{
"username": "@billy_pilgrim",
"work_id": 1,
"edition_id": 1,
"bookshelf_id": 2,
},
]
BOOKNOTES_SETUP_ROWS = [
{"username": "@kilgore_trout", "work_id": 1, "edition_id": 1, "notes": "Hello"},
{"username": "@billy_pilgrim", "work_id": 1, "edition_id": 1, "notes": "World"},
]
RATINGS_SETUP_ROWS = [
{"username": "@kilgore_trout", "work_id": 1, "edition_id": 1, "rating": 4},
{"username": "@billy_pilgrim", "work_id": 5, "edition_id": 1, "rating": 2},
]
OBSERVATIONS_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 3,
"observation_type": 1,
"observation_value": 2,
},
{
"username": "@billy_pilgrim",
"work_id": 2,
"edition_id": 4,
"observation_type": 4,
"observation_value": 1,
},
]

EDITS_QUEUE_SETUP_ROWS = [
{
"title": "One Fish, Two Fish, Red Fish, Blue Fish",
"submitter": "@kilgore_trout",
"reviewer": None,
"url": "/works/merge?records=OL1W,OL2W,OL3W",
"status": 1,
},
{
"title": "The Lorax",
"submitter": "@kilgore_trout",
"reviewer": "@billy_pilgrim",
"url": "/works/merge?records=OL4W,OL5W,OL6W",
"status": 2,
},
{
"title": "Green Eggs and Ham",
"submitter": "@eliot_rosewater",
"reviewer": None,
"url": "/works/merge?records=OL10W,OL11W,OL12W,OL13W",
"status": 1,
},
]
READING_LOG_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 1,
"bookshelf_id": 1,
},
{
"username": "@kilgore_trout",
"work_id": 2,
"edition_id": 2,
"bookshelf_id": 1,
},
{
"username": "@billy_pilgrim",
"work_id": 1,
"edition_id": 1,
"bookshelf_id": 2,
},
]

BOOKNOTES_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 1,
"notes": "Hello",
},
{
"username": "@billy_pilgrim",
"work_id": 1,
"edition_id": 1,
"notes": "World",
},
]

RATINGS_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 1,
"rating": 4,
},
{
"username": "@billy_pilgrim",
"work_id": 5,
"edition_id": 1,
"rating": 2,
},
]

OBSERVATIONS_SETUP_ROWS = [
{
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 3,
"observation_type": 1,
"observation_value": 2,
},
{
"username": "@billy_pilgrim",
"work_id": 2,
"edition_id": 4,
"observation_type": 4,
"observation_value": 1,
},
]

EDITS_QUEUE_SETUP_ROWS = [
{
"title": "One Fish, Two Fish, Red Fish, Blue Fish",
"submitter": "@kilgore_trout",
"reviewer": None,
"url": "/works/merge?records=OL1W,OL2W,OL3W",
"status": 1,
},
{
"title": "The Lorax",
"submitter": "@kilgore_trout",
"reviewer": "@billy_pilgrim",
"url": "/works/merge?records=OL4W,OL5W,OL6W",
"status": 2,
},
{
"title": "Green Eggs and Ham",
"submitter": "@eliot_rosewater",
"reviewer": None,
"url": "/works/merge?records=OL10W,OL11W,OL12W,OL13W",
"status": 1,
},
]


class TestUsernameUpdate:
@classmethod
def setup_class(cls):
web.config.db_parameters = {"dbn": "sqlite", "db": ":memory:"}
Expand All @@ -235,10 +259,10 @@ def setup_class(cls):

def setup_method(self):
self.db = get_db()
self.db.multiple_insert("bookshelves_books", self.READING_LOG_SETUP_ROWS)
self.db.multiple_insert("booknotes", self.BOOKNOTES_SETUP_ROWS)
self.db.multiple_insert("ratings", self.RATINGS_SETUP_ROWS)
self.db.multiple_insert("observations", self.OBSERVATIONS_SETUP_ROWS)
self.db.multiple_insert("bookshelves_books", READING_LOG_SETUP_ROWS)
self.db.multiple_insert("booknotes", BOOKNOTES_SETUP_ROWS)
self.db.multiple_insert("ratings", RATINGS_SETUP_ROWS)
self.db.multiple_insert("observations", OBSERVATIONS_SETUP_ROWS)

def teardown_method(self):
self.db.query("delete from bookshelves_books;")
Expand All @@ -264,7 +288,7 @@ def test_delete_all_by_username(self):
assert len(list(self.db.select("observations"))) == 1

def test_update_username(self):
self.db.multiple_insert("community_edits_queue", self.EDITS_QUEUE_SETUP_ROWS)
self.db.multiple_insert("community_edits_queue", EDITS_QUEUE_SETUP_ROWS)
before_where = {"username": "@kilgore_trout"}
after_where = {"username": "@anonymous"}

Expand Down Expand Up @@ -307,58 +331,59 @@ def test_update_username(self):
self.db.query('delete from community_edits_queue;')


class TestCheckIns:
BOOKSHELVES_EVENTS_SETUP_ROWS = [
{
"id": 1,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 1,
"event_date": "2022-04-17",
},
{
"id": 2,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 2,
"event_date": "2022-05-10",
},
{
"id": 3,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 3,
"event_date": "2022-06-20",
},
{
"id": 4,
"username": "@billy_pilgrim",
"work_id": 3,
"edition_id": 4,
"event_type": 1,
"event_date": "2020",
},
{
"id": 5,
"username": "@eliot_rosewater",
"work_id": 3,
"edition_id": 4,
"event_type": 3,
"event_date": "2019-08-20",
},
{
"id": 6,
"username": "@eliot_rosewater",
"work_id": 3,
"edition_id": 4,
"event_type": 3,
"event_date": "2019-10",
},
]
BOOKSHELVES_EVENTS_SETUP_ROWS = [
{
"id": 1,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 1,
"event_date": "2022-04-17",
},
{
"id": 2,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 2,
"event_date": "2022-05-10",
},
{
"id": 3,
"username": "@kilgore_trout",
"work_id": 1,
"edition_id": 2,
"event_type": 3,
"event_date": "2022-06-20",
},
{
"id": 4,
"username": "@billy_pilgrim",
"work_id": 3,
"edition_id": 4,
"event_type": 1,
"event_date": "2020",
},
{
"id": 5,
"username": "@eliot_rosewater",
"work_id": 3,
"edition_id": 4,
"event_type": 3,
"event_date": "2019-08-20",
},
{
"id": 6,
"username": "@eliot_rosewater",
"work_id": 3,
"edition_id": 4,
"event_type": 3,
"event_date": "2019-10",
},
]


class TestCheckIns:
@classmethod
def setup_class(cls):
web.config.db_parameters = {"dbn": "sqlite", "db": ":memory:"}
Expand All @@ -367,9 +392,7 @@ def setup_class(cls):

def setup_method(self):
self.db = get_db()
self.db.multiple_insert(
'bookshelves_events', self.BOOKSHELVES_EVENTS_SETUP_ROWS
)
self.db.multiple_insert('bookshelves_events', BOOKSHELVES_EVENTS_SETUP_ROWS)

def teardown_method(self):
self.db.query("delete from bookshelves_events;")
Expand Down Expand Up @@ -480,27 +503,29 @@ def test_get_latest_event_date(self):
assert BookshelvesEvents.get_latest_event_date('@eliot_rosewater', 3, 1) is None


SETUP_ROWS = [
{
'username': '@billy_pilgrim',
'year': 2022,
'target': 5,
'current': 6,
},
{
'username': '@billy_pilgrim',
'year': 2023,
'target': 7,
'current': 0,
},
{
'username': '@kilgore_trout',
'year': 2022,
'target': 4,
'current': 4,
},
]


class TestYearlyReadingGoals:
SETUP_ROWS = [
{
'username': '@billy_pilgrim',
'year': 2022,
'target': 5,
'current': 6,
},
{
'username': '@billy_pilgrim',
'year': 2023,
'target': 7,
'current': 0,
},
{
'username': '@kilgore_trout',
'year': 2022,
'target': 4,
'current': 4,
},
]

TABLENAME = YearlyReadingGoals.TABLENAME

Expand All @@ -512,7 +537,7 @@ def setup_class(cls):

def setup_method(self):
self.db = get_db()
self.db.multiple_insert(self.TABLENAME, self.SETUP_ROWS)
self.db.multiple_insert(self.TABLENAME, SETUP_ROWS)

def teardown_method(self):
self.db.query('delete from yearly_reading_goals')
Expand Down
Loading

0 comments on commit 8b54d97

Please sign in to comment.