예제 #1
0
    def test_create_duplicate_company(self):
        """
        Should not be able to retrieve a company with a duplicate name.

        """
        with transaction():
            Company(name="name").create()

        company = Company(name="name")
        assert_that(calling(company.create), raises(DuplicateModelError))
예제 #2
0
    def test_create_update_duplicate_company(self):
        """
        Should be not able to update a company to a duplicate name.

        """
        with transaction():
            Company(name="name1").create()
            company = Company(name="name2").create()

        company.name = "name1"
        assert_that(calling(company.update), raises(DuplicateModelError))
예제 #3
0
    def test_create_search_count_company(self):
        """
        Should be able to search and count companies after creation.

        """
        with transaction():
            company1 = Company(name="name1").create()
            company2 = Company(name="name2").create()

        assert_that(Company.count(), is_(equal_to(2)))

        # Pagination fields do not affect count calculations
        assert_that(self.company_store.count(offset=1, limit=1),
                    is_(equal_to(2)))

        assert_that([company.id for company in Company.search()],
                    contains_inanyorder(company1.id, company2.id))
예제 #4
0
    def test_delete_company_with_employees(self):
        """
        Should be not able to delete a company with employees.

        """
        with transaction():
            Company(name="name1", ).create()
            company = Company(name="name2", ).create()

        with transaction():
            Employee(
                first="first",
                last="last",
                company_id=company.id,
            ).create()

        assert_that(calling(company.delete), raises(ReferencedModelError))
예제 #5
0
    def test_create_update_company(self):
        """
        Should be able to update a company after creating it.

        """
        with transaction():
            company = Company(name="name", ).create()

        with transaction():
            updated_company = Company(
                id=company.id,
                name="new_name",
            ).update()
            assert_that(updated_company.name, is_(equal_to("new_name")))

        with transaction():
            retrieved_company = Company.retrieve(company.id)
            assert_that(retrieved_company.name, is_(equal_to("new_name")))
예제 #6
0
    def test_create_delete_company_complicated_expression(self):
        """
        Delete should support more complicated criterion with the `fetch` synchronization strategy enabled.

        """
        with transaction():
            company = Company(name="name").create()
            Company(name="other-name").create()

        with transaction():
            self.company_store._delete(Company.name.in_(["name"]),
                                       synchronize_session="fetch")

        assert_that(
            calling(Company.retrieve).with_args(company.id),
            raises(ModelNotFoundError, pattern="Company not found"),
        )

        assert_that(Company.count(), is_(equal_to(1)))
예제 #7
0
    def test_clone(self):
        with transaction():
            company = Company(
                name="name",
                type=CompanyType.private,
            ).create()
            copy = clone(company, dict(name="newname"))

        assert_that(copy.id, is_not(equal_to(company.id)))
        assert_that(self.company_store.retrieve(copy.id), is_(not_none()))
예제 #8
0
    def test_create_update_with_diff_company(self):
        """
        Should be able to update a company after creating it and get a diff.

        """
        with transaction():
            company = Company(name="name").create()

        with transaction():
            _, diff = Company(
                id=company.id,
                name="new_name",
            ).update_with_diff()
            assert_that(list(diff.keys()),
                        contains_inanyorder("name", "updated_at"))
            assert_that(diff["name"].before, is_(equal_to("name")))
            assert_that(diff["name"].after, is_(equal_to("new_name")))

        with transaction():
            retrieved_company = Company.retrieve(company.id)
            assert_that(retrieved_company.name, is_(equal_to("new_name")))
예제 #9
0
    def setup(self):
        self.graph = create_object_graph(name="example",
                                         testing=True,
                                         import_name="microcosm_postgres")
        self.company_store = self.graph.company_store
        self.employee_store = self.graph.employee_store

        self.context = SessionContext(self.graph)
        self.context.recreate_all()
        self.context.open()

        with transaction():
            self.company = Company(name="name").create()
예제 #10
0
    def test_create_retrieve_company(self):
        """
        Should be able to retrieve a company after creating it.

        """
        with transaction():
            company = Company(
                name="name",
                type=CompanyType.private,
            ).create()

        retrieved_company = Company.retrieve(company.id)
        assert_that(retrieved_company.name, is_(equal_to("name")))
        assert_that(retrieved_company.type, is_(equal_to(CompanyType.private)))
