def test_save_object_basics(): s = Scraper('jurisdiction', '2013', '/tmp/') p = Person('Michael Jordan') with mock.patch('json.dump') as json_dump: s.save_object(p) # saved in right place filename = 'person_' + p._id + '.json' assert_in(filename, s.output_names['person']) json_dump.assert_called_once_with(p.as_dict(), mock.ANY, cls=mock.ANY)
def test_save_related(): s = Scraper('jurisdiction', '2013', '/tmp/') p = Person('Michael Jordan') o = Organization('Chicago Bulls') p._related.append(o) with mock.patch('json.dump') as json_dump: s.save_object(p) assert_equal(json_dump.mock_calls, [ mock.call(p.as_dict(), mock.ANY, cls=mock.ANY), mock.call(o.as_dict(), mock.ANY, cls=mock.ANY) ])
def migrate_people(state): spec = {} if state: spec["state"] = state for entry in db.legislators.find(spec): who = Person(entry['full_name']) who.openstates_id = entry['_id'] for k, v in { "photo_url": "image", "chamber": "chamber", "district": "district", }.items(): if entry.get(k, None): setattr(who, v, entry[k]) who.sources = entry['sources'] home = entry.get('url', None) if home: who.add_link(home, "Homepage") blacklist = ["photo_url", "chamber", "district", "url", "roles", "offices", "updated_at", "created_at", "party", "state", "_locked_fields", "sources", "active", "old_roles"] for key, value in entry.items(): if key in blacklist or not value or key.startswith("_"): continue who.extras[key] = value legislature = lookup_entry_id('organizations', entry['state']) if legislature is None: raise Exception("Someone's in the void.") save_object(who) # gives who an id, btw. party = entry.get('party', None) nudb.memberships.remove({"person_id": who._id}, safe=True) if party: m = Membership(who._id, create_or_get_party(entry['party'])) save_object(m) m = Membership(who._id, legislature) chamber, district = (entry.get(x, None) for x in ['chamber', 'district']) if chamber: m.chamber = chamber if district: m.district = district for office in entry.get('offices', []): note = office['name'] for key, value in office.items(): if not value or key in ["name", "type"]: continue m.add_contact_detail(type=key, value=value, note=note) save_object(m)