mypebble / django-pgviews

Fork of django-postgres that focuses on maintaining and improving support for Postgres SQL Views.
The Unlicense
195 stars 41 forks source link

Tests materialized views #59

Closed andrehenriquemendes closed 3 years ago

andrehenriquemendes commented 3 years ago

Hey guys,

I have a issue when test materialized views with pytest.

Basically, when I refresh the materialized views by endpoint works, but when I refresh with tests, don't works.

The refresh method works, but don't update datas on materialized view.

Thank you!

mikicz commented 3 years ago

Hi. Could you maybe post a snippet of the testing code, so we can see when/how you're refreshing the materialized view?

andrehenriquemendes commented 3 years ago

Hi. Could you maybe post a snippet of the testing code, so we can see when/how you're refreshing the materialized view?

Hi, @mikicz! After create the original instance, it call refresh_materialized_view() to refresh

def refresh_discounts_materialized_view(self):
      with connection.cursor() as cursor:
              cursor.execute(f'REFRESH MATERIALIZED VIEW discount_checker_setup_view')

def test_list_discounts(self, mocked):
        self.create_discount() # create original instance
        self.refresh_discounts_materialized_view()

PS:

mikicz commented 3 years ago

Have you tried refreshing with this instead of the cursor query?

DiscourdCheckerSetupView.refresh()

(assuming that's the class where you're defining the mat. view)

andrehenriquemendes commented 3 years ago

I tried too, it was my first attempt

mikicz commented 3 years ago

Hm, that's strange them. It's hard for me to judge without seeing more of the code unfortunately. I use materialized views with pytest and they work fine, so not sure what's exactly going on.

Two things I'd investigate:

andrehenriquemendes commented 3 years ago

@mikicz, can you show me how is your model and migration? Just to compare with mine. I have been with this problem for days

mikicz commented 3 years ago

I can't provide any actual examples from my codebase unfortunately. But setup-wise there's nothing special, a regular django model with standard migrations and a materialized view without any special stuff, like it's in the documentation: https://github.com/mikicz/django-pgviews#materialized-views