예제 #11
0
    def test_create_delete_company(self):
        """
        Should not be able to retrieve a company after deleting it.

        """
        with transaction():
            company = Company(name="name").create()

        with transaction():
            company.delete()

        assert_that(
            calling(Company.retrieve).with_args(company.id),
            raises(ModelNotFoundError, pattern="Company not found"),
        )
예제 #12
0
    def test_search_company(self):
        """
        Should be able to search for companies.

        """
        with transaction():
            company = Company(
                name="name",
                type=CompanyType.private,
            ).create()

        assert_that(Company.search(), contains(company))
        assert_that(Company.search(name="whatever"), is_(empty()))
        assert_that(Company.search(name=company.name), contains(company))
        # NB: filtering is skipped if None
        assert_that(Company.search(name=None), contains(company))
예제 #13
0
    def setup(self):
        self.graph = create_object_graph(name="example", testing=True, import_name="microcosm_postgres")
        self.company_store = self.graph.company_store
        self.employee_store = self.graph.employee_store

        with SessionContext(self.graph) as context:
            context.recreate_all()

            with transaction():
                self.company = Company(
                    name="name",
                    type=CompanyType.private,
                ).create()
                self.employee = Employee(
                    first="first",
                    last="last",
                    company_id=self.company.id,
                ).create()
예제 #14
0
    def test_search_by_company_kwargs(self):
        """
        Should be able to filter searches using kwargs.

        """
        with transaction():
            employee1 = Employee(
                first="first",
                last="last",
                company_id=self.company.id,
            ).create()
            employee2 = Employee(
                first="Jane",
                last="Doe",
                company_id=self.company.id,
            ).create()
            company2 = Company(name="other", ).create()
            employee3 = Employee(
                first="John",
                last="Doe",
                company_id=company2.id,
            ).create()

        assert_that(Employee.count(), is_(equal_to(3)))
        assert_that([
            employee.id for employee in self.employee_store.search(
                company_id=self.company.id, offset=0, limit=10)
        ], contains_inanyorder(employee1.id, employee2.id))
        assert_that(
            self.employee_store.count(company_id=self.company.id,
                                      offset=0,
                                      limit=10), is_(equal_to(2)))
        assert_that([
            employee.id for employee in self.employee_store.search(
                company_id=company2.id, offset=0, limit=10)
        ], contains_inanyorder(employee3.id))
        assert_that(
            self.employee_store.count(company_id=company2.id,
                                      offset=0,
                                      limit=10), is_(equal_to(1)))
예제 #15
0
    def test_search_by_company(self):
        """
        Should be able to retrieve an employee after creating it.

        """
        with transaction():
            employee1 = Employee(
                first="first",
                last="last",
                company_id=self.company.id,
            ).create()
            employee2 = Employee(
                first="Jane",
                last="Doe",
                company_id=self.company.id,
            ).create()
            company2 = Company(name="other", ).create()
            employee3 = Employee(
                first="John",
                last="Doe",
                company_id=company2.id,
            ).create()

        assert_that(Employee.count(), is_(equal_to(3)))
        assert_that(
            [
                employee.last for employee in
                self.employee_store.search_by_company(self.company.id)
            ],
            contains("Doe", "last"),
        )
        assert_that([
            employee.id for employee in self.employee_store.search_by_company(
                self.company.id)
        ], contains_inanyorder(employee1.id, employee2.id))
        assert_that([
            employee.id
            for employee in self.employee_store.search_by_company(company2.id)
        ], contains_inanyorder(employee3.id))
예제 #16
0
    def setup(self):
        self.graph = create_object_graph(
            name="example",
            testing=True,
            import_name="microcosm_postgres",
            loader=load_from_environ,
        )
        self.graph.use("sessionmaker")
        self.company_store = self.graph.company_store
        self.employee_store = self.graph.employee_store
        self.employee_data_store = self.graph.employee_data_store

        with SessionContext(self.graph) as context:
            context.recreate_all()

            with transaction():
                self.company = Company(name="name", ).create()
                self.employee = Employee(
                    first="first",
                    last="last",
                    company_id=self.company.id,
                ).create()
예제 #17
0
 def _make_company(self):
     with SessionContext(self.graph), transaction():
         return Company(
             name="name",
             type=CompanyType.private,
         ).create()