Esempio n. 1
0
    def test_employees(self):

        num_people = 30
        num_employees = 15
        per_page = 10

        Company(index=self.company_id, name="Company One").save()

        people = [
            Person(
                name=f'Person {n}',
                age=30 + n,
                address=f'address {n}',
                phone=f'phone {n}',
                company_id=self.company_id
                if n < num_employees else self.company_id + 1,
            ).save() for n in range(num_people)
        ]

        self.assertEqual(Person.objects.count(), num_people)

        tests = [
            (0, people[:10]),
            (1, people[10:num_employees]),
            (2, []),
        ]
        for page, employees in tests:
            expected = {'employees': Person.dump(employees, many=True)}
            with app.test_client() as client:
                r = client.get(self.url,
                               query_string=dict(page=page, per_page=per_page))
                self.assertEqual(r.status_code, 200)
                actual = r.get_json()
                self.assertEqual(actual, expected)
Esempio n. 2
0
 def test_employees_empty(self):
     Company(index=self.company_id, name="Company One").save()
     with app.test_client() as client:
         r = client.get(self.url)
         self.assertEqual(r.status_code, 200)
         expected = []
         actual = r.get_json()['employees']
         self.assertEqual(actual, expected)
    def test_common_friends(self):

        # Friends in common: 2 and 3
        self.people[0].friends = [{
            'index': 1
        }, {
            'index': 2
        }, {
            'index': 3
        }, {
            'index': 4
        }]
        self.people[1].friends = [{'index': 2}, {'index': 3}, {'index': 4}]

        # Friend 2 has wrong eye color - no match
        self.people[2].eye_color = 'red'
        self.people[2].has_died = False

        # Friend 3 has wrong eye color - match
        self.people[3].eye_color = 'brown'
        self.people[3].has_died = False

        # Friend 4 has died - no match
        self.people[4].eye_color = 'brown'
        self.people[4].has_died = True

        for person in self.people:
            person.save()
        self.assertEqual(Person.objects.count(), self.num_people)

        with app.test_client() as client:
            r = client.get(self.url)
            self.assertEqual(r.status_code, 200)
            expected = {
                "person1": {
                    "name": self.people[0].name,
                    "age": self.people[0].age,
                    "address": self.people[0].address,
                    "phone": self.people[0].phone,
                },
                "person2": {
                    "name": self.people[1].name,
                    "age": self.people[1].age,
                    "address": self.people[1].address,
                    "phone": self.people[1].phone,
                },
                "friends": [3],
            }
            actual = r.get_json()
            self.assertEqual(actual, expected)
Esempio n. 4
0
 def test_favourite_food(self):
     name = 'Person One'
     age = 30
     fruits = sorted(["banana", "apple"])
     vegetables = sorted(["beetroot", "lettuce"])
     person = Person(
         index=self.person_idx,
         name=name,
         age=age,
         favourite_food=fruits + vegetables,
     ).save()
     with app.test_client() as client:
         r = client.get(self.favourite_food_url)
         self.assertEqual(r.status_code, 200)
         expected = {
             "username": name,
             "age": age,
             "fruits": fruits,
             "vegetables": vegetables,
         }
         actual = r.get_json()
         self.assertEqual(actual, expected)
Esempio n. 5
0
 def test_employees_no_company(self):
     with app.test_client() as client:
         r = client.get(self.url)
         self.assertEqual(r.status_code, 404)
def client():
    app.config['TESTING'] = True
    client = app.test_client()

    yield client
Esempio n. 7
0
 def test_favourite_food_no_such_person(self):
     with app.test_client() as client:
         r = client.get(self.favourite_food_url)
         self.assertEqual(r.status_code, 404)
 def test_common_friends_no_such_person(self):
     self.assertEqual(Person.objects.count(), 0)
     with app.test_client() as client:
         r = client.get(self.url)
         self.assertEqual(r.status_code, 404)