1Java: OrderRepository chỉ là interface kế thừa JpaRepository<Order, Long>, không có class cài đặt. Lúc khởi động, Spring Data sinh proxy cài đặt nó; save, findById, findAll có sẵn từ JpaRepository.Java: OrderRepository is just an interface extending JpaRepository<Order, Long>, with no implementing class. At startup Spring Data generates a proxy implementing it; save, findById and findAll come from JpaRepository.
2OrderRepositoryTest (@DataJpaTest) gọi orders.save(...) ba lần: khách 42 PAID 150.000đ, khách 42 CANCELLED 90.000đ, khách 7 PAID 50.000đ. Với JPA, proxy chuyển mỗi lần thành INSERT vào bảng orders; trong bản demo, InMemoryRepositoryProxy lưu vào Map và gán id 1, 2, 3.OrderRepositoryTest (@DataJpaTest) calls orders.save(...) three times: customer 42 PAID 150,000, customer 42 CANCELLED 90,000, customer 7 PAID 50,000. With JPA the proxy turns each into an INSERT into orders; in the demo, InMemoryRepositoryProxy stores them in a Map and assigns ids 1, 2, 3.
3Test gọi findByCustomerIdAndStatus(42, PAID). Spring đọc tên method: findBy + CustomerId + And + Status thành WHERE customer_id = ? AND status = ?, rồi trả List<Order> chỉ có một đơn 150.000đ.The test calls findByCustomerIdAndStatus(42, PAID). Spring parses the name: findBy + CustomerId + And + Status becomes WHERE customer_id = ? AND status = ?, returning a List<Order> with one 150,000 order.
4Go: khách 42 mở lịch sử đơn. Service gọi Repository.FindByCustomer(ctx, 42) trên interface order.Repository, không biết tên bảng hay cột. Ở production, main() đã truyền vào postgres.NewOrderRepo(pool).Go: customer 42 opens their order history. The service calls Repository.FindByCustomer(ctx, 42) on the order.Repository interface, unaware of tables or columns. In production, main() passed in postgres.NewOrderRepo(pool).
5OrderRepo gửi truy vấn có tham số $1 và ORDER BY created_at DESC, rồi pgx.CollectRows với RowToStructByPos[order.Order] map từng dòng thành Order. Dòng var _ order.Repository = (*OrderRepo)(nil) bảo đảm lúc compile rằng nó đủ method.OrderRepo sends a $1-parameterized query with ORDER BY created_at DESC, then pgx.CollectRows with RowToStructByPos[order.Order] maps each row into an Order. The line var _ order.Repository = (*OrderRepo)(nil) guarantees at compile time that it has every method.
6Khi FindByID(ctx, 999) không có dòng nào, pgx trả pgx.ErrNoRows; OrderRepo dịch thành order.ErrNotFound. Service chỉ cần errors.Is(err, order.ErrNotFound), không biết driver là gì.When FindByID(ctx, 999) finds no row, pgx returns pgx.ErrNoRows; OrderRepo translates it into order.ErrNotFound. The service only needs errors.Is(err, order.ErrNotFound), knowing nothing about the driver.
7Trong main.go (hoặc unit test), service được tạo với order.NewFakeRepo(). Save gán nextID và lưu vào map; FindByCustomer lọc map theo CustomerID. Cùng lời gọi, cùng hợp đồng, nhưng không mở kết nối database nào.In main.go (or a unit test), the service is built with order.NewFakeRepo(). Save assigns nextID and stores into the map; FindByCustomer filters the map by CustomerID. Same call, same contract, but no database connection.
8fakeRepo trả cùng lỗi domain ErrNotFound như bản thật, nên test nghiệp vụ không bị xanh giả. SQL và mapping của postgres.OrderRepo vẫn cần kiểm thử tích hợp riêng, giống OrderRepositoryTest phía Java.fakeRepo returns the same domain error ErrNotFound as the real one, so business tests are not falsely green. postgres.OrderRepo's SQL and mapping still need their own integration tests, like OrderRepositoryTest on the Java side.