Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expense migration #176

Merged
merged 5 commits into from
Nov 22, 2024
Merged

fix: expense migration #176

merged 5 commits into from
Nov 22, 2024

Conversation

ruuushhh
Copy link
Contributor

@ruuushhh ruuushhh commented Nov 22, 2024

Description

fix: expense migration

Clickup

https://app.clickup.com/

Summary by CodeRabbit

  • New Features

    • Introduced a new field, is_posted_at_null, to track the posting status of expenses.
    • Enhanced expense data capture during creation and updates.
    • Updated test fixtures to include the new posting status attribute for expenses.
    • Adjusted mapping settings in test configurations for improved accuracy.
  • Bug Fixes

    • Ensured correct handling of the new field within existing expense creation logic.
  • Chores

    • Updated package dependency for fyle-integrations-platform-connector to the latest version.

Copy link

coderabbitai bot commented Nov 22, 2024

Walkthrough

This pull request introduces a migration and model update in the fyle application. A new Boolean field, is_posted_at_null, is added to the expense model to indicate whether the posted_at attribute is null. The migration ensures that the database schema is updated correctly, while the model modification allows this new field to be populated during the creation or updating of Expense objects. Additionally, test fixtures are updated to reflect these changes.

Changes

File Change Summary
apps/fyle/migrations/0004_expense_is_posted_at_null.py Added a migration script to introduce the is_posted_at_null Boolean field in the expense model.
apps/fyle/models.py Updated the Expense class to include the is_posted_at_null field in the create_expense_objects method.
tests/test_business_central/fixtures.py Added 'is_posted_at_null': False to the expense object in the expenses list.
tests/test_fyle/fixtures.py Added 'is_posted_at_null': True in default_raw_expense and 'is_posted_at_null': True in expenses; updated mapping settings in import_settings_response.
requirements.txt Updated fyle-integrations-platform-connector version from 1.38.4 to 1.39.3.
tests/test_fyle/test_tasks.py Modified import statements and rearranged the order of imports in test_update_non_exported_expenses.

Possibly related PRs

  • feat: handle posted at #172: The changes in this PR directly relate to the addition of the is_posted_at_null field in the Expense model, which is the same field introduced in the main PR.

Suggested labels

deploy

Poem

In the fields where expenses grow,
A flag is added, now we know!
Is posted at null? We can see,
With this new field, it's clear as can be.
Hopping along, we celebrate,
For better tracking, isn't it great? 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 0fa88a8 and fdcd17c.

📒 Files selected for processing (1)
  • apps/fyle/models.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/fyle/models.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the size/XS Extra Small PR label Nov 22, 2024
Copy link

Tests Skipped Failures Errors Time
123 0 💤 2 ❌ 41 🔥 8.082s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
apps/fyle/migrations/0004_expense_is_posted_at_null.py (1)

13-17: Consider adding an index for query performance.

The migration looks good structurally. Since this field might be used in filtering queries (e.g., finding expenses with null posted_at), consider adding a database index if this field will be frequently queried.

If you expect frequent queries on this field, you can modify the field definition to include an index:

-field=models.BooleanField(default=False, help_text='Flag check if posted at is null or not'),
+field=models.BooleanField(default=False, help_text='Flag check if posted at is null or not', db_index=True),
apps/fyle/models.py (2)

148-148: Consider adding model-level validation for is_posted_at_null

The new field should be kept in sync with the posted_at field's null status. Consider adding a model-level validation to ensure consistency.

 class Expense(BaseForeignWorkspaceModel):
+    def clean(self):
+        super().clean()
+        if self.is_posted_at_null != (self.posted_at is None):
+            raise ValidationError({
+                'is_posted_at_null': 'Field value must match posted_at null status'
+            })

148-148: Update method documentation

The create_expense_objects method's docstring should be updated to include the new is_posted_at_null field in the expected input format.

     @staticmethod
     def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update: bool = False):
         """
         Bulk create expense objects
+
+        Args:
+            expenses: List of dictionaries containing expense data
+                Required fields: [...existing fields...]
+                is_posted_at_null: Boolean indicating if posted_at is null
+            workspace_id: Integer workspace identifier
+            skip_update: Boolean to skip certain field updates
         """
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9dd3179 and 61502f7.

📒 Files selected for processing (2)
  • apps/fyle/migrations/0004_expense_is_posted_at_null.py (1 hunks)
  • apps/fyle/models.py (1 hunks)
🔇 Additional comments (1)
apps/fyle/models.py (1)

148-148: Verify migration and field usage

Let's verify the migration file and any queries using this field.

✅ Verification successful

Migration and field usage verified successfully

The verification shows:

  • Migration file 0004_expense_is_posted_at_null.py exists and properly adds the boolean field with default value
  • Field is only used in models.py for definition and serialization
  • No existing queries depend on this field, suggesting it's a new addition
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if migration file exists and contains the field
echo "Checking migration file..."
fd --type f "0004_expense_is_posted_at_null.py" apps/fyle/migrations/

# Check for any queries using this field
echo "Checking field usage in queries..."
rg "is_posted_at_null" --type py

Length of output: 610


Script:

