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

Add ActiveRecord as a dependency #5

Closed
alexandreruban opened this issue Aug 5, 2023 · 5 comments
Closed

Add ActiveRecord as a dependency #5

alexandreruban opened this issue Aug 5, 2023 · 5 comments

Comments

@alexandreruban
Copy link
Collaborator

There are two TODOs in the code related to adding ActiveRecord as a dependency:

# test/test_oaken.rb

class TestOaken < Oaken::Test
  def test_updating_fixture
    # TODO: Hack to pretend we've got transactional tests working.
    # Remove after we've connected Active Record.
  end
end
# lib/oaken.rb

module Oaken
  class Table
    def update(name, **attributes)
      require "ostruct" # TODO: Remove OpenStruct relatively soon.
    end
  end
end

Should we add ActiveRecord now with a memory database to be able to test on real models? I'd be happy to work on this if you think it's the right moment.

Also if someone wants to do some pair-programming with me on this feel free to tell me!

@kaspth
Copy link
Owner

kaspth commented Aug 5, 2023

@alexandreruban nice, thanks for writing this up! Right now, I'm trying to see if I can build some structure around supporting both in-memory models and database based ones. So the Active Record connection bit is coming up soon.

Let me try to work on the above, and then I'll get back to you.

@kaspth
Copy link
Owner

kaspth commented Aug 5, 2023

Ok, so I pretty much restructured the internals entirely in #7 and removed OpenStruct. I also added the basics of file loading in #8.

We've still got the transactional TODO, but I wonder if we should somehow support transactional updates for in-memory models. Eh, that's probably a bit much.

@alexandreruban
Copy link
Collaborator Author

Ok, so I pretty much restructured the internals entirely in #7 and removed OpenStruct. I also added the basics of file loading in #8.

Nice!

I wonder if we should somehow support transactional updates for in-memory models. Eh, that's probably a bit much.

Could we write a custom after_teardown hook to revert the changes made during a test?

We could change the API to distinguish create and update actions. The create method would be called in seed files like this:

# test/seeds/users.rb

User = Struct.new(:name, keyword_init: true)
memory.register :users, User

# We call `create` in the seed files. 
# As we are "creating" the data, I think it also makes a clearer API.
users.create :kasper, name: "Kasper"
# test/oaken_test.rb

def test_updating_fixture
  # Calling `update` stores the original object somewhere and enables us 
  # to interact with a copy.
  users.update :kasper, name: "Kasper2"
  assert_equal "Kasper2", users.kasper.name

  # The `after_teardown` hook drops the copy and goes back to referencing the
  # original object so we don't need this anymore.
  # users.update :kasper, name: "Kasper" 
end

What do you think? It may be a silly idea!

@kaspth
Copy link
Owner

kaspth commented Aug 9, 2023

Could we write a custom after_teardown hook to revert the changes made during a test?

Potentially, yeah! I think this is a little further along for me right now, if you or someone else wants to trial it out in a PR, please do!

We could change the API to distinguish create and update actions. The create method would be called in seed files like this:

I'm curious to see what it enables or unlocks if we build with the idea that seeds can also be rerun incrementally and doesn't always have to be rerun from scratch. So I want to stick with just update for now.

kaspth added a commit that referenced this issue Aug 9, 2023
This starts us off with a connection to a memory based SQLite connection as discussed in #5.

With transactional tests enabled we can now remove the TODO.

Closes #5
@kaspth kaspth closed this as completed in 4592132 Aug 9, 2023
@kaspth
Copy link
Owner

kaspth commented Aug 9, 2023

I just realized we probably will need the create API for times when you don't need the named reference, ala:

100.times do |n|
  posts.create title: "Post number #{n}"
end

But it might be nice to have a dedicated API that could match that and skip the generation if needed. Maybe something like this?

posts.fill_in 100.times, title: "Title :n" # Use :n to fill in the count via String#%

Anyway, that's probably for a little further down the line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants