Compare commits

...

4 commits

Author SHA1 Message Date
d8ff99475e Add the Rails tests to the pre-commit hook
I'm gonna work on adding modeling tests, and I want to not be breaking
them without realizing! The trade history ones are good to be checking
more often like this, too.
2024-10-21 14:35:26 -07:00
9726ecb1a5 Fix trade activity tests: use a valid item fixture, not a placeholder
In 540ce08caa, I updated the Item class
to be more explicit about what fields are required, so this test would
fail in a more helpful way, instead of just crashing from `name` being
`nil` when trying to infer the Dyeworks info.

Now, we update the test to use Rails's standard "fixture" system to set
up a more-correct placeholder item, instead!
2024-10-21 14:26:36 -07:00
540ce08caa Handle invalid Item state a bit better
Catch missing fields in validation before sending it to the DB, and
skip the Dyeworks stuff if the name is missing.

I ran into this looking into `test/trade_activity_test.rb`, which fails
right now because we try to create a boring placeholder item with
minimal fields, which Dyeworks can't call `name.match()` on!

Now, the test fails with a more helpful error about the item being
invalid. Next, I'll fix that!
2024-10-21 14:24:45 -07:00
881e63cfbd Output JSON from rails pets:load[pet_name]
Gonna use this for making mock data for automatic testing!
2024-10-21 14:03:56 -07:00
6 changed files with 20 additions and 9 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh" . "$(dirname -- "$0")/_/husky.sh"
yarn lint --max-warnings=0 --fix yarn lint --max-warnings=0 --fix && bin/rails test

View file

@ -23,6 +23,8 @@ class Item < ApplicationRecord
has_many :dyeworks_variants, class_name: "Item", has_many :dyeworks_variants, class_name: "Item",
inverse_of: :dyeworks_base_item inverse_of: :dyeworks_base_item
validates_presence_of :name, :description, :thumbnail_url, :rarity, :price,
:zones_restrict
attr_writer :current_body_id, :owned, :wanted attr_writer :current_body_id, :owned, :wanted

View file

@ -117,7 +117,7 @@ class Item
)\z )\z
}x }x
def inferred_dyeworks_base_item def inferred_dyeworks_base_item
name_match = name.match(DYEWORKS_NAME_PATTERN) name_match = (name || "").match(DYEWORKS_NAME_PATTERN)
return nil if name_match.nil? return nil if name_match.nil?
Item.find_by_name(name_match["base"]) Item.find_by_name(name_match["base"])

View file

@ -1,7 +1,8 @@
namespace :pets do namespace :pets do
desc "Load a pet's viewer data" desc "Load a pet's viewer data"
task :load, [:name] => [:environment] do |task, args| task :load, [:name] => [:environment] do |task, args|
pp Neopets::CustomPets.fetch_viewer_data(args[:name]) viewer_data = Neopets::CustomPets.fetch_viewer_data(args[:name])
puts JSON.pretty_generate(viewer_data)
end end
desc "Find pets that were, last we saw, of the given color and species" desc "Find pets that were, last we saw, of the given color and species"

14
test/fixtures/items.yml vendored Normal file
View file

@ -0,0 +1,14 @@
straw_hat:
id: 58
name: Straw Hat
description: "This straw hat will keep the sun out of your pets eyes in
bright sunlight."
thumbnail_url: https://images.neopets.com/items/straw-hat.gif
type: Clothes
category: Clothes
rarity: Very Rare
rarity_index: 90
price: 376
weight_lbs: 1
zones_restrict: 0000000000000000000000000001000000001010000000000000
species_support_ids: "35"

View file

@ -203,12 +203,6 @@ class TradeActivityTest < ActiveSupport::TestCase
setup do setup do
freeze_time # to compare timestamps accurately freeze_time # to compare timestamps accurately
Item.create!(
thumbnail_url: "https://images.neopets.com/foo.png",
zones_restrict: "",
price: 123,
)
end end
private private