CI/CD Django and Postgress: failure in tests in models that uses signals

Viewed 90

I try to configure CI/CD on Github with my Django project.

I've found some unusual issues. Some of my tests that work perfectly on the local server failed on CI/CD. Problem with a model that sends a signal to another model for creating a new object. In the local server, I've got 8 signals while tests work but in CI/CD it is only one. The other tests pass without any problems.

Maybe someone can help me with this issue.

My .yml file

name: CI/CD Django, Postgress

on: [ push ]

jobs:
  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:12.5
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: github-actions
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up Python 3.10
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"
      - name: Install Requirements
        run: |
          pip install -r requirements.txt
      - name: Run migration
        env:
          DEBUG: "0"
          SECRET_KEY: CD_CI_TEST_KEY
          USER: postgres
          PASSWORD: postgres
          NAME: github-actions
          HOST: localhost
          PORT: 5432
        run: |
          python manage.py migrate

      - name: Run tests
        env:
          DEBUG: "0"
          SECRET_KEY: CD_CI_TEST_KEY
          USER: postgres
          PASSWORD: postgres
          NAME: github-actions
          HOST: localhost
          PORT: 5432
        run: |
          python manage.py test

Models

class Chat(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='post')
    owner = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='owner')
    customer = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='customer')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    last_message = models.TextField(null=True, blank=True)


class Message(models.Model):
    chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='chat')
    message = models.TextField()
    sender = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='sender')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


# Hooks a signal from Chat model and create a new message in Message model
@receiver(post_save, sender=Chat)
def save_chat(sender, instance, **kwargs):
    if instance.last_message:
        Message.objects.create(
            chat_id=instance.id,
            message=instance.last_message,
            sender_id=instance.customer_id,
            updated_at=instance.updated_at
        )


# Hooks a signal from Message and update the last_message field in Chat model
@receiver(post_save, sender=Message)
def save_chat(instance, created, **kwargs):
    if created:
        Chat.objects.filter(id=instance.chat_id).update(
            last_message=instance.message,
            updated_at=instance.updated_at
        )
0 Answers
Related