Class: ActiveShopifyGraphQL::Testing::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shopify_graphql/testing/store.rb

Overview

In-memory data store for test records. Stores records keyed by model class, with IDs normalized to GID format. Supports lookup by ID, filtering by hash/string conditions, and extracting inline connection data from parent records.

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



10
11
12
# File 'lib/active_shopify_graphql/testing/store.rb', line 10

def initialize
  @data = {}
end

Instance Method Details

#clearObject

Clear all stored data.



74
75
76
# File 'lib/active_shopify_graphql/testing/store.rb', line 74

def clear
  @data.clear
end

#connections_for(model_class, id, connection_name) ⇒ Array<Hash>, ...

Extract inline connection data from a parent's stored record.

Parameters:

  • model_class (Class)

    The parent model class

  • id (String)

    The parent's GID

  • connection_name (Symbol)

    The connection key name

Returns:

  • (Array<Hash>, Hash, nil)

    The connection data



66
67
68
69
70
71
# File 'lib/active_shopify_graphql/testing/store.rb', line 66

def connections_for(model_class, id, connection_name)
  record = find(model_class, id)
  return nil unless record

  record[connection_name]
end

#find(model_class, id) ⇒ Hash?

Find a single record by normalized GID.

Parameters:

  • model_class (Class)

    The model class

  • id (String)

    The GID to look up

Returns:

  • (Hash, nil)

    The matching record or nil



34
35
36
37
# File 'lib/active_shopify_graphql/testing/store.rb', line 34

def find(model_class, id)
  records = @data[model_class] || []
  records.find { |r| r[:id] == id }
end

#register(model_class, records) ⇒ Object

Register test records for a model class. IDs are normalized to GID format using the model's graphql_type.

Parameters:

  • model_class (Class)

    The model class (e.g., Customer)

  • records (Array<Hash>)

    Array of attribute hashes



19
20
21
22
23
24
25
26
27
# File 'lib/active_shopify_graphql/testing/store.rb', line 19

def register(model_class, records)
  graphql_type = model_class.graphql_type_for_loader(model_class.send(:default_loader_class))

  @data[model_class] = records.map do |record|
    record = record.dup
    record[:id] = GidHelper.normalize_gid(record[:id], graphql_type) if record.key?(:id)
    record
  end
end

#where(model_class, conditions) ⇒ Array<Hash>

Filter records by conditions. Hash conditions: match against ALL keys in stored hash (attributes + search fields). String conditions: parse simple key:value patterns, fallback to return all. Comparison uses .to_s on both sides so plain IDs match naturally.

Parameters:

  • model_class (Class)

    The model class

  • conditions (Hash, String)

    The filter conditions

Returns:

  • (Array<Hash>)

    Matching records



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_shopify_graphql/testing/store.rb', line 47

def where(model_class, conditions)
  records = @data[model_class] || []

  case conditions
  when Hash
    filter_by_hash(records, normalize_conditions(model_class, conditions))
  when String
    filter_by_string(records, conditions)
  else
    records
  end
end