tangled
alpha
login
or
join now
bernsteinbear.com
/
db.py
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
.
bernsteinbear.com
2 years ago
cea5ce12
0c30df93
+24
-2
2 changed files
expand all
collapse all
unified
split
db.py
db_tests.py
+1
-1
db.py
···
74
74
table = self.tables[table_name]
75
75
table.rows = (*table.rows, *rows)
76
76
77
77
-
def UPDATE(self, table, set, pred):
77
77
+
def UPDATE(self, table, set, pred=lambda _: True):
78
78
return Table(
79
79
table.name, [{**row, **set} if pred(row) else row for row in table.rows]
80
80
)
+23
-1
db_tests.py
···
123
123
result = db.WHERE(table, lambda row: row["a"] % 2 == 0)
124
124
self.assertEqual(result.rows, ({"a": 2}, {"a": 4}))
125
125
126
126
-
def test_update_returns_updated_table(self):
126
126
+
def test_update_returns_updated_table_with_all_rows_modified(self):
127
127
+
db = Database()
128
128
+
rows = (
129
129
+
{"id": 1, "name": "Josh", "department_id": 1, "salary": 50000},
130
130
+
{"id": 2, "name": "Ruth", "department_id": 2, "salary": 60000},
131
131
+
{"id": 3, "name": "Greg", "department_id": 5, "salary": 70000},
132
132
+
{"id": 4, "name": "Pat", "department_id": 1, "salary": 80000},
133
133
+
)
134
134
+
table = Table("employee", rows)
135
135
+
result = db.UPDATE(table, {"salary": 10})
136
136
+
self.assertEqual(result.name, table.name)
137
137
+
self.assertEqual(
138
138
+
result.rows,
139
139
+
(
140
140
+
{"id": 1, "name": "Josh", "department_id": 1, "salary": 10},
141
141
+
{"id": 2, "name": "Ruth", "department_id": 2, "salary": 10},
142
142
+
{"id": 3, "name": "Greg", "department_id": 5, "salary": 10},
143
143
+
{"id": 4, "name": "Pat", "department_id": 1, "salary": 10},
144
144
+
),
145
145
+
)
146
146
+
self.assertEqual(table.rows[0]["salary"], 50000)
147
147
+
148
148
+
def test_update_returns_updated_table_with_pred(self):
127
149
db = Database()
128
150
rows = (
129
151
{"id": 1, "name": "Josh", "department_id": 1, "salary": 50000},