Skip to content

Commit

Permalink
test code 충돌 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
haeunia committed Jan 11, 2025
2 parents 760a609 + b337b1c commit 1df57f7
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 130 deletions.
107 changes: 53 additions & 54 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,69 +43,68 @@ jobs:
- name: Django 테스트 실행
env:
DJANGO_SETTINGS_MODULE: 'config.settings'
DJANGO_SETTINGS_MODULE: "config.settings"
DJANGO_DEBUG_MODE: true
UPCY_SECRET_KEY: 'test123123123123123123123' # 진짜 SECRET 키 아닙니다.
UPCY_SECRET_KEY: "test123123123123123123123" # 진짜 SECRET 키 아닙니다.
run: |
poetry run python manage.py migrate # 데이터베이스 마이그레이션
poetry run python manage.py test # Django 전체 테스트 실행
deploy:
name: Deploy Docker image to ECR and Run docker image in EC2 instance
# PR이 머지되었을 때만 실행
if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:
- name: 코드 Checkout
uses: actions/checkout@v4

- name: AWS Credentials 가져오기
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{secrets.AWS_REGION}}

- name: AWS ECR 로그인
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: 백엔드 도커 이미지 생성 및 배포
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }}
IMAGE_TAG: ${{ github.sha }}
run: |
# 도커 이미지 생성
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
# ECR로 도커 이미지 PUSH
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
- name: EC2에 배포 및 백엔드 어플리케이션 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: ./deploy.sh

- name: 배포 결과 확인
if: always()
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
# 컨테이너 상태 확인
if docker ps | grep -q "${{ secrets.ECR_REPOSITORY_NAME }}"; then
echo "Container is running successfully"
exit 0
else
echo "Container is not running"
exit 1
fi
- name: 코드 Checkout
uses: actions/checkout@v4

- name: AWS Credentials 가져오기
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}

- name: AWS ECR 로그인
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: 백엔드 도커 이미지 생성 및 배포
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }}
IMAGE_TAG: ${{ github.sha }}
run: |
# 도커 이미지 생성
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
# ECR로 도커 이미지 PUSH
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
- name: EC2에 배포 및 백엔드 어플리케이션 실행
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: ./deploy.sh

- name: 배포 결과 확인
if: always()
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
script: |
# 컨테이너 상태 확인
if docker ps | grep -q "${{ secrets.ECR_REPOSITORY_NAME }}"; then
echo "Container is running successfully"
exit 0
else
echo "Container is not running"
exit 1
fi
3 changes: 0 additions & 3 deletions core/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from django.test import TestCase

# Create your tests here.
2 changes: 1 addition & 1 deletion market/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_service_queryset_by_market_uuid_with_temporary(
) -> QuerySet:
queryset: QuerySet = self.model.objects.filter(
market__market_uuid=market_uuid, temporary=temporary
).select_related("market")
).select_related("market", "market__reformer", "market__reformer__user")
if not queryset.exists():
raise ObjectDoesNotExist("Service not found")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ class ServiceRetrieveSerializer(serializers.ModelSerializer):
market_uuid = serializers.ReadOnlyField(
source="market.market_uuid"
) # https://www.django-rest-framework.org/api-guide/fields/#readonlyfield
reformer_nickname = serializers.ReadOnlyField(
source="market.reformer.user.nickname"
)

class Meta:
model = Service
fields = [
"reformer_nickname",
"market_uuid",
"service_uuid",
"service_title",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Meta:
"service_period",
"basic_price",
"max_price",
"suspended",
]
extra_kwargs = {
"service_title": {"required": False},
Expand All @@ -21,6 +22,7 @@ class Meta:
"service_period": {"required": False},
"basic_price": {"required": False},
"max_price": {"required": False},
"suspended": {"required": False},
}

def validate(self, attrs):
Expand Down Expand Up @@ -68,5 +70,6 @@ def update(self, instance, validated_data):
instance.service_period = validated_data.get(
"service_period", instance.service_period
)
instance.suspended = validated_data.get("suspended", instance.suspended)
instance.save()
return instance
123 changes: 116 additions & 7 deletions market/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import override_settings
from rest_framework.test import APIClient, APITestCase

from market.models import Market
from market.models import Market, Service
from users.models.reformer import Reformer
from users.models.user import User

Expand All @@ -17,6 +17,36 @@ class MarketTestCase(APITestCase):
TEST_MARKET_NAME = "test market"
TEST_MARKET_INTRODUCE = "asdfasdfasdf"
TEST_MARKET_ADDRESS = "Seoul"
TEST_SERVICE_CREATE_DATA = {
"service_title": "reform service",
"service_content": "asdfadfgf",
"service_category": "category1",
"service_style": [{"style_name": "style 1"}, {"style_name": "style 2"}],
"service_period": 7,
"basic_price": 12345,
"max_price": 99999,
"service_option": [
{
"option_name": "option 1",
"option_content": "asdfasdfa",
"option_price": 123123,
},
{
"option_name": "option 2",
"option_content": "asdfasdfa",
"option_price": 1256473123,
},
{
"option_name": "option 3",
"option_content": "asdfasdfa",
"option_price": 12312543,
},
],
"service_material": [
{"material_name": "material 1"},
{"material_name": "material 2"},
],
}

