Example #1
0
    def set_up_test_db():
        person1 = Person(index=0, name='Test', age=30, has_died=False, eye_color='black', phone='000', address='test')
        person2 = Person(index=1, name='Test', age=30, has_died=False, eye_color='black', phone='000', address='test')
        friend = Person(index=2, name='Friend', age=30, has_died=False, eye_color='brown', phone='000', address='test')
        company = Company(index=0, name='Test')
        no_employees_company = Company(index=1, name='No Employees')
        vegetable = Food(index=0, name='Lettuce', type=Food.VEGETABLE)
        fruit = Food(index=1, name='Banana', type=Food.FRUIT)

        person1.company = company
        person1.foods.append(vegetable)
        person1.foods.append(fruit)
        person2.company = company
        person1.befriend(friend)
        person2.befriend(friend)

        db.session.add(person1)
        db.session.add(no_employees_company)
Example #2
0
 def test_befriend(self):
     person = Person(index=0,
                     name='Test',
                     age=30,
                     has_died=False,
                     eye_color='black',
                     phone='000',
                     address='test')
     friend = Person(index=1,
                     name='Friend',
                     age=30,
                     has_died=False,
                     eye_color='blue',
                     phone='000',
                     address='test')
     person.befriend(friend)
     self.assertEqual(1, len(person.friends))
     self.assertEqual(person.friends[0].to_dict(), friend.to_dict())
Example #3
0
def run():
    """Import data from companies.json and people.json"""
    app = create_app(os.getenv('FLASK_CONFIG') or 'default')

    with app.app_context():
        db.drop_all()
        db.create_all()

        # Process companies.json
        companies_to_add = []

        with open('resources/companies.json', 'r') as f:
            companies = json.load(f)
            for item in companies:
                company = Company(index=item['index'], name=item['company'])
                companies_to_add.append(company)

        db.session.add_all(companies_to_add)

        # Process people.json
        people_to_add = []
        with open('resources/people.json', 'r') as f:
            people = json.load(f)
            for item in people:
                print('Processing {}'.format(item['index']))
                # Import a person
                person = Person(index=item['index'],
                                name=item['name'],
                                has_died=item['has_died'],
                                age=item['age'],
                                eye_color=item['eyeColor'],
                                phone=item['phone'],
                                address=item['address'],
                                )

                # Assign company to person
                company = Company.query.filter_by(index=item['company_id']).first()
                if not company:
                    # If we don't have a company with this index, this person cannot be imported.
                    print('Company not found index {} for person index {} {}. Skip this item'.format(item['company_id'],
                                                                                                     item['index'],
                                                                                                     item['name']))
                    continue

                person.company = company

                # Assign food to person, also record new food item as we go
                for food_item in item['favouriteFood']:
                    food = Food.query.filter_by(name=food_item).first()
                    if not food:
                        food = Food(name=food_item, type=determine_food_type(food_item))
                    person.foods.append(food)

            # Process friendship
            for item in people:
                print('Processing {}'.format(item['index']))
                person = Person.query.filter_by(index=item['index']).first()
                if not person:
                    continue

                for friend_item in item['friends']:
                    friend = Person.query.filter_by(index=friend_item['index']).first()
                    if not friend:
                        continue
                    if person.index == friend.index:
                        continue

                    print('{} befriends {}'.format(person.index, friend.index))
                    person.befriend(friend)

                people_to_add.append(person)

        db.session.add_all(people_to_add)

        # Finalise changes to database
        db.session.commit()