Skip to content

Commit 755bc93

Browse files
committed
updated some NoSQL db examples in class
1 parent a4ae4f7 commit 755bc93

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

examples/nosql/address_book_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Business(Household):
108108
pass
109109

110110

111-
class AddressBook(object):
111+
class AddressBook:
112112
"""
113113
And address book -- has people, households, businesses.
114114

examples/nosql/address_book_mongo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Business(Household):
168168
pass
169169

170170

171-
class AddressBook(object):
171+
class AddressBook:
172172
"""
173173
An address book -- has people, households, businesses.
174174
@@ -199,13 +199,13 @@ def __init__(self,
199199
self.households = db.households
200200

201201
def add_person(self, person):
202-
self.people.insert(person.to_dict())
202+
self.people.insert_one(person.to_dict())
203203

204204
def add_household(self, household):
205-
self.households.insert(household.to_dict())
205+
self.households.insert_one(household.to_dict())
206206

207207
def add_business(self, business):
208-
self.businesses.insert(business.to_dict())
208+
self.businesses.insert_one(business.to_dict())
209209

210210
def __str__(self):
211211
msg = ["An Address Book:"]
@@ -341,7 +341,7 @@ def create_sample():
341341
)
342342

343343

344-
address_book = AddressBook()
344+
address_book = AddressBook(fresh=True)
345345

346346
address_book.add_person(chris)
347347
address_book.add_person(donna)

examples/nosql/address_book_zodb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import persistent # from ZODB
1111
from persistent.list import PersistentList
1212

13+
1314
class Person(persistent.Persistent):
1415
"""
1516
class to represent an individual person
@@ -69,7 +70,8 @@ def __init__(self,
6970
self.zip_code = str(zip_code).strip()
7071

7172
def __str__(self):
72-
msg = "{line_1}\n{line_2}\n{city} {state} {zip_code}\n".format(**self.__dict__)
73+
msg = "an address"
74+
#msg = "{line_1}\n{line_2}\n{city} {state} {zip_code}\n".format()
7375
return msg
7476

7577

examples/nosql/test_address_book_model.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@
66
$ mongod --dbpath=mongo_data/
77
"""
88

9-
import address_book_model as model
9+
import pytest
10+
11+
import ZODB
12+
13+
# import address_book_model as model
1014
# import address_book_zodb as model
11-
# import address_book_mongo as model
15+
import address_book_mongo as model
1216

1317

14-
a_book = model.create_sample()
18+
@pytest.fixture
19+
def a_book():
20+
return model.create_sample()
1521

1622

17-
def test_name_search():
23+
def test_name_search(a_book):
1824
"""find a single person by first name"""
1925

2026
people = a_book.find_people('chris')
@@ -24,7 +30,7 @@ def test_name_search():
2430
assert people[0].last_name == 'Barker'
2531

2632

27-
def test_name_search2():
33+
def test_name_search2(a_book):
2834
people = a_book.find_people('barKer')
2935
first_names = [p.first_name for p in people]
3036

@@ -33,16 +39,45 @@ def test_name_search2():
3339
assert 'Donna' in first_names
3440

3541

36-
def test_zip_search():
42+
def test_zip_search(a_book):
3743
locations = a_book.find_zip_codes(98105)
3844

3945
assert len(locations) == 1
4046
assert locations[0].name == 'Python Certification Program'
4147

4248

43-
def test_state_search():
49+
def test_state_search(a_book):
4450
locations = a_book.find_state('WA')
4551
names = [l.name for l in locations]
4652

4753
assert "The Barkers" in names
4854
assert "Python Certification Program" in names
55+
56+
57+
# def test_household():
58+
# house = model.household()
59+
60+
61+
# def test_add_person_to_household(a_book):
62+
# sassy = model.Person(first_name="Sassy",
63+
# last_name="Barker"
64+
# )
65+
66+
# print(a_book.households)
67+
# house = a_book.locations[0]
68+
# house.people.append(sassy)
69+
70+
71+
# db = ZODB.DB('address_book_zodb.fs')
72+
# connection = db.open()
73+
# root = connection.root
74+
# a_book = root.address_book
75+
76+
# house = a_book.households[0]
77+
# print(house)
78+
79+
# assert False
80+
81+
82+
83+

0 commit comments

Comments
 (0)