@classmethod
def setUpTestData(cls):
Expand All @@ -26,7 +56,7 @@ def setUpTestData(cls):
email=cls.TEST_CUSTOMER_EMAIL,
password=cls.TEST_PASSWORD,
phone="01012341234",
nickname="nickname",
nickname="test_customer",
introduce="hello, django",
role="customer",
is_active=True,
Expand All @@ -49,7 +79,7 @@ def setUp(self):
email=self.TEST_EMAIL,
password=self.TEST_PASSWORD,
phone="01012341234",
nickname="nickname",
nickname="test_user_hello",
introduce="hello, django",
role="reformer",
is_active=True,
Expand Down Expand Up @@ -88,7 +118,7 @@ def test_market_create_without_reformer(self):
path="/api/market",
data={
"market_name": "Invalid market",
"market_introduce": "SHOULD NOT CREATED",
"market_introduce": "SHOULD NOT BE CREATED",
"market_address": "ABC",
},
format="json",
Expand Down Expand Up @@ -127,7 +157,7 @@ def test_forbid_market_create_more_than_one(self):
self.assertEqual(invalid_response.status_code, 400)

def test_get_market_info(self):
# 마켓 정보 생성
# 마켓 정보 가져오기 테스트
self.client.post(
path="/api/market",
data={
Expand Down Expand Up @@ -262,13 +292,92 @@ def test_report_market(self):
"reason": "허위 정보 게시",
"details": "잘못된 정보를 포함한 마켓 설명",
},
format="json",
)
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data["reported_user"], self.test_user.id)
self.assertEqual(response.data["reason"], "허위 정보 게시")

def test_get_service_list(self):
# 서비스 리스트 가져오기 테스트

# 1. 테스트 마켓 생성
response = self.client.post(
path="/api/market",
data={
"market_name": self.TEST_MARKET_NAME,
"market_introduce": self.TEST_MARKET_INTRODUCE,
"market_address": self.TEST_MARKET_ADDRESS,
},
format="json",
)
self.assertEqual(response.status_code, 201)

market_uuid = response.data.get("market_uuid", None)
self.assertIsNotNone(market_uuid, None)

# 2. 해당 마켓에 대한 서비스 10개 생성
for i in range(10):
response = self.client.post(
path=f"/api/market/{market_uuid}/service",
data=self.TEST_SERVICE_CREATE_DATA,
format="json",
)
self.assertEqual(response.status_code, 201)

# 3. DB에 존재하는 전체 서비스 리스트 가져오기 (10개 만들었으니까 총 10개 있어야 함)
response = self.client.get(path=f"/api/market/services", format="json")
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data.get("results", None)), 10)

# 4. 특정 market에 속한 서비스 리스트 가져오기
# market_uuid에 10개 만들었으므로 10개 있어야함
response = self.client.get(
path=f"/api/market/{market_uuid}/service", format="json"
)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 10)

def test_service_update_suspend_field(self):
# 1. 테스트 마켓 생성
response = self.client.post(
path="/api/market",
data={
"market_name": self.TEST_MARKET_NAME,
"market_introduce": self.TEST_MARKET_INTRODUCE,
"market_address": self.TEST_MARKET_ADDRESS,
},
format="json",
)
self.assertEqual(response.status_code, 201)
market_uuid = response.data.get("market_uuid", None)
self.assertIsNotNone(market_uuid, None)

# 2. 해당 마켓에 대한 서비스 생성
response = self.client.post(
path=f"/api/market/{market_uuid}/service",
data=self.TEST_SERVICE_CREATE_DATA,
format="json",
)
self.assertEqual(response.status_code, 201)
service_uuid = response.data.get("service_uuid", None)
self.assertNotEqual(service_uuid, None)

service = Service.objects.filter(service_uuid=service_uuid).first()
self.assertEqual(service.suspended, False)

# 3. 서비스 suspended 업데이트 시도
response = self.client.put(
path=f"/api/market/{market_uuid}/service/{service_uuid}",
data={"suspended": True},
format="json",
)
self.assertEqual(response.status_code, 200)
updated_service: Service = Service.objects.filter(
service_uuid=service_uuid
).first()
self.assertNotEqual(service.suspended, updated_service.suspended)

def tearDown(self):
Service.objects.all().delete()
Market.objects.all().delete()
Reformer.objects.all().delete()
User.objects.all().delete()
2 changes: 1 addition & 1 deletion market/views/service_view/service_create_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_permissions(self):
@view_exception_handler
def get(self, request, **kwargs):
"""
market uuid를 사용하여 서비스 리스트를 반환하는 메서드
마켓에 생성된 서비스 리스트를 반환하는 로직을 처리하는 View
"""
temporary_status = temporary_status_check(request)
queryset = self.get_queryset(
Expand Down
Loading

0 comments on commit 1df57f7

Please sign in to comment.