#!/bin/bash
# Let's check the content of the migration file
echo "Checking migration file content..."
cat apps/fyle/migrations/0004_expense_is_posted_at_null.py

# Let's also check for any tests using this field
echo "Checking test files..."
rg "is_posted_at_null" --type py --glob "*test*.py"

Length of output: 704

Comment on lines +12 to +18
operations = [
migrations.AddField(
model_name='expense',
name='is_posted_at_null',
field=models.BooleanField(default=False, help_text='Flag check if posted at is null or not'),
),
]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add data migration for existing records.

The migration adds the field with a default False value, but existing records should have this field populated based on their actual posted_at values. Consider adding a data migration to set the correct values.

Would you like me to help create a data migration that:

  1. Queries all existing expenses
  2. Sets is_posted_at_null=True for records where posted_at IS NULL
  3. Sets is_posted_at_null=False for records where posted_at IS NOT NULL

@@ -145,6 +145,7 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update:
'report_id': expense['report_id'],
'spent_at': expense['spent_at'],
'posted_at': expense['posted_at'],
'is_posted_at_null': expense['is_posted_at_null'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation for is_posted_at_null

The method assumes is_posted_at_null will be present in the input dictionary. Consider adding validation or a default value to handle missing or invalid data.

 defaults = {
-    'is_posted_at_null': expense['is_posted_at_null'],
+    'is_posted_at_null': bool(expense.get('is_posted_at_null', False)),
     'fund_source': SOURCE_ACCOUNT_MAP[expense['source_account_type']],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'is_posted_at_null': expense['is_posted_at_null'],
'is_posted_at_null': bool(expense.get('is_posted_at_null', False)),

@github-actions github-actions bot added size/S Small PR and removed size/XS Extra Small PR labels Nov 22, 2024
Copy link

Tests Skipped Failures Errors Time
123 0 💤 1 ❌ 0 🔥 9.316s ⏱️

Copy link

Tests Skipped Failures Errors Time
123 0 💤 0 ❌ 0 🔥 8.378s ⏱️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
tests/test_fyle/test_tasks.py (2)

46-46: Remove debug print statement.

Debug print statements should not be committed to the codebase. Please remove this line as it adds noise to the test output.

-    print(payload['data'])

Line range hint 12-71: Add test coverage for is_posted_at_null field.

The test should verify the behavior of the newly added is_posted_at_null field in the Expense model. Consider adding assertions to check:

  1. The field is correctly set when expenses are created/updated
  2. The field's impact on expense updates through webhooks

Example additions:

# After expense creation
assert expense_created.is_posted_at_null == True  # or expected value

# After update
expense = Expense.objects.get(expense_id='txhJLOSKs1iN', org_id=org_id)
assert expense.is_posted_at_null == True  # or expected value
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5c5e1ad and 3ffa913.

📒 Files selected for processing (4)
  • apps/fyle/models.py (2 hunks)
  • requirements.txt (1 hunks)
  • tests/test_fyle/fixtures.py (3 hunks)
  • tests/test_fyle/test_tasks.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • requirements.txt
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/fyle/models.py
  • tests/test_fyle/fixtures.py
🔇 Additional comments (2)
tests/test_fyle/test_tasks.py (2)

3-9: LGTM! Import organization looks good.

The imports are now well-organized, following a logical grouping pattern from external to internal dependencies.


Line range hint 12-71: Verify test fixtures include the new field.

Let's verify that the test fixtures are properly updated with the new is_posted_at_null field.

✅ Verification successful

Test fixtures are properly updated with the new field

The test fixtures in tests/test_fyle/fixtures.py include the is_posted_at_null field in both the raw_expense and default_raw_expense fixtures with appropriate values:

  • raw_expense: 'is_posted_at_null': True
  • default_raw_expense: 'is_posted_at_null': True
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if test fixtures include the new is_posted_at_null field

# Search for is_posted_at_null in fixture files
rg "is_posted_at_null" tests/test_fyle/fixtures.py

# Show the raw_expense and default_raw_expense fixtures
rg -A 10 "raw_expense = \{|default_raw_expense = \{" tests/test_fyle/fixtures.py

Length of output: 243


Script:

#!/bin/bash
# Let's get more context around the fixtures by showing the entire fixtures.py file
cat tests/test_fyle/fixtures.py

Length of output: 22841

Copy link

Tests Skipped Failures Errors Time
123 0 💤 0 ❌ 0 🔥 8.386s ⏱️

@@ -104,6 +104,7 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int, skip_update:

# Create an empty list to store expense objects
expense_objects = []
print(expenses)
Copy link
Contributor

@Hrishabh17 Hrishabh17 Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print pls remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed sir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.44%. Comparing base (24079f1) to head (0fa88a8).
Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #176      +/-   ##
==========================================
+ Coverage   96.38%   96.44%   +0.06%     
==========================================
  Files          90       90              
  Lines        4981     5041      +60     
==========================================
+ Hits         4801     4862      +61     
+ Misses        180      179       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link

Tests Skipped Failures Errors Time
123 0 💤 0 ❌ 0 🔥 9.005s ⏱️

@ruuushhh ruuushhh merged commit 302ffb2 into master Nov 22, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/S Small